feature: add create and update cart hooks (#8481)

This commit is contained in:
Harminder Virk authored and GitHub committed 2024-08-07 16:00:37 +05:30
1 parent 78eb457487
commit 7695d30952
6 files changed
+58 -18

No files matched your search

@@ -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,
},
})
+2 -2
View File
@@ -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
+18 -1
View File
@@ -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,