Skip to content

Commit

Permalink
[CPDLP-3079] Update Application table with fields for participant pro…
Browse files Browse the repository at this point in the history
…file from ECF (#1398)

* [CPDLP-3079] Update Application table with fields for participant profile from ECF

* [CPDLP-3079] Fix broken tests

* [CPDLP-3079] Address PR comments
  • Loading branch information
leandroalemao authored May 30, 2024
1 parent 265f883 commit 089f20b
Show file tree
Hide file tree
Showing 39 changed files with 173 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/models/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Application < ApplicationRecord

has_many :ecf_sync_request_logs, as: :syncable, dependent: :destroy
has_many :participant_id_changes, through: :user
has_many :application_states

scope :unsynced, -> { where(ecf_id: nil) }
scope :expired_applications, -> { where(lead_provider_approval_status: "rejected").where("created_at < ?", cut_off_date_for_expired_applications) }
Expand Down Expand Up @@ -56,6 +57,12 @@ class Application < ApplicationRecord
rejected: "rejected",
}

enum training_status: {
active: "active",
deferred: "deferred",
withdrawn: "withdrawn",
}

def previously_funded?
# This is an optimization used by the API Applications::Query in order
# to speed up the bulk-retrieval of Applications.
Expand Down
12 changes: 12 additions & 0 deletions app/models/application_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class ApplicationState < ApplicationRecord
belongs_to :application
belongs_to :lead_provider, optional: true

enum state: {
active: "active",
deferred: "deferred",
withdrawn: "withdrawn",
}
end
5 changes: 5 additions & 0 deletions db/migrate/20240529145610_create_application_statuses_enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CreateApplicationStatusesEnum < ActiveRecord::Migration[7.1]
def change
create_enum :application_statuses, %w[active deferred withdrawn]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTrainingStatusToApplications < ActiveRecord::Migration[7.1]
def change
add_column :applications, :training_status, :enum, enum_type: "application_statuses", default: "active", null: false
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240529151128_create_application_states.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateApplicationStates < ActiveRecord::Migration[7.1]
def change
create_table :application_states do |t|
t.references :application, null: false, foreign_key: true
t.references :lead_provider, foreign_key: true
t.enum :state, enum_type: "application_statuses", default: "active", null: false
t.text :reason

t.timestamps
end
end
end
17 changes: 16 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_05_28_103508) do
ActiveRecord::Schema[7.1].define(version: 2024_05_29_151128) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gin"
enable_extension "citext"
Expand All @@ -19,6 +19,7 @@

# Custom types defined in this database.
# Note that some types may not work with other database engines. Be careful if changing database.
create_enum "application_statuses", ["active", "deferred", "withdrawn"]
create_enum "funding_choices", ["school", "trust", "self", "another", "employer"]
create_enum "headteacher_statuses", ["no", "yes_when_course_starts", "yes_in_first_two_years", "yes_over_two_years", "yes_in_first_five_years", "yes_over_five_years"]
create_enum "lead_provider_approval_statuses", ["pending", "accepted", "rejected"]
Expand All @@ -45,6 +46,17 @@
t.index ["lead_provider_id"], name: "index_api_tokens_on_lead_provider_id"
end

create_table "application_states", force: :cascade do |t|
t.bigint "application_id", null: false
t.bigint "lead_provider_id"
t.enum "state", default: "active", null: false, enum_type: "application_statuses"
t.text "reason"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["application_id"], name: "index_application_states_on_application_id"
t.index ["lead_provider_id"], name: "index_application_states_on_lead_provider_id"
end

create_table "applications", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "course_id", null: false
Expand Down Expand Up @@ -89,6 +101,7 @@
t.string "notes"
t.bigint "cohort_id"
t.boolean "funded_place"
t.enum "training_status", default: "active", null: false, enum_type: "application_statuses"
t.index ["cohort_id"], name: "index_applications_on_cohort_id"
t.index ["course_id"], name: "index_applications_on_course_id"
t.index ["itt_provider_id"], name: "index_applications_on_itt_provider_id"
Expand Down Expand Up @@ -394,6 +407,8 @@
end

add_foreign_key "api_tokens", "lead_providers"
add_foreign_key "application_states", "applications"
add_foreign_key "application_states", "lead_providers"
add_foreign_key "applications", "cohorts"
add_foreign_key "applications", "courses"
add_foreign_key "applications", "itt_providers"
Expand Down
28 changes: 28 additions & 0 deletions db/seeds/base/add_applications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,33 @@
participant_outcome_state: "failed",
cohort: Cohort.all.sample,
)

# users with one deferred application each
FactoryBot.create_list(
:application,
4,
:deferred,
:with_random_user,
:with_random_work_setting,
:with_random_lead_provider_approval_status,
:with_random_participant_outcome_state,
lead_provider:,
course: Course.all.sample,
cohort: Cohort.all.sample,
)

# users with one withdrawn application each
FactoryBot.create_list(
:application,
4,
:withdrawn,
:with_random_user,
:with_random_work_setting,
:with_random_lead_provider_approval_status,
:with_random_participant_outcome_state,
lead_provider:,
course: Course.all.sample,
cohort: Cohort.all.sample,
)
end
end
8 changes: 8 additions & 0 deletions spec/factories/application_states.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

FactoryBot.define do
factory :application_state do
application
lead_provider { LeadProvider.all.sample }
end
end
24 changes: 24 additions & 0 deletions spec/factories/applications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,29 @@
FactoryBot.create(:participant_id_change, to_participant: user, user:)
end
end

trait :withdrawn do
after(:create) do |application|
application.update!(training_status: ApplicationState.states[:withdrawn])

FactoryBot.create(:application_state,
application:,
lead_provider: application.lead_provider,
state: ApplicationState.states[:withdrawn],
reason: "other")
end
end

trait :deferred do
after(:create) do |application|
application.update!(training_status: ApplicationState.states[:deferred])

FactoryBot.create(:application_state,
application:,
lead_provider: application.lead_provider,
state: ApplicationState.states[:deferred],
reason: "other")
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def run_scenario(*)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def run_scenario(*)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => true,
"number_of_pupils" => 150,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def run_scenario(js:) # rubocop:disable Lint/UnusedMethodArgument
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => true,
"number_of_pupils" => 150,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def run_scenario(*)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def run_scenario(*)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def run_scenario(js:)
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def run_scenario
"teacher_catchment_country" => nil,
"teacher_catchment_iso_country_code" => nil,
"teacher_catchment_synced_to_ecf" => false,
"training_status" => "active",
"ukprn" => nil,
"primary_establishment" => false,
"number_of_pupils" => 0,
Expand Down
Loading

0 comments on commit 089f20b

Please sign in to comment.