feat(core-flows, medusa): add shipping methods to cart API (#7150)

* feat(core-flows, medusa): add shipping methods to cart API

* chore: change id to option_id

* chore: use list listShippingOptionsForContext instead of validateShippingOption

* chore: remove comment

* chore: add refresh shipping methods step

* chore: set cart step

* chore: update all workflows to refresh shipping methods

* chore: add tests + cleanup
This commit is contained in:
Riqwan Thamir authored and GitHub committed 2024-04-29 09:25:23 +02:00
1 parent 4b57c5d286
commit d2393f004e
19 files changed
+688 -80

No files matched your search

@@ -0,0 +1,32 @@
import { addShippingMethodToWorkflow } from "@medusajs/core-flows"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
import { refetchCart } from "../../helpers"
import { StoreAddCartShippingMethodsType } from "../../validators"
export const POST = async (
req: MedusaRequest<StoreAddCartShippingMethodsType>,
res: MedusaResponse
) => {
const workflow = addShippingMethodToWorkflow(req.scope)
const payload = req.validatedBody
const { errors } = await workflow.run({
input: {
options: [{ id: payload.option_id, data: payload.data }],
cart_id: req.params.id,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const cart = await refetchCart(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ cart })
}
@@ -6,6 +6,7 @@ import * as QueryConfig from "./query-config"
import {
StoreAddCartLineItem,
StoreAddCartPromotions,
StoreAddCartShippingMethods,
StoreCalculateCartTaxes,
StoreCreateCart,
StoreGetCartsCart,
@@ -121,6 +122,17 @@ export const storeCartRoutesMiddlewares: MiddlewareRoute[] = [
),
],
},
{
method: ["POST"],
matcher: "/store/carts/:id/shipping-methods",
middlewares: [
validateAndTransformBody(StoreAddCartShippingMethods),
validateAndTransformQuery(
StoreGetCartsCart,
QueryConfig.retrieveTransformQueryConfig
),
],
},
{
method: ["DELETE"],
matcher: "/store/carts/:id/promotions",
@@ -72,3 +72,13 @@ export const StoreUpdateCartLineItem = z.object({
quantity: z.number(),
metadata: z.record(z.unknown()).optional(),
})
export type StoreAddCartShippingMethodsType = z.infer<
typeof StoreAddCartShippingMethods
>
export const StoreAddCartShippingMethods = z
.object({
option_id: z.string(),
data: z.record(z.unknown()).optional(),
})
.strict()