diff --git a/app/models/coupon.rb b/app/models/coupon.rb index 934c338d724..8cf5ef935a0 100644 --- a/app/models/coupon.rb +++ b/app/models/coupon.rb @@ -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 { diff --git a/spec/models/coupon_spec.rb b/spec/models/coupon_spec.rb index 89cb7ef0de7..249b62103ca 100644 --- a/spec/models/coupon_spec.rb +++ b/spec/models/coupon_spec.rb @@ -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! diff --git a/spec/requests/api/v1/applied_coupons_spec.rb b/spec/requests/api/v1/applied_coupons_spec.rb index af32ca9a376..9ff7d1a0889 100644 --- a/spec/requests/api/v1/applied_coupons_spec.rb +++ b/spec/requests/api/v1/applied_coupons_spec.rb @@ -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, @@ -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, @@ -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, diff --git a/spec/services/credits/applied_coupon_service_spec.rb b/spec/services/credits/applied_coupon_service_spec.rb index ff4120d4ae2..de2dc1ade7e 100644 --- a/spec/services/credits/applied_coupon_service_spec.rb +++ b/spec/services/credits/applied_coupon_service_spec.rb @@ -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) @@ -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( diff --git a/spec/services/credits/applied_coupons_service_spec.rb b/spec/services/credits/applied_coupons_service_spec.rb index 5e2be8c7582..c6d2520b750 100644 --- a/spec/services/credits/applied_coupons_service_spec.rb +++ b/spec/services/credits/applied_coupons_service_spec.rb @@ -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, @@ -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,