Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update existing data requests #2503

Draft
wants to merge 32 commits into
base: cdpt-1850-commissioning-and-chase-redesign
Choose a base branch
from

Conversation

lucas-shaw
Copy link
Contributor

Description

All existing data requests need to be associated to their relevant data request area

Self-review checklist

  • (1) Quick stakeholder demo done OR
  • (2) ...bug with before and after screenshots
  • (3) Tests passing
  • (4) Branch ready to be merged (not work in progress)
  • (5) No superfluous changes in diff
  • (6) No TODO's without new ticket numbers
  • (7) PR Prefixed with ticket number e.g. CT-7654 ...
  • (8) Data migration script is created if any of letter templates is changed

Screenshots

Related JIRA tickets

https://dsdmoj.atlassian.net/jira/software/c/projects/CDPT/boards/1152?selectedIssue=CDPT-1858

Deployment

Manual testing instructions

@lucas-shaw lucas-shaw changed the base branch from main to cdpt-1850-commissioning-and-chase-redesign October 23, 2024 09:08
@lucas-shaw
Copy link
Contributor Author

lucas-shaw commented Oct 23, 2024

I havent added in the below areas as they dont exist in the live data

SECURITY_DATA_REQUEST_TYPES = %w[g2_security g3_security].freeze
OTHER_DEPARTMENT_DATA_REQUEST_TYPES = %w[other_department].freeze

request.update!(data_request_area_id: data_request_area.id)

# TEMP LOGGING INFO
Rails.logger.debug "Updated DataRequest ##{request.id} - #{request.request_type}, with data_request_area_id: #{data_request_area.id} (area_type: #{data_request_area_type}, contact_id: #{data_request_area.contact_id}, data_request_area.user_id: #{data_request_area.user_id}, data_request_area.case_id: #{data_request_area.case_id}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left in for local testing purposes, will remove when ready

@lucas-shaw
Copy link
Contributor Author

lucas-shaw commented Oct 24, 2024

The script used to generate data requests for local testing purposes.

This has to be used as creating a DataRequest normally through the service, no longer gives a value to location or contact_id, as this is now done when creating a DataRequestArea

# SEED FOR LOCAL TESTING
request_types = %w[all_prison_records cctv bwcf probation_records security_records mappa telephone_recordings probation_archive dps cross_borders cctv_and_bwcf court other]

# Create multiple DataRequest records
request_types.cycle(10) do |request_type|

  # Create DataRequest
  data_request = DataRequest.create!(
    user_id: 19, # RELEVANT USER ID NEEDED
    case_id: [1, 2, 3, 4, 5, 6, 7].sample, # Covers the different case states - ready_to_dispatch, ready_to_copy, waiting_for_data, data_to_be_requested, ready_for_vetting, vetting_in_progress, closed
    request_type:,
    date_requested: Time.zone.today - rand(50..100),
    cached_num_pages: rand(1..10),
    completed: false,
    contact_id: [1, 2, 3].sample,
    request_type_note: "other information",
    location: ["location_one", "location_two", nil].sample,
  )

  # Create a commissioning document for a data request rather than an area to imitate existing process
  # purposefully dont always create a commissioning document
  if [true, false].sample
    # Create a commissioning document only for specific types if needed
    doc = CommissioningDocument.new(
      data_request:,
      template_name: %w[cctv cat_a cross_border mappa pdp prison probation security telephone].sample,
      attachment_id: [1, 2, 3].sample,
    )
    doc.save!(validate: false) # skip validation to allow saving without a data_request_area
  end

  # Log creation for verification
  Rails.logger.debug "Created DataRequest ##{data_request.id} (#{request_type}, case_id: #{case_id}, completed: #{data_request.completed})"
end


lucas-shaw and others added 4 commits October 24, 2024 14:37
…ministryofjustice/correspondence_tool_staff into cdpt-1858-update-existing-data-requests
# Get the data_request_area_type for this request
data_request_area_type = data_request_area_type_for(request.request_type)

Rails.logger.debug "Finding DataRequestArea with user_id: #{request.user_id}, case_id: #{request.case_id}, contact_id: #{request.contact_id.presence}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left in for local testing purposes, will remove when ready

data_request_area.commissioning_document = request.commissioning_document
else
template_name = data_request_area_type == "mappa" ? "mappa" : "standard"
data_request_area.build_commissioning_document(template_name: template_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this happen automatically with the after_create block on data_request_area?
You will need to adjust that to allow for an existing commissioning_document to be linked without a new one being created.

end

# Ensure that any uploaded attachments are kept
if request.commissioning_document&.attachment
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem necessary. Aren't request.commissioning_document and data_request_area.commissioning_document the same object?

@lucas-shaw
Copy link
Contributor Author

After the migration, should we delete all of the commissioning documents from the DataRequests?

@lucas-shaw
Copy link
Contributor Author

lucas-shaw commented Nov 7, 2024

Test script to confirm that the commissioning_documents have been successfully transferred from data_request to data_request_area

migrated_data_requests = DataRequest.where.associated(:commissioning_document).pluck(:id, :data_request_area_id)

# Check each DataRequest and its DataRequestArea have the same commissioning_document
mismatched_documents = migrated_data_requests.select do |data_request_id, data_request_area_id|
  data_request = DataRequest.find(data_request_id)
  data_request_area = DataRequestArea.find(data_request_area_id)

  # check the commissioning_document matches
  data_request.commissioning_document.id != data_request_area.commissioning_document&.id
end

# Log to confirm they match
if mismatched_documents.empty?
  puts "they all match"
else
  puts "there is a mismatch"
  mismatched_documents.each { |data_request_id, data_request_area_id| puts "DataRequest id: #{data_request_id}, DataRequestArea id: #{data_request_area_id}" }
end

@lucas-shaw
Copy link
Contributor Author

lucas-shaw commented Nov 7, 2024

Another test script to ensure the 1 to 1 transfer is done. After the migration the count should be the same.

DataRequest.where.associated(:commissioning_document).count should equalDataRequestArea.where.associated(:commissioning_document).count

DataRequest.count should equal DataRequestArea.count

end

# Update DataRequest with the correct data_request_area_id
request.update(data_request_area_id: data_request_area.id) # rubocop:disable Rails/SaveBang
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update will save the record, so this line is the issue. You can replace with update_attribute which won't run validation but will still run callbacks.

The additional save is not required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants