@@ -1,4 +1,7 @@
|
||||
import { updateCartsWorkflow } from "@medusajs/core-flows"
|
||||
import { UpdateCartDataDTO } from "@medusajs/types"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
|
||||
import { defaultStoreCartRemoteQueryObject } from "../query-config"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
@@ -17,3 +20,23 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
|
||||
res.json({ cart })
|
||||
}
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const updateCartWorkflow = updateCartsWorkflow(req.scope)
|
||||
|
||||
const workflowInput = {
|
||||
selector: { id: req.params.id },
|
||||
update: req.validatedBody as UpdateCartDataDTO,
|
||||
}
|
||||
|
||||
const { result, errors } = await updateCartWorkflow.run({
|
||||
input: workflowInput,
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ cart: result[0] })
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import { StoreGetCartsCartParams, StorePostCartReq } from "./validators"
|
||||
import {
|
||||
StoreGetCartsCartParams,
|
||||
StorePostCartReq,
|
||||
StorePostCartsCartReq,
|
||||
} from "./validators"
|
||||
|
||||
export const storeCartRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
@@ -19,4 +23,9 @@ export const storeCartRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
matcher: "/store/carts",
|
||||
middlewares: [transformBody(StorePostCartReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/store/carts/:id",
|
||||
middlewares: [transformBody(StorePostCartsCartReq)],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Type } from "class-transformer"
|
||||
import {
|
||||
IsArray,
|
||||
IsEmail,
|
||||
IsInt,
|
||||
IsNotEmpty,
|
||||
IsObject,
|
||||
@@ -8,7 +9,8 @@ import {
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { FindParams } from "../../../types/common"
|
||||
import { AddressPayload, FindParams } from "../../../types/common"
|
||||
import { IsType } from "../../../utils"
|
||||
|
||||
export class StoreGetCartsCartParams extends FindParams {}
|
||||
|
||||
@@ -35,6 +37,7 @@ export class StorePostCartReq {
|
||||
@IsString()
|
||||
email?: string
|
||||
|
||||
// TODO: Remove in favor of using region currencies, as in the core
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
currency_code?: string
|
||||
@@ -53,3 +56,45 @@ export class StorePostCartReq {
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export class StorePostCartsCartReq {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
region_id?: string
|
||||
|
||||
@IsEmail()
|
||||
@IsOptional()
|
||||
email?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsType([AddressPayload, String])
|
||||
billing_address?: AddressPayload | string
|
||||
|
||||
@IsOptional()
|
||||
@IsType([AddressPayload, String])
|
||||
shipping_address?: AddressPayload | string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
customer_id?: string
|
||||
|
||||
@IsEmail()
|
||||
@IsOptional()
|
||||
sales_channel_id?: string
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
|
||||
// @IsOptional()
|
||||
// @IsArray()
|
||||
// @ValidateNested({ each: true })
|
||||
// @Type(() => GiftCard)
|
||||
// gift_cards?: GiftCard[]
|
||||
|
||||
// @IsOptional()
|
||||
// @IsArray()
|
||||
// @ValidateNested({ each: true })
|
||||
// @Type(() => Discount)
|
||||
// discounts?: Discount[]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user