Skip to content
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 #2958

Merged
1 change: 1 addition & 0 deletions api/app/controllers/spree/api/checkouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def state_callback(before_or_after = :before)

def after_update_attributes
if params[:order] && params[:order][:coupon_code].present?
Spree::Deprecation.warn('This method is deprecated. Please, use `Spree::Api::CouponCodesController` endpoints instead.')
aitbw marked this conversation as resolved.
Show resolved Hide resolved
handler = PromotionHandler::Coupon.new(@order)
handler.apply

Expand Down
28 changes: 28 additions & 0 deletions api/app/controllers/spree/api/coupon_codes_controller.rb
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
2 changes: 2 additions & 0 deletions api/app/controllers/spree/api/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def mine
end

def apply_coupon_code
Spree::Deprecation.warn('This endpoint is deprecated. Please, use `Spree::Api::CouponCodesController` endpoints instead.')
aitbw marked this conversation as resolved.
Show resolved Hide resolved

authorize! :update, @order, order_token
@order.coupon_code = params[:coupon_code]
@handler = PromotionHandler::Coupon.new(@order).apply
Expand Down
2 changes: 2 additions & 0 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
put :empty
put :apply_coupon_code
end

resources :coupon_codes, only: :create
end

resources :zones
Expand Down
2 changes: 1 addition & 1 deletion api/spec/features/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def create_line_item(variant, quantity = 1)

def add_promotion(_promotion)
expect {
put "/api/orders/#{@order.number}/apply_coupon_code",
post "/api/orders/#{@order.number}/coupon_codes",
params: { coupon_code: promotion_code.value }
}.to change { @order.promotions.count }.by 1
expect(response).to have_http_status(:ok)
Expand Down
2 changes: 2 additions & 0 deletions api/spec/requests/spree/api/checkouts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ module Spree
end

it "can apply a coupon code to an order" do
expect(Spree::Deprecation).to receive(:warn)
order.update_column(:state, "payment")
expect(PromotionHandler::Coupon).to receive(:new).with(order).and_call_original
expect_any_instance_of(PromotionHandler::Coupon).to receive(:apply).and_return({ coupon_applied?: true })
Expand All @@ -365,6 +366,7 @@ module Spree
end

it "renders error failing to apply coupon" do
expect(Spree::Deprecation).to receive(:warn)
order.update_column(:state, "payment")
put spree.api_checkout_path(order.to_param), params: { order_token: order.guest_token, order: { coupon_code: "foobar" } }
expect(response.status).to eq(422)
Expand Down
50 changes: 50 additions & 0 deletions api/spec/requests/spree/api/coupon_codes_controller_spec.rb
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
4 changes: 4 additions & 0 deletions api/spec/requests/spree/api/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,8 @@ module Spree
let(:order) { create(:order_with_line_items) }

it 'applies the coupon' do
expect(Spree::Deprecation).to receive(:warn)

put spree.apply_coupon_code_api_order_path(order), params: { coupon_code: promo_code.value }

expect(response.status).to eq 200
Expand All @@ -871,6 +873,8 @@ module Spree
let(:order) { create(:order) } # no line items to apply the code to

it 'returns an error' do
expect(Spree::Deprecation).to receive(:warn)

put spree.apply_coupon_code_api_order_path(order), params: { coupon_code: promo_code.value }

expect(response.status).to eq 422
Expand Down
4 changes: 2 additions & 2 deletions api/spec/requests/spree/api/promotion_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Spree::Api

it "can apply a coupon code to the order" do
expect(order.total).to eq(110.00)
put spree.apply_coupon_code_api_order_path(order), params: { coupon_code: "10off", order_token: order.guest_token }
post spree.api_order_coupon_codes_path(order), params: { coupon_code: "10off", order_token: order.guest_token }
expect(response.status).to eq(200)
expect(order.reload.total).to eq(109.00)
expect(json_response["success"]).to eq("The coupon code was successfully applied to your order.")
Expand All @@ -37,7 +37,7 @@ module Spree::Api
end

it "fails to apply" do
put spree.apply_coupon_code_api_order_path(order), params: { coupon_code: "10off", order_token: order.guest_token }
post spree.api_order_coupon_codes_path(order), params: { coupon_code: "10off", order_token: order.guest_token }
expect(response.status).to eq(422)
expect(json_response["success"]).to be_blank
expect(json_response["error"]).to eq("The coupon code is expired")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Spree.ready(function() {
}

Spree.ajax({
type: 'PUT',
type: 'POST',
url: Spree.routes.apply_coupon_code(window.order_number),
data: {
coupon_code: $("#coupon_code").val(),
Expand Down
2 changes: 1 addition & 1 deletion core/app/assets/javascripts/spree.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Spree.ajax = function(url, options) {
Spree.routes = {
states_search: Spree.pathFor('api/states'),
apply_coupon_code: function(order_id) {
return Spree.pathFor("api/orders/" + order_id + "/apply_coupon_code");
return Spree.pathFor("api/orders/" + order_id + "/coupon_codes");
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Spree.onCouponCodeApply = function(e) {
coupon_code: couponCode
};
var req = Spree.ajax({
method: "PUT",
method: 'POST',
url: Spree.routes.apply_coupon_code(Spree.current_order_id),
data: JSON.stringify(data),
contentType: "application/json"
Expand Down
1 change: 1 addition & 0 deletions frontend/app/controllers/spree/checkout_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def completion_route

def apply_coupon_code
if update_params[:coupon_code].present?
Spree::Deprecation.warn('This endpoint is deprecated. Please, use `Spree::CouponCodesController` endpoints instead.')
aitbw marked this conversation as resolved.
Show resolved Hide resolved
@order.coupon_code = update_params[:coupon_code]

handler = PromotionHandler::Coupon.new(@order).apply
Expand Down
33 changes: 33 additions & 0 deletions frontend/app/controllers/spree/coupon_codes_controller.rb
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
1 change: 1 addition & 0 deletions frontend/app/controllers/spree/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def assign_order

def apply_coupon_code
if order_params[:coupon_code].present?
Spree::Deprecation.warn('This endpoint is deprecated. Please, use `Spree::CouponCodesController` endpoints instead.')
@order.coupon_code = order_params[:coupon_code]

handler = PromotionHandler::Coupon.new(@order).apply
Expand Down
6 changes: 6 additions & 0 deletions frontend/app/views/spree/coupon_codes/new.html.erb
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>
11 changes: 5 additions & 6 deletions frontend/app/views/spree/orders/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@
<div data-hook="inside_cart_form">

<div data-hook="cart_items">
<%= render 'form', order_form: order_form %>
<%= render 'spree/orders/form', order_form: order_form %>
</div>

<div class="links columns sixteen alpha omega" data-hook="cart_buttons">
<%= order_form.text_field :coupon_code, size: 10, placeholder: t('spree.coupon_code') %>
<%= button_tag class: 'primary', id: 'update-button' do %>
<%= t('spree.update') %>
<% end %>
<%= button_tag t("spree.update"), class: "primary", id: "update-button" %>

<%= button_tag class: 'button checkout primary', id: 'checkout-link', name: 'checkout' do %>
<%= t('spree.checkout') %>
<% end %>
</div>

</div>
<% end %>
</div>
Expand All @@ -41,6 +38,8 @@
<%= link_to t('spree.continue_shopping'), products_path, class: 'continue button gray' %>
</p>
<% end %>

<%= render template: 'spree/coupon_codes/new' %>
</div>

<% end %>
Expand Down
1 change: 1 addition & 0 deletions frontend/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

resources :orders, except: [:index, :new, :create, :destroy] do
post :populate, on: :collection
resources :coupon_codes, only: :create
end

get '/cart', to: 'orders#edit', as: :cart
Expand Down
6 changes: 6 additions & 0 deletions frontend/spec/controllers/spree/checkout_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ def post_address
let(:promotion_handler) { instance_double('Spree::PromotionHandler::Coupon', error: nil, success: 'Coupon Applied!') }

it "continues checkout flow normally" do
expect(Spree::Deprecation).to receive(:warn)

expect(Spree::PromotionHandler::Coupon)
.to receive_message_chain(:new, :apply)
.and_return(promotion_handler)
Expand All @@ -515,6 +517,8 @@ def post_address
let(:promotion_handler) { instance_double('Spree::PromotionHandler::Coupon', error: 'Some error', success: false) }

it "setups the current step correctly before rendering" do
expect(Spree::Deprecation).to receive(:warn)

expect(Spree::PromotionHandler::Coupon)
.to receive_message_chain(:new, :apply)
.and_return(promotion_handler)
Expand All @@ -524,6 +528,8 @@ def post_address
end

it "render cart with coupon error" do
expect(Spree::Deprecation).to receive(:warn)

expect(Spree::PromotionHandler::Coupon)
.to receive_message_chain(:new, :apply)
.and_return(promotion_handler)
Expand Down
4 changes: 4 additions & 0 deletions frontend/spec/controllers/spree/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
let(:promotion_handler) { instance_double('Spree::PromotionHandler::Coupon', error: nil, success: 'Coupon Applied!') }

it "continues checkout flow normally" do
expect(Spree::Deprecation).to receive(:warn)

expect(Spree::PromotionHandler::Coupon)
.to receive_message_chain(:new, :apply)
.and_return(promotion_handler)
Expand All @@ -163,6 +165,8 @@
let(:promotion_handler) { instance_double('Spree::PromotionHandler::Coupon', error: 'Some error', success: false) }

it "render cart with coupon error" do
expect(Spree::Deprecation).to receive(:warn)

expect(Spree::PromotionHandler::Coupon)
.to receive_message_chain(:new, :apply)
.and_return(promotion_handler)
Expand Down
Loading