feature: add create and update cart hooks (#8481)
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import { CartDTO, CreateCartWorkflowInputDTO } from "@medusajs/types"
|
||||
import {
|
||||
AdditionalData,
|
||||
CartDTO,
|
||||
CreateCartWorkflowInputDTO,
|
||||
} from "@medusajs/types"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createHook,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
@@ -29,9 +34,7 @@ import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-colle
|
||||
export const createCartWorkflowId = "create-cart"
|
||||
export const createCartWorkflow = createWorkflow(
|
||||
createCartWorkflowId,
|
||||
(
|
||||
input: WorkflowData<CreateCartWorkflowInputDTO>
|
||||
): WorkflowResponse<CartDTO> => {
|
||||
(input: WorkflowData<CreateCartWorkflowInputDTO & AdditionalData>) => {
|
||||
const variantIds = transform({ input }, (data) => {
|
||||
return (data.input.items ?? []).map((i) => i.variant_id)
|
||||
})
|
||||
@@ -158,6 +161,13 @@ export const createCartWorkflow = createWorkflow(
|
||||
},
|
||||
})
|
||||
|
||||
return new WorkflowResponse(cart)
|
||||
const cartCreated = createHook("cartCreated", {
|
||||
cart,
|
||||
additional_data: input.additional_data,
|
||||
})
|
||||
|
||||
return new WorkflowResponse(cart, {
|
||||
hooks: [cartCreated],
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { UpdateCartWorkflowInputDTO } from "@medusajs/types"
|
||||
import { AdditionalData, UpdateCartWorkflowInputDTO } from "@medusajs/types"
|
||||
import { MedusaError, PromotionActions, isPresent } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createHook,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
@@ -22,7 +24,7 @@ import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-colle
|
||||
export const updateCartWorkflowId = "update-cart"
|
||||
export const updateCartWorkflow = createWorkflow(
|
||||
updateCartWorkflowId,
|
||||
(input: WorkflowData<UpdateCartWorkflowInputDTO>): WorkflowData<void> => {
|
||||
(input: WorkflowData<UpdateCartWorkflowInputDTO & AdditionalData>) => {
|
||||
const [salesChannel, region, customerData] = parallelize(
|
||||
findSalesChannelStep({
|
||||
salesChannelId: input.sales_channel_id,
|
||||
@@ -96,5 +98,14 @@ export const updateCartWorkflow = createWorkflow(
|
||||
cart_id: input.id,
|
||||
},
|
||||
})
|
||||
|
||||
const cartUpdated = createHook("cartUpdated", {
|
||||
cart,
|
||||
additional_data: input.additional_data,
|
||||
})
|
||||
|
||||
return new WorkflowResponse(void 0, {
|
||||
hooks: [cartUpdated],
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { updateCartWorkflow } from "@medusajs/core-flows"
|
||||
import { UpdateCartDataDTO } from "@medusajs/types"
|
||||
import { AdditionalData, UpdateCartDataDTO } from "@medusajs/types"
|
||||
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
import { refetchCart } from "../helpers"
|
||||
@@ -15,14 +15,14 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: MedusaRequest<UpdateCartDataDTO>,
|
||||
req: MedusaRequest<UpdateCartDataDTO & AdditionalData>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const workflow = updateCartWorkflow(req.scope)
|
||||
|
||||
await workflow.run({
|
||||
input: {
|
||||
...(req.validatedBody as UpdateCartDataDTO),
|
||||
...req.validatedBody,
|
||||
id: req.params.id,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { createCartWorkflow } from "@medusajs/core-flows"
|
||||
import { CreateCartWorkflowInputDTO } from "@medusajs/types"
|
||||
import { AdditionalData, CreateCartWorkflowInputDTO } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
import { refetchCart } from "./helpers"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<CreateCartWorkflowInputDTO>,
|
||||
req: AuthenticatedMedusaRequest<CreateCartWorkflowInputDTO & AdditionalData>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const workflowInput = {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { z } from "zod"
|
||||
import { AddressPayload } from "../../utils/common-validators"
|
||||
import { createSelectParams } from "../../utils/validators"
|
||||
import { createSelectParams, WithAdditionalData } from "../../utils/validators"
|
||||
|
||||
export type StoreGetPromotionType = z.infer<typeof StoreGetCartsCart>
|
||||
export const StoreGetCartsCart = createSelectParams()
|
||||
@@ -11,8 +11,8 @@ const ItemSchema = z.object({
|
||||
metadata: z.record(z.unknown()).nullish(),
|
||||
})
|
||||
|
||||
export type StoreCreateCartType = z.infer<typeof StoreCreateCart>
|
||||
export const StoreCreateCart = z
|
||||
export type StoreCreateCartType = z.infer<typeof CreateCart>
|
||||
export const CreateCart = z
|
||||
.object({
|
||||
region_id: z.string().nullish(),
|
||||
shipping_address: z.union([AddressPayload, z.string()]).optional(),
|
||||
@@ -24,6 +24,7 @@ export const StoreCreateCart = z
|
||||
metadata: z.record(z.unknown()).nullish(),
|
||||
})
|
||||
.strict()
|
||||
export const StoreCreateCart = WithAdditionalData(CreateCart)
|
||||
|
||||
export type StoreAddCartPromotionsType = z.infer<typeof StoreAddCartPromotions>
|
||||
export const StoreAddCartPromotions = z
|
||||
@@ -41,8 +42,8 @@ export const StoreRemoveCartPromotions = z
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type StoreUpdateCartType = z.infer<typeof StoreUpdateCart>
|
||||
export const StoreUpdateCart = z
|
||||
export type StoreUpdateCartType = z.infer<typeof UpdateCart>
|
||||
export const UpdateCart = z
|
||||
.object({
|
||||
region_id: z.string().nullish(),
|
||||
email: z.string().email().nullish(),
|
||||
@@ -53,6 +54,7 @@ export const StoreUpdateCart = z
|
||||
promo_codes: z.array(z.string()).optional(),
|
||||
})
|
||||
.strict()
|
||||
export const StoreUpdateCart = WithAdditionalData(UpdateCart)
|
||||
|
||||
export type StoreCalculateCartTaxesType = z.infer<
|
||||
typeof StoreCalculateCartTaxes
|
||||
|
||||
@@ -1,4 +1,21 @@
|
||||
import { z } from "zod"
|
||||
import { z, ZodObject } from "zod"
|
||||
|
||||
/**
|
||||
* Wraps the original schema to a function to accept and merge
|
||||
* additional_data schema
|
||||
*/
|
||||
export const WithAdditionalData = (originalSchema: ZodObject<any, any>) => {
|
||||
return (additionalDataValidator?: ZodObject<any, any>) => {
|
||||
if (!additionalDataValidator) {
|
||||
return originalSchema.extend({
|
||||
additional_data: z.record(z.unknown()).nullish(),
|
||||
})
|
||||
}
|
||||
return originalSchema.extend({
|
||||
additional_data: additionalDataValidator,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const createBatchBody = (
|
||||
createValidator: z.ZodType,
|
||||
|
||||
Reference in New Issue
Block a user