generated from BCDevOps/opendev-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjurisdiction_template_version_customization.rb
More file actions
207 lines (177 loc) · 6.42 KB
/
jurisdiction_template_version_customization.rb
File metadata and controls
207 lines (177 loc) · 6.42 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class JurisdictionTemplateVersionCustomization < ApplicationRecord
# The expected schema of the :customizations json is the following
# {
# requirement_block_changes?: Record<UUID, {
# tip?: string
# enabled_elective_field_ids?: Array<UUID>
# }>
# }
# Where the key to requirement_block_changes object is the id of the requirement_block affected.
# Where the elective_fields are the ids of the requirement_fields that are elective and have been
# enabled
belongs_to :sandbox, optional: true
belongs_to :jurisdiction
belongs_to :template_version
before_save :sanitize_tip
# Ensure that there is no two customizations with the same sandbox, jurisdiction, and template_version
validate :unique_combination_of_jurisdiction_sandbox_and_template_version
after_commit :reindex_jurisdiction_templates_used_size
after_commit :publish_customization_event, on: %i[update]
validate :ensure_reason_set_for_enabled_elective_fields
validate :sandbox_belongs_to_jurisdiction
scope :sandboxed, -> { where.not(sandbox_id: nil) }
scope :live, -> { where(sandbox_id: nil) }
scope :for_sandbox, ->(sandbox) { where(sandbox_id: sandbox&.id) }
ACCEPTED_ENABLED_ELECTIVE_FIELD_REASONS = %w[bylaw policy zoning].freeze
def elective_enabled?(requirement_block_id, requirement_id)
if customizations.blank? ||
customizations["requirement_block_changes"].blank?
return false
end
!!customizations.dig(
"requirement_block_changes",
requirement_block_id,
"enabled_elective_field_ids"
)&.include?(requirement_id)
end
def update_event_notification_data
{
"id" => SecureRandom.uuid,
"action_type" => Constants::NotificationActionTypes::CUSTOMIZATION_UPDATE,
"action_text" =>
"#{I18n.t("notification.template_version.new_customization_notification", jurisdiction_name: jurisdiction.qualified_name, template_label: template_version.label)}",
"object_data" => {
"template_version_id" => template_version.id,
"customizations" => customizations
}
}
end
def label
"#{jurisdiction.name} #{template_version.label}"
end
def self.requirement_count_by_reason(
requirement_block_id,
requirement_id,
reason
)
return 0 unless ACCEPTED_ENABLED_ELECTIVE_FIELD_REASONS.include?(reason)
JurisdictionTemplateVersionCustomization
.joins(:template_version)
.where(template_versions: { status: "published" })
.where(
"customizations -> 'requirement_block_changes' -> :requirement_block_id -> 'enabled_elective_field_ids' @> :id",
requirement_block_id: requirement_block_id,
id: "[\"#{requirement_id}\"]"
)
.select do |jtvc|
jtvc
.customizations
.dig(
"requirement_block_changes",
requirement_block_id,
"enabled_elective_field_reasons"
)
&.values
&.include?(reason)
end
.count
end
def self.count_of_jurisdictions_using_requirement(
requirement_block_id,
requirement_id
)
JurisdictionTemplateVersionCustomization
.joins(:template_version)
.where(template_versions: { status: "published" })
.where(
"customizations -> 'requirement_block_changes' -> :requirement_block_id -> 'enabled_elective_field_ids' @> :id",
requirement_block_id: requirement_block_id,
id: "[\"#{requirement_id}\"]"
)
.count
end
def promote
# Find or create a record with same jurisdiction_id and template_version_id but with sandbox_id == nil
target_record =
JurisdictionTemplateVersionCustomization.find_or_create_by(
jurisdiction_id: jurisdiction_id,
template_version_id: template_version_id,
sandbox_id: nil
)
target_record.customizations = customizations
target_record.save!
end
private
def reindex_jurisdiction_templates_used_size
return unless jurisdiction.present?
return unless new_record? || destroyed? || saved_change_to_jurisdiction_id?
jurisdiction.reindex
end
def sanitize_tip
if customizations.blank? ||
customizations["requirement_block_changes"].blank?
return
end
customizations["requirement_block_changes"].each do |key, value|
next if value["tip"].blank?
customizations["requirement_block_changes"][key][
"tip"
] = ActionController::Base.helpers.sanitize(value["tip"])
end
end
def ensure_reason_set_for_enabled_elective_fields
if customizations.blank? ||
customizations["requirement_block_changes"].blank?
return
end
customizations["requirement_block_changes"].each do |_key, value|
next if value["enabled_elective_field_ids"].blank?
any_missing_or_incorrect_reason =
value["enabled_elective_field_ids"].any? do |field_id|
return true if value["enabled_elective_field_reasons"].blank?
value["enabled_elective_field_reasons"][field_id].blank? ||
ACCEPTED_ENABLED_ELECTIVE_FIELD_REASONS.none? do |reason|
reason == value["enabled_elective_field_reasons"][field_id]
end
end
if any_missing_or_incorrect_reason
errors.add(
:customizations,
I18n.t(
"model_validation.jurisdiction_template_version_customization.enabled_elective_field_reason_incorrect",
accepted_reasons: ACCEPTED_ENABLED_ELECTIVE_FIELD_REASONS.join(", ")
)
)
end
end
end
def publish_customization_event
NotificationService.publish_customization_update_event(self)
end
def unique_combination_of_jurisdiction_sandbox_and_template_version
# Construct the query for finding duplicates
existing_record =
JurisdictionTemplateVersionCustomization.where(
jurisdiction_id: jurisdiction_id,
template_version_id: template_version_id,
sandbox_id: sandbox_id
)
# Allow updates on the same record (ignore self)
existing_record = existing_record.where.not(id: id) if persisted?
# If such a record exists, add an error
if existing_record.exists?
errors.add(
:base,
I18n.t(
"activerecord.errors.models.jurisdiction_template_version_customizations.uniqueness"
)
)
end
end
def sandbox_belongs_to_jurisdiction
return unless sandbox
unless jurisdiction.sandboxes.include?(sandbox)
errors.add(:sandbox, "must belong to the jurisdiction")
end
end
end