Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Rename Orders::Promotions to Orders::CouponCodes

Damian Legawiec3ba83aecdb7c
diff --git a/api/app/controllers/spree/api/v3/store/orders/coupon_codes_controller.rb b/api/app/controllers/spree/api/v3/store/orders/coupon_codes_controller.rb
new file mode 100644
index 0000000000..8928be90bc
--- /dev/null
+++ b/api/app/controllers/spree/api/v3/store/orders/coupon_codes_controller.rb
@@ -0,0 +1,71 @@
+module Spree
+ module Api
+ module V3
+ module Store
+ module Orders
+ class CouponCodesController < ResourceController
+ include Spree::Api::V3::OrderConcern
+
+ before_action :authorize_order_access!
+ skip_before_action :set_resource, only: [:create]
+
+ # POST /api/v3/store/orders/:order_id/coupon_codes
+ # Apply a coupon code to the order
+ def create
+ @parent.coupon_code = permitted_params[:coupon_code]
+
+ coupon_handler.apply
+
+ if coupon_handler.successful?
+ render json: order_serializer.new(@parent.reload, params: serializer_params).to_h, status: :created
+ else
+ render_errors(coupon_handler.error)
+ end
+ end
+
+ # DELETE /api/v3/store/orders/:order_id/coupon_codes/:id
+ # Remove a coupon code from the order
+ # :id is the promotion prefix_id (e.g., promo_xxx)
+ def destroy
+ coupon_code = @resource.code.presence || @resource.name
+
+ coupon_handler.remove(coupon_code)
+
+ if coupon_handler.successful?
+ render json: order_serializer.new(@parent.reload, params: serializer_params).to_h
+ else
+ render_errors(coupon_handler.error)
+ end
+ end
+
+ protected
+
+ def parent_association
+ :order_promotions
+ end
+
+ def coupon_handler
+ @coupon_handler ||= Spree.coupon_handler.new(@parent)
+ end
+
+ def permitted_params
+ params.require(:coupon_code).permit(:coupon_code)
+ end
+
+ def model_class
+ Spree::OrderPromotion
+ end
+
+ def serializer_class
+ Spree.api.order_promotion_serializer
+ end
+
+ def order_serializer
+ Spree.api.order_serializer.constantize
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/controllers/spree/api/v3/store/orders/promotions_controller.rb b/api/app/controllers/spree/api/v3/store/orders/promotions_controller.rb
deleted file mode 100644
index 064f241534..0000000000
--- a/api/app/controllers/spree/api/v3/store/orders/promotions_controller.rb
+++ /dev/null
@@ -1,63 +0,0 @@
-module Spree
- module Api
- module V3
- module Store
- module Orders
- class PromotionsController < ResourceController
- include Spree::Api::V3::OrderConcern
-
- before_action :authorize_order_access!
-
- # POST /api/v3/store/orders/:order_id/promotions
- def create
- @parent.coupon_code = permitted_params[:coupon_code]
-
- promotion_handler.apply
-
- if promotion_handler.successful?
- head :no_content, status: :created
- else
- render_errors(promotion_handler.error)
- end
- end
-
- # DELETE /api/v3/store/orders/:order_id/promotions/:id
- def destroy
- coupon_code = @resource.code.presence || @resource.name
-
- promotion_handler.remove(coupon_code)
-
- if promotion_handler.successful?
- head :no_content
- else
- render_errors(promotion_handler.error)
- end
- end
-
- protected
-
- def parent_association
- :order_promotions
- end
-
- def promotion_handler
- @promotion_handler ||= Spree.coupon_handler.new(@parent)
- end
-
- def permitted_params
- params.require(:promotion).permit(:coupon_code)
- end
-
- def model_class
- Spree::OrderPromotion
- end
-
- def serializer_class
- Spree.api.order_promotion_serializer
- end
- end
- end
- end
- end
- end
-end
diff --git a/api/config/routes.rb b/api/config/routes.rb
index fab2d42799..ea26f6ebc9 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -40,7 +40,7 @@ Spree::Core::Engine.add_routes do
# Nested resources - all require order access
resource :store_credits, only: [:create, :destroy], controller: 'orders/store_credits'
resources :line_items, only: [:create, :update, :destroy], controller: 'orders/line_items'
- resources :promotions, only: [:create, :destroy], controller: 'orders/promotions'
+ resources :coupon_codes, only: [:create, :destroy], controller: 'orders/coupon_codes'
resources :payments, only: [:index, :show], controller: 'orders/payments'
resources :payment_methods, only: [:index], controller: 'orders/payment_methods'
resources :shipments, only: [:index, :show, :update], controller: 'orders/shipments'
diff --git a/sdk/src/client.ts b/sdk/src/client.ts
index edb5f6a379..ed658ca71b 100644
--- a/sdk/src/client.ts
+++ b/sdk/src/client.ts
@@ -23,6 +23,7 @@ import type {
StoreTaxon,
StorePayment,
StorePaymentMethod,
+ StoreShipment,
StoreStore,
StoreWishlist,
StoreWishedItem,
@@ -532,6 +533,72 @@ export class SpreeClient {
options
),
},
+
+ /**
+ * Nested resource: Coupon codes
+ */
+ couponCodes: {
+ /**
+ * Apply a coupon code to an order
+ */
+ apply: (
+ orderId: string,
+ couponCode: string,
+ options?: RequestOptions
+ ): Promise<StoreOrder> =>
+ this.request<StoreOrder>('POST', `/orders/${orderId}/coupon_codes`, {
+ ...options,
+ body: { coupon_code: { coupon_code: couponCode } },
+ }),
+
+ /**
+ * Remove a coupon code from an order
+ * @param promotionId - The promotion prefix_id (e.g., 'promo_xxx')
+ */
+ remove: (
+ orderId: string,
+ promotionId: string,
+ options?: RequestOptions
+ ): Promise<StoreOrder> =>
+ this.request<StoreOrder>(
+ 'DELETE',
+ `/orders/${orderId}/coupon_codes/${promotionId}`,
+ options
+ ),
+ },
+
+ /**
+ * Nested resource: Shipments
+ */
+ shipments: {
+ /**
+ * List shipments for an order
+ */
+ list: (
+ orderId: string,
+ options?: RequestOptions
+ ): Promise<{ data: StoreShipment[] }> =>
+ this.request<{ data: StoreShipment[] }>(
+ 'GET',
+ `/orders/${orderId}/shipments`,
+ options
+ ),
+
+ /**
+ * Update a shipment (e.g., select shipping rate)
+ */
+ update: (
+ orderId: string,
+ shipmentId: string,
+ params: { selected_shipping_rate_id: string },
+ options?: RequestOptions
+ ): Promise<StoreShipment> =>
+ this.request<StoreShipment>(
+ 'PATCH',
+ `/orders/${orderId}/shipments/${shipmentId}`,
+ { ...options, body: { shipment: params } }
+ ),
+ },
};
// ============================================