Skip to content

Commit

Permalink
Merge pull request #9538 from DFE-Digital/604-ca-clock-workers-assess…
Browse files Browse the repository at this point in the history
…-impact-of-submittedat-on-workers

[604] Update queries so that unsubmitted works as intended
  • Loading branch information
elceebee authored Jul 10, 2024
2 parents fca5201 + fb504a6 commit aae781c
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ def self.call
def self.deadline_reminder_query
ApplicationForm
.joins(:candidate)
.where(submitted_at: nil, recruitment_cycle_year: RecruitmentCycle.current_year)
.current_cycle
.unsubmitted
.where.not(candidate: { unsubscribed_from_emails: true })
.where.not(candidate: { submission_blocked: true })
.where.not(candidate: { account_locked: true })
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class GetIncompleteCourseChoiceApplicationsReadyToNudge

def call
ApplicationForm
.unsubmitted
.joins(:candidate)
.where.not('candidate.submission_blocked': true)
.where.not('candidate.account_locked': true)
.where.not('candidate.unsubscribed_from_emails': true)
.inactive_since(7.days.ago)
.with_completion(COMPLETION_ATTRS)
.current_cycle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ class GetIncompletePersonalStatementApplicationsReadyToNudge

def call
ApplicationForm
.unsubmitted
.inactive_since(7.days.ago)
.with_completion(COMPLETION_ATTRS)
.current_cycle
.joins(:candidate)
.where.not('candidate.submission_blocked': true)
.where.not('candidate.account_locked': true)
.where.not('candidate.unsubscribed_from_emails': true)
.where(INCOMPLETION_ATTRS.map { |attr| "#{attr} = false" }.join(' AND '))
.has_not_received_email(MAILER, MAIL_TEMPLATE)
.includes(:application_choices).where('application_choices.status': 'unsubmitted')
.joins(:application_choices).where('application_choices.status': 'unsubmitted')
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ def call
uk_and_irish = uk_and_irish_names.map { |name| ActiveRecord::Base.connection.quote(name) }.join(',')

ApplicationForm
.joins(:candidate)
.where.not('candidate.submission_blocked': true)
.where.not('candidate.account_locked': true)
.where.not('candidate.unsubscribed_from_emails': true)
.current_cycle
.unsubmitted
.inactive_since(7.days.ago)
.with_completion(COMMON_COMPLETION_ATTRS)
.has_not_received_email(MAILER, MAIL_TEMPLATE)
Expand Down Expand Up @@ -40,7 +43,7 @@ def call
.joins(
"LEFT OUTER JOIN \"application_choices\" ON \"application_choices\".application_form_id = application_forms.id AND \"application_choices\".status = 'unsubmitted'",
)
.group('application_forms.id')
.group('application_forms.id', 'candidate.id')
.having('count("references".id) < 2 AND count("application_choices".id) > 0')
end
end
7 changes: 5 additions & 2 deletions app/queries/get_unsubmitted_applications_ready_to_nudge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ def call
uk_and_irish = uk_and_irish_names.map { |name| ActiveRecord::Base.connection.quote(name) }.join(',')

ApplicationForm
.unsubmitted
.inactive_since(7.days.ago)
.with_completion(COMMON_COMPLETION_ATTRS)
.current_cycle
.has_not_received_email(MAILER, MAIL_TEMPLATE)
.includes(:application_choices).where('application_choices.status': 'unsubmitted')
.joins(:candidate)
.where.not('candidate.submission_blocked': true)
.where.not('candidate.account_locked': true)
.where.not('candidate.unsubscribed_from_emails': true)
.joins(:application_choices).where('application_choices.status': 'unsubmitted')
.and(ApplicationForm
.where(science_gcse_completed: true)
.or(
Expand Down
13 changes: 10 additions & 3 deletions app/queries/get_unsuccessful_and_unsubmitted_candidates.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
class GetUnsuccessfulAndUnsubmittedCandidates
def self.call
previous_recruitment_year = RecruitmentCycle.previous_year
# Candidates who didn't have successful applications last year
Candidate
.left_outer_joins(:application_forms)
.where.not(candidates: { unsubscribed_from_emails: true })
.where.not(candidates: { submission_blocked: true })
.where.not(candidates: { account_locked: true })
.where(
'(application_forms.recruitment_cycle_year = 2023 AND NOT EXISTS (:successful))',
'(application_forms.recruitment_cycle_year = (:previous_recruitment_year) AND NOT EXISTS (:successful))',
previous_recruitment_year:,
successful: ApplicationChoice
.select(1)
.where(status: %w[recruited pending_conditions offer offer_deferred])
.where(status: ApplicationStateChange::SUCCESSFUL_STATES)
.where('application_choices.application_form_id = application_forms.id'),
)
.or(
# Candidates who have started working on applications this year, but not submitted.
Candidate
.where(application_forms: { recruitment_cycle_year: 2024 })
.where(application_forms: {
recruitment_cycle_year: RecruitmentCycle.current_year,
submitted_at: nil,
})
.where.not(candidates: { unsubscribed_from_emails: true })
.where.not(candidates: { submission_blocked: true })
.where.not(candidates: { account_locked: true }),
Expand Down
6 changes: 2 additions & 4 deletions app/workers/cancel_unsubmitted_applications_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ def unsubmitted_applications_from_earlier_cycle
return [] unless CycleTimetable.cancel_unsubmitted_applications?

ApplicationForm
.where(submitted_at: nil)
.where(recruitment_cycle_year: RecruitmentCycle.current_year)
.current_cycle
.where.not(application_forms: { candidate_id: Candidate.where(hide_in_reporting: true).select(:id) })
.where(application_forms: { id: ApplicationChoice.unsubmitted.select(:application_form_id) })
.includes(:application_choices)
.includes(:application_choices).where('application_choices.status': 'unsubmitted')
.distinct
end
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
require 'rails_helper'

RSpec.describe GetIncompleteCourseChoiceApplicationsReadyToNudge do
it 'includes unsubmitted applications that have no application choices' do
it 'includes applications that have no application choices' do
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
)
application_form.update_columns(
updated_at: 10.days.ago,
Expand All @@ -14,11 +13,47 @@
expect(described_class.new.call).to eq([application_form])
end

it 'omits candidates with locked accounts' do
candidate = create(:candidate, account_locked: true)
application_form = create(
:completed_application_form,
:with_completed_references,
candidate:,
)

application_form.update_columns(updated_at: 10.days.ago)

expect(described_class.new.call).not_to include(application_form)
end

it 'omits candidates with submission blocked' do
candidate = create(:candidate, submission_blocked: true)
application_form = create(
:completed_application_form,
:with_completed_references,
candidate:,
)
application_form.update_columns(updated_at: 10.days.ago)

expect(described_class.new.call).not_to include(application_form)
end

it 'omits candidates who have unsubscribed from emails' do
candidate = create(:candidate, unsubscribed_from_emails: true)
application_form = create(
:completed_application_form,
:with_completed_references,
candidate:,
)
application_form.update_columns(updated_at: 10.days.ago)

expect(described_class.new.call).not_to include(application_form)
end

it 'omits unsubmitted applications that have an application choice' do
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
)
create(:application_choice, application_form:)
application_form.update_columns(
Expand All @@ -32,8 +67,8 @@
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: Time.zone.now,
)

create(:application_choice, status: ApplicationStateChange::STATES_VISIBLE_TO_PROVIDER.sample, application_form:)
application_form.update_columns(
updated_at: 10.days.ago,
Expand All @@ -46,7 +81,6 @@
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
)
application_form.update_columns(
references_completed: false,
Expand All @@ -60,7 +94,6 @@
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
)
application_form.update_columns(
becoming_a_teacher_completed: false,
Expand All @@ -74,7 +107,6 @@
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
course_choices_completed: false,
)
application_form.update_columns(
Expand All @@ -88,7 +120,6 @@
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
course_choices_completed: false,
recruitment_cycle_year: RecruitmentCycle.previous_year,
)
Expand All @@ -103,7 +134,6 @@
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
course_choices_completed: false,
)
application_form.update_columns(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,68 @@
require 'rails_helper'

RSpec.describe GetIncompletePersonalStatementApplicationsReadyToNudge do
it 'includes unsubmitted applications which don\'t have any completed personal statements' do
it 'includes unsubmitted applications choices which don\'t have any completed personal statements' do
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
submitted_at: 10.days.ago,
becoming_a_teacher_completed: false,
)
create(:application_choice, application_form:)
create(:application_choice, :unsubmitted, application_form:)
application_form.update_columns(
updated_at: 10.days.ago,
)

expect(described_class.new.call).to eq([application_form])
end

it 'omits candidates with locked accounts' do
candidate = create(:candidate, account_locked: true)
application_form = create(
:completed_application_form,
:with_completed_references,
candidate:,
submitted_at: 10.days.ago,
becoming_a_teacher_completed: false,
)

application_form.update_columns(updated_at: 10.days.ago)

expect(described_class.new.call).not_to include(application_form)
end

it 'omits candidates with submission blocked' do
candidate = create(:candidate, submission_blocked: true)
application_form = create(
:completed_application_form,
:with_completed_references,
candidate:,
submitted_at: 10.days.ago,
becoming_a_teacher_completed: false,
)
application_form.update_columns(updated_at: 10.days.ago)

expect(described_class.new.call).not_to include(application_form)
end

it 'omits candidates who have unsubscribed from emails' do
candidate = create(:candidate, unsubscribed_from_emails: true)
application_form = create(
:completed_application_form,
:with_completed_references,
candidate:,
submitted_at: 10.days.ago,
becoming_a_teacher_completed: false,
)
application_form.update_columns(updated_at: 10.days.ago)

expect(described_class.new.call).not_to include(application_form)
end

it 'omits unsubmitted applications that have not completed references' do
application_form = create(
:completed_application_form,
submitted_at: nil,
submitted_at: 10.days.ago,
becoming_a_teacher_completed: true,
references_completed: false,
)
Expand All @@ -35,7 +78,7 @@
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
submitted_at: 10.days.ago,
becoming_a_teacher_completed: true,
)
create(:application_choice, application_form:)
Expand All @@ -50,7 +93,7 @@
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
submitted_at: 10.days.ago,
becoming_a_teacher_completed: false,
)
create(:application_choice, application_form:)
Expand All @@ -61,11 +104,11 @@
expect(described_class.new.call).to eq([])
end

it 'omits unsubmitted applications that have no application choices' do
it 'omits applications that have no application choices' do
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
submitted_at: 10.days.ago,
becoming_a_teacher_completed: false,
)
application_form.update_columns(
Expand All @@ -79,7 +122,7 @@
application_form = create(
:completed_application_form,
:with_completed_references,
submitted_at: nil,
submitted_at: 10.days.ago,
becoming_a_teacher_completed: false,
)
create(:application_choice, application_form:)
Expand Down
Loading

0 comments on commit aae781c

Please sign in to comment.