feat(medusa,core-flows): complete cart [part-1] (#7201)
what: - adds a very basic complete cart endpoint that creates an order - the complete cart workflow currently does the following: - create tax lines - create order
This commit is contained in:
1 parent
e78362c000
commit
f129415650
19 files changed
+554
-34
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { completeCartWorkflow } from "@medusajs/core-flows"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
|
||||
import { refetchOrder } from "../../../orders/helpers"
|
||||
import { StoreCompleteCartType } from "../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: MedusaRequest<StoreCompleteCartType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { idempotency_key: idempotencyKey } = req.validatedBody
|
||||
|
||||
// If the idempotencyKey is present:
|
||||
// - is workflow is running?
|
||||
// = throw error
|
||||
// - else
|
||||
// - re-run the workflow at the failed step
|
||||
|
||||
const { errors, result } = await completeCartWorkflow(req.scope).run({
|
||||
input: { id: req.params.id },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const order = await refetchOrder(
|
||||
result.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ order })
|
||||
}
|
||||
@@ -2,12 +2,15 @@ import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||
import * as OrderQueryConfig from "../orders/query-config"
|
||||
import { StoreGetOrder } from "../orders/validators"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
StoreAddCartLineItem,
|
||||
StoreAddCartPromotions,
|
||||
StoreAddCartShippingMethods,
|
||||
StoreCalculateCartTaxes,
|
||||
StoreCompleteCart,
|
||||
StoreCreateCart,
|
||||
StoreGetCartsCart,
|
||||
StoreRemoveCartPromotions,
|
||||
@@ -144,4 +147,15 @@ export const storeCartRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/store/carts/:id/complete",
|
||||
middlewares: [
|
||||
validateAndTransformBody(StoreCompleteCart),
|
||||
validateAndTransformQuery(
|
||||
StoreGetOrder,
|
||||
OrderQueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -82,3 +82,10 @@ export const StoreAddCartShippingMethods = z
|
||||
data: z.record(z.unknown()).optional(),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export const StoreCompleteCart = z
|
||||
.object({
|
||||
idempotency_key: z.string().optional(),
|
||||
})
|
||||
.strict()
|
||||
export type StoreCompleteCartType = z.infer<typeof StoreCompleteCart>
|
||||
@@ -0,0 +1,10 @@
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import { refetchEntity } from "../../utils/refetch-entity"
|
||||
|
||||
export const refetchOrder = async (
|
||||
idOrFilter: string | object,
|
||||
scope: MedusaContainer,
|
||||
fields: string[]
|
||||
) => {
|
||||
return await refetchEntity("order", idOrFilter, scope, fields)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// TODO: This is copied over from admin. Scope what fields and relations are allowed for store
|
||||
|
||||
export const defaultStoreOrderFields = [
|
||||
"id",
|
||||
"status",
|
||||
"version",
|
||||
"summary",
|
||||
"metadata",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
]
|
||||
|
||||
export const defaultStoreRetrieveOrderFields = [
|
||||
"id",
|
||||
"status",
|
||||
"version",
|
||||
"summary",
|
||||
"total",
|
||||
"subtotal",
|
||||
"tax_total",
|
||||
"discount_total",
|
||||
"discount_tax_total",
|
||||
"original_total",
|
||||
"original_tax_total",
|
||||
"item_total",
|
||||
"item_subtotal",
|
||||
"item_tax_total",
|
||||
"original_item_total",
|
||||
"original_item_subtotal",
|
||||
"original_item_tax_total",
|
||||
"shipping_total",
|
||||
"shipping_subtotal",
|
||||
"shipping_tax_total",
|
||||
"original_shipping_tax_total",
|
||||
"original_shipping_tax_subtotal",
|
||||
"original_shipping_total",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"*items",
|
||||
"*items.tax_lines",
|
||||
"*items.adjustments",
|
||||
"*items.detail",
|
||||
"*items.tax_lines",
|
||||
"*items.adjustments",
|
||||
"*shipping_address",
|
||||
"*billing_address",
|
||||
"*shipping_methods",
|
||||
"*shipping_methods.tax_lines",
|
||||
"*shipping_methods.adjustments",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultStoreRetrieveOrderFields,
|
||||
isList: false,
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { z } from "zod"
|
||||
import { createSelectParams } from "../../utils/validators"
|
||||
|
||||
export const StoreGetOrder = createSelectParams()
|
||||
export type StoreGetOrderType = z.infer<typeof StoreGetOrder>
|
||||
Reference in new issue
Block a user