mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-06 02:51:34 +00:00
Merge pull request #13528 from dacook/vine-expiry-message-13495
[VINE] Show helpful message if voucher expired
This commit is contained in:
@@ -90,11 +90,13 @@ class VoucherAdjustmentsController < BaseController
|
||||
voucher_code: voucher_params[:voucher_code], enterprise: @order.distributor
|
||||
)
|
||||
voucher = vine_voucher_validator.validate
|
||||
errors = vine_voucher_validator.errors
|
||||
|
||||
return nil if vine_voucher_validator.errors[:not_found_voucher].present?
|
||||
return nil if errors[:not_found_voucher].present?
|
||||
|
||||
if vine_voucher_validator.errors.present?
|
||||
@order.errors.add(:voucher_code, I18n.t('checkout.errors.add_voucher_error'))
|
||||
if errors.present?
|
||||
message = errors[:invalid_voucher] || I18n.t('checkout.errors.add_voucher_error')
|
||||
@order.errors.add(:voucher_code, message)
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
module Vine
|
||||
class VoucherValidatorService
|
||||
VINE_ERRORS = {
|
||||
# https://github.com/openfoodfoundation/vine/blob/main/app/Enums/ApiResponse.php
|
||||
"This voucher has expired." => :expired,
|
||||
}.freeze
|
||||
|
||||
attr_reader :voucher_code, :errors
|
||||
|
||||
def initialize(voucher_code:, enterprise:)
|
||||
@@ -42,8 +47,10 @@ module Vine
|
||||
end
|
||||
|
||||
def handle_errors(response)
|
||||
if response[:status] == 400
|
||||
errors[:invalid_voucher] = I18n.t("vine_voucher_validator_service.errors.invalid_voucher")
|
||||
if [400, 409].include?(response[:status])
|
||||
message = response[:body] && JSON.parse(response[:body]).dig("meta", "message")
|
||||
key = VINE_ERRORS.fetch(message, :invalid_voucher)
|
||||
errors[:invalid_voucher] = I18n.t("vine_voucher_validator_service.errors.#{key}")
|
||||
elsif response[:status] == 404
|
||||
errors[:not_found_voucher] =
|
||||
I18n.t("vine_voucher_validator_service.errors.not_found_voucher")
|
||||
|
||||
@@ -627,6 +627,7 @@ en:
|
||||
errors:
|
||||
vine_api: "There was an error communicating with the API, please try again later."
|
||||
invalid_voucher: "The voucher is not valid"
|
||||
expired: "The voucher has expired"
|
||||
not_found_voucher: "Sorry, we couldn't find that voucher, please check the code."
|
||||
vine_voucher_redeemer_service:
|
||||
errors:
|
||||
|
||||
@@ -161,7 +161,19 @@ RSpec.describe VoucherAdjustmentsController do
|
||||
post("/voucher_adjustments", params:)
|
||||
|
||||
expect(response).to be_unprocessable
|
||||
expect(flash[:error]).to match "There was an error while adding the voucher"
|
||||
expect(flash[:error]).to match "The voucher is not valid"
|
||||
end
|
||||
end
|
||||
|
||||
context "when voucher has expired" do
|
||||
it "returns 422 and an error message" do
|
||||
mock_vine_voucher_validator(voucher: nil,
|
||||
errors: { invalid_voucher: "The voucher has expired" })
|
||||
|
||||
post("/voucher_adjustments", params:)
|
||||
|
||||
expect(response).to be_unprocessable
|
||||
expect(flash[:error]).to match "The voucher has expired"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -219,6 +231,20 @@ RSpec.describe VoucherAdjustmentsController do
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "when voucher has expired" do
|
||||
it "returns 422 and an error message" do
|
||||
vine_voucher = build(:vine_voucher, code: vine_voucher_code,
|
||||
enterprise: distributor)
|
||||
mock_vine_voucher_validator(voucher: vine_voucher,
|
||||
errors: { invalid_voucher: "The voucher has expired" })
|
||||
|
||||
post("/voucher_adjustments", params:)
|
||||
|
||||
expect(response).to be_unprocessable
|
||||
expect(flash[:error]).to match "The voucher has expired"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -270,11 +270,12 @@ RSpec.describe Vine::VoucherValidatorService, feature: :connected_apps do
|
||||
enterprise: distributor, data: { api_key: "1234568", secret: "my_secret" }
|
||||
)
|
||||
}
|
||||
# Faraday returns un-parsed json
|
||||
let(:data) {
|
||||
{
|
||||
meta: { responseCode: 400, limit: 50, offset: 0, message: "Invalid merchant team." },
|
||||
data: []
|
||||
}.deep_stringify_keys
|
||||
}.to_json
|
||||
}
|
||||
|
||||
before do
|
||||
@@ -285,7 +286,7 @@ RSpec.describe Vine::VoucherValidatorService, feature: :connected_apps do
|
||||
expect_validate_to_be_nil
|
||||
end
|
||||
|
||||
it "adds an error message" do
|
||||
it "adds a general error message" do
|
||||
validate_voucher_service.validate
|
||||
|
||||
expect(validate_voucher_service.errors).to include(
|
||||
@@ -293,6 +294,23 @@ RSpec.describe Vine::VoucherValidatorService, feature: :connected_apps do
|
||||
)
|
||||
end
|
||||
|
||||
context "it is expired" do
|
||||
let(:data) {
|
||||
{
|
||||
meta: { responseCode: 400, limit: 50, offset: 0, message: "This voucher has expired." },
|
||||
data: []
|
||||
}.to_json
|
||||
}
|
||||
|
||||
it "adds a specific error message" do
|
||||
validate_voucher_service.validate
|
||||
|
||||
expect(validate_voucher_service.errors).to include(
|
||||
{ invalid_voucher: "The voucher has expired" }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
it "doesn't creates a new VINE voucher" do
|
||||
expect_voucher_count_not_to_change
|
||||
end
|
||||
@@ -420,6 +438,25 @@ RSpec.describe Vine::VoucherValidatorService, feature: :connected_apps do
|
||||
}.not_to change { voucher.reload.amount }
|
||||
end
|
||||
end
|
||||
|
||||
context "it is expired" do
|
||||
let(:data) {
|
||||
{
|
||||
meta: { responseCode: 400, limit: 50, offset: 0, message: "This voucher has expired." },
|
||||
data: []
|
||||
}.to_json
|
||||
}
|
||||
|
||||
it "adds a specific error message" do
|
||||
mock_api_exception(type: Faraday::BadRequestError, status: 409, body: data)
|
||||
|
||||
validate_voucher_service.validate
|
||||
|
||||
expect(validate_voucher_service.errors).to include(
|
||||
{ invalid_voucher: "The voucher has expired" }
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -198,7 +198,7 @@ RSpec.describe "As a consumer, I want to checkout my order" do
|
||||
fill_in "Enter voucher code", with: "KM1891"
|
||||
click_button("Apply")
|
||||
|
||||
expect(page).to have_content("There was an error while adding the voucher")
|
||||
expect(page).to have_content("The voucher is not valid")
|
||||
expect(Vouchers::Vine.find_by(code: "KM1891", enterprise: distributor)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user