feat(core-flows,framework,medusa): list shipping options pass in cart as pricing context (#10374)
* feat(core-flows,framework,medusa): list shipping options pass in cart as pricing context * chore: add test for shipping options returning free shipping
This commit is contained in:
1 parent
eacd691951
commit
11bd556133
12 files changed
+284
-156
No files matched your search
@@ -86,7 +86,7 @@ export const AdminCreateShippingOptionTypeObject = z
|
||||
|
||||
const AdminPriceRules = z.array(
|
||||
z.object({
|
||||
attribute: z.literal("cart_total"),
|
||||
attribute: z.literal("total"),
|
||||
operator: z.nativeEnum(PricingRuleOperator),
|
||||
value: z.number(),
|
||||
})
|
||||
|
||||
@@ -1,52 +1,17 @@
|
||||
import { listShippingOptionsForCartWorkflow } from "@medusajs/core-flows"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { HttpTypes, ICartModuleService } from "@medusajs/framework/types"
|
||||
import { MedusaError, Modules } from "@medusajs/framework/utils"
|
||||
import { StoreGetShippingOptionsType } from "./validators"
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
|
||||
export const GET = async (
|
||||
req: MedusaRequest<HttpTypes.StoreGetShippingOptionList>,
|
||||
req: MedusaRequest<{}, HttpTypes.StoreGetShippingOptionList>,
|
||||
res: MedusaResponse<HttpTypes.StoreShippingOptionListResponse>
|
||||
) => {
|
||||
const { cart_id, is_return } =
|
||||
req.filterableFields as StoreGetShippingOptionsType
|
||||
const { cart_id, is_return } = req.filterableFields
|
||||
|
||||
if (!cart_id) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
"You must provide the cart_id to list shipping options"
|
||||
)
|
||||
}
|
||||
|
||||
const cartService = req.scope.resolve<ICartModuleService>(Modules.CART)
|
||||
|
||||
const cart = await cartService.retrieveCart(cart_id, {
|
||||
select: [
|
||||
"id",
|
||||
"sales_channel_id",
|
||||
"currency_code",
|
||||
"region_id",
|
||||
"shipping_address.city",
|
||||
"shipping_address.country_code",
|
||||
"shipping_address.province",
|
||||
],
|
||||
relations: ["shipping_address"],
|
||||
const workflow = listShippingOptionsForCartWorkflow(req.scope)
|
||||
const { result: shipping_options } = await workflow.run({
|
||||
input: { cart_id, is_return: !!is_return },
|
||||
})
|
||||
|
||||
const { result } = await listShippingOptionsForCartWorkflow(req.scope).run({
|
||||
input: {
|
||||
cart_id: cart.id,
|
||||
sales_channel_id: cart.sales_channel_id,
|
||||
currency_code: cart.currency_code,
|
||||
region_id: cart.region_id,
|
||||
is_return: !!is_return,
|
||||
shipping_address: {
|
||||
city: cart.shipping_address?.city,
|
||||
country_code: cart.shipping_address?.country_code,
|
||||
province: cart.shipping_address?.province,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
res.json({ shipping_options: result })
|
||||
res.json({ shipping_options })
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
import { z } from "zod"
|
||||
import { createFindParams } from "../../utils/validators"
|
||||
import { applyAndAndOrOperators } from "../../utils/common-validators"
|
||||
import { createFindParams } from "../../utils/validators"
|
||||
|
||||
export const StoreGetShippingOptionsFields = z.object({
|
||||
cart_id: z.string(),
|
||||
is_return: z.boolean().optional(),
|
||||
})
|
||||
export const StoreGetShippingOptionsFields = z
|
||||
.object({
|
||||
cart_id: z.string(),
|
||||
is_return: z.boolean().optional(),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type StoreGetShippingOptionsType = z.infer<
|
||||
typeof StoreGetShippingOptions
|
||||
|
||||
Reference in new issue
Block a user