feat(core-flows,medusa,types,utils): adds update cart API with promotions (#6514)
what: - adds update cart API - workflow - promotions - sales channel - region - customer
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { updateCartPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { PromotionActions } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
|
||||
import { StorePostCartsCartPromotionsReq } from "../../validators"
|
||||
|
||||
@@ -10,6 +11,7 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
input: {
|
||||
promoCodes: payload.promo_codes,
|
||||
cartId: req.params.id,
|
||||
action: PromotionActions.ADD,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
@@ -29,7 +31,7 @@ export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
input: {
|
||||
promoCodes: payload.promo_codes,
|
||||
cartId: req.params.id,
|
||||
removePromotions: true,
|
||||
action: PromotionActions.REMOVE,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
|
||||
import { updateCartWorkflow } from "@medusajs/core-flows"
|
||||
import { UpdateCartDataDTO } from "@medusajs/types"
|
||||
import { defaultStoreCartFields } from "../query-config"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { updateCartsWorkflow } from "@medusajs/core-flows"
|
||||
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
import { defaultStoreCartFields } from "../query-config"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
@@ -24,15 +24,13 @@ export const POST = async (
|
||||
req: MedusaRequest<UpdateCartDataDTO>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const updateCartWorkflow = updateCartsWorkflow(req.scope)
|
||||
const workflow = updateCartWorkflow(req.scope)
|
||||
|
||||
const workflowInput = {
|
||||
selector: { id: req.params.id },
|
||||
update: req.validatedBody,
|
||||
}
|
||||
|
||||
const { result, errors } = await updateCartWorkflow.run({
|
||||
input: workflowInput,
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
...(req.validatedBody as UpdateCartDataDTO),
|
||||
id: req.params.id,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
@@ -40,5 +38,16 @@ export const POST = async (
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ cart: result[0] })
|
||||
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 })
|
||||
}
|
||||
|
||||
@@ -10,8 +10,14 @@ export const defaultStoreCartFields = [
|
||||
"items.title",
|
||||
"items.quantity",
|
||||
"items.unit_price",
|
||||
"items.adjustments.id",
|
||||
"items.adjustments.code",
|
||||
"items.adjustments.amount",
|
||||
"customer.id",
|
||||
"customer.email",
|
||||
"shipping_methods.adjustments.id",
|
||||
"shipping_methods.adjustments.code",
|
||||
"shipping_methods.adjustments.amount",
|
||||
"shipping_address.id",
|
||||
"shipping_address.first_name",
|
||||
"shipping_address.last_name",
|
||||
@@ -40,20 +46,24 @@ export const defaultStoreCartFields = [
|
||||
|
||||
export const defaultStoreCartRelations = [
|
||||
"items",
|
||||
"items.adjustments",
|
||||
"region",
|
||||
"customer",
|
||||
"shipping_address",
|
||||
"billing_address",
|
||||
"shipping_methods",
|
||||
"shipping_methods.adjustments",
|
||||
]
|
||||
|
||||
export const allowedRelations = [
|
||||
"items",
|
||||
"items.adjustments",
|
||||
"region",
|
||||
"customer",
|
||||
"shipping_address",
|
||||
"billing_address",
|
||||
"shipping_methods",
|
||||
"shipping_methods.adjustments",
|
||||
"sales_channel",
|
||||
]
|
||||
|
||||
|
||||
@@ -81,14 +81,19 @@ export class StorePostCartsCartReq {
|
||||
@IsType([AddressPayload, String])
|
||||
shipping_address?: AddressPayload | string
|
||||
|
||||
@IsEmail()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
sales_channel_id?: string
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@Type(() => String)
|
||||
promo_codes?: string[]
|
||||
|
||||
// @IsOptional()
|
||||
// @IsArray()
|
||||
// @ValidateNested({ each: true })
|
||||
|
||||
Reference in New Issue
Block a user