mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-11 03:40:20 +00:00
Add CustomerAccountTransaction model
It's used to store payment against a customer, to model customer credit
This commit is contained in:
16
app/models/customer_account_transaction.rb
Normal file
16
app/models/customer_account_transaction.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "spree/localized_number"
|
||||
|
||||
class CustomerAccountTransaction < ApplicationRecord
|
||||
extend Spree::LocalizedNumber
|
||||
|
||||
localize_number :amount
|
||||
|
||||
belongs_to :customer
|
||||
belongs_to :payment_method, class_name: "Spree::PaymentMethod"
|
||||
belongs_to :payment, class_name: "Spree::Payment", optional: true
|
||||
|
||||
validates :amount, presence: true
|
||||
validates :currency, presence: true
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CreateCustomerAccountTransactions < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
create_table :customer_account_transactions do |t|
|
||||
t.references :customer, null: false, foreign_key: { to_table: :customers }
|
||||
t.decimal :amount, null: false, precision: 10, scale: 2
|
||||
t.string :currency
|
||||
t.string :description
|
||||
t.references :payment_method, null: false, foreign_key: { to_table: :spree_payment_methods }
|
||||
t.integer :payment_id
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
17
db/schema.rb
17
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: 2025_11_26_005628) do
|
||||
ActiveRecord::Schema[7.1].define(version: 2026_01_19_024509) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "pg_stat_statements"
|
||||
enable_extension "plpgsql"
|
||||
@@ -88,6 +88,19 @@ ActiveRecord::Schema[7.1].define(version: 2025_11_26_005628) do
|
||||
t.index ["enterprise_id"], name: "index_custom_tabs_on_enterprise_id"
|
||||
end
|
||||
|
||||
create_table "customer_account_transactions", force: :cascade do |t|
|
||||
t.bigint "customer_id", null: false
|
||||
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.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"
|
||||
end
|
||||
|
||||
create_table "customers", id: :serial, force: :cascade do |t|
|
||||
t.string "email", limit: 255, null: false
|
||||
t.integer "enterprise_id", null: false
|
||||
@@ -1157,6 +1170,8 @@ ActiveRecord::Schema[7.1].define(version: 2025_11_26_005628) do
|
||||
add_foreign_key "coordinator_fees", "enterprise_fees", name: "coordinator_fees_enterprise_fee_id_fk"
|
||||
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 "customers", "enterprises", name: "customers_enterprise_id_fk"
|
||||
add_foreign_key "customers", "spree_addresses", column: "bill_address_id", name: "customers_bill_address_id_fk"
|
||||
add_foreign_key "customers", "spree_addresses", column: "ship_address_id", name: "customers_ship_address_id_fk"
|
||||
|
||||
10
spec/factories/customer_account_transaction_factory.rb
Normal file
10
spec/factories/customer_account_transaction_factory.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :customer_account_transaction do
|
||||
customer { build(:customer) }
|
||||
amount { 10.00 }
|
||||
currency { "AUD" }
|
||||
payment_method { build(:payment_method) }
|
||||
end
|
||||
end
|
||||
20
spec/models/customer_account_transaction_spec.rb
Normal file
20
spec/models/customer_account_transaction_spec.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe CustomerAccountTransaction do
|
||||
describe 'validations' do
|
||||
subject { build(:customer_account_transaction) }
|
||||
|
||||
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 }
|
||||
end
|
||||
|
||||
context "extends LocalizedNumber" do
|
||||
subject { build_stubbed(:customer_account_transaction) }
|
||||
it_behaves_like "a model using the LocalizedNumber module", [:amount]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user