Skip to content

Commit

Permalink
Add voucher_type to voucher
Browse files Browse the repository at this point in the history
And update related specs
voucher_type doesn't do anything for now.
  • Loading branch information
rioug committed May 9, 2023
1 parent 74fee33 commit 28b3163
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 28 deletions.
15 changes: 14 additions & 1 deletion app/models/voucher.rb
Expand Up @@ -11,7 +11,20 @@ class Voucher < ApplicationRecord
dependent: :nullify

validates :code, presence: true, uniqueness: { scope: :enterprise_id }
validates :amount, presence: true, numericality: { greater_than: 0 }
validates :amount,
presence: true,
numericality: { greater_than: 0 },
if: ->(v) { v.voucher_type == FLAT_RATE }
validates :amount,
presence: true,
numericality: { greater_than: 0, less_than_or_equal_to: 100 },
if: ->(v) { v.voucher_type == PERCENTAGE_RATE }

FLAT_RATE = 'flat'.freeze
PERCENTAGE_RATE = 'percentage'.freeze
TYPES = [FLAT_RATE, PERCENTAGE_RATE].freeze

validates :voucher_type, inclusion: TYPES

def display_value
Spree::Money.new(amount)
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230508035306_add_type_to_vouchers.rb
@@ -0,0 +1,5 @@
class AddTypeToVouchers < ActiveRecord::Migration[7.0]
def change
add_column :vouchers, :voucher_type, :string, limit: 255
end
end
27 changes: 14 additions & 13 deletions db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_04_24_141213) do
ActiveRecord::Schema[7.0].define(version: 2023_05_08_035306) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"
Expand Down Expand Up @@ -1190,27 +1190,28 @@
t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
end

create_table "webhook_endpoints", force: :cascade do |t|
t.string "url", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id", default: 0, null: false
t.index ["user_id"], name: "index_webhook_endpoints_on_user_id"
end

create_table "vouchers", force: :cascade do |t|
t.string "code", limit: 255, null: false
t.datetime "expiry_date"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "enterprise_id"
t.datetime "deleted_at"
t.datetime "deleted_at", precision: nil
t.decimal "amount", precision: 10, scale: 2, default: "0.0", null: false
t.string "voucher_type", limit: 255
t.index ["code", "enterprise_id"], name: "index_vouchers_on_code_and_enterprise_id", unique: true
t.index ["deleted_at"], name: "index_vouchers_on_deleted_at"
t.index ["enterprise_id"], name: "index_vouchers_on_enterprise_id"
end

create_table "webhook_endpoints", force: :cascade do |t|
t.string "url", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id", default: 0, null: false
t.index ["user_id"], name: "index_webhook_endpoints_on_user_id"
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "adjustment_metadata", "enterprises", name: "adjustment_metadata_enterprise_id_fk"
Expand Down Expand Up @@ -1315,6 +1316,6 @@
add_foreign_key "subscriptions", "spree_shipping_methods", column: "shipping_method_id", name: "subscriptions_shipping_method_id_fk"
add_foreign_key "variant_overrides", "enterprises", column: "hub_id", name: "variant_overrides_hub_id_fk"
add_foreign_key "variant_overrides", "spree_variants", column: "variant_id", name: "variant_overrides_variant_id_fk"
add_foreign_key "webhook_endpoints", "spree_users", column: "user_id"
add_foreign_key "vouchers", "enterprises"
add_foreign_key "webhook_endpoints", "spree_users", column: "user_id"
end
2 changes: 1 addition & 1 deletion spec/controllers/split_checkout_controller_spec.rb
Expand Up @@ -237,7 +237,7 @@
end

describe "Vouchers" do
let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor) }
let(:voucher) { create(:voucher_flat_rate, code: 'some_code', enterprise: distributor) }

describe "adding a voucher" do
let(:checkout_params) do
Expand Down
11 changes: 9 additions & 2 deletions spec/factories/voucher_factory.rb
@@ -1,8 +1,15 @@
# frozen_string_literal: true

FactoryBot.define do
factory :voucher do
factory :voucher_flat_rate, class: Voucher do
enterprise { build(:distributor_enterprise) }
amount { rand(200.0) }
voucher_type { Voucher::FLAT_RATE }
amount { rand(1.0..200.0) }
end

factory :voucher_percentage, class: Voucher do
enterprise { build(:distributor_enterprise) }
voucher_type { Voucher::PERCENTAGE_RATE }
amount { rand(1..100) }
end
end
2 changes: 1 addition & 1 deletion spec/models/spree/order_spec.rb
Expand Up @@ -1434,7 +1434,7 @@ def advance_to_delivery_state(order)
describe "#voucher_adjustments" do
let(:distributor) { create(:distributor_enterprise) }
let(:order) { create(:order, user: user, distributor: distributor) }
let(:voucher) { create(:voucher, code: 'new_code', enterprise: order.distributor) }
let(:voucher) { create(:voucher_flat_rate, code: 'new_code', enterprise: order.distributor) }

context "when no voucher adjustment" do
it 'returns an empty array' do
Expand Down
24 changes: 20 additions & 4 deletions spec/models/voucher_spec.rb
Expand Up @@ -11,16 +11,30 @@
end

describe 'validations' do
subject { build(:voucher, code: 'new_code', enterprise: enterprise) }
subject { build(:voucher_flat_rate, code: 'new_code', enterprise: enterprise) }

it { is_expected.to validate_presence_of(:code) }
it { is_expected.to validate_uniqueness_of(:code).scoped_to(:enterprise_id) }
it { is_expected.to validate_presence_of(:amount) }
it { is_expected.to validate_numericality_of(:amount).is_greater_than(0) }
it { is_expected.to validate_inclusion_of(:voucher_type).in_array(Voucher::TYPES) }

context "when voucher_type is flat rate" do
it { is_expected.to validate_numericality_of(:amount).is_greater_than(0) }
end

context "when voucher_type is percentage rate" do
subject { build(:voucher_percentage, code: 'new_code', enterprise: enterprise) }

it do
is_expected.to validate_numericality_of(:amount)
.is_greater_than(0)
.is_less_than_or_equal_to(100)
end
end
end

describe '#compute_amount' do
subject { create(:voucher, code: 'new_code', enterprise: enterprise, amount: 10) }
subject { create(:voucher_flat_rate, code: 'new_code', enterprise: enterprise, amount: 10) }

let(:order) { create(:order_with_totals) }

Expand All @@ -41,7 +55,9 @@
describe '#create_adjustment' do
subject(:adjustment) { voucher.create_adjustment(voucher.code, order) }

let(:voucher) { create(:voucher, code: 'new_code', enterprise: enterprise, amount: 25) }
let(:voucher) do
create(:voucher_flat_rate, code: 'new_code', enterprise: enterprise, amount: 25)
end
let(:order) { create(:order_with_line_items, line_items_count: 3, distributor: enterprise) }

it 'includes the full voucher amount' do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/voucher_adjustments_spec.rb
Expand Up @@ -6,7 +6,7 @@
let(:user) { order.user }
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
let(:order) { create( :order_with_line_items, line_items_count: 1, distributor: distributor) }
let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor) }
let(:voucher) { create(:voucher_flat_rate, code: 'some_code', enterprise: distributor) }
let!(:adjustment) { voucher.create_adjustment(voucher.code, order) }

before do
Expand Down
4 changes: 3 additions & 1 deletion spec/services/voucher_adjustments_service_spec.rb
Expand Up @@ -5,7 +5,9 @@
describe VoucherAdjustmentsService do
describe '.calculate' do
let(:enterprise) { build(:enterprise) }
let(:voucher) { create(:voucher, code: 'new_code', enterprise: enterprise, amount: 10) }
let(:voucher) do
create(:voucher_flat_rate, code: 'new_code', enterprise: enterprise, amount: 10)
end

context 'when voucher covers the order total' do
subject { order.voucher_adjustments.first }
Expand Down
6 changes: 4 additions & 2 deletions spec/system/consumer/split_checkout_spec.rb
Expand Up @@ -721,7 +721,7 @@

context "with voucher available" do
let!(:voucher) do
create(:voucher, code: 'some_code', enterprise: distributor, amount: 15)
create(:voucher_flat_rate, code: 'some_code', enterprise: distributor, amount: 15)
end

before do
Expand Down Expand Up @@ -1113,7 +1113,9 @@
end

describe "vouchers" do
let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor, amount: 6) }
let(:voucher) do
create(:voucher_flat_rate, code: 'some_code', enterprise: distributor, amount: 6)
end

before do
# Add voucher to the order
Expand Down
2 changes: 1 addition & 1 deletion spec/system/consumer/split_checkout_tax_incl_spec.rb
Expand Up @@ -137,7 +137,7 @@

context "when using a voucher" do
let!(:voucher) do
create(:voucher, code: 'some_code', enterprise: distributor, amount: 10)
create(:voucher_flat_rate, code: 'some_code', enterprise: distributor, amount: 10)
end

it "will include a tax included amount on the voucher adjustment" do
Expand Down
2 changes: 1 addition & 1 deletion spec/system/consumer/split_checkout_tax_not_incl_spec.rb
Expand Up @@ -145,7 +145,7 @@

context "when using a voucher" do
let!(:voucher) do
create(:voucher, code: 'some_code', enterprise: distributor, amount: 10)
create(:voucher_flat_rate, code: 'some_code', enterprise: distributor, amount: 10)
end

it "will include a tax included amount on the voucher adjustment" do
Expand Down

0 comments on commit 28b3163

Please sign in to comment.