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