Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: entity creation in sync collections #2234

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@
## [v1.9.3]
> (2024-05-13)

* Features and enhancements
* structure editor compatibility ([#1894](https://github.com/ComPlat/chemotion_ELN/pull/1894))
* enable spectra report for samples ([#1902](https://github.com/ComPlat/chemotion_ELN/pull/1902))
* activate segment visibility field in generic elements ([#1702](https://github.com/ComPlat/chemotion_ELN/pull/1702))

* Bug fixes
* advanced search ([#1888](https://github.com/ComPlat/chemotion_ELN/pull/1888))
* converter inbox issue for NMR ([#1903](https://github.com/ComPlat/chemotion_ELN/pull/1903))
* add updating of annotations also in reaction container ([#1913](https://github.com/ComPlat/chemotion_ELN/pull/1913))
* Yield calculation with purity ([#1904](https://github.com/ComPlat/chemotion_ELN/pull/1904))
* ensure wait_until is set when initializing cron delayed jobs ([#1892](https://github.com/ComPlat/chemotion_ELN/pull/1892))

* Chores
* Bump react-pdf from 5.7.0 to 7.7.3 ([#1909](https://github.com/ComPlat/chemotion_ELN/pull/1909))


## [v1.9.3]
> (2024-05-13)

* Features and enhancements
* structure editor compatibility ([#1894](https://github.com/ComPlat/chemotion_ELN/pull/1894))
* enable spectra report for samples ([#1902](https://github.com/ComPlat/chemotion_ELN/pull/1902))
Expand Down
47 changes: 30 additions & 17 deletions app/api/chemotion/reaction_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class ReactionAPI < Grape::API
optional :variations, type: [Hash]
optional :vessel_size, type: Hash
optional :gaseous, type: Boolean
optional :is_sync_to_me, type: Boolean, default: false
end

post do
Expand All @@ -257,9 +258,8 @@ class ReactionAPI < Grape::API
container_info = params[:container]
attributes.delete(:container)
attributes.delete(:segments)
attributes.delete(:user_labels)

collection = current_user.collections.where(id: collection_id).take
is_sync_to_me = attributes.delete(:is_sync_to_me)

attributes[:created_by] = current_user.id
reaction = Reaction.create!(attributes)
recent_ols_term_update('rxno', [params[:rxno]]) if params[:rxno].present?
Expand Down Expand Up @@ -294,26 +294,39 @@ class ReactionAPI < Grape::API
reaction.save!
update_element_labels(reaction, params[:user_labels], current_user.id)
reaction.save_segments(segments: params[:segments], current_user_id: current_user.id)
CollectionsReaction.create(reaction: reaction, collection: collection) if collection.present?

is_shared_collection = false
if collection.blank?
if is_sync_to_me
sync_collection = current_user.all_sync_in_collections_users.where(id: collection_id).take
if sync_collection.present?
is_shared_collection = true
sync_in_collection_receiver = Collection.find(sync_collection['collection_id'])
CollectionsReaction.create(reaction: reaction,
collection: sync_in_collection_receiver)
sync_out_collection_sharer = Collection.get_all_collection_for_user(sync_collection['shared_by_id'])

collection = Collection.find(sync_collection['collection_id'])
CollectionsReaction.create(reaction: reaction, collection: collection) if collection.present?

sync_out_collection_sharer = Collection.get_all_collection_for_user(sync_collection['shared_by_id'])
CollectionsReaction.create(reaction: reaction, collection: sync_out_collection_sharer)
else
collection = current_user.collections.where(id: collection_id).take
CollectionsReaction.create(reaction: reaction, collection: collection) if collection.present?

is_shared_collection = false
if collection.blank?
sync_collection = current_user.all_sync_in_collections_users.where(id: collection_id).take
if sync_collection.present?
is_shared_collection = true
sync_in_collection_receiver = Collection.find(sync_collection['collection_id'])
CollectionsReaction.create(reaction: reaction,
collection: sync_in_collection_receiver)
sync_out_collection_sharer = Collection.get_all_collection_for_user(sync_collection['shared_by_id'])
CollectionsReaction.create(reaction: reaction,
collection: sync_out_collection_sharer)
end
end

unless is_shared_collection
CollectionsReaction.create(reaction: reaction,
collection: sync_out_collection_sharer)
collection: Collection.get_all_collection_for_user(current_user.id))
end
end

unless is_shared_collection
CollectionsReaction.create(reaction: reaction,
collection: Collection.get_all_collection_for_user(current_user.id))
end
CollectionsReaction.update_tag_by_element_ids(reaction.id)
if reaction
if attributes['origin'] && attributes['origin']['short_label'] && materials['products'].present?
Expand Down
36 changes: 24 additions & 12 deletions app/api/chemotion/research_plan_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class ResearchPlanAPI < Grape::API
requires :container, type: Hash, desc: 'Containers'
optional :segments, type: Array, desc: 'Segments'
optional :attachments, type: Array, desc: 'Attachments'
optional :is_sync_to_me, type: Boolean, default: false
end
post do
attributes = {
Expand All @@ -85,24 +86,35 @@ class ResearchPlanAPI < Grape::API
clone_attachs = params[:attachments]&.reject { |a| a[:is_new] }
Usecases::Attachments::Copy.execute!(clone_attachs, research_plan, current_user.id) if clone_attachs

if params[:collection_id]
collection = current_user.collections.where(id: params[:collection_id]).take
research_plan.collections << collection if collection.present?
end

is_shared_collection = false
unless collection.present?
if params[:is_sync_to_me]
Rails.logger.debug('Creating rplan in sync collection due to user request (param :is_sync_to_me is set).')
sync_collection = current_user.all_sync_in_collections_users.where(id: params[:collection_id]).take
if sync_collection.present?
is_shared_collection = true
research_plan.collections << Collection.find(sync_collection['collection_id'])
research_plan.collections << Collection.get_all_collection_for_user(sync_collection['shared_by_id'])
else
error!('400 Bad Request (Cant find sync collection)', 400)
end
else
if params[:collection_id]
collection = current_user.collections.where(id: params[:collection_id]).take
research_plan.collections << collection if collection.present?
end
end

unless is_shared_collection
all_coll = Collection.get_all_collection_for_user(current_user.id)
research_plan.collections << all_coll
is_shared_collection = false
if collection.blank?
sync_collection = current_user.all_sync_in_collections_users.where(id: params[:collection_id]).take
if sync_collection.present?
is_shared_collection = true
research_plan.collections << Collection.find(sync_collection['collection_id'])
research_plan.collections << Collection.get_all_collection_for_user(sync_collection['shared_by_id'])
end
end

unless is_shared_collection
all_coll = Collection.get_all_collection_for_user(current_user.id)
research_plan.collections << all_coll
end
end

present research_plan, with: Entities::ResearchPlanEntity, root: :research_plan
Expand Down
36 changes: 24 additions & 12 deletions app/api/chemotion/sample_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ class SampleAPI < Grape::API
optional :inventory_sample, type: Boolean, default: false
optional :molecular_mass, type: Float
optional :sum_formula, type: String
optional :is_sync_to_me, type: Boolean, default: false
end
post do
molecule_id = if params[:decoupled] && params[:molfile].blank?
Expand Down Expand Up @@ -551,24 +552,35 @@ class SampleAPI < Grape::API

sample = Sample.new(attributes)

if params[:collection_id]
collection = current_user.collections.where(id: params[:collection_id]).take
sample.collections << collection if collection.present?
end

is_shared_collection = false
if collection.blank?
if params[:is_sync_to_me]
Rails.logger.debug('Creating sample in sync collection due to user request (param :is_sync_to_me is set).')
sync_collection = current_user.all_sync_in_collections_users.where(id: params[:collection_id]).take
if sync_collection.present?
is_shared_collection = true
sample.collections << Collection.find(sync_collection['collection_id'])
sample.collections << Collection.get_all_collection_for_user(sync_collection['shared_by_id'])
else
error!('400 Bad Request (Cant find sync collection)', 400)
end
else
if params[:collection_id]
collection = current_user.collections.where(id: params[:collection_id]).take
sample.collections << collection if collection.present?
end
end

unless is_shared_collection
all_coll = Collection.get_all_collection_for_user(current_user.id)
sample.collections << all_coll
is_shared_collection = false
if collection.blank?
sync_collection = current_user.all_sync_in_collections_users.where(id: params[:collection_id]).take
if sync_collection.present?
is_shared_collection = true
sample.collections << Collection.find(sync_collection['collection_id'])
sample.collections << Collection.get_all_collection_for_user(sync_collection['shared_by_id'])
end
end

unless is_shared_collection
all_coll = Collection.get_all_collection_for_user(current_user.id)
sample.collections << all_coll
end
end

sample.container = update_datamodel(params[:container])
Expand Down
30 changes: 21 additions & 9 deletions app/api/chemotion/screen_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class ScreenAPI < Grape::API
requires :container, type: Hash
optional :segments, type: Array, desc: 'Segments'
optional :component_graph_data, type: JSON
optional :is_sync_to_me, type: Boolean, default: false
end
post do
attributes = {
Expand All @@ -191,24 +192,35 @@ class ScreenAPI < Grape::API
screen.save!
screen.save_segments(segments: params[:segments], current_user_id: current_user.id)

#save to profile
# save to profile
kinds = screen.container&.analyses&.pluck(Arel.sql("extended_metadata->'kind'"))
recent_ols_term_update('chmo', kinds) if kinds&.length&.positive?

collection = current_user.collections.where(id: params[:collection_id]).take
screen.collections << collection if collection.present?

is_shared_collection = false
unless collection.present?
if params[:is_sync_to_me]
Rails.logger.debug('Creating screen in sync collection due to user request (param :is_sync_to_me is set).')
sync_collection = current_user.all_sync_in_collections_users.where(id: params[:collection_id]).take
if sync_collection.present?
is_shared_collection = true
screen.collections << Collection.find(sync_collection['collection_id'])
screen.collections << Collection.get_all_collection_for_user(sync_collection['shared_by_id'])
else
error!('400 Bad Request (Cant find sync collection)', 400)
end
else
collection = current_user.collections.where(id: params[:collection_id]).take
screen.collections << collection if collection.present?

is_shared_collection = false
if collection.blank?
sync_collection = current_user.all_sync_in_collections_users.where(id: params[:collection_id]).take
if sync_collection.present?
is_shared_collection = true
screen.collections << Collection.find(sync_collection['collection_id'])
screen.collections << Collection.get_all_collection_for_user(sync_collection['shared_by_id'])
end
end
end

screen.collections << Collection.get_all_collection_for_user(current_user.id) unless is_shared_collection
screen.collections << Collection.get_all_collection_for_user(current_user.id) unless is_shared_collection
end

params[:wellplate_ids].each do |id|
ScreensWellplate.find_or_create_by(wellplate_id: id, screen_id: screen.id)
Expand Down
1 change: 1 addition & 0 deletions app/api/chemotion/wellplate_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class WellplateAPI < Grape::API # rubocop:disable Metrics/ClassLength
optional :height, type: Integer, default: 8, values: 1..100
optional :width, type: Integer, default: 12, values: 1..100
optional :segments, type: Array, desc: 'Segments'
optional :is_sync_to_me, type: Boolean, default: false
end
post do
container = params[:container]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ export default class ReactionDetails extends Component {

const { reaction } = this.state;
if (reaction && reaction.isNew) {
const { currentCollection } = UIStore.getState();
reaction["is_sync_to_me"] = currentCollection ? Boolean(currentCollection["is_sync_to_me"]) : false;
ElementActions.createReaction(reaction);
} else {
ElementActions.updateReaction(reaction, closeView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export default class ResearchPlanDetails extends Component {
this.context.attachmentNotificationStore.clearMessages();

if (researchPlan.isNew) {
const { currentCollection } = UIStore.getState();
researchPlan["is_sync_to_me"] = currentCollection ? Boolean(currentCollection["is_sync_to_me"]) : false;
ElementActions.createResearchPlan(researchPlan);
} else {
ElementActions.updateResearchPlan(researchPlan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ export default class SampleDetails extends React.Component {
const wellplate = sample.belongTo;
ElementActions.updateSampleForWellplate(sample, wellplate);
} else if (sample.isNew) {
const { currentCollection } = UIStore.getState();
sample["is_sync_to_me"] = currentCollection ? Boolean(currentCollection["is_sync_to_me"]) : false;
ElementActions.createSample(sample, closeView);
} else {
sample.cleanBoilingMelting();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export default class ScreenDetails extends Component {
LoadingActions.start();

if (screen.isNew) {
const { currentCollection } = UIStore.getState();
screen["is_sync_to_me"] = currentCollection ? Boolean(currentCollection["is_sync_to_me"]) : false;
ElementActions.createScreen(screen);
} else {
ElementActions.updateScreen(screen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export default class WellplateDetails extends Component {
this.context.attachmentNotificationStore.clearMessages();
LoadingActions.start();
if (wellplate.isNew) {
const { currentCollection } = UIStore.getState();
wellplate["is_sync_to_me"] = currentCollection ? Boolean(currentCollection["is_sync_to_me"]) : false;
ElementActions.createWellplate(wellplate);
} else {
ElementActions.updateWellplate(wellplate);
Expand Down
30 changes: 30 additions & 0 deletions app/packs/src/components/generic/GenericElDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const onNaviClick = (type, id) => {
export default class GenericElDetails extends Component {
constructor(props) {
super(props);
// generic type
this.type = props.genericEl.type;

this.state = {
genericEl: props.genericEl,
activeTab: 0,
Expand All @@ -82,6 +85,33 @@ export default class GenericElDetails extends Component {
this.handleExport = this.handleExport.bind(this);
}

/**
* This method retrieves values for the detailed
* layout. If the user profile lacks these values,
* they are automatically added.
*/
setupDetailLayoutProfile() {

const userProfile = UserStore.getState().profile;
const layout = userProfile && userProfile.data && userProfile.data[`layout_detail_${this.type}`];
if(!layout) {
const layoutName = `data.layout_detail_${this.type}`;
const defaultLayout = {
properties: 1, analyses: 2, attachments: 3
};
const currentCollection = UIStore.getState().currentCollection;
let tabSegment = currentCollection?.tabs_segment;
lowdashset(tabSegment, `${this.type}`, defaultLayout);
tabSegment = { ...tabSegment, [`${this.type}`]: defaultLayout };
if (currentCollection && !currentCollection.is_sync_to_me) {
CollectionActions.updateTabsSegment({ segment: tabSegment, cId: currentCollection.id });
}
lowdashset(userProfile, layoutName, defaultLayout);

UserActions.updateUserProfile(userProfile);
}
}

componentDidMount() {
UIStore.listen(this.onChangeUI);
ElementStore.listen(this.onChangeElement);
Expand Down
2 changes: 1 addition & 1 deletion app/packs/src/fetchers/ReactionsFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class ReactionsFetcher {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(reaction.serialize())
body: JSON.stringify({...reaction.serialize(), is_sync_to_me: reaction["is_sync_to_me"]})
}).then((response) => response.json())
.then((json) => GenericElsFetcher.uploadGenericFiles(reaction, json.reaction.id, 'Reaction')
.then(() => ReactionsFetcher.updateAnnotationsInReaction(reaction))
Expand Down
2 changes: 1 addition & 1 deletion app/packs/src/fetchers/ResearchPlansFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class ResearchPlansFetcher {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(researchPlan.serialize())
body: JSON.stringify({...researchPlan.serialize(), is_sync_to_me: researchPlan["is_sync_to_me"]})
})
.then((response) => response.json())
.then((json) => AttachmentFetcher.updateAttachables(
Expand Down
2 changes: 1 addition & 1 deletion app/packs/src/fetchers/SamplesFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default class SamplesFetcher {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(sample.serialize())
body: JSON.stringify({...sample.serialize(), is_sync_to_me: sample["is_sync_to_me"]})
}).then((response) => response.json())
.then((json) => GenericElsFetcher.uploadGenericFiles(sample, json.sample.id, 'Sample')
.then(() => this.fetchById(json.sample.id))).catch((errorMessage) => {
Expand Down
2 changes: 1 addition & 1 deletion app/packs/src/fetchers/ScreensFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class ScreensFetcher {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(screen.serialize())
body: JSON.stringify({...screen.serialize(), is_sync_to_me: screen["is_sync_to_me"]})
}).then(response => response.json())
.then(json => GenericElsFetcher.uploadGenericFiles(screen, json.screen.id, 'Screen')
.then(() => this.fetchById(json.screen.id))).catch((errorMessage) => {
Expand Down
Loading
Loading