From f7993dc21ecb3fc5059d44b94588faa099894ecc Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 24 Dec 2024 14:14:50 +0000 Subject: [PATCH 01/21] feat: add payment type and reference --- ..._payment_type_and_reference_to_payments.rb | 30 +++++++++++++++++++ db/schema.rb | 5 +++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb diff --git a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb new file mode 100644 index 00000000000..daa50183838 --- /dev/null +++ b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class AddPaymentTypeAndReferenceToPayments < ActiveRecord::Migration[7.1] + disable_ddl_transaction! + + def up + create_enum :payment_type, %w[provider manual] + + add_column :payments, :payment_type, :enum, enum_type: 'payment_type', null: true + add_column :payments, :reference, :string, default: nil + + # Backfill existing records + Payment.in_batches(of: 10_000).update_all(payment_type: 'provider') # rubocop:disable Rails/SkipsModelValidations + + safety_assured do + execute <<~SQL + ALTER TABLE payments ALTER COLUMN payment_type SET DEFAULT 'provider'; + SQL + execute <<~SQL + ALTER TABLE payments ALTER COLUMN payment_type SET NOT NULL; + SQL + end + end + + def down + remove_column :payments, :payment_type + remove_column :payments, :reference + drop_enum :payment_type + end +end diff --git a/db/schema.rb b/db/schema.rb index 610465f71c9..22c99d05a42 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: 2024_12_23_154437) do +ActiveRecord::Schema[7.1].define(version: 2024_12_24_141116) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -23,6 +23,7 @@ create_enum "customer_type", ["company", "individual"] create_enum "inbound_webhook_status", ["pending", "processing", "succeeded", "failed"] create_enum "payment_payable_payment_status", ["pending", "processing", "succeeded", "failed"] + create_enum "payment_type", ["provider", "manual"] create_enum "subscription_invoicing_reason", ["subscription_starting", "subscription_periodic", "subscription_terminating", "in_advance_charge", "in_advance_charge_periodic", "progressive_billing"] create_enum "tax_status", ["pending", "succeeded", "failed"] @@ -1115,6 +1116,8 @@ t.uuid "payable_id" t.jsonb "provider_payment_data", default: {} t.enum "payable_payment_status", enum_type: "payment_payable_payment_status" + t.enum "payment_type", default: "provider", null: false, enum_type: "payment_type" + t.string "reference" t.index ["invoice_id"], name: "index_payments_on_invoice_id" t.index ["payable_id", "payable_type"], name: "index_payments_on_payable_id_and_payable_type", unique: true, where: "(payable_payment_status = ANY (ARRAY['pending'::payment_payable_payment_status, 'processing'::payment_payable_payment_status]))" t.index ["payable_type", "payable_id"], name: "index_payments_on_payable_type_and_payable_id" From f1352cb9c76e78c00d7faa74ef0c025d2231ff30 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 24 Dec 2024 14:45:59 +0000 Subject: [PATCH 02/21] feat: add invoice total paid amount --- ...add_total_paid_amount_cents_to_invoices.rb | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb diff --git a/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb b/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb new file mode 100644 index 00000000000..7cef71dc18a --- /dev/null +++ b/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class AddTotalPaidAmountCentsToInvoices < ActiveRecord::Migration[7.1] + disable_ddl_transaction! + + def up + add_column :invoices, :total_paid_amount_cents, :bigint, null: true, default: 0 + # Backfill + Invoice.in_batches(of: 10_000).each do |batch| + batch.update_all(total_paid_amount_cents: 0) # rubocop:disable Rails/SkipsModelValidations + end + + safety_assured do + execute <<~SQL + ALTER TABLE invoices ALTER COLUMN total_paid_amount_cents SET DEFAULT 0; + SQL + execute <<~SQL + ALTER TABLE invoices ALTER COLUMN total_paid_amount_cents SET NOT NULL; + SQL + end + end + + def down + # Remove the column + remove_column :invoices, :total_paid_amount_cents + end +end \ No newline at end of file From 7b57d879e597af677ea677525b7d51fc8653bf42 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 24 Dec 2024 14:46:36 +0000 Subject: [PATCH 03/21] feat: add invoice total paid amount --- db/schema.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 22c99d05a42..67b0dbf3fdf 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: 2024_12_24_141116) do +ActiveRecord::Schema[7.1].define(version: 2024_12_24_142141) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -935,6 +935,7 @@ t.bigint "negative_amount_cents", default: 0, null: false t.bigint "progressive_billing_credit_amount_cents", default: 0, null: false t.enum "tax_status", enum_type: "tax_status" + t.bigint "total_paid_amount_cents", default: 0, null: false t.index ["customer_id", "sequential_id"], name: "index_invoices_on_customer_id_and_sequential_id", unique: true t.index ["customer_id"], name: "index_invoices_on_customer_id" t.index ["issuing_date"], name: "index_invoices_on_issuing_date" From 6e830002139d3c0d4cd4a32aa0b296e8ca4909a4 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 24 Dec 2024 14:48:02 +0000 Subject: [PATCH 04/21] feat: rubocop --- .../20241224142141_add_total_paid_amount_cents_to_invoices.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb b/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb index 7cef71dc18a..c67e31a5d28 100644 --- a/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb +++ b/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb @@ -24,4 +24,4 @@ def down # Remove the column remove_column :invoices, :total_paid_amount_cents end -end \ No newline at end of file +end From b4e6ab1ab86dcedd2555e3bcc3efc487e47471d8 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 24 Dec 2024 14:52:00 +0000 Subject: [PATCH 05/21] feat: rubocop --- ...1224141116_add_payment_type_and_reference_to_payments.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb index daa50183838..2f0380117ad 100644 --- a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb +++ b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb @@ -6,8 +6,10 @@ class AddPaymentTypeAndReferenceToPayments < ActiveRecord::Migration[7.1] def up create_enum :payment_type, %w[provider manual] - add_column :payments, :payment_type, :enum, enum_type: 'payment_type', null: true - add_column :payments, :reference, :string, default: nil + change_table :payments, bulk: true do |t| + t.column :payment_type, :enum, enum_type: 'payment_type', null: true + t.column :reference, :string, default: nil + end # Backfill existing records Payment.in_batches(of: 10_000).update_all(payment_type: 'provider') # rubocop:disable Rails/SkipsModelValidations From f8a260217f4c84a017e8b8842e622060bde6ae1f Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 24 Dec 2024 14:54:38 +0000 Subject: [PATCH 06/21] feat: rubocop --- app/models/invoice.rb | 1 + app/models/payment.rb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 861f07f593f..accd00eb5e1 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -483,6 +483,7 @@ def status_changed_to_finalized? # taxes_rate :float default(0.0), not null # timezone :string default("UTC"), not null # total_amount_cents :bigint default(0), not null +# total_paid_amount_cents :bigint default(0), not null # version_number :integer default(4), not null # voided_at :datetime # created_at :datetime not null diff --git a/app/models/payment.rb b/app/models/payment.rb index 4bc5415fc6d..2dfef4378bd 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -32,7 +32,9 @@ def should_sync_payment? # amount_currency :string not null # payable_payment_status :enum # payable_type :string default("Invoice"), not null +# payment_type :enum default("provider"), not null # provider_payment_data :jsonb +# reference :string # status :string not null # created_at :datetime not null # updated_at :datetime not null From 45a7d20ea43a9501cec8888bf13e1309acbd4f77 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 24 Dec 2024 16:19:58 +0000 Subject: [PATCH 07/21] feat: rubocop --- ...224141116_add_payment_type_and_reference_to_payments.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb index 2f0380117ad..1b54988a45b 100644 --- a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb +++ b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb @@ -25,8 +25,11 @@ def up end def down - remove_column :payments, :payment_type - remove_column :payments, :reference + change_table :payments, bulk: true do |t| + t.remove :payment_type + t.remove :reference + end + drop_enum :payment_type end end From 406ca7153102b14f15e3cc07d75423b8553e0e81 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Fri, 27 Dec 2024 11:39:44 +0000 Subject: [PATCH 08/21] feat: rubocop --- ...24141116_add_payment_type_and_reference_to_payments.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb index 1b54988a45b..f9eb4197284 100644 --- a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb +++ b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb @@ -6,9 +6,11 @@ class AddPaymentTypeAndReferenceToPayments < ActiveRecord::Migration[7.1] def up create_enum :payment_type, %w[provider manual] - change_table :payments, bulk: true do |t| - t.column :payment_type, :enum, enum_type: 'payment_type', null: true - t.column :reference, :string, default: nil + safety_assured do + change_table :payments, bulk: true do |t| + t.column :payment_type, :enum, enum_type: 'payment_type', null: true + t.column :reference, :string, default: nil + end end # Backfill existing records From d1cfdb21d1ecb9dcd4fa5f684c00f640870d7c3c Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 30 Dec 2024 14:36:51 +0000 Subject: [PATCH 09/21] feat: add index to the migration --- ...241224141116_add_payment_type_and_reference_to_payments.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb index f9eb4197284..4c51dd8e3d8 100644 --- a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb +++ b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb @@ -24,9 +24,13 @@ def up ALTER TABLE payments ALTER COLUMN payment_type SET NOT NULL; SQL end + + add_index :payments, :payment_type, algorithm: :concurrently end def down + remove_index :payments, column: :payment_type + change_table :payments, bulk: true do |t| t.remove :payment_type t.remove :reference From 5f9cadae02127e341e72b70ad0ef01e92ec3c5ba Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 30 Dec 2024 14:55:01 +0000 Subject: [PATCH 10/21] feat: add index to the migration --- app/models/payment.rb | 2 +- ...241224141116_add_payment_type_and_reference_to_payments.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/payment.rb b/app/models/payment.rb index 2dfef4378bd..650ff8e3955 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -7,7 +7,7 @@ class Payment < ApplicationRecord belongs_to :payable, polymorphic: true belongs_to :payment_provider, optional: true, class_name: 'PaymentProviders::BaseProvider' - belongs_to :payment_provider_customer, class_name: 'PaymentProviderCustomers::BaseCustomer' + belongs_to :payment_provider_customer, optional: true, class_name: 'PaymentProviderCustomers::BaseCustomer' has_many :refunds has_many :integration_resources, as: :syncable diff --git a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb index 4c51dd8e3d8..3497c76c6cd 100644 --- a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb +++ b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb @@ -25,7 +25,9 @@ def up SQL end - add_index :payments, :payment_type, algorithm: :concurrently + safety_assured do + add_index :payments, :payment_type, algorithm: :concurrently + end end def down From d569f1269020fd6114ef67c9e3f3090b3d2284fc Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 30 Dec 2024 15:20:07 +0000 Subject: [PATCH 11/21] feat: add index to the migration --- app/models/payment.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/payment.rb b/app/models/payment.rb index 650ff8e3955..ea38ca05d79 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -51,6 +51,7 @@ def should_sync_payment? # index_payments_on_payable_type_and_payable_id (payable_type,payable_id) # index_payments_on_payment_provider_customer_id (payment_provider_customer_id) # index_payments_on_payment_provider_id (payment_provider_id) +# index_payments_on_payment_type (payment_type) # # Foreign Keys # From b2ed9373bfb2da3409ea3561f06990c8c995dd45 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 30 Dec 2024 15:30:14 +0000 Subject: [PATCH 12/21] feat: add index to the migration --- ...241224141116_add_payment_type_and_reference_to_payments.rb | 4 +--- db/schema.rb | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb index 3497c76c6cd..4c51dd8e3d8 100644 --- a/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb +++ b/db/migrate/20241224141116_add_payment_type_and_reference_to_payments.rb @@ -25,9 +25,7 @@ def up SQL end - safety_assured do - add_index :payments, :payment_type, algorithm: :concurrently - end + add_index :payments, :payment_type, algorithm: :concurrently end def down diff --git a/db/schema.rb b/db/schema.rb index 67b0dbf3fdf..46cad0d3b8c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1124,6 +1124,7 @@ t.index ["payable_type", "payable_id"], name: "index_payments_on_payable_type_and_payable_id" t.index ["payment_provider_customer_id"], name: "index_payments_on_payment_provider_customer_id" t.index ["payment_provider_id"], name: "index_payments_on_payment_provider_id" + t.index ["payment_type"], name: "index_payments_on_payment_type" end create_table "plans", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| From e8f9582cd9a47ada7ec196f5db354ebac9a13d31 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 30 Dec 2024 16:20:56 +0000 Subject: [PATCH 13/21] feat: add index to the migration --- app/models/payment.rb | 5 +++++ spec/factories/payments.rb | 1 + spec/models/payment_spec.rb | 6 +++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/models/payment.rb b/app/models/payment.rb index ea38ca05d79..2c788baabec 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -12,6 +12,11 @@ class Payment < ApplicationRecord has_many :refunds has_many :integration_resources, as: :syncable + PAYMENT_TYPES = {provider: "provider", manual: "manual"} + enum :payment_type, PAYMENT_TYPES, default: :provider, prefix: :payment_type + validates :reference, length: {maximum: 40} + + delegate :customer, to: :payable enum payable_payment_status: PAYABLE_PAYMENT_STATUS.map { |s| [s, s] }.to_h diff --git a/spec/factories/payments.rb b/spec/factories/payments.rb index 19de49b1ebb..af4638bc2cf 100644 --- a/spec/factories/payments.rb +++ b/spec/factories/payments.rb @@ -10,6 +10,7 @@ amount_currency { 'EUR' } provider_payment_id { SecureRandom.uuid } status { 'pending' } + payment_type { 'provider' } trait :adyen_payment do association :payment_provider, factory: :adyen_provider diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index 1b688f37ef0..f4b1012e493 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -3,7 +3,11 @@ require 'rails_helper' RSpec.describe Payment, type: :model do - subject(:payment) { create(:payment) } + subject(:payment) { build(:payment, payment_type:, provider_payment_id:, reference:) } + + let(:payment_type) { 'provider' } + let(:provider_payment_id) { SecureRandom.uuid } + let(:reference) { nil } it_behaves_like 'paper_trail traceable' From c16c510a48173e51ec1d63abf99aa1054f8f9acd Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 30 Dec 2024 16:24:51 +0000 Subject: [PATCH 14/21] feat: add index to the migration --- app/models/payment.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/payment.rb b/app/models/payment.rb index 2c788baabec..9537c57e28f 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -13,6 +13,7 @@ class Payment < ApplicationRecord has_many :integration_resources, as: :syncable PAYMENT_TYPES = {provider: "provider", manual: "manual"} + attribute :payment_type, :string enum :payment_type, PAYMENT_TYPES, default: :provider, prefix: :payment_type validates :reference, length: {maximum: 40} From 05e8567f61fe406cf2258ded2ece6f1619c47e49 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 30 Dec 2024 16:27:17 +0000 Subject: [PATCH 15/21] feat: add index to the migration --- app/models/payment.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/models/payment.rb b/app/models/payment.rb index 9537c57e28f..921c0a8ccc8 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -17,7 +17,6 @@ class Payment < ApplicationRecord enum :payment_type, PAYMENT_TYPES, default: :provider, prefix: :payment_type validates :reference, length: {maximum: 40} - delegate :customer, to: :payable enum payable_payment_status: PAYABLE_PAYMENT_STATUS.map { |s| [s, s] }.to_h From d9750c16cf8dc3889a0a84d24c1f640511429f4a Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 30 Dec 2024 16:51:45 +0000 Subject: [PATCH 16/21] feat: add index to the migration --- spec/models/payment_spec.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index f4b1012e493..f09e9cd12a5 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -15,6 +15,28 @@ it { is_expected.to belong_to(:payable) } it { is_expected.to delegate_method(:customer).to(:payable) } + it do + expect(subject) + .to define_enum_for(:payment_type) + .with_values(Payment::PAYMENT_TYPES) + .with_prefix(:payment_type) + .backed_by_column_of_type(:enum) + end + + describe 'validations' do + let(:errors) { payment.errors } + + before { payment.valid? } + + context 'when reference is more than 40 characters' do + let(:reference) { 'a' * 41 } + + it 'adds an error' do + expect(errors.where(:reference, :too_long)).to be_present + end + end + end + describe '#should_sync_payment?' do subject(:method_call) { payment.should_sync_payment? } From a830cd3887da12492e6bd68d5ede0073b1fcfff8 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 31 Dec 2024 10:05:12 +0000 Subject: [PATCH 17/21] feat: add index to the migration --- app/models/payment.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/payment.rb b/app/models/payment.rb index 921c0a8ccc8..b926190a852 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -15,7 +15,9 @@ class Payment < ApplicationRecord PAYMENT_TYPES = {provider: "provider", manual: "manual"} attribute :payment_type, :string enum :payment_type, PAYMENT_TYPES, default: :provider, prefix: :payment_type - validates :reference, length: {maximum: 40} + validates :payment_type, presence: true + validates :reference, presence: true, length: {maximum: 40}, if: -> { payment_type_manual? } + validates :reference, absence: true, if: -> { payment_type_provider? } delegate :customer, to: :payable From 9f6ab10dcd88270e8c298bfa11d0a5a4a631a7d9 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 31 Dec 2024 10:12:58 +0000 Subject: [PATCH 18/21] feat: add specs --- spec/models/payment_spec.rb | 40 +++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index f09e9cd12a5..6369cedd91d 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -28,11 +28,43 @@ before { payment.valid? } - context 'when reference is more than 40 characters' do - let(:reference) { 'a' * 41 } + describe 'of reference' do + context 'when payment type is provider' do + context 'when reference is present' do + let(:reference) { '123' } - it 'adds an error' do - expect(errors.where(:reference, :too_long)).to be_present + it 'adds an error' do + expect(errors.where(:reference, :present)).to be_present + end + end + + context 'when reference is not present' do + it 'does not add an error' do + expect(errors.where(:reference, :present)).not_to be_present + end + end + end + + context 'when payment type is manual' do + let(:payment_type) { 'manual' } + + context 'when reference is present' do + context 'when reference is less than 40 characters' do + let(:reference) { '123' } + + it 'does not add an error' do + expect(errors.where(:reference, :blank)).not_to be_present + end + end + + context 'when reference is more than 40 characters' do + let(:reference) { 'a' * 41 } + + it 'adds an error' do + expect(errors.where(:reference, :too_long)).to be_present + end + end + end end end end From 6d41c10fcd7795afd0dafb91083b072a227c33b3 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 31 Dec 2024 10:16:16 +0000 Subject: [PATCH 19/21] feat: add specs --- spec/models/payment_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index 6369cedd91d..79749a2a02b 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -14,6 +14,7 @@ it { is_expected.to have_many(:integration_resources) } it { is_expected.to belong_to(:payable) } it { is_expected.to delegate_method(:customer).to(:payable) } + it { is_expected.to validate_presence_of(:payment_type) } it do expect(subject) From a2ca810417ecc833ce627919b2a63ce927ed5476 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 31 Dec 2024 12:16:56 +0000 Subject: [PATCH 20/21] feat: remove default --- .../20241224142141_add_total_paid_amount_cents_to_invoices.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb b/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb index c67e31a5d28..467b3c7a5e3 100644 --- a/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb +++ b/db/migrate/20241224142141_add_total_paid_amount_cents_to_invoices.rb @@ -4,7 +4,7 @@ class AddTotalPaidAmountCentsToInvoices < ActiveRecord::Migration[7.1] disable_ddl_transaction! def up - add_column :invoices, :total_paid_amount_cents, :bigint, null: true, default: 0 + add_column :invoices, :total_paid_amount_cents, :bigint, null: true # Backfill Invoice.in_batches(of: 10_000).each do |batch| batch.update_all(total_paid_amount_cents: 0) # rubocop:disable Rails/SkipsModelValidations From 1bf7af1d7be5ba555934e5aced725eb1c7a6c3b9 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 7 Jan 2025 09:21:48 +0000 Subject: [PATCH 21/21] feat: added specs --- spec/models/payment_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index 79749a2a02b..bfb31a3a020 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -49,6 +49,12 @@ context 'when payment type is manual' do let(:payment_type) { 'manual' } + context 'when reference is not present' do + it 'adds an error' do + expect(errors[:reference]).to include('value_is_mandatory') + end + end + context 'when reference is present' do context 'when reference is less than 40 characters' do let(:reference) { '123' }