Skip to content

Commit

Permalink
fix: you can now create entities in sync collections if you own a col…
Browse files Browse the repository at this point in the history
…lection with same id as the id of the share
  • Loading branch information
ptrxyz committed Nov 7, 2024
1 parent e5d86dd commit be59ce7
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 68 deletions.
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
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
2 changes: 1 addition & 1 deletion app/packs/src/fetchers/WellplatesFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class WellplatesFetcher {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(wellplate.serialize())
body: JSON.stringify({...wellplate.serialize(), is_sync_to_me: wellplate["is_sync_to_me"]})
})
.then((response) => response.json())
.then((json) => {
Expand Down
40 changes: 27 additions & 13 deletions app/usecases/wellplates/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,39 @@ def initialize(params, current_user)

def execute! # rubocop:disable Metrics/AbcSize
ActiveRecord::Base.transaction do
wellplate = Wellplate.create(params.except(:collection_id, :wells, :segments, :size))
is_sync_to_me = params[:is_sync_to_me]

wellplate = Wellplate.create(params.except(:collection_id, :wells, :segments, :is_sync_to_me))
wellplate.set_short_label(user: @current_user)
wellplate.reload
wellplate.save_segments(segments: params[:segments], current_user_id: @current_user.id)

is_shared_collection = false
if user_collection
CollectionsWellplate.create(wellplate: wellplate, collection: user_collection)
elsif sync_collection_user
is_shared_collection = true
CollectionsWellplate.create(wellplate: wellplate, collection: sync_collection_user.collection)
if is_sync_to_me
Rails.logger.debug('Creating wellplate 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
error!('400 Bad Request (Cant find sync collection)', 400) if sync_collection.blank?

CollectionsWellplate.create(wellplate: wellplate, collection: all_collection_of_sharer)
end
collection = Collection.find(sync_collection['collection_id'])
CollectionsWellplate.create(wellplate: wellplate, collection: collection) if collection.present?

sync_out_collection_sharer = Collection.get_all_collection_for_user(sync_collection['shared_by_id'])
CollectionsWellplate.create(wellplate: wellplate, collection: sync_out_collection_sharer)
else
is_shared_collection = false
if user_collection
CollectionsWellplate.create(wellplate: wellplate, collection: user_collection)
elsif sync_collection_user
is_shared_collection = true
CollectionsWellplate.create(wellplate: wellplate, collection: sync_collection_user.collection)

CollectionsWellplate.create(
wellplate: wellplate,
collection: all_collection_of_current_user
) unless is_shared_collection
CollectionsWellplate.create(wellplate: wellplate, collection: all_collection_of_sharer)
end

CollectionsWellplate.create(
wellplate: wellplate,
collection: all_collection_of_current_user
) unless is_shared_collection
end

WellplateUpdater
.new(wellplate: wellplate, current_user: current_user)
Expand Down

0 comments on commit be59ce7

Please sign in to comment.