feat: Line Items API Routes (#6478)
**What** - `POST /store/carts/:id/line-items` - `POST /store/carts/:id/line-items/:id` - `DELETE /store/carts/:id/line-items/:id` **Outstanding** - Integration tests - Module integrations: Payment, Fulfillment, Promotions Depends on #6475 and #6449.
This commit is contained in:
1 parent
f5c2256286
commit
3ee0f599c1
30 files changed
+937
-208
No files matched your search
@@ -0,0 +1,84 @@
|
||||
import {
|
||||
deleteLineItemsWorkflow,
|
||||
updateLineItemInCartWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ICartModuleService } from "@medusajs/types"
|
||||
import { MedusaError, remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing"
|
||||
import { defaultStoreCartFields } from "../../../query-config"
|
||||
import { StorePostCartsCartLineItemsItemReq } from "./validators"
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const cartModuleService = req.scope.resolve<ICartModuleService>(
|
||||
ModuleRegistrationName.CART
|
||||
)
|
||||
|
||||
const cart = await cartModuleService.retrieve(req.params.id, {
|
||||
select: ["id", "region_id", "currency_code"],
|
||||
relations: ["region", "items", "items.variant_id"],
|
||||
})
|
||||
|
||||
const item = cart.items?.find((i) => i.id === req.params.line_id)
|
||||
|
||||
if (!item) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Line item with id: ${req.params.line_id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
const input = {
|
||||
cart,
|
||||
item,
|
||||
update: req.validatedBody as StorePostCartsCartLineItemsItemReq,
|
||||
}
|
||||
|
||||
const { errors } = await updateLineItemInCartWorkflow(req.scope).run({
|
||||
input,
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "cart",
|
||||
fields: defaultStoreCartFields,
|
||||
})
|
||||
|
||||
const [updatedCart] = await remoteQuery(query, {
|
||||
cart: { id: req.params.id },
|
||||
})
|
||||
|
||||
res.status(200).json({ cart: updatedCart })
|
||||
}
|
||||
|
||||
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const id = req.params.line_id
|
||||
|
||||
const { errors } = await deleteLineItemsWorkflow(req.scope).run({
|
||||
input: { ids: [id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "cart",
|
||||
fields: defaultStoreCartFields,
|
||||
})
|
||||
|
||||
const [cart] = await remoteQuery(query, {
|
||||
cart: { id: req.params.id },
|
||||
})
|
||||
|
||||
res.status(200).json({ cart })
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { IsInt, IsOptional } from "class-validator"
|
||||
|
||||
export class StorePostCartsCartLineItemsItemReq {
|
||||
@IsInt()
|
||||
quantity: number
|
||||
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown> | undefined
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import { StorePostCartsCartLineItemsItemReq } from "./[id]/line-items/[line_id]/validators"
|
||||
import { StorePostCartsCartLineItemsReq } from "./[id]/line-items/validators"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
@@ -48,6 +49,14 @@ export const storeCartRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/store/carts/:id/line-items/:line_id",
|
||||
middlewares: [transformBody(StorePostCartsCartLineItemsItemReq)],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/store/carts/:id/line-items/:line_id",
|
||||
},
|
||||
{
|
||||
matcher: "/store/carts/:id/promotions",
|
||||
middlewares: [transformBody(StorePostCartsCartPromotionsReq)],
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user