-
Notifications
You must be signed in to change notification settings - Fork 3
/
destroy_service.rb
80 lines (61 loc) · 2.61 KB
/
destroy_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
# frozen_string_literal: true
module Samples
# Service used to Delete Samples
class DestroyService < BaseService
attr_accessor :sample, :sample_ids, :project
def initialize(project, user = nil, params = {})
super(user, params)
@project = project
@sample = params[:sample] if params[:sample]
@sample_ids = params[:sample_ids] if params[:sample_ids]
end
def execute
authorize! project, to: :destroy_sample?
sample.nil? ? destroy_multiple : destroy_single
end
private
def destroy_single
sample_destroyed = sample.destroy
if sample_destroyed
Sample.search_index.refresh
@project.namespace.create_activity key: 'namespaces_project_namespace.samples.destroy',
owner: current_user,
parameters:
{
sample_puid: sample.puid,
action: 'sample_destroy'
}
end
update_samples_count if @project.parent.type == 'Group'
update_metadata_summary(sample)
end
def destroy_multiple # rubocop:disable Metrics/MethodLength
samples = Sample.where(id: sample_ids).where(project_id: project.id)
samples_to_delete_count = samples.count
samples_deleted_puids = []
samples = samples.destroy_all
samples.each do |sample|
# Sample.searchkick_index.remove(sample)
update_metadata_summary(sample)
samples_deleted_puids << sample.puid
end
Sample.search_index.refresh
update_samples_count(samples_to_delete_count) if @project.parent.type == 'Group'
@project.namespace.create_activity key: 'namespaces_project_namespace.samples.destroy_multiple',
owner: current_user,
parameters:
{
deleted_count: samples_to_delete_count,
samples_deleted_puids:,
action: 'sample_destroy_multiple'
}
samples_to_delete_count
end
def update_metadata_summary(sample)
sample.project.namespace.update_metadata_summary_by_sample_deletion(sample) if sample.deleted?
end
def update_samples_count(deleted_samples_count = 1)
@project.parent.update_samples_count_by_destroy_service(deleted_samples_count)
end
end
end