Skip to content

Add validation for multiple attributes on Refund and Activity #1246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: remove-reassignment
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions app/legacy_lib/insert_refunds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,34 @@ def self.modern_refund(charge, h)
results = InsertRefunds.perform_stripe_refund(nonprofit_id: charge["nonprofit_id"], refund_data: refund_data, charge_date: charge["created_at"])

Refund.transaction do
refund = Refund.create!({amount: h["amount"],
comment: h["comment"],
reason: h["reason"],
stripe_refund_id: results[:stripe_refund].id,
charge_id: charge["id"]})

refund.create_misc_refund_info(is_modern: true, stripe_application_fee_refund_id: results[:stripe_app_fee_refund]&.id)

gross = -h["amount"]
fees = (results[:stripe_app_fee_refund] && results[:stripe_app_fee_refund].amount) || 0
net = gross + fees
gross_amount = -h["amount"]
fee_total = results[:stripe_app_fee_refund]&.amount || 0
net_amount = gross_amount + fee_total
date = Time.current

# Create a corresponding./run negative payment record
payment = Payment.create!({
gross_amount: gross,
fee_total: fees,
net_amount: net,
gross_amount:,
fee_total:,
net_amount:,
kind: "Refund",
towards: original_payment.towards,
date: refund.created_at,
date:,
nonprofit_id: charge["nonprofit_id"],
supporter_id: charge["supporter_id"]
})

InsertActivities.for_refunds([payment.id])
refund = Refund.create!(
amount: h["amount"],
comment: h["comment"],
reason: h["reason"],
stripe_refund_id: results[:stripe_refund].id,
charge_id: charge["id"],
payment: payment
)

# Update the refund to have the above payment_id
refund.payment = payment
refund.save!
refund.create_misc_refund_info(is_modern: true, stripe_application_fee_refund_id: results[:stripe_app_fee_refund]&.id)

InsertActivities.for_refunds([payment.id])
# Update original payment to increment its refund_total for any future refund attempts
original_payment.refund_total += h["amount"].to_i
original_payment.save!
Expand Down
7 changes: 4 additions & 3 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions app/models/refund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class Refund < ApplicationRecord

attr_accessor :failure_message

belongs_to :charge
belongs_to :payment
belongs_to :charge, optional: false
belongs_to :payment, optional: false
belongs_to :user, optional: true
has_one :subtransaction_payment, through: :payment
has_one :misc_refund_info
has_one :nonprofit, through: :charge
Expand All @@ -27,6 +28,8 @@ class Refund < ApplicationRecord

has_many :manual_balance_adjustments, as: :entity

validates :amount, presence: true, numericality: {only_integer: true, greater_than: 0}

def original_payment
charge&.payment
end
Expand Down
13 changes: 13 additions & 0 deletions spec/models/activity_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions spec/models/refund_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
require "rails_helper"

RSpec.describe Refund, type: :model do
it { is_expected.to belong_to(:charge) }
it { is_expected.to belong_to(:payment) }
it { is_expected.to belong_to(:charge).required(true) }
it { is_expected.to belong_to(:payment).required(true) }
it { is_expected.to belong_to(:user).required(false) }
it { is_expected.to have_one(:subtransaction_payment).through(:payment) }
it { is_expected.to have_one(:misc_refund_info) }

it { is_expected.to have_one(:nonprofit).through(:charge) }
it { is_expected.to have_one(:supporter).through(:charge) }
it { is_expected.to have_many(:manual_balance_adjustments) }

it { is_expected.to validate_presence_of(:amount) }
it { is_expected.to validate_numericality_of(:amount).only_integer.is_greater_than(0) }

describe "#from_donation?" do
it "is true when refund is associated with a donation" do
expect(build(:refund, :from_donation).from_donation?).to eq true
Expand Down
Loading