Skip to content

Commit

Permalink
Stop using requestable state
Browse files Browse the repository at this point in the history
We're going to remove this field as it's no longer necessary so we need
to first stop using it.
  • Loading branch information
thomasleese committed Sep 27, 2023
1 parent 7529f6a commit 80bcdd6
Show file tree
Hide file tree
Showing 35 changed files with 127 additions and 194 deletions.
5 changes: 0 additions & 5 deletions app/controllers/personas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ class PersonasController < ApplicationController

def index
@staff = Staff.all

@reference_requests =
ReferenceRequest.states.values.filter_map do |state|
ReferenceRequest.find_by(state:)
end
end

def eligible_sign_in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def show
@reference_request =
ReferenceRequest
.joins(:application_form)
.received
.where.not(received_at: nil)
.or(ReferenceRequest.respondable)
.includes(:work_history, :application_form)
.find_by!(slug: params[:slug])
Expand Down
8 changes: 6 additions & 2 deletions app/jobs/expire_requestable_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

class ExpireRequestableJob < ApplicationJob
def perform(requestable)
if requestable.requested? && requestable.expires_at.present? &&
Time.zone.now > requestable.expires_at
if requestable.received? || requestable.expired? ||
!requestable.requested? || requestable.expires_at.nil?
return
end

if Time.zone.now > requestable.expires_at
ExpireRequestable.call(requestable:, user: "Expirer")
end
end
Expand Down
10 changes: 7 additions & 3 deletions app/jobs/expire_requestables_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

class ExpireRequestablesJob < ApplicationJob
def perform(requestable_class_name)
requestable_class_name.constantize.requested.find_each do |requestable|
ExpireRequestableJob.perform_later(requestable)
end
requestable_class_name
.constantize
.where(expired_at: nil, received_at: nil)
.where.not(requested_at: nil)
.find_each do |requestable|
ExpireRequestableJob.perform_later(requestable)
end
end
end
2 changes: 1 addition & 1 deletion app/lib/application_form_status_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def overdue?(requestables:)
end

def waiting_on?(requestables:)
requestables.reject(&:reviewed?).any?(&:requested?)
requestables.reject(&:reviewed?).reject(&:received?).any?(&:requested?)
end

def received?(requestables:)
Expand Down
8 changes: 8 additions & 0 deletions app/models/concerns/expirable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ def expires_at
requested_at + expires_after
end

def expired!
update!(expired_at: Time.zone.now)
end

def expired?
expired_at != nil
end

def after_expired(user:)
# implement logic after an expiration of this object
end
Expand Down
54 changes: 27 additions & 27 deletions app/models/concerns/requestable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,28 @@ module Requestable
included do
belongs_to :assessment

enum :state,
{ requested: "requested", received: "received", expired: "expired" },
default: "requested"

validates :state, presence: true, inclusion: { in: states.values }

validates :requested_at, presence: true, if: :requested?
validates :received_at, presence: true, if: :received?
validates :expired_at, presence: true, if: :expired?
validates :reviewed_at, presence: true, unless: -> { passed.nil? }

scope :respondable, -> { not_received.merge(ApplicationForm.assessable) }
scope :respondable,
-> { where(received_at: nil).merge(ApplicationForm.assessable) }

define_method :requested! do
update!(state: "requested", requested_at: Time.zone.now)
end
has_one :application_form, through: :assessment
end

define_method :received! do
update!(state: "received", received_at: Time.zone.now)
end
def requested!
update!(requested_at: Time.zone.now)
end

define_method :expired! do
update!(state: "expired", expired_at: Time.zone.now)
end
def requested?
requested_at != nil
end

has_one :application_form, through: :assessment
def received!
update!(received_at: Time.zone.now)
end

def received?
received_at != nil
end

def reviewed!(passed)
Expand All @@ -44,19 +40,23 @@ def reviewed?
passed != nil
end

def overdue?
expired? || (received? && expires_at.present? && received_at > expires_at)
end

def failed
return nil if passed.nil?
passed == false
end

def status
return state if passed.nil?

passed ? "accepted" : "rejected"
if reviewed_at.present?
passed ? "accepted" : "rejected"
elsif received_at.present?
"received"
elsif expired_at.present?
"overdue"
elsif requested_at.present?
"waiting_on"
else
"not_started"
end
end

def after_requested(user:)
Expand Down
6 changes: 3 additions & 3 deletions app/models/further_information_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class FurtherInformationRequest < ApplicationRecord

scope :remindable,
-> do
requested.joins(assessment: :application_form).merge(
ApplicationForm.assessable,
)
where(received_at: nil, expired_at: nil).joins(
assessment: :application_form,
).merge(ApplicationForm.assessable)
end

FOUR_WEEK_COUNTRY_CODES = %w[AU CA GI NZ US].freeze
Expand Down
6 changes: 3 additions & 3 deletions app/models/reference_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class ReferenceRequest < ApplicationRecord

scope :remindable,
-> do
requested.joins(assessment: :application_form).merge(
ApplicationForm.assessable,
)
where(received_at: nil, expired_at: nil).joins(
assessment: :application_form,
).merge(ApplicationForm.assessable)
end

with_options if: :received? do
Expand Down
2 changes: 1 addition & 1 deletion app/services/expire_requestable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(requestable:, user:)
end

def call
raise NotRequested unless requestable.requested?
raise NotRequested if requestable.requested_at.nil?

ActiveRecord::Base.transaction do
requestable.expired!
Expand Down
1 change: 0 additions & 1 deletion app/services/send_reminder_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def call
attr_reader :remindable

def send_reminder?
return false if remindable.try(:expired_at).present?
return false unless remindable.expires_at

remindable.should_send_reminder_email?(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def further_information_request
@further_information_request ||=
FurtherInformationRequest
.joins(assessment: :application_form)
.received
.where.not(received_at: nil)
.where(
assessment_id: params[:assessment_id],
assessment: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ def show_professional_standing_request_expired_content?
professional_standing_request&.expired? || false
end

def request_further_information?
further_information_request.present? &&
further_information_request.requested? &&
!further_information_request.received?
end

def request_professional_standing_certificate?
teaching_authority_provides_written_statement &&
professional_standing_request&.requested? &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def further_information_request
@further_information_request ||=
FurtherInformationRequest
.joins(:assessment)
.requested
.where(assessments: { application_form: })
.where(received_at: nil, assessments: { application_form: })
.find(params[:id])
end

Expand Down Expand Up @@ -74,7 +73,7 @@ def item_value(item)
item.document
elsif item.work_history_contact?
"Contact name: #{item.contact_name}<br/>
Contact job: #{item.contact_job}<br/>
Contact job: #{item.contact_job}<br/>
Contact email: #{item.contact_email}".html_safe
end
end
Expand Down
34 changes: 0 additions & 34 deletions app/views/personas/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -147,37 +147,3 @@
end %>
<% end %>
</section>

<section id="app-personas-references" class="app-personas">
<h2 class="govuk-heading-l">References</h2>

<% if @reference_requests.empty? %>
<p class="govuk-body">No reference requests.</p>
<% else %>
<%= govuk_table do |table|
table.with_head do |head|
head.with_row do |row|
row.with_cell(header: true, text: "Teacher email")
row.with_cell(header: true, text: "State")
row.with_cell(header: true, text: "Actions", numeric: true)
end
end

table.with_body do |body|
@reference_requests.each do |reference_request|
body.with_row do |row|
row.with_cell(text: reference_request.application_form.teacher.email)

row.with_cell(width: "one-third") do
render(StatusTag::Component.new(status: reference_request.state))
end

row.with_cell(numeric: true) do
govuk_button_link_to "Sign&nbsp;in".html_safe, teacher_interface_reference_request_path(slug: reference_request.slug)
end
end
end
end
end %>
<% end %>
</section>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<%= render "teacher_interface/application_forms/show/declined", view_object: @view_object %>
<% elsif @view_object.application_form.awarded? %>
<%= render "teacher_interface/application_forms/show/awarded", view_object: @view_object %>
<% elsif @view_object.further_information_request&.requested? %>
<% elsif @view_object.request_further_information? %>
<%= render "teacher_interface/application_forms/show/further_information_requested", view_object: @view_object %>
<% elsif @view_object.request_professional_standing_certificate? %>
<%= render "teacher_interface/application_forms/show/request_professional_standing_certificate", view_object: @view_object %>
Expand Down
3 changes: 1 addition & 2 deletions lib/tasks/example_data.rake
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,9 @@ def create_application_forms
end
elsif (work_history = application_form.work_histories.first) &&
rand(2).zero?
reference_request_trait = ReferenceRequest.states.keys.sample
FactoryBot.create(
:reference_request,
reference_request_trait,
%i[requested received expired].sample,
assessment:,
work_history:,
)
Expand Down
16 changes: 7 additions & 9 deletions spec/factories/further_information_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,32 @@
factory :further_information_request do
association :assessment

requested

trait :requested do
state { "requested" }
requested_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
end

trait :received do
state { "received" }
received_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
end

trait :expired do
state { "expired" }
expired_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
end

trait :reviewed do
received
reviewed_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
end

trait :passed do
reviewed
passed { true }
reviewed_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
received
end

trait :failed do
reviewed
passed { false }
reviewed_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
failure_assessor_note { "Notes." }
received
end

trait :with_items do
Expand Down
5 changes: 0 additions & 5 deletions spec/factories/professional_standing_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,16 @@
factory :professional_standing_request do
association :assessment

requested

trait :requested do
state { "requested" }
requested_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
end

trait :received do
state { "received" }
received_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
receivable
end

trait :expired do
state { "expired" }
expired_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
end

Expand Down
5 changes: 0 additions & 5 deletions spec/factories/qualification_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,16 @@
association :assessment
association :qualification, :completed

requested

trait :requested do
state { "requested" }
requested_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
end

trait :received do
state { "received" }
received_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
receivable
end

trait :expired do
state { "expired" }
expired_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) }
end

Expand Down
Loading

0 comments on commit 80bcdd6

Please sign in to comment.