Skip to content

Commit

Permalink
feat (coupons): add few more coupon validations (#912)
Browse files Browse the repository at this point in the history
* add few more coupon validations

* fix specs

* fix linter issues
  • Loading branch information
lovrocolic authored Mar 10, 2023
1 parent c88798e commit 5d8007d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
5 changes: 5 additions & 0 deletions app/models/coupon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ class Coupon < ApplicationRecord
validates :name, presence: true
validates :code, uniqueness: { conditions: -> { where(deleted_at: nil) }, scope: :organization_id }

validates :amount_cents, presence: true, if: :fixed_amount?
validates :amount_cents, numericality: { greater_than: 0 }, allow_nil: true

validates :amount_currency, presence: true, if: :fixed_amount?
validates :amount_currency, inclusion: { in: currency_list }, allow_nil: true

validates :percentage_rate, presence: true, if: :percentage?

default_scope -> { kept }
scope :order_by_status_and_expiration,
lambda {
Expand Down
44 changes: 44 additions & 0 deletions spec/models/coupon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,52 @@
RSpec.describe Coupon, type: :model do
subject(:coupon) { create(:coupon) }

let(:organization) { create(:organization) }

it_behaves_like 'paper_trail traceable'

describe 'validations' do
context 'when coupon is fixed amount' do
it 'validates amount_cents' do
expect(coupon).to be_valid

coupon.amount_cents = nil
expect(coupon).not_to be_valid
end

it 'validates amount_currency' do
coupon.amount_currency = nil
expect(coupon).not_to be_valid
end

it 'validates percentage_rate' do
coupon.percentage_rate = nil
expect(coupon).to be_valid
end
end

context 'when coupon is percentage' do
subject(:coupon) { create(:coupon, coupon_type: 'percentage', percentage_rate: 10) }

it 'validates percentage_rate' do
expect(coupon).to be_valid

coupon.percentage_rate = nil
expect(coupon).not_to be_valid
end

it 'validates amount_cents' do
coupon.amount_cents = nil
expect(coupon).to be_valid
end

it 'validates amount_currency' do
coupon.amount_currency = nil
expect(coupon).to be_valid
end
end
end

describe '.mark_as_terminated' do
it 'terminates the coupon' do
coupon.mark_as_terminated!
Expand Down
8 changes: 5 additions & 3 deletions spec/requests/api/v1/applied_coupons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@
end

context 'with pagination' do
let(:coupon_latest) { create(:coupon, coupon_type: 'percentage', organization: organization) }
let(:coupon_latest) do
create(:coupon, coupon_type: 'percentage', percentage_rate: 10, organization:)
end
let(:applied_coupon_latest) do
create(
:applied_coupon,
Expand Down Expand Up @@ -117,7 +119,7 @@
end

context 'with status param' do
let(:coupon_latest) { create(:coupon, coupon_type: 'percentage', organization: organization) }
let(:coupon_latest) { create(:coupon, coupon_type: 'percentage', percentage_rate: 10, organization:) }
let(:applied_coupon_latest) do
create(
:applied_coupon,
Expand All @@ -144,7 +146,7 @@

context 'with external_customer_id params' do
let(:customer_new) { create(:customer, organization: organization) }
let(:coupon_latest) { create(:coupon, coupon_type: 'percentage', organization: organization) }
let(:coupon_latest) { create(:coupon, coupon_type: 'percentage', percentage_rate: 10, organization:) }
let(:applied_coupon_latest) do
create(
:applied_coupon,
Expand Down
6 changes: 4 additions & 2 deletions spec/services/credits/applied_coupon_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
end

context 'when coupon is percentage' do
let(:coupon) { create(:coupon, coupon_type: 'percentage') }
let(:coupon) { create(:coupon, coupon_type: 'percentage', percentage_rate: 10.00) }

let(:applied_coupon) do
create(:applied_coupon, coupon:, percentage_rate: 20.00)
Expand Down Expand Up @@ -227,7 +227,9 @@
end

context 'when coupon is recurring and percentage' do
let(:coupon) { create(:coupon, frequency: 'recurring', frequency_duration: 3, coupon_type: 'percentage') }
let(:coupon) do
create(:coupon, frequency: 'recurring', frequency_duration: 3, coupon_type: 'percentage', percentage_rate: 10)
end

let(:applied_coupon) do
create(
Expand Down
4 changes: 2 additions & 2 deletions spec/services/credits/applied_coupons_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
amount_currency: plan.amount_currency,
)
end
let(:coupon_latest) { create(:coupon, coupon_type: 'percentage') }
let(:coupon_latest) { create(:coupon, coupon_type: 'percentage', percentage_rate: 10.00) }
let(:applied_coupon_latest) do
create(
:applied_coupon,
Expand Down Expand Up @@ -98,7 +98,7 @@
end

context 'when both coupons are percentage' do
let(:coupon) { create(:coupon, coupon_type: 'percentage') }
let(:coupon) { create(:coupon, coupon_type: 'percentage', percentage_rate: 10.00) }
let(:applied_coupon) do
create(
:applied_coupon,
Expand Down

0 comments on commit 5d8007d

Please sign in to comment.