generated from BCDevOps/opendev-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsubmission_version.rb
More file actions
137 lines (109 loc) · 3.54 KB
/
submission_version.rb
File metadata and controls
137 lines (109 loc) · 3.54 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class SubmissionVersion < ApplicationRecord
belongs_to :permit_application
has_many :revision_requests, dependent: :destroy
has_many :supporting_documents, dependent: :destroy
accepts_nested_attributes_for :revision_requests, allow_destroy: true
after_commit :notify_user_application_viewed
delegate :sandbox, to: :permit_application
scope :sandboxed,
-> do
joins(:permit_application).where.not(
permit_applications: {
sandbox_id: nil
}
)
end
scope :live,
-> do
joins(:permit_application).where(
permit_applications: {
sandbox_id: nil
}
)
end
scope :for_sandbox,
->(sandbox) do
joins(:permit_application).where(
permit_applications: {
sandbox_id: sandbox.id
}
)
end
def missing_pdfs
missing_data_keys = []
existing_application_pdf =
supporting_documents.find_by(
data_key: SupportingDocument::APPLICATION_PDF_DATA_KEY
)
if existing_application_pdf.blank? || existing_application_pdf.file.blank?
missing_data_keys << "#{SupportingDocument::APPLICATION_PDF_DATA_KEY}_#{id}"
end
existing_checklist_pdf =
supporting_documents.find_by(
data_key: SupportingDocument::CHECKLIST_PDF_DATA_KEY
)
if has_step_code_checklist? &&
(existing_checklist_pdf.blank? || existing_checklist_pdf.file.blank?)
missing_data_keys << "#{SupportingDocument::CHECKLIST_PDF_DATA_KEY}_#{id}"
end
missing_data_keys
end
def missing_permit_application_pdf?
existing_document =
supporting_documents.find_by(
data_key: SupportingDocument::APPLICATION_PDF_DATA_KEY
)
existing_document.blank? || existing_document.file.blank?
end
def missing_step_code_checklist_pdf?
return false unless has_step_code_checklist?
existing_document =
supporting_documents.find_by(
data_key: SupportingDocument::CHECKLIST_PDF_DATA_KEY
)
existing_document.blank? || existing_document.file.blank?
end
def has_step_code_checklist?
step_code_checklist_json.present? && !step_code_checklist_json.empty?
end
def missing_pdfs?
!missing_pdfs.empty?
end
def formatted_submission_data(current_user: nil)
PermitApplication::SubmissionDataService.new(
permit_application
).formatted_submission_data(
current_user: current_user,
submission_data: submission_data
)
end
def version_number
permit_application
.submission_versions
.order(:created_at)
.pluck(:id)
.index(id) + 1
end
def revision_requests_for_submitter_based_on_user_permissions(user: nil)
return revision_requests if user.blank?
permissions =
permit_application.submission_requirement_block_edit_permissions(
user_id: user.id
)
return revision_requests if permissions == :all
return [] if permissions.blank?
revision_requests.select do |r|
return false if r.requirement_json["key"].blank?
rb_id = r.requirement_json["key"][/RB([a-zA-Z0-9\-]+)/, 1]
permissions.include?(rb_id)
end
end
def notify_user_application_viewed
return if new_record?
viewed_at_change = previous_changes.dig("viewed_at")
# Check if the `viewed_at` was `nil` before the change and is now not `nil`.
if (viewed_at_change&.first.nil? && viewed_at_change&.last.present?)
NotificationService.publish_application_view_event(permit_application)
end
end
end