feat(workflows-sdk,core-flows,medusa,types): add workflow to update promotions to cart (#6474)
what: - adds API + workflow to add/remove promotions in a cart - minor fixes in promotions module - minor type fixes in cart module - typing fix in workflows-sdk (Thanks @adrien2p) - fix step result in workflows-sdk (Thanks @adrien2p) RESOLVES CORE-1768 Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
co-authored by
Adrien de Peretti
parent
63be07031b
commit
ac86362e81
@@ -0,0 +1,42 @@
|
||||
import { updateCartPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
|
||||
import { StorePostCartsCartPromotionsReq } from "../../validators"
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const workflow = updateCartPromotionsWorkflow(req.scope)
|
||||
const payload = req.validatedBody as StorePostCartsCartPromotionsReq
|
||||
|
||||
const { result, errors } = await workflow.run({
|
||||
input: {
|
||||
promoCodes: payload.promo_codes,
|
||||
cartId: req.params.id,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ cart: result })
|
||||
}
|
||||
|
||||
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const workflow = updateCartPromotionsWorkflow(req.scope)
|
||||
const payload = req.validatedBody as StorePostCartsCartPromotionsReq
|
||||
|
||||
const { result, errors } = await workflow.run({
|
||||
input: {
|
||||
promoCodes: payload.promo_codes,
|
||||
cartId: req.params.id,
|
||||
removePromotions: true,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ cart: result })
|
||||
}
|
||||
@@ -3,8 +3,10 @@ import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
StoreDeleteCartsCartPromotionsReq,
|
||||
StoreGetCartsCartParams,
|
||||
StorePostCartReq,
|
||||
StorePostCartsCartPromotionsReq,
|
||||
StorePostCartsCartReq,
|
||||
} from "./validators"
|
||||
|
||||
@@ -38,4 +40,14 @@ export const storeCartRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
matcher: "/store/carts/:id",
|
||||
middlewares: [transformBody(StorePostCartsCartReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/store/carts/:id/promotions",
|
||||
middlewares: [transformBody(StorePostCartsCartPromotionsReq)],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/store/carts/:id/promotions",
|
||||
middlewares: [transformBody(StoreDeleteCartsCartPromotionsReq)],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -52,6 +52,18 @@ export class StorePostCartReq {
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export class StorePostCartsCartPromotionsReq {
|
||||
@IsArray()
|
||||
@Type(() => String)
|
||||
promo_codes: string[]
|
||||
}
|
||||
|
||||
export class StoreDeleteCartsCartPromotionsReq {
|
||||
@IsArray()
|
||||
@Type(() => String)
|
||||
promo_codes: string[]
|
||||
}
|
||||
|
||||
export class StorePostCartsCartReq {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
|
||||
Reference in New Issue
Block a user