diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 3d7d646c6c..a5d4fe6ff6 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -65,6 +65,7 @@ class Enterprise < ApplicationRecord has_many :inventory_items has_many :tag_rules has_one :stripe_account, dependent: :destroy + has_many :vouchers delegate :latitude, :longitude, :city, :state_name, to: :address diff --git a/app/models/voucher.rb b/app/models/voucher.rb new file mode 100644 index 0000000000..a36da2f218 --- /dev/null +++ b/app/models/voucher.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: false + +class Voucher < ApplicationRecord + belongs_to :enterprise + + validates :code, presence: true, uniqueness: { scope: :enterprise_id } +end diff --git a/db/migrate/20230215034821_create_vouchers.rb b/db/migrate/20230215034821_create_vouchers.rb new file mode 100644 index 0000000000..acd2e247a7 --- /dev/null +++ b/db/migrate/20230215034821_create_vouchers.rb @@ -0,0 +1,12 @@ +class CreateVouchers < ActiveRecord::Migration[6.1] + def change + create_table :vouchers do |t| + t.string :code, null: false, limit: 255 + t.datetime :expiry_date + + t.timestamps + end + add_reference :vouchers, :enterprise, foreign_key: true + add_index :vouchers, [:code, :enterprise_id], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 75cccc9216..d125bec563 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1197,6 +1197,16 @@ ActiveRecord::Schema[7.0].define(version: 2023_02_13_160135) do 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.bigint "enterprise_id" + t.index ["code", "enterprise_id"], name: "index_vouchers_on_code_and_enterprise_id", unique: true + t.index ["enterprise_id"], name: "index_vouchers_on_enterprise_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" @@ -1302,4 +1312,5 @@ ActiveRecord::Schema[7.0].define(version: 2023_02_13_160135) do 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" end diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index f6290a8687..5600216689 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -24,6 +24,7 @@ describe Enterprise do it { is_expected.to have_many(:distributed_orders) } it { is_expected.to belong_to(:address) } it { is_expected.to belong_to(:business_address) } + it { is_expected.to have_many(:vouchers) } it "destroys enterprise roles upon its own demise" do e = create(:enterprise) diff --git a/spec/models/voucher_spec.rb b/spec/models/voucher_spec.rb new file mode 100644 index 0000000000..9d9590455f --- /dev/null +++ b/spec/models/voucher_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Voucher do + describe 'associations' do + it { is_expected.to belong_to(:enterprise) } + end + + describe 'validations' do + subject { Voucher.new(code: 'new_code', enterprise: enterprise) } + + let(:enterprise) { build(:enterprise) } + + it { is_expected.to validate_presence_of(:code) } + it { is_expected.to validate_uniqueness_of(:code).scoped_to(:enterprise_id) } + end +end