Skip to content

Commit

Permalink
save result as JSON within service
Browse files Browse the repository at this point in the history
  • Loading branch information
alkesh committed Jan 6, 2025
1 parent 910fcd7 commit fbc55e8
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ def perform(bulk_operation_id:)
bulk_operation = BulkOperation::RevertApplicationsToPending.find(bulk_operation_id)
application_ecf_ids = CSV.parse(bulk_operation.file.download, headers: false).flatten
Rails.logger.info("Bulk Operation started - bulk_operation_id: #{bulk_operation_id}")
result = BulkOperation::BulkChangeApplicationsToPending.new(application_ecf_ids:).run!(dry_run: false)
bulk_operation.update!(result:, finished_at: Time.zone.now)
BulkOperation::BulkChangeApplicationsToPending.new(application_ecf_ids:, bulk_operation:).run!(dry_run: false)
Rails.logger.info("Bulk Operation finished - bulk_operation_id: #{bulk_operation_id}")
end
end
3 changes: 1 addition & 2 deletions app/jobs/bulk_operation/bulk_reject_applications_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ def perform(bulk_operation_id:)
bulk_operation = BulkOperation::RejectApplications.find(bulk_operation_id)
application_ecf_ids = CSV.parse(bulk_operation.file.download, headers: false).flatten
Rails.logger.info("Bulk Operation started - bulk_operation_id: #{bulk_operation_id}")
result = BulkOperation::BulkRejectApplications.new(application_ecf_ids:).run!
bulk_operation.update!(result:, finished_at: Time.zone.now)
BulkOperation::BulkRejectApplications.new(application_ecf_ids:, bulk_operation:).run!
Rails.logger.info("Bulk Operation finished - bulk_operation_id: #{bulk_operation_id}")
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

class BulkOperation::BulkChangeApplicationsToPending
attr_reader :application_ecf_ids
attr_reader :application_ecf_ids, :bulk_operation

def initialize(application_ecf_ids:)
def initialize(application_ecf_ids:, bulk_operation:)
@application_ecf_ids = application_ecf_ids
@bulk_operation = bulk_operation
end

def run!(dry_run: true)
Expand All @@ -16,6 +17,7 @@ def run!(dry_run: true)
success = revert_to_pending.revert
hash[application_ecf_id] = outcome(success, application, revert_to_pending.errors)
end
bulk_operation.update!(result: result.to_json, finished_at: Time.zone.now)

raise ActiveRecord::Rollback if dry_run
end
Expand Down
6 changes: 4 additions & 2 deletions app/services/bulk_operation/bulk_reject_applications.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

class BulkOperation::BulkRejectApplications
attr_reader :application_ecf_ids
attr_reader :application_ecf_ids, :bulk_operation

def initialize(application_ecf_ids:)
def initialize(application_ecf_ids:, bulk_operation:)
@application_ecf_ids = application_ecf_ids
@bulk_operation = bulk_operation
end

def run!
Expand All @@ -16,6 +17,7 @@ def run!
success = reject_service.reject
hash[application_ecf_id] = outcome(success, application, reject_service.errors)
end
bulk_operation.update!(result: result.to_json, finished_at: Time.zone.now)
end

result
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20241204120620_create_bulk_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_table :bulk_operations do |t|
t.integer "admin_id", null: false
t.integer "row_count"
t.text "result"
t.jsonb "result"
t.string "type", null: false
t.datetime "started_at"
t.datetime "finished_at"
Expand Down
24 changes: 5 additions & 19 deletions 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_12_18_101840) do
ActiveRecord::Schema[7.1].define(version: 2025_01_03_111630) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gin"
enable_extension "citext"
Expand Down Expand Up @@ -70,7 +70,7 @@
end

create_table "api_tokens", force: :cascade do |t|
t.bigint "lead_provider_id"
t.bigint "lead_provider_id", null: false
t.string "hashed_token", null: false
t.datetime "last_used_at"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -147,6 +147,7 @@
t.index ["course_id"], name: "index_applications_on_course_id"
t.index ["ecf_id"], name: "index_applications_on_ecf_id", unique: true
t.index ["itt_provider_id"], name: "index_applications_on_itt_provider_id"
t.index ["lead_provider_approval_status", "lead_provider_id"], name: "idx_on_lead_provider_approval_status_lead_provider__299e5bac06"
t.index ["lead_provider_id"], name: "index_applications_on_lead_provider_id"
t.index ["private_childcare_provider_id"], name: "index_applications_on_private_childcare_provider_id"
t.index ["schedule_id"], name: "index_applications_on_schedule_id"
Expand All @@ -157,7 +158,7 @@
create_table "bulk_operations", force: :cascade do |t|
t.integer "admin_id", null: false
t.integer "row_count"
t.text "result"
t.jsonb "result"
t.string "type", null: false
t.datetime "started_at"
t.datetime "finished_at"
Expand Down Expand Up @@ -336,15 +337,6 @@
t.index ["ecf_id"], name: "index_lead_providers_on_ecf_id", unique: true
end

create_table "legacy_passed_participant_outcomes", force: :cascade do |t|
t.string "trn", null: false
t.string "course_short_code"
t.date "completion_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["trn"], name: "index_legacy_passed_participant_outcomes_on_trn"
end

create_table "local_authorities", force: :cascade do |t|
t.text "ukprn"
t.text "name"
Expand Down Expand Up @@ -561,13 +553,6 @@
t.index ["lead_provider_id"], name: "index_statements_on_lead_provider_id"
end

create_table "trs_data", force: :cascade do |t|
t.string "trn", null: false
t.string "npq_type"
t.date "awarded_date"
t.index ["trn"], name: "index_trs_data_on_trn"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.datetime "created_at", null: false
Expand All @@ -593,6 +578,7 @@
t.string "archived_email"
t.datetime "archived_at"
t.datetime "significantly_updated_at", default: -> { "CURRENT_TIMESTAMP" }, null: false
t.index ["created_at"], name: "index_users_on_created_at"
t.index ["ecf_id"], name: "index_users_on_ecf_id", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["provider"], name: "index_users_on_provider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
let(:application_ecf_ids) { [SecureRandom.uuid, SecureRandom.uuid] }

it "calls BulkOperation::BulkChangeApplicationsToPending" do
expect(BulkOperation::BulkChangeApplicationsToPending).to receive(:new).with(application_ecf_ids:).and_call_original
expect(BulkOperation::BulkChangeApplicationsToPending).to receive(:new).with(application_ecf_ids:, bulk_operation:).and_call_original
subject
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
let(:application_ecf_ids) { [SecureRandom.uuid, SecureRandom.uuid] }

it "calls BulkOperation::BulkRejectApplications" do
expect(BulkOperation::BulkRejectApplications).to receive(:new).with(application_ecf_ids:).and_call_original
expect(BulkOperation::BulkRejectApplications).to receive(:new).with(application_ecf_ids:, bulk_operation:).and_call_original
subject
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

RSpec.describe BulkOperation::BulkChangeApplicationsToPending do
let(:application_ecf_ids) { [application.ecf_id] }
let(:instance) { described_class.new(application_ecf_ids:) }
let(:bulk_operation) { create(:revert_applications_to_pending_bulk_operation, admin: create(:admin)) }
let(:instance) { described_class.new(application_ecf_ids:, bulk_operation:) }

describe "#run!" do
let(:dry_run) { false }
Expand All @@ -11,12 +12,20 @@

RSpec.shared_examples "changes to pending" do |initial_state|
it { expect { run }.to(change { application.reload.lead_provider_approval_status }.from(initial_state).to("pending")) }
it { expect(run[application.ecf_id]).to eq("Changed to pending") }

it "saves the result" do
run
expect(JSON.parse(bulk_operation.result)[application.ecf_id]).to eq("Changed to pending")
end
end

RSpec.shared_examples "does not change to pending" do |result|
it { expect { run }.not_to(change { application.reload.lead_provider_approval_status }) }
it { expect(run[application.ecf_id]).to match(result) }

it "saves the result" do
run
expect(JSON.parse(bulk_operation.reload.result)[application.ecf_id]).to match(result)
end
end

context "when there is an accepted application" do
Expand Down
9 changes: 7 additions & 2 deletions spec/services/bulk_operation/bulk_reject_applications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

RSpec.describe BulkOperation::BulkRejectApplications do
let(:application_ecf_ids) { [application.ecf_id] }
let(:instance) { described_class.new(application_ecf_ids:) }
let(:bulk_operation) { create(:reject_applications_bulk_operation, admin: create(:admin)) }
let(:instance) { described_class.new(application_ecf_ids:, bulk_operation:) }

describe "#run!" do
subject(:run) { instance.run! }

RSpec.shared_examples "does not change to rejected" do |result|
it { expect { run }.not_to(change { application.reload.lead_provider_approval_status }) }
it { expect(run[application.ecf_id]).to match(result) }

it "saves the result" do
run
expect(JSON.parse(bulk_operation.result)[application.ecf_id]).to match(result)
end
end

context "when the application has lead_provider_approval_status: accepted" do
Expand Down

0 comments on commit fbc55e8

Please sign in to comment.