mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Update various voucher related file to use the new Vouchers::Vine
This commit is contained in:
committed by
Rachel Arnould
parent
48ad7ed8a0
commit
d413a142c9
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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"],
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user