-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_service.rb
93 lines (73 loc) · 3.33 KB
/
create_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true
module DataExports
# Service used to Create Data Export
class CreateService < BaseService
DataExportCreateError = Class.new(StandardError)
def initialize(user = nil, params = {})
super
end
def execute
@data_export = DataExport.new(params)
assign_initial_export_attributes
if @data_export.valid?
@data_export.export_type == 'analysis' ? validate_analysis_ids : validate_sample_ids
@data_export.save
DataExports::CreateJob.perform_later(@data_export)
end
@data_export
rescue DataExports::CreateService::DataExportCreateError => e
@data_export.errors.add(:base, e.message)
@data_export
end
private
# sample and linelist exports pass the namespace the user is exporting from and authorize the selected samples
# based on the namespace
def validate_sample_ids
namespace = Namespace.find(params['export_parameters']['namespace_id'])
authorize! namespace, to: :export_data?
samples = authorized_export_samples(namespace, params['export_parameters']['ids'])
return unless samples.count != params['export_parameters']['ids'].count
raise DataExportCreateError,
I18n.t('services.data_exports.create.invalid_export_samples')
end
def validate_analysis_ids
workflow_executions = if params['export_parameters']['analysis_type'] == 'project'
authorized_export_project_workflows
else
authorized_export_user_workflows
end
if workflow_executions.count != params['export_parameters']['ids'].count
raise DataExportCreateError,
I18n.t('services.data_exports.create.invalid_export_workflow_executions')
end
validate_workflow_executions_state(workflow_executions)
end
def assign_initial_export_attributes
@data_export.user = current_user
@data_export.status = 'processing'
@data_export.name = nil if params.key?('name') && params['name'].empty?
end
def authorized_export_samples(namespace, sample_ids)
authorized_scope(Sample, type: :relation, as: :namespace_samples,
scope_options: { namespace:, minimum_access_level: Member::AccessLevel::ANALYST })
.where(id: sample_ids)
end
def authorized_export_project_workflows
project_namespace = Namespace.find(params['export_parameters']['namespace_id'])
authorize! project_namespace, to: :export_data?
authorized_scope(WorkflowExecution, type: :relation, as: :automated,
scope_options: { project: project_namespace.project })
.where(id: params['export_parameters']['ids'])
end
def authorized_export_user_workflows
authorized_scope(WorkflowExecution, type: :relation, as: :user, scope_options: { user: current_user })
.where(id: params['export_parameters']['ids'])
end
def validate_workflow_executions_state(workflow_executions)
completed_workflow_executions = workflow_executions.where(state: 'completed')
return if completed_workflow_executions.count == workflow_executions.count
raise DataExportCreateError,
I18n.t('services.data_exports.create.non_completed_workflow_executions')
end
end
end