From f015c8c56e8487dc7b6641c8d82c01afa2243f2b Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Fri, 3 Nov 2023 09:46:05 +0100 Subject: [PATCH] Create a copy of the synced file in case of conflict [SCI-9640] --- app/controllers/asset_sync_controller.rb | 28 +++++++++++++++++++++++- app/models/asset.rb | 4 ++++ config/locales/en.yml | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/app/controllers/asset_sync_controller.rb b/app/controllers/asset_sync_controller.rb index 1eff86d04..5cf619ccc 100644 --- a/app/controllers/asset_sync_controller.rb +++ b/app/controllers/asset_sync_controller.rb @@ -24,7 +24,13 @@ class AssetSyncController < ApplicationController end def update - head(:conflict) and return if @asset_sync_token.conflicts?(request.headers['VersionToken']) + if @asset_sync_token.conflicts?(request.headers['VersionToken']) + render( + json: AssetSyncTokenSerializer.new(conflicting_asset_copy_token).as_json, + status: :conflict + ) + return + end @asset.file.attach(io: request.body, filename: @asset.file.filename) @asset.touch @@ -34,6 +40,26 @@ class AssetSyncController < ApplicationController # private + def conflicting_asset_copy_token + Asset.transaction do + new_asset = @asset.dup + new_asset.save + new_asset.file.attach( + io: request.body, + filename: "#{@asset.file.filename.base} (#{t('general.copy')}).#{@asset.file.filename.extension}" + ) + + case @asset.parent + when Step + StepAsset.create!(step: @asset.step, asset: new_asset) + when Result + ResultAsset.create!(result: @asset.result, asset: new_asset) + end + + current_user.asset_sync_tokens.create!(asset_id: new_asset.id) + end + end + def authenticate_asset_sync_token! @asset_sync_token = AssetSyncToken.find_by(token: request.headers['Authentication']) diff --git a/app/models/asset.rb b/app/models/asset.rb index 1429f6a11..1c6387828 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -466,6 +466,10 @@ class Asset < ApplicationRecord (result || step)&.my_module end + def parent + step || result || repository_cell + end + private def tempdir diff --git a/config/locales/en.yml b/config/locales/en.yml index bdb781e4a..773218f9e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3690,6 +3690,7 @@ en: edit: "Edit" delete: "Delete" cancel: "Cancel" + copy: "Copy" duplicate: "Duplicate" okay: "Okay" back: "Back"