forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate existing coupon codes methods #19
Closed
Closed
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
81bdd88
Deprecate current endpoints for coupon code usage
aitbw 8c5131e
Create Spree::Api::CouponCodesController
aitbw 0538c24
Add missing Spree::Deprecation expectations
aitbw ba08159
Create Spree::CouponCodesController for frontend
aitbw 30de152
Refactor response when applying coupon code for DRYness
aitbw 70b3f36
Use new API endpoint for coupon codes on promotion application spec
aitbw 4559a00
Make deprecation messages for coupon codes endpoints more descriptive
aitbw fb8454b
Add authorization system for new coupon codes endpoints
aitbw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,28 @@ | ||
# 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 | ||
@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 |
---|---|---|
|
@@ -69,6 +69,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
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
module Spree | ||
describe Api::CouponCodesController, type: :request do | ||
before do | ||
stub_authentication! | ||
end | ||
|
||
describe '#create' do | ||
let(:promo) { create(:promotion_with_item_adjustment, code: 'night_melody') } | ||
let(:promo_code) { promo.codes.first } | ||
|
||
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
class CouponCodesController < Spree::StoreController | ||
before_action :load_order, only: :create | ||
around_action :lock_order, only: :create | ||
|
||
def create | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this tests look like duplicated of what we have here, right? If yes, maybe we can do another PR to remove duplication?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted 👌