Skip to content

Commit

Permalink
Close #769 discount maximum cap
Browse files Browse the repository at this point in the history
  • Loading branch information
kimsrung committed Nov 13, 2023
1 parent 418d1c1 commit 818d389
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/services/spree_cm_commissioner/discount_cap_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module SpreeCmCommissioner
class DiscountCapCalculator
def initialize(order_amount, percentage_discount, discount_cap)
@order_amount = order_amount
@percentage_discount = percentage_discount
@discount_cap = discount_cap
end

def calculate_discount
calculated_discount = (@order_amount * @percentage_discount) / 100
final_discount = [calculated_discount, @discount_cap].min
final_discount.to_f
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "spec_helper"

RSpec.describe SpreeCmCommissioner::DiscountCapCalculator do
let(:order1) { create(:order, total: 100 ) }
let(:order2) { create(:order, total: 30 ) }
let(:percentage_discount) {50}
let(:discount_cap) {20}

describe '#calculate_discount' do
it 'calculates the discount with a cap' do
subject = described_class.new(order1.total, percentage_discount, discount_cap)

final_discount = subject.calculate_discount

expect(final_discount).to eq(20.0)
end

it 'calculates the discount without a cap' do
subject = described_class.new(order2.total, percentage_discount, discount_cap)

final_discount = subject.calculate_discount

expect(final_discount).to eq(15.0)
end
end
end

0 comments on commit 818d389

Please sign in to comment.