From 70beec648adc9a7d0243b6f402098e3d2b06b4b9 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 10 Apr 2024 11:25:59 +0200 Subject: [PATCH] Fix draft creation to prevent dupciates [SCI-10598] --- app/controllers/protocols_controller.rb | 6 +++++- app/javascript/vue/protocol/protocolMetadata.vue | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index b86cd8ce0..b0ade7fb4 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -371,7 +371,11 @@ class ProtocolsController < ApplicationController def save_as_draft Protocol.transaction do - draft = @protocol.save_as_draft(current_user) + draft = nil + + @protocol.with_lock do + draft = @protocol.save_as_draft(current_user) + end if draft.invalid? render json: { error: draft.errors.messages.map { |_, value| value }.join(' ') }, status: :unprocessable_entity diff --git a/app/javascript/vue/protocol/protocolMetadata.vue b/app/javascript/vue/protocol/protocolMetadata.vue index f6645a258..54bf6fad2 100644 --- a/app/javascript/vue/protocol/protocolMetadata.vue +++ b/app/javascript/vue/protocol/protocolMetadata.vue @@ -25,7 +25,7 @@ @click="$emit('publish')" class="btn btn-primary"> {{ i18n.t("protocols.header.publish") }} @@ -121,7 +121,8 @@ export default { }, data() { return { - VersionsModalObject: null + VersionsModalObject: null, + creatingDraft: false }; }, computed: { @@ -141,8 +142,18 @@ export default { }, methods: { saveAsdraft() { + if (this.creatingDraft) { + return; + } + + this.creatingDraft = true; + $.post(this.protocol.attributes.urls.save_as_draft_url, (result) => { + this.creatingDraft = false; window.location.replace(result.url); + }).error(() => { + this.creatingDraft = false; + HelperModule.flashAlertMsg(this.i18n.t('errors.general')); }); }, updateAuthors(authors) {