-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update existing data requests (#2503)
Co-authored-by: Andrew Pepler <[email protected]>
- Loading branch information
1 parent
1dc0b66
commit 1d04c1e
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
db/data_migrations/20241024131255_migrate_data_request_areas.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
class MigrateDataRequestAreas < ActiveRecord::DataMigration | ||
def data_request_area_type_for(request_type) | ||
case request_type | ||
when *DataRequest::BRANSTON_DATA_REQUEST_TYPES | ||
"branston" | ||
when *DataRequest::BRANSTON_REGISTRY_DATA_REQUEST_TYPES | ||
"branston_registry" | ||
when *DataRequest::MAPPA_DATA_REQUEST_TYPES | ||
"mappa" | ||
when *DataRequest::PRISON_DATA_REQUEST_TYPES + %w[cctv_and_bwcf] - %w[other] | ||
"prison" | ||
when *DataRequest::PROBATION_DATA_REQUEST_TYPES + %w[court] - %w[other] | ||
"probation" | ||
when "other" | ||
"other_department" | ||
end | ||
end | ||
|
||
def up | ||
# loop through the DataRequest records | ||
DataRequest.find_each do |request| | ||
# Skip if the DataRequest already has a DataRequestArea | ||
if request.data_request_area_id.present? | ||
next | ||
end | ||
|
||
if request.contact_id.nil? && request.location.blank? | ||
request.update!(location: "Unknown") # some older requests have no location or contact_id | ||
end | ||
|
||
# Get the data_request_area_type for this request | ||
data_request_area_type = data_request_area_type_for(request.request_type) | ||
|
||
data_request_area = DataRequestArea.create!( | ||
user_id: request.user_id, | ||
case_id: request.case_id, | ||
data_request_area_type:, | ||
contact_id: request.contact_id, | ||
location: request.location.presence, | ||
) | ||
|
||
# Associate existing commissioning_document if present | ||
if request.commissioning_document.present? | ||
data_request_area.commissioning_document.destroy! | ||
request.commissioning_document.update!(data_request_area_id: data_request_area.id) | ||
end | ||
|
||
# Associate existing data_request_emails if present | ||
request.data_request_emails.each do |email| | ||
email.update!(data_request_area_id: data_request_area.id) | ||
end | ||
|
||
# Update DataRequest with the correct data_request_area_id | ||
request.update_attribute(:data_request_area_id, data_request_area.id) # rubocop:disable Rails/SkipsModelValidations | ||
end | ||
end | ||
end |