diff --git a/app/controllers/voucher_adjustments_controller.rb b/app/controllers/voucher_adjustments_controller.rb index 8f85798c2f..3b774d46ea 100644 --- a/app/controllers/voucher_adjustments_controller.rb +++ b/app/controllers/voucher_adjustments_controller.rb @@ -78,9 +78,9 @@ class VoucherAdjustmentsController < BaseController end def load_voucher - voucher = Voucher.local.find_by(code: voucher_params[:voucher_code], - enterprise: @order.distributor) - return voucher unless voucher.nil? + voucher = Voucher.find_by(code: voucher_params[:voucher_code], + enterprise: @order.distributor) + return voucher unless voucher.nil? || voucher.is_a?(Vouchers::Vine) vine_voucher end diff --git a/app/services/vine/voucher_redeemer_service.rb b/app/services/vine/voucher_redeemer_service.rb index 6e03e0d2b8..25fb26b505 100644 --- a/app/services/vine/voucher_redeemer_service.rb +++ b/app/services/vine/voucher_redeemer_service.rb @@ -14,7 +14,7 @@ module Vine voucher_adjustment = order.voucher_adjustments.first @voucher = voucher_adjustment&.originator - return true if voucher_adjustment.nil? || !@voucher.vine? + return true if voucher_adjustment.nil? || !@voucher.is_a?(Vouchers::Vine) if vine_settings.nil? errors[:vine_settings] = I18n.t("vine_voucher_redeemer_service.errors.vine_settings") diff --git a/app/services/vine/voucher_validator_service.rb b/app/services/vine/voucher_validator_service.rb index c8f9aa628d..0eba5ee3b3 100644 --- a/app/services/vine/voucher_validator_service.rb +++ b/app/services/vine/voucher_validator_service.rb @@ -61,7 +61,7 @@ module Vine voucher_data = response.body["data"] # Check if voucher already exist - voucher = Voucher.vine.find_or_initialize_by( + voucher = Vouchers::Vine.find_or_initialize_by( code: voucher_code, enterprise: @enterprise, external_voucher_id: voucher_data["id"], diff --git a/app/views/admin/enterprises/form/_vouchers.html.haml b/app/views/admin/enterprises/form/_vouchers.html.haml index 042d1e7470..faea0baa58 100644 --- a/app/views/admin/enterprises/form/_vouchers.html.haml +++ b/app/views/admin/enterprises/form/_vouchers.html.haml @@ -3,7 +3,7 @@ = t('.add_new') %br -- if @enterprise.vouchers.local.with_deleted.present? +- if @enterprise.vouchers.where.not(type: "Vouchers::Vine").with_deleted.present? %table %thead %tr @@ -17,7 +17,7 @@ /%th= t('.customers') /%th= t('.net_value') %tbody - - @enterprise.vouchers.local.with_deleted.order(deleted_at: :desc, code: :asc).each do |voucher| + - @enterprise.vouchers.where.not(type: "Vouchers::Vine").with_deleted.order(deleted_at: :desc, code: :asc).each do |voucher| %tr %td= voucher.code %td= voucher.display_value diff --git a/spec/requests/voucher_adjustments_spec.rb b/spec/requests/voucher_adjustments_spec.rb index cdfdebb899..074b8e1a8a 100644 --- a/spec/requests/voucher_adjustments_spec.rb +++ b/spec/requests/voucher_adjustments_spec.rb @@ -56,7 +56,7 @@ RSpec.describe VoucherAdjustmentsController, type: :request do # Create a non valid adjustment bad_adjustment = build(:adjustment, label: nil) allow(voucher).to receive(:create_adjustment).and_return(bad_adjustment) - allow(Voucher).to receive_message_chain(:local, :find_by).and_return(voucher) + allow(Voucher).to receive(:find_by).and_return(voucher) post("/voucher_adjustments", params:) @@ -106,7 +106,7 @@ RSpec.describe VoucherAdjustmentsController, type: :request do end it "adds a voucher to the user's current order" do - vine_voucher = create(:voucher_flat_rate, code: vine_voucher_code) + vine_voucher = create(:vine_voucher, code: vine_voucher_code) mock_vine_voucher_validator(voucher: vine_voucher) post("/voucher_adjustments", params:) @@ -170,8 +170,8 @@ RSpec.describe VoucherAdjustmentsController, type: :request do context "when creating a new voucher fails" do it "returns 422 and an error message" do - vine_voucher = build(:voucher_flat_rate, code: vine_voucher_code, - enterprise: distributor, amount: "") + vine_voucher = build(:vine_voucher, code: vine_voucher_code, + enterprise: distributor, amount: "") mock_vine_voucher_validator(voucher: vine_voucher) post("/voucher_adjustments", params:) @@ -197,8 +197,8 @@ RSpec.describe VoucherAdjustmentsController, type: :request do end it "adds a voucher to the user's current order" do - vine_voucher = create(:voucher_flat_rate, code: vine_voucher_code, - enterprise: distributor) + vine_voucher = create(:vine_voucher, code: vine_voucher_code, + enterprise: distributor) mock_vine_voucher_validator(voucher: vine_voucher) expect { @@ -209,8 +209,8 @@ RSpec.describe VoucherAdjustmentsController, type: :request do context "when updating the voucher fails" do it "returns 422 and an error message" do - vine_voucher = build(:voucher_flat_rate, code: vine_voucher_code, - enterprise: distributor, amount: "") + vine_voucher = build(:vine_voucher, code: vine_voucher_code, + enterprise: distributor, amount: "") mock_vine_voucher_validator(voucher: vine_voucher) post("/voucher_adjustments", params:) diff --git a/spec/services/vine/voucher_validator_service_spec.rb b/spec/services/vine/voucher_validator_service_spec.rb index 5d95262c00..ad795d43c7 100644 --- a/spec/services/vine/voucher_validator_service_spec.rb +++ b/spec/services/vine/voucher_validator_service_spec.rb @@ -55,9 +55,9 @@ RSpec.describe Vine::VoucherValidatorService, feature: :connected_apps do vine_voucher = validate_voucher_service.validate expect(vine_voucher).not_to be_nil + expect(vine_voucher).to be_a(Vouchers::Vine) expect(vine_voucher.code).to eq(voucher_code) expect(vine_voucher.amount).to eq(5.00) - expect(vine_voucher.vine?).to eq(true) expect(vine_voucher.external_voucher_id).to eq(vine_voucher_id) expect(vine_voucher.external_voucher_set_id).to eq(vine_voucher_set_id) end @@ -96,7 +96,7 @@ RSpec.describe Vine::VoucherValidatorService, feature: :connected_apps do expect(vine_voucher.enterprise).to eq(distributor) expect(vine_voucher.code).to eq(voucher_code) expect(vine_voucher.amount).to eq(2.50) - expect(vine_voucher.vine?).to eq(true) + expect(vine_voucher).to be_a(Vouchers::Vine) expect(vine_voucher.external_voucher_id).to eq(vine_voucher_id) expect(vine_voucher.external_voucher_set_id).to eq(vine_voucher_set_id) end @@ -137,7 +137,7 @@ RSpec.describe Vine::VoucherValidatorService, feature: :connected_apps do expect(vine_voucher.enterprise).to eq(distributor) expect(vine_voucher.code).to eq(voucher_code) expect(vine_voucher.amount).to eq(1.40) - expect(vine_voucher.vine?).to eq(true) + expect(vine_voucher).to be_a(Vouchers::Vine) expect(vine_voucher.external_voucher_id).to eq(new_vine_voucher_id) expect(vine_voucher.external_voucher_set_id).to eq(new_vine_voucher_set_id) end diff --git a/spec/system/admin/vouchers_spec.rb b/spec/system/admin/vouchers_spec.rb index 2673cb2403..3a6fb74a79 100644 --- a/spec/system/admin/vouchers_spec.rb +++ b/spec/system/admin/vouchers_spec.rb @@ -11,6 +11,7 @@ RSpec.describe ' let(:enterprise) { create(:supplier_enterprise, name: 'Feedme', sells: 'own') } let(:voucher_code) { 'awesomevoucher' } + let(:vine_voucher_code) { 'vine_voucher' } let(:amount) { 25 } let(:enterprise_user) { create(:user, enterprise_limit: 1) } @@ -22,6 +23,7 @@ RSpec.describe ' it 'lists enterprise vouchers' do # Given an enterprise with vouchers create(:voucher_flat_rate, enterprise:, code: voucher_code, amount:) + create(:vine_voucher, enterprise:, code: vine_voucher_code, amount:) # When I go to the enterprise voucher tab visit edit_admin_enterprise_path(enterprise) @@ -31,6 +33,8 @@ RSpec.describe ' # Then I see a list of vouchers expect(page).to have_content voucher_code expect(page).to have_content amount + + expect(page).not_to have_content vine_voucher_code end describe "adding voucher" do diff --git a/spec/system/consumer/checkout/payment_spec.rb b/spec/system/consumer/checkout/payment_spec.rb index d53e9c8908..0f3caddea8 100644 --- a/spec/system/consumer/checkout/payment_spec.rb +++ b/spec/system/consumer/checkout/payment_spec.rb @@ -189,7 +189,8 @@ RSpec.describe "As a consumer, I want to checkout my order" do expect(page).to have_content "$5.00 Voucher" expect(order.reload.voucher_adjustments.length).to eq(1) - expect(Voucher.vine.find_by(code: "CI3922", enterprise: distributor)).not_to be_nil + expect(Vouchers::Vine.find_by(code: "CI3922", + enterprise: distributor)).not_to be_nil end context "with an invalid voucher" do @@ -198,7 +199,7 @@ RSpec.describe "As a consumer, I want to checkout my order" do click_button("Apply") expect(page).to have_content("There was an error while adding the voucher") - expect(Voucher.vine.find_by(code: "non_code", enterprise: distributor)).to be_nil + expect(Vouchers::Vine.find_by(code: "KM1891", enterprise: distributor)).to be_nil end end end