Skip to content

Commit

Permalink
Merge pull request #2547 from DFE-Digital/AQTS-688-dev-code-architect…
Browse files Browse the repository at this point in the history
…ure-update-new-trs-trn-request-table

[AQTS-688] New trs_trn_requests table to replace dqt_trn_requests
  • Loading branch information
Hassanmir92 authored Dec 10, 2024
2 parents 38296a1 + 3762924 commit 559d04b
Show file tree
Hide file tree
Showing 25 changed files with 555 additions and 57 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ gem "pundit"
gem "rack-attack"
gem "rails_semantic_logger"
gem "rmagick"
gem "ruby-progressbar"
gem "ruby-vips"
gem "sentry-rails"
gem "sentry-ruby"
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ DEPENDENCIES
rspec-rails
rspec-retry!
rubocop-govuk
ruby-progressbar
ruby-vips
sentry-rails
sentry-ruby
Expand Down
43 changes: 28 additions & 15 deletions app/jobs/update_trs_trn_request_job.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
# frozen_string_literal: true

class UpdateTRSTRNRequestJob < ApplicationJob
def perform(dqt_trn_request)
return if dqt_trn_request.complete?
def perform(trs_trn_request)
return if trs_trn_request.complete?

application_form = dqt_trn_request.application_form
application_form = trs_trn_request.application_form

response = fetch_response(dqt_trn_request)
# TODO: We can remove this block once the migration between DQTTRNRequest to TRSTRNRequest
# has fully completed. This job may have quite a few instances in our scheduled queue
# and as a result we need to ensure they switch into using the TRSTRNRequest once we
# migrate all existing DQTTRNRequests into a record within the TRSTRNRequests table.
if trs_trn_request.is_a?(DQTTRNRequest)
if application_form.trs_trn_request
trs_trn_request = application_form.trs_trn_request
else
UpdateTRSTRNRequestJob.set(wait: 1.hour).perform_later(trs_trn_request)
return
end
end

response = fetch_response(trs_trn_request)

dqt_trn_request.pending! if dqt_trn_request.initial?
trs_trn_request.pending! if trs_trn_request.initial?

potential_duplicate = response[:potential_duplicate]
if potential_duplicate != dqt_trn_request.potential_duplicate
dqt_trn_request.update!(potential_duplicate:)
if potential_duplicate != trs_trn_request.potential_duplicate
trs_trn_request.update!(potential_duplicate:)
end

ApplicationFormStatusUpdater.call(application_form:, user: "DQT")
Expand All @@ -25,24 +38,24 @@ def perform(dqt_trn_request)
access_your_teaching_qualifications_url:
response[:access_your_teaching_qualifications_link],
)
dqt_trn_request.complete!
trs_trn_request.complete!
end

if dqt_trn_request.pending?
UpdateTRSTRNRequestJob.set(wait: 1.hour).perform_later(dqt_trn_request)
if trs_trn_request.pending?
UpdateTRSTRNRequestJob.set(wait: 1.hour).perform_later(trs_trn_request)
end
end

private

def fetch_response(dqt_trn_request)
if dqt_trn_request.initial?
def fetch_response(trs_trn_request)
if trs_trn_request.initial?
TRS::Client::CreateTRNRequest.call(
request_id: dqt_trn_request.request_id,
application_form: dqt_trn_request.application_form,
request_id: trs_trn_request.request_id,
application_form: trs_trn_request.application_form,
)
else
TRS::Client::ReadTRNRequest.call(request_id: dqt_trn_request.request_id)
TRS::Client::ReadTRNRequest.call(request_id: trs_trn_request.request_id)
end
rescue Faraday::Error => e
Sentry.configure_scope do |scope|
Expand Down
10 changes: 5 additions & 5 deletions app/lib/application_form_status_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def action_required_by
application_form.declined_at.present? ||
application_form.awarded_at.present?
"none"
elsif dqt_trn_request.present? || assessment_in_review? ||
elsif trs_trn_request.present? || assessment_in_review? ||
overdue_further_information || received_further_information
"assessor"
elsif preliminary_check? || need_to_request_lops? ||
Expand All @@ -76,7 +76,7 @@ def stage
application_form.declined_at.present? ||
application_form.awarded_at.present?
"completed"
elsif assessment_in_review? || dqt_trn_request.present?
elsif assessment_in_review? || trs_trn_request.present?
"review"
elsif preliminary_check? ||
(teaching_authority_provides_written_statement && waiting_on_lops)
Expand Down Expand Up @@ -107,8 +107,8 @@ def statuses
%w[declined]
elsif application_form.awarded_at.present?
%w[awarded]
elsif dqt_trn_request.present?
if dqt_trn_request.potential_duplicate?
elsif trs_trn_request.present?
if trs_trn_request.potential_duplicate?
%w[potential_duplicate_in_dqt]
else
%w[awarded_pending_checks]
Expand All @@ -133,7 +133,7 @@ def statuses
end

delegate :assessment,
:dqt_trn_request,
:trs_trn_request,
:region,
:teacher,
:teaching_authority_provides_written_statement,
Expand Down
2 changes: 1 addition & 1 deletion app/lib/fake_data/application_form_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def award_application_form(user:)
access_your_teaching_qualifications_url = Faker::Internet.url

date_generator.travel_to_next_long do
DQTTRNRequest.create!(request_id: SecureRandom.uuid, application_form:)
TRSTRNRequest.create!(request_id: SecureRandom.uuid, application_form:)
ApplicationFormStatusUpdater.call(application_form:, user:)
end

Expand Down
1 change: 1 addition & 0 deletions app/models/application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class ApplicationForm < ApplicationRecord
has_many :timeline_events
has_one :country, through: :region
has_one :dqt_trn_request
has_one :trs_trn_request
has_one :assessment
has_many :notes, dependent: :destroy

Expand Down
30 changes: 30 additions & 0 deletions app/models/trs_trn_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: trs_trn_requests
#
# id :bigint not null, primary key
# potential_duplicate :boolean
# state :string default("initial"), not null
# created_at :datetime not null
# updated_at :datetime not null
# application_form_id :bigint not null
# request_id :uuid not null
#
# Indexes
#
# index_trs_trn_requests_on_application_form_id (application_form_id)
#
# Foreign Keys
#
# fk_rails_... (application_form_id => application_forms.id)
#
class TRSTRNRequest < ApplicationRecord
belongs_to :application_form

enum :state,
{ initial: "initial", pending: "pending", complete: "complete" },
default: :initial
validates :state, presence: true, inclusion: { in: states.values }
end
2 changes: 1 addition & 1 deletion app/services/award_qts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(
def call
return if application_form.awarded_at.present?

raise InvalidState if application_form.dqt_trn_request.nil?
raise InvalidState if application_form.trs_trn_request.nil?

raise MissingTRN if trn.blank?

Expand Down
4 changes: 2 additions & 2 deletions app/services/create_trs_trn_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def initialize(application_form:, user:)

def call
request_id = SecureRandom.uuid
dqt_trn_request = DQTTRNRequest.create!(request_id:, application_form:)
trs_trn_request = TRSTRNRequest.create!(request_id:, application_form:)
ApplicationFormStatusUpdater.call(application_form:, user:)
UpdateTRSTRNRequestJob.perform_later(dqt_trn_request)
UpdateTRSTRNRequestJob.perform_later(trs_trn_request)
end

private
Expand Down
2 changes: 2 additions & 0 deletions app/services/destroy_application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def call
ActiveRecord::Base.transaction do
timeline_events.destroy_all
dqt_trn_request&.destroy!
trs_trn_request&.destroy!
assessment&.destroy!
application_form.destroy!
teacher.destroy! unless teacher.application_forms.exists?
Expand All @@ -25,5 +26,6 @@ def call
:teacher,
:timeline_events,
:dqt_trn_request,
:trs_trn_request,
to: :application_form
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<p class="govuk-body">You’ve successfully sent your further information request email to the applicant.</p>
<% elsif @view_object.application_form.declined_at.present? %>
<p class="govuk-body">The application will be deleted after 90 days unless the applicant chooses to appeal.</p>
<% elsif @view_object.application_form.dqt_trn_request.present? %>
<% elsif @view_object.application_form.trs_trn_request.present? || @view_object.application_form.dqt_trn_request.present? %>
<p class="govuk-body">This status will appear while the award is reconciled with the information in the Database of Qualified Teachers (DQT).</p>
<p class="govuk-body">Once these checks are complete, the status will change to ‘Awarded’ and the applicant will receive the email telling them they’ve been awarded QTS.</p>
<% end %>
Expand Down
8 changes: 8 additions & 0 deletions config/analytics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@
- trn
- updated_at
- uuid
:trs_trn_requests:
- application_form_id
- created_at
- id
- potential_duplicate
- request_id
- state
- updated_at
:uploads:
- created_at
- document_id
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20241205125213_create_trs_trn_requests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class CreateTRSTRNRequests < ActiveRecord::Migration[7.2]
def change
create_table :trs_trn_requests do |t|
t.references :application_form, null: false, foreign_key: true
t.uuid :request_id, null: false
t.string :state, null: false, default: "pending"
t.boolean :potential_duplicate

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions db/scripts/sanitise.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ SET
-- clear these as the scheduled jobs might run against them
DELETE FROM "dqt_trn_requests";

-- TRSTRNRequest
-- clear these as the scheduled jobs might run against them
DELETE FROM "trs_trn_requests";

-- EligibilityCheck
-- no update required

Expand Down
Binary file modified docs/diagram.pdf
Binary file not shown.
26 changes: 26 additions & 0 deletions lib/tasks/dqt_to_trs_migration.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "ruby-progressbar"

namespace :dqt_to_trs_migration do
desc "Backfill the trs_trn_requests table with what is within dqt_trn_requests"
task replace_dqt_trn_requests_table_with_trs_trn_requests: :environment do
progressbar =
ProgressBar.create(name: "Creation", count: DQTTRNRequest.count) # rubocop:disable Rails/SaveBang

ActiveRecord::Base.transaction do
DQTTRNRequest.find_each do |dqt_trn_request|
progressbar.increment

TRSTRNRequest.create!(
potential_duplicate: dqt_trn_request.potential_duplicate,
state: dqt_trn_request.state,
created_at: dqt_trn_request.created_at,
updated_at: dqt_trn_request.updated_at,
application_form_id: dqt_trn_request.application_form_id,
request_id: dqt_trn_request.request_id,
)
end
end
end
end
1 change: 1 addition & 0 deletions lib/tasks/fake_data.rake
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace :fake_data do

TimelineEvent.delete_all
DQTTRNRequest.delete_all
TRSTRNRequest.delete_all
ReminderEmail.delete_all
ConsentRequest.delete_all
FurtherInformationRequestItem.delete_all
Expand Down
33 changes: 33 additions & 0 deletions spec/factories/trs_trn_requests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: trs_trn_requests
#
# id :bigint not null, primary key
# potential_duplicate :boolean
# state :string default("initial"), not null
# created_at :datetime not null
# updated_at :datetime not null
# application_form_id :bigint not null
# request_id :uuid not null
#
# Indexes
#
# index_trs_trn_requests_on_application_form_id (application_form_id)
#
# Foreign Keys
#
# fk_rails_... (application_form_id => application_forms.id)
#
FactoryBot.define do
factory :trs_trn_request do
association :application_form, :awarded_pending_checks

request_id { SecureRandom.uuid }

trait :potential_duplicate do
potential_duplicate { true }
end
end
end
Loading

0 comments on commit 559d04b

Please sign in to comment.