-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2958 from aitbw/nebulab/deprecate_coupon_code_met…
…hods Deprecate existing coupon codes methods
- Loading branch information
Showing
23 changed files
with
191 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Api | ||
class CouponCodesController < Spree::Api::BaseController | ||
before_action :load_order, only: :create | ||
around_action :lock_order, only: :create | ||
|
||
def create | ||
authorize! :update, @order, order_token | ||
|
||
@order.coupon_code = params[:coupon_code] | ||
@handler = PromotionHandler::Coupon.new(@order).apply | ||
|
||
if @handler.successful? | ||
render 'spree/api/promotions/handler', status: 200 | ||
else | ||
logger.error("apply_coupon_code_error=#{@handler.error.inspect}") | ||
render 'spree/api/promotions/handler', status: 422 | ||
end | ||
end | ||
|
||
private | ||
|
||
def load_order | ||
@order = Spree::Order.find_by!(number: params[:order_id]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,8 @@ | |
put :empty | ||
put :apply_coupon_code | ||
end | ||
|
||
resources :coupon_codes, only: :create | ||
end | ||
|
||
resources :zones | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
api/spec/requests/spree/api/coupon_codes_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
module Spree | ||
describe Api::CouponCodesController, type: :request do | ||
let(:current_api_user) do | ||
user = Spree.user_class.new(email: "spree@example.com") | ||
user.generate_spree_api_key! | ||
user | ||
end | ||
|
||
before do | ||
stub_authentication! | ||
end | ||
|
||
describe '#create' do | ||
let(:promo) { create(:promotion_with_item_adjustment, code: 'night_melody') } | ||
let(:promo_code) { promo.codes.first } | ||
|
||
before do | ||
allow_any_instance_of(Order).to receive_messages user: current_api_user | ||
end | ||
|
||
context 'when successful' do | ||
let(:order) { create(:order_with_line_items) } | ||
|
||
it 'applies the coupon' do | ||
post spree.api_order_coupon_codes_path(order), params: { coupon_code: promo_code.value } | ||
|
||
expect(response.status).to eq(200) | ||
expect(order.reload.promotions).to eq([promo]) | ||
expect(json_response).to eq({ | ||
"success" => I18n.t('spree.coupon_code_applied'), | ||
"error" => nil, | ||
"successful" => true, | ||
"status_code" => "coupon_code_applied" | ||
}) | ||
end | ||
end | ||
|
||
context 'when unsuccessful' do | ||
let(:order) { create(:order) } | ||
|
||
it 'returns an error' do | ||
post spree.api_order_coupon_codes_path(order), params: { coupon_code: promo_code.value } | ||
|
||
expect(response.status).to eq(422) | ||
expect(order.reload.promotions).to eq([]) | ||
expect(json_response).to eq({ | ||
"success" => nil, | ||
"error" => I18n.t('spree.coupon_code_unknown_error'), | ||
"successful" => false, | ||
"status_code" => "coupon_code_unknown_error" | ||
}) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
class CouponCodesController < Spree::StoreController | ||
before_action :load_order, only: :create | ||
around_action :lock_order, only: :create | ||
|
||
def create | ||
authorize! :update, @order, cookies.signed[:guest_token] | ||
|
||
if params[:coupon_code].present? | ||
@order.coupon_code = params[:coupon_code] | ||
handler = PromotionHandler::Coupon.new(@order).apply | ||
|
||
respond_with(@order) do |format| | ||
format.html do | ||
if handler.successful? | ||
flash[:success] = handler.success | ||
redirect_to cart_path | ||
else | ||
flash.now[:error] = handler.error | ||
render 'spree/coupon_codes/new' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def load_order | ||
@order = current_order | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<div id="coupon_code" data-hook="coupon_code"> | ||
<%= form_tag order_coupon_codes_path(@order), method: :post do %> | ||
<%= text_field_tag :coupon_code, nil, placeholder: t("spree.coupon_code"), size: 10 %> | ||
<%= submit_tag t("spree.apply_code") %> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.