From e7a2b7ea48aec5450764155df02e86882382d640 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 6 Mar 2026 12:58:07 +1100 Subject: [PATCH] Remove payment method from customer account transaction --- app/models/customer_account_transaction.rb | 3 --- app/models/spree/payment_method.rb | 1 - .../customer_account_transaction/index.turbo_stream.haml | 4 ---- .../spree/users/_customer_account_transactions.html.haml | 4 ---- config/locales/en.yml | 2 -- ..._payment_method_from_customer_account_transactions.rb | 8 ++++++++ db/schema.rb | 7 ++----- spec/factories/customer_account_transaction_factory.rb | 1 - spec/models/customer_account_transaction_spec.rb | 1 - spec/models/spree/payment_method_spec.rb | 1 - spec/services/orders/customer_credit_service_spec.rb | 9 +-------- spec/system/consumer/checkout/details_spec.rb | 3 +-- spec/system/consumer/checkout/payment_spec.rb | 1 - spec/system/consumer/checkout/summary_spec.rb | 2 -- 14 files changed, 12 insertions(+), 35 deletions(-) create mode 100644 db/migrate/20260306015040_delete_payment_method_from_customer_account_transactions.rb diff --git a/app/models/customer_account_transaction.rb b/app/models/customer_account_transaction.rb index 5527008038..52bdcc77b7 100644 --- a/app/models/customer_account_transaction.rb +++ b/app/models/customer_account_transaction.rb @@ -8,9 +8,6 @@ class CustomerAccountTransaction < ApplicationRecord localize_number :amount belongs_to :customer - belongs_to :payment_method, -> { - internal - }, class_name: "Spree::PaymentMethod", inverse_of: :customer_account_transactions belongs_to :payment, class_name: "Spree::Payment", optional: true belongs_to :created_by, class_name: "Spree::User", optional: true diff --git a/app/models/spree/payment_method.rb b/app/models/spree/payment_method.rb index 43f47bebea..f8cd301a21 100644 --- a/app/models/spree/payment_method.rb +++ b/app/models/spree/payment_method.rb @@ -15,7 +15,6 @@ module Spree default_scope -> { where(deleted_at: nil).where.not(type: INTERNAL) } has_many :credit_cards, class_name: "Spree::CreditCard", dependent: :destroy - has_many :customer_account_transactions, dependent: :restrict_with_error has_many :payments, class_name: "Spree::Payment", dependent: :restrict_with_error validates :name, presence: true diff --git a/app/views/admin/customer_account_transaction/index.turbo_stream.haml b/app/views/admin/customer_account_transaction/index.turbo_stream.haml index 6291d83e3a..a16486adf5 100644 --- a/app/views/admin/customer_account_transaction/index.turbo_stream.haml +++ b/app/views/admin/customer_account_transaction/index.turbo_stream.haml @@ -9,8 +9,6 @@ = t(".transaction_date") %th.description = t(".description") - %th.payment-method - = t(".payment_method") %th.created-by = t(".created_by") %th.amount @@ -25,8 +23,6 @@ = transaction.updated_at.strftime("%Y-%m-%d") %td.description = transaction.description - %td.payment-method - = transaction.payment_method.display_name %td.created-by = transaction.created_by&.email %td.amount diff --git a/app/views/spree/users/_customer_account_transactions.html.haml b/app/views/spree/users/_customer_account_transactions.html.haml index 269fbe277f..cc14ae4320 100644 --- a/app/views/spree/users/_customer_account_transactions.html.haml +++ b/app/views/spree/users/_customer_account_transactions.html.haml @@ -26,19 +26,15 @@ %tr %th.order1= t(".transaction_date") %th.order2= t(".description") - %th.order3= t(".payment_method") %th.order4= t(".amount") %th.order5= t(".running_balance") %tbody.transaction-group - data_loader.customer_account_transactions.each do |transaction| %tr.transaction-row.even %td.order1 - - # TODO move to helper = transaction.updated_at.strftime("%Y-%m-%d") %td.order2 = transaction.description - %td.order3 - = transaction.payment_method.display_name %td.order4 = Spree::Money.new(transaction.amount) %td.order5 diff --git a/config/locales/en.yml b/config/locales/en.yml index 1a60b6e077..e1663f12a4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -900,7 +900,6 @@ en: available_credit: "Available credit: %{available_credit}" transaction_date: Transaction Date description: Description - payment_method: Payment method amount: Amount created_by: Created by running_balance: Running balance @@ -5052,7 +5051,6 @@ en: title: Customer Transactions credit_available: "Credit available: %{credit}" transaction_date: Transaction Date - payment_method: Payment method description: Description amount: Amount running_balance: Running balance diff --git a/db/migrate/20260306015040_delete_payment_method_from_customer_account_transactions.rb b/db/migrate/20260306015040_delete_payment_method_from_customer_account_transactions.rb new file mode 100644 index 0000000000..a20bbcd182 --- /dev/null +++ b/db/migrate/20260306015040_delete_payment_method_from_customer_account_transactions.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class DeletePaymentMethodFromCustomerAccountTransactions < ActiveRecord::Migration[7.1] + def change + remove_reference :customer_account_transactions, + :payment_method, index: true, foreign_key: { to_table: :spree_payment_methods } + end +end diff --git a/db/schema.rb b/db/schema.rb index 83583134a0..aaef15970f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2026_03_01_031845) do +ActiveRecord::Schema[7.1].define(version: 2026_03_06_015040) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "plpgsql" @@ -93,16 +93,14 @@ ActiveRecord::Schema[7.1].define(version: 2026_03_01_031845) do t.decimal "amount", precision: 10, scale: 2, null: false t.string "currency" t.string "description" - t.bigint "payment_method_id", null: false t.integer "payment_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.decimal "balance", precision: 10, scale: 2, default: "0.0" t.bigint "created_by_id" t.index ["created_by_id"], name: "index_customer_account_transactions_on_created_by_id" - t.index ["payment_id"], name: "index_customer_account_transactions_on_payment_id" t.index ["customer_id"], name: "index_customer_account_transactions_on_customer_id" - t.index ["payment_method_id"], name: "index_customer_account_transactions_on_payment_method_id" + t.index ["payment_id"], name: "index_customer_account_transactions_on_payment_id" end create_table "customers", id: :serial, force: :cascade do |t| @@ -1175,7 +1173,6 @@ ActiveRecord::Schema[7.1].define(version: 2026_03_01_031845) do add_foreign_key "coordinator_fees", "order_cycles", name: "coordinator_fees_order_cycle_id_fk" add_foreign_key "custom_tabs", "enterprises", on_delete: :cascade add_foreign_key "customer_account_transactions", "customers" - add_foreign_key "customer_account_transactions", "spree_payment_methods", column: "payment_method_id" add_foreign_key "customer_account_transactions", "spree_payments", column: "payment_id" add_foreign_key "customer_account_transactions", "spree_users", column: "created_by_id" add_foreign_key "customers", "enterprises", name: "customers_enterprise_id_fk" diff --git a/spec/factories/customer_account_transaction_factory.rb b/spec/factories/customer_account_transaction_factory.rb index 265408f3d4..40c780328c 100644 --- a/spec/factories/customer_account_transaction_factory.rb +++ b/spec/factories/customer_account_transaction_factory.rb @@ -5,6 +5,5 @@ FactoryBot.define do customer { build(:customer) } amount { 10.00 } currency { "AUD" } - payment_method { build(:api_customer_credit_payment_method) } end end diff --git a/spec/models/customer_account_transaction_spec.rb b/spec/models/customer_account_transaction_spec.rb index b9ab276486..60160b0b31 100644 --- a/spec/models/customer_account_transaction_spec.rb +++ b/spec/models/customer_account_transaction_spec.rb @@ -9,7 +9,6 @@ RSpec.describe CustomerAccountTransaction do it { is_expected.to belong_to(:customer) } it { is_expected.to validate_presence_of(:amount) } it { is_expected.to validate_presence_of(:currency) } - it { is_expected.to belong_to(:payment_method) } it { is_expected.to belong_to(:payment).optional } it { is_expected.to belong_to(:created_by).optional } end diff --git a/spec/models/spree/payment_method_spec.rb b/spec/models/spree/payment_method_spec.rb index fa4e85cde6..e74707e0a3 100644 --- a/spec/models/spree/payment_method_spec.rb +++ b/spec/models/spree/payment_method_spec.rb @@ -7,7 +7,6 @@ RSpec.describe Spree::PaymentMethod do describe "associations" do subject { build(:payment_method) } - it { is_expected.to have_many(:customer_account_transactions) } it { is_expected.to have_many(:payments) } end diff --git a/spec/services/orders/customer_credit_service_spec.rb b/spec/services/orders/customer_credit_service_spec.rb index 43efc65156..cab6169a29 100644 --- a/spec/services/orders/customer_credit_service_spec.rb +++ b/spec/services/orders/customer_credit_service_spec.rb @@ -20,8 +20,7 @@ RSpec.describe Orders::CustomerCreditService do create( :customer_account_transaction, amount: 100.00, - customer: order.customer, - payment_method: credit_payment_method + customer: order.customer ) subject.apply @@ -45,7 +44,6 @@ RSpec.describe Orders::CustomerCreditService do :customer_account_transaction, amount: 100.00, customer: order.customer, - payment_method: credit_payment_method ) subject.apply @@ -66,7 +64,6 @@ RSpec.describe Orders::CustomerCreditService do :customer_account_transaction, amount: 5.00, customer: order.customer, - payment_method: credit_payment_method ) subject.apply @@ -82,7 +79,6 @@ RSpec.describe Orders::CustomerCreditService do :customer_account_transaction, amount: 5.00, customer: order.customer, - payment_method: credit_payment_method ) allow_any_instance_of(Spree::Payment).to receive(:internal_purchase!) .and_raise(Spree::Core::GatewayError) @@ -104,12 +100,10 @@ RSpec.describe Orders::CustomerCreditService do context "when credit payment method is missing" do before do # Add credit - payment_method = create(:api_customer_credit_payment_method) create( :customer_account_transaction, amount: 5.00, customer: order.customer, - payment_method: ) credit_payment_method.destroy! end @@ -155,7 +149,6 @@ RSpec.describe Orders::CustomerCreditService do subject.refund(user: ) last_transaction = order.customer.customer_account_transactions.last - expect(last_transaction.payment_method).to eq(credit_payment_method) expect(last_transaction.amount).to eq(12.00) expect(last_transaction.created_by).to eq(user) end diff --git a/spec/system/consumer/checkout/details_spec.rb b/spec/system/consumer/checkout/details_spec.rb index 12c31a2694..4691fa5e05 100644 --- a/spec/system/consumer/checkout/details_spec.rb +++ b/spec/system/consumer/checkout/details_spec.rb @@ -352,7 +352,7 @@ RSpec.describe "As a consumer, I want to checkout my order" do end context "wiht customer credit" do - let(:credit_payment_method) { create(:customer_credit_payment_method) } + let!(:credit_payment_method) { create(:customer_credit_payment_method) } let(:credit_amount) { 100.00 } let(:customer) { create(:customer, user:, enterprise: distributor) } @@ -364,7 +364,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do :customer_account_transaction, amount: credit_amount, customer: order.customer, - payment_method: credit_payment_method ) visit checkout_step_path(:details) fill_out_details diff --git a/spec/system/consumer/checkout/payment_spec.rb b/spec/system/consumer/checkout/payment_spec.rb index 80d6f9f480..e9374b4a53 100644 --- a/spec/system/consumer/checkout/payment_spec.rb +++ b/spec/system/consumer/checkout/payment_spec.rb @@ -116,7 +116,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do create( :customer_account_transaction, amount: 100, customer: order.customer, - payment_method: credit_payment_method ) # Add credit payment payment = order.payments.create!(payment_method: credit_payment_method, diff --git a/spec/system/consumer/checkout/summary_spec.rb b/spec/system/consumer/checkout/summary_spec.rb index def11d6688..e099b1ffb2 100644 --- a/spec/system/consumer/checkout/summary_spec.rb +++ b/spec/system/consumer/checkout/summary_spec.rb @@ -393,7 +393,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do :customer_account_transaction, amount: 100, customer: order.customer, - payment_method: credit_payment_method ) # Add credit payment payment = order.payments.create!(payment_method: credit_payment_method, @@ -424,7 +423,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do visit checkout_step_path(:summary) place_order - # TODO it should be displaying some kind indication it was paid with credit expect(page).to have_content "PAID" expect(page).to have_content "Paying via: Customer credit" expect(page).to have_selector("#customer-credit", text: with_currency(-10.00))