From c95c7358ab750d3b74053d9123b4ac711d722671 Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Mon, 14 Jul 2025 12:00:22 -0500 Subject: [PATCH 1/3] Add spec for Activity --- app/models/activity.rb | 7 ++++--- spec/models/activity_spec.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 spec/models/activity_spec.rb diff --git a/app/models/activity.rb b/app/models/activity.rb index 88c34b554..9ce32b1dd 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -1,6 +1,7 @@ # License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later class Activity < ApplicationRecord - belongs_to :attachment, polymorphic: true - belongs_to :supporter - belongs_to :nonprofit + belongs_to :attachment, polymorphic: true, optional: false + belongs_to :supporter, optional: false + belongs_to :nonprofit, optional: false + belongs_to :user, optional: true end diff --git a/spec/models/activity_spec.rb b/spec/models/activity_spec.rb new file mode 100644 index 000000000..b58028acc --- /dev/null +++ b/spec/models/activity_spec.rb @@ -0,0 +1,13 @@ +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +require "rails_helper" +RSpec.describe Activity, type: :model do + context "validation" do + it { is_expected.to belong_to(:attachment).required(true) } + it { is_expected.to belong_to(:supporter).required(true) } + + it { is_expected.to belong_to(:nonprofit).required(true) } + + it { is_expected.to belong_to(:user).required(false) } + + end +end From 196fa0e14e1efaa17f0fb4b479986f9184f69275 Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Mon, 14 Jul 2025 12:01:11 -0500 Subject: [PATCH 2/3] Switch `InsertActivity.for_refunds` to accept `Payment`s --- app/legacy_lib/insert_activities.rb | 20 ++++++++++++++++---- app/legacy_lib/insert_refunds.rb | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/legacy_lib/insert_activities.rb b/app/legacy_lib/insert_activities.rb index 6b6cce178..944150b1e 100644 --- a/app/legacy_lib/insert_activities.rb +++ b/app/legacy_lib/insert_activities.rb @@ -82,10 +82,22 @@ def self.insert_tickets_expr .add_join("events AS event", "event.id=tickets.event_id") end - def self.for_refunds(payment_ids) - insert_refunds_expr - .and_where("payments.id IN ($ids)", ids: payment_ids) - .execute + def self.for_refunds(*payments) + payments.map do |payment| + refund = payment.refund + Activity.create( + attachment: payment, + supporter: payment.supporter, + nonprofit: payment.nonprofit, + kind: "Refund", + json_data: { + gross_amount: payment.gross_amount, + reason: refund.reason, + email: refund.user&.email + }, + user: refund.user + ) + end end def self.insert_refunds_expr diff --git a/app/legacy_lib/insert_refunds.rb b/app/legacy_lib/insert_refunds.rb index eb0fadfb1..3ee6b6d88 100644 --- a/app/legacy_lib/insert_refunds.rb +++ b/app/legacy_lib/insert_refunds.rb @@ -53,7 +53,7 @@ def self.modern_refund(charge, h) supporter_id: charge["supporter_id"] }) - InsertActivities.for_refunds([payment.id]) + InsertActivities.for_refunds(payment) # Update the refund to have the above payment_id refund.payment = payment From 83a3be7961d9af8c8c7291c5db639e1ec1bc3540 Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Mon, 14 Jul 2025 12:01:52 -0500 Subject: [PATCH 3/3] Verify `InsertActivity.for_refunds` is being passed `Payment`s --- spec/lib/insert/insert_refunds_spec.rb | 2 +- spec/lib/query/query_payments_spec.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/lib/insert/insert_refunds_spec.rb b/spec/lib/insert/insert_refunds_spec.rb index 1decfa910..571e63ccd 100644 --- a/spec/lib/insert/insert_refunds_spec.rb +++ b/spec/lib/insert/insert_refunds_spec.rb @@ -86,7 +86,7 @@ "reason" => reason }, charge_date: charge.created_at ).and_return(perform_stripe_refund_result) - expect(InsertActivities).to receive(:for_refunds) + expect(InsertActivities).to receive(:for_refunds).with(an_instance_of(Payment)) end let!(:modern_refund_call) do diff --git a/spec/lib/query/query_payments_spec.rb b/spec/lib/query/query_payments_spec.rb index b6b0fc57e..e9135c8f7 100644 --- a/spec/lib/query/query_payments_spec.rb +++ b/spec/lib/query/query_payments_spec.rb @@ -184,7 +184,7 @@ def generate_donation(h) "charge" => charge.stripe_charge_id }, charge_date: charge.created_at ).and_return(perform_stripe_refund_result) - expect(InsertActivities).to receive(:for_refunds) + expect(InsertActivities).to receive(:for_refunds).with(an_instance_of(Payment)) InsertRefunds.with_stripe(charge.attributes, {amount: 100}.with_indifferent_access) } @@ -196,7 +196,7 @@ def generate_donation(h) "charge" => charge.stripe_charge_id }, charge_date: charge.created_at ).and_return(perform_stripe_refund_result) - expect(InsertActivities).to receive(:for_refunds) + expect(InsertActivities).to receive(:for_refunds).with(an_instance_of(Payment)) InsertRefunds.with_stripe(charge.attributes, {amount: 50}.with_indifferent_access) } @@ -681,7 +681,7 @@ def generate_donation(h) "charge" => charge.stripe_charge_id }, charge_date: charge.created_at ).and_return(perform_stripe_refund_result) - expect(InsertActivities).to receive(:for_refunds) + expect(InsertActivities).to receive(:for_refunds).with(an_instance_of(Payment)) InsertRefunds.with_stripe(charge.attributes, {amount: 100}.with_indifferent_access) } @@ -693,7 +693,7 @@ def generate_donation(h) "charge" => charge.stripe_charge_id }, charge_date: charge.created_at ).and_return(perform_stripe_refund_result) - expect(InsertActivities).to receive(:for_refunds) + expect(InsertActivities).to receive(:for_refunds).with(an_instance_of(Payment)) InsertRefunds.with_stripe(charge.attributes, {amount: 50}.with_indifferent_access) } @@ -774,7 +774,7 @@ def generate_donation(h) "charge" => charge.stripe_charge_id }, charge_date: charge.created_at ).and_return(perform_stripe_refund_result) - expect(InsertActivities).to receive(:for_refunds) + expect(InsertActivities).to receive(:for_refunds).with(an_instance_of(Payment)) InsertRefunds.with_stripe(charge.attributes, {amount: 50}.with_indifferent_access) }