2023-08-02 20:13:04 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class GeneSequenceAssetsController < ApplicationController
|
|
|
|
include ActiveStorage::SetCurrent
|
|
|
|
|
|
|
|
skip_before_action :verify_authenticity_token
|
|
|
|
|
2023-08-17 15:33:20 +08:00
|
|
|
before_action :check_open_vector_service_enabled, except: %i(new edit)
|
2023-08-02 20:13:04 +08:00
|
|
|
before_action :load_vars, except: %i(new create)
|
|
|
|
before_action :load_create_vars, only: %i(new create)
|
|
|
|
|
|
|
|
before_action :check_read_permission
|
|
|
|
before_action :check_manage_permission, only: %i(new update create)
|
|
|
|
|
|
|
|
def new
|
|
|
|
render :edit, layout: false
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@file_url = rails_representation_url(@asset.file)
|
|
|
|
@file_name = @asset.render_file_name
|
2023-08-29 15:42:51 +08:00
|
|
|
log_activity('sequence_asset_edit_started')
|
2023-08-02 20:13:04 +08:00
|
|
|
render :edit, layout: false
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
save_asset!
|
2023-09-27 18:47:35 +08:00
|
|
|
|
2023-09-27 07:45:29 +08:00
|
|
|
case @parent
|
|
|
|
when Step
|
|
|
|
log_activity('sequence_asset_added')
|
|
|
|
when Result
|
|
|
|
log_result_activity(
|
|
|
|
:sequence_on_result_added,
|
|
|
|
@parent,
|
|
|
|
file: @asset.file_name,
|
2023-09-27 18:47:35 +08:00
|
|
|
user: current_user.id
|
2023-09-27 07:45:29 +08:00
|
|
|
)
|
|
|
|
end
|
2023-09-27 18:47:35 +08:00
|
|
|
|
2023-08-02 20:13:04 +08:00
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
save_asset!
|
2023-09-27 07:45:29 +08:00
|
|
|
|
|
|
|
case @parent
|
|
|
|
when Step
|
|
|
|
log_activity('sequence_asset_edit_finished')
|
|
|
|
when Result
|
|
|
|
log_result_activity(
|
|
|
|
:sequence_on_result_edited,
|
|
|
|
@parent,
|
|
|
|
file: @asset.file_name,
|
2023-09-27 18:47:35 +08:00
|
|
|
user: current_user.id
|
2023-09-27 07:45:29 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-08-22 20:24:43 +08:00
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2023-08-29 15:42:51 +08:00
|
|
|
log_activity('sequence_asset_deleted')
|
2023-08-02 20:13:04 +08:00
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def save_asset!
|
|
|
|
ActiveRecord::Base.transaction do
|
2023-10-04 22:16:55 +08:00
|
|
|
view_mode = @asset.view_mode if @asset
|
|
|
|
|
2023-08-02 20:13:04 +08:00
|
|
|
ensure_asset!
|
|
|
|
|
|
|
|
@asset.file.purge
|
|
|
|
@asset.preview_image.purge
|
|
|
|
|
|
|
|
@asset.file.attach(
|
|
|
|
io: StringIO.new(params[:sequence_data].to_json),
|
|
|
|
filename: "#{params[:sequence_name]}.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
@asset.preview_image.attach(
|
|
|
|
io: StringIO.new(Base64.decode64(params[:base64_image].split(',').last)),
|
|
|
|
filename: "#{params[:sequence_name]}.png"
|
|
|
|
)
|
|
|
|
|
|
|
|
file = @asset.file
|
|
|
|
|
|
|
|
file.blob.metadata['asset_type'] = 'gene_sequence'
|
|
|
|
file.blob.metadata['name'] = params[:sequence_name]
|
|
|
|
file.save!
|
2023-10-04 22:16:55 +08:00
|
|
|
@asset.view_mode = view_mode || @parent.assets_view_mode
|
2024-01-24 22:18:34 +08:00
|
|
|
@asset.last_modified_by = current_user
|
2023-08-02 20:13:04 +08:00
|
|
|
@asset.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_asset!
|
|
|
|
return if @asset
|
|
|
|
return unless @parent
|
|
|
|
|
|
|
|
@asset = @parent.assets.create!(last_modified_by: current_user, team: current_team)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_vars
|
2023-08-17 15:33:20 +08:00
|
|
|
@ove_enabled = OpenVectorEditorService.enabled?
|
2023-08-02 20:13:04 +08:00
|
|
|
@asset = current_team.assets.find_by(id: params[:id])
|
|
|
|
return render_404 unless @asset
|
|
|
|
|
|
|
|
@parent ||= @asset.step
|
|
|
|
@parent ||= @asset.result
|
|
|
|
|
|
|
|
case @parent
|
|
|
|
when Step
|
|
|
|
@protocol = @parent.protocol
|
|
|
|
when Result
|
|
|
|
@my_module = @parent.my_module
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_create_vars
|
2023-08-17 15:33:20 +08:00
|
|
|
@ove_enabled = OpenVectorEditorService.enabled?
|
2023-08-02 20:13:04 +08:00
|
|
|
@parent = case params[:parent_type]
|
|
|
|
when 'Step'
|
|
|
|
Step.find_by(id: params[:parent_id])
|
|
|
|
when 'Result'
|
|
|
|
Result.find_by(id: params[:parent_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
case @parent
|
|
|
|
when Step
|
|
|
|
@protocol = @parent.protocol
|
|
|
|
when Result
|
|
|
|
@result = @parent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_read_permission
|
|
|
|
case @parent
|
|
|
|
when Step
|
|
|
|
return render_403 unless can_read_protocol_in_module?(@protocol) ||
|
|
|
|
can_read_protocol_in_repository?(@protocol)
|
|
|
|
when Result
|
2023-09-14 19:45:40 +08:00
|
|
|
return render_403 unless can_read_result?(@parent)
|
2023-08-02 20:13:04 +08:00
|
|
|
else
|
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_manage_permission
|
2023-08-09 20:37:57 +08:00
|
|
|
render_403 unless asset_managable?
|
2023-08-02 21:46:07 +08:00
|
|
|
end
|
|
|
|
|
2023-08-09 20:37:57 +08:00
|
|
|
def check_open_vector_service_enabled
|
|
|
|
render_403 unless OpenVectorEditorService.enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
helper_method :asset_managable?
|
|
|
|
def asset_managable?
|
2023-08-02 20:13:04 +08:00
|
|
|
case @parent
|
|
|
|
when Step
|
2023-08-02 21:46:07 +08:00
|
|
|
can_manage_step?(@parent)
|
2023-08-02 20:13:04 +08:00
|
|
|
when Result
|
2023-09-14 19:45:40 +08:00
|
|
|
can_manage_result?(@parent)
|
2023-08-02 20:13:04 +08:00
|
|
|
else
|
2023-08-02 21:46:07 +08:00
|
|
|
false
|
2023-08-02 20:13:04 +08:00
|
|
|
end
|
|
|
|
end
|
2023-08-22 20:24:43 +08:00
|
|
|
|
2023-08-29 15:42:51 +08:00
|
|
|
def log_activity(type_of, project = nil, message_items = {})
|
2023-08-22 20:24:43 +08:00
|
|
|
return unless @parent.is_a?(Step)
|
|
|
|
|
|
|
|
my_module = @parent.my_module
|
|
|
|
default_items = {
|
2023-08-29 15:42:51 +08:00
|
|
|
protocol: @parent.protocol.id,
|
2023-08-22 20:24:43 +08:00
|
|
|
step: @parent.id,
|
|
|
|
asset_name: { id: @asset.id, value_for: 'file_name' },
|
|
|
|
step_position: { id: @parent.id, value_for: 'position_plus_one' }
|
|
|
|
}
|
2023-08-29 15:42:51 +08:00
|
|
|
|
|
|
|
if my_module
|
|
|
|
project = my_module.project
|
|
|
|
default_items[:my_module] = my_module.id
|
|
|
|
type_of = "task_#{type_of}".to_sym
|
|
|
|
else
|
|
|
|
type_of = "protocol_#{type_of}".to_sym
|
|
|
|
end
|
|
|
|
|
2023-08-22 20:24:43 +08:00
|
|
|
message_items = default_items.merge(message_items)
|
|
|
|
|
2023-08-29 15:42:51 +08:00
|
|
|
Activities::CreateActivityService.call(
|
|
|
|
activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
team: @parent.protocol.team,
|
|
|
|
subject: @parent.protocol,
|
|
|
|
message_items: message_items,
|
|
|
|
project: project
|
|
|
|
)
|
2023-08-22 20:24:43 +08:00
|
|
|
end
|
2023-09-27 07:45:29 +08:00
|
|
|
|
|
|
|
def log_result_activity(type_of, result, message_items)
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: result,
|
|
|
|
team: result.my_module.team,
|
|
|
|
project: result.my_module.project,
|
|
|
|
message_items: {
|
|
|
|
result: result.id
|
|
|
|
}.merge(message_items))
|
|
|
|
end
|
2023-08-02 20:13:04 +08:00
|
|
|
end
|