feat: Create cart with line items (#6449)

**What**
- Add support for creating a cart with items
- Add endpoint `POST /store/carts/:id/line-items`
- Add `CreateCartWorkflow`
- Add `AddToCartWorkflow`
- Add steps for both workflows

**Testing**
- Endpoints
- Workflows

I would still call this a first iteration, as we are missing a few pieces of the full flow, such as payment sessions, discounts, and taxes.

Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
Oli Juhl
2024-02-26 13:32:16 +00:00
committed by GitHub
co-authored by Adrien de Peretti
parent ac86362e81
commit 7ebe885ec9
26 changed files with 1100 additions and 157 deletions
@@ -0,0 +1,44 @@
import { addToCartWorkflow } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICartModuleService } from "@medusajs/types"
import { remoteQueryObjectFromString } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
import { defaultStoreCartFields } from "../../query-config"
import { StorePostCartsCartLineItemsReq } 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"],
})
const workflowInput = {
items: [req.validatedBody as StorePostCartsCartLineItemsReq],
cart,
}
const { errors } = await addToCartWorkflow(req.scope).run({
input: workflowInput,
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 })
}
@@ -0,0 +1,12 @@
import { IsInt, IsOptional, IsString } from "class-validator"
export class StorePostCartsCartLineItemsReq {
@IsString()
variant_id: string
@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 { StorePostCartsCartLineItemsReq } from "./[id]/line-items/validators"
import * as QueryConfig from "./query-config"
import {
StoreDeleteCartsCartPromotionsReq,
@@ -40,6 +41,11 @@ export const storeCartRoutesMiddlewares: MiddlewareRoute[] = [
matcher: "/store/carts/:id",
middlewares: [transformBody(StorePostCartsCartReq)],
},
{
method: ["POST"],
matcher: "/store/carts/:id/line-items",
middlewares: [transformBody(StorePostCartsCartLineItemsReq)],
},
{
method: ["POST"],
matcher: "/store/carts/:id/promotions",
@@ -3,13 +3,9 @@ import { CreateCartWorkflowInputDTO } from "@medusajs/types"
import { remoteQueryObjectFromString } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
import { defaultStoreCartFields } from "../carts/query-config"
import { StorePostCartReq } from "./validators"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
const input = req.validatedBody as StorePostCartReq
const workflowInput: CreateCartWorkflowInputDTO = {
...input,
}
const workflowInput = req.validatedBody as CreateCartWorkflowInputDTO
// If the customer is logged in, we auto-assign them to the cart
if (req.auth_user?.app_metadata?.customer_id) {