Skip to content

Commit

Permalink
[RUBY-3387] Remove govpay_status field from Order model and refac…
Browse files Browse the repository at this point in the history
…tor related methods

- Removed `govpay_status` field from `Order` model.
- Added `govpay_status` method to dynamically fetch status from associated `Payment`.
- Updated `update_after_online_payment` method to no longer set `govpay_status`.
- Modified `govpay_callback_service` to reflect changes in `Order` model.
- Updated tests to remove checks for `govpay_status` field and added new
  • Loading branch information
jjromeo committed Sep 16, 2024
1 parent d4d20df commit dc5b28f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
12 changes: 8 additions & 4 deletions app/models/waste_carriers_engine/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Order
field :dateCreated, as: :date_created, type: DateTime
field :worldPayStatus, as: :world_pay_status, type: String
field :govpayId, as: :govpay_id, type: String
field :govpayStatus, as: :govpay_status, type: String
field :dateLastUpdated, as: :date_last_updated, type: DateTime
field :updatedByUser, as: :updated_by_user, type: String
field :description, type: String
Expand All @@ -42,13 +41,19 @@ def self.new_order_for(user_email)
order
end

def govpay_status
return nil unless govpay_id

payment = Payment.find_by(govpay_id: govpay_id)
payment.govpay_payment_status if payment
end

def add_bank_transfer_attributes
self.payment_method = "OFFLINE"
end

def add_govpay_attributes
self.payment_method = "ONLINE"
self.govpay_status = "IN_PROGRESS"
end

def generate_id
Expand All @@ -59,8 +64,7 @@ def set_description
self.description = generate_description
end

def update_after_online_payment(status, govpay_id = nil)
self.govpay_status = status
def update_after_online_payment(govpay_id = nil)
self.govpay_id = govpay_id if govpay_id
self.date_last_updated = Time.current
save!
Expand Down
4 changes: 2 additions & 2 deletions app/services/waste_carriers_engine/govpay_callback_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ def order_by_payment_uuid
def valid_unsuccessful_payment?(validation_method)
return false unless govpay_response_validator(@payment_status).public_send(validation_method)

@order.update_after_online_payment(@payment_status)
@order.update_after_online_payment
true
end

def update_payment_data
@order.update_after_online_payment(Payment::STATUS_SUCCESS)
@order.update_after_online_payment
payment = Payment.new_from_online_payment(@order, user_email)
payment.update_after_online_payment(
govpay_status: Payment::STATUS_SUCCESS,
Expand Down
44 changes: 39 additions & 5 deletions spec/models/waste_carriers_engine/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ module WasteCarriersEngine
let(:finance_details) { transient_registration.prepare_for_payment(:govpay, current_user) }
let(:order) { finance_details.orders.first }

it "copies the govpay status to the order" do
order.update_after_online_payment(Payment::STATUS_CREATED)
expect(order.govpay_status).to eq(Payment::STATUS_CREATED)
end

it "updates the date_last_updated" do
Timecop.freeze(Time.new(2004, 8, 15, 16, 23, 42)) do
# Wipe the date first so we know the value has been added
Expand Down Expand Up @@ -51,6 +46,45 @@ module WasteCarriersEngine
expect(order.payment_uuid).to eq uuid
end
end

describe "#govpay_status" do
let(:order) { build(:order) }

context "when govpay_id is nil" do
before { order.govpay_id = nil }

it "returns nil" do
expect(order.govpay_status).to be_nil
end
end

context "when govpay_id is present" do
let(:govpay_id) { "gov-pay-id-123" }
before { order.govpay_id = govpay_id }

context "when associated payment exists" do
let(:payment) { build(:payment, govpay_id: govpay_id, govpay_payment_status: "success") }

before do
allow(WasteCarriersEngine::Payment).to receive(:find_by).with(govpay_id: govpay_id).and_return(payment)
end

it "returns the govpay_payment_status of the associated payment" do
expect(order.govpay_status).to eq("success")
end
end

context "when associated payment does not exist" do
before do
allow(WasteCarriersEngine::Payment).to receive(:find_by).with(govpay_id: govpay_id).and_return(nil)
end

it "returns nil" do
expect(order.govpay_status).to be_nil
end
end
end
end
end
end
end
6 changes: 1 addition & 5 deletions spec/models/waste_carriers_engine/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,10 @@ module WasteCarriersEngine
before do
Timecop.freeze(Time.new(2018, 3, 4)) do
transient_registration.prepare_for_payment(:govpay, current_user)
payment.update_after_online_payment({ govpay_status: Payment::STATUS_CREATED })
payment.update_after_online_payment
end
end

it "updates the payment status" do
expect(payment.govpay_payment_status).to eq(Payment::STATUS_CREATED)
end

it "updates the payment date_received" do
expect(payment.date_received).to eq(Date.new(2018, 3, 4))
end
Expand Down

0 comments on commit dc5b28f

Please sign in to comment.