feat: Use tag ids instead of values wherever possible (#8394)
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import { BatchMethodRequest } from "@medusajs/types"
|
||||
import { ProductStatus } from "@medusajs/utils"
|
||||
import { z } from "zod"
|
||||
import { GetProductsParams } from "../../utils/common-validators"
|
||||
import {
|
||||
GetProductsParams,
|
||||
transformProductParams,
|
||||
} from "../../utils/common-validators"
|
||||
import {
|
||||
createFindParams,
|
||||
createOperatorMap,
|
||||
@@ -38,17 +41,19 @@ export type AdminGetProductsParamsType = z.infer<typeof AdminGetProductsParams>
|
||||
export const AdminGetProductsParams = createFindParams({
|
||||
offset: 0,
|
||||
limit: 50,
|
||||
}).merge(
|
||||
z
|
||||
.object({
|
||||
variants: AdminGetProductVariantsParams.optional(),
|
||||
price_list_id: z.string().array().optional(),
|
||||
status: statusEnum.array().optional(),
|
||||
$and: z.lazy(() => AdminGetProductsParams.array()).optional(),
|
||||
$or: z.lazy(() => AdminGetProductsParams.array()).optional(),
|
||||
})
|
||||
.merge(GetProductsParams)
|
||||
)
|
||||
})
|
||||
.merge(
|
||||
z
|
||||
.object({
|
||||
variants: AdminGetProductVariantsParams.optional(),
|
||||
price_list_id: z.string().array().optional(),
|
||||
status: statusEnum.array().optional(),
|
||||
$and: z.lazy(() => AdminGetProductsParams.array()).optional(),
|
||||
$or: z.lazy(() => AdminGetProductsParams.array()).optional(),
|
||||
})
|
||||
.merge(GetProductsParams)
|
||||
)
|
||||
.transform(transformProductParams)
|
||||
|
||||
export type AdminGetProductOptionsParamsType = z.infer<
|
||||
typeof AdminGetProductOptionsParams
|
||||
@@ -195,7 +200,7 @@ export const AdminBatchUpdateProductVariant = AdminUpdateProductVariant.extend({
|
||||
id: z.string(),
|
||||
})
|
||||
|
||||
export const AdminCreateProductProductCategory = z.object({
|
||||
export const IdAssociation = z.object({
|
||||
id: z.string(),
|
||||
})
|
||||
|
||||
@@ -213,8 +218,8 @@ export const AdminCreateProduct = z
|
||||
status: statusEnum.nullish().default(ProductStatus.DRAFT),
|
||||
type_id: z.string().nullish(),
|
||||
collection_id: z.string().nullish(),
|
||||
categories: z.array(AdminCreateProductProductCategory).optional(),
|
||||
tags: z.array(AdminUpdateProductTag).optional(),
|
||||
categories: z.array(IdAssociation).optional(),
|
||||
tags: z.array(IdAssociation).optional(),
|
||||
options: z.array(AdminCreateProductOption).optional(),
|
||||
variants: z.array(AdminCreateProductVariant).optional(),
|
||||
sales_channels: z.array(z.object({ id: z.string() })).optional(),
|
||||
@@ -246,8 +251,8 @@ export const AdminUpdateProduct = z
|
||||
handle: z.string().nullish(),
|
||||
type_id: z.string().nullish(),
|
||||
collection_id: z.string().nullish(),
|
||||
categories: z.array(AdminCreateProductProductCategory).optional(),
|
||||
tags: z.array(AdminUpdateProductTag).optional(),
|
||||
categories: z.array(IdAssociation).optional(),
|
||||
tags: z.array(IdAssociation).optional(),
|
||||
sales_channels: z.array(z.object({ id: z.string() })).optional(),
|
||||
weight: z.number().nullish(),
|
||||
length: z.number().nullish(),
|
||||
@@ -271,13 +276,6 @@ export const AdminBatchUpdateProduct = AdminUpdateProduct.extend({
|
||||
export type AdminExportProductType = z.infer<typeof AdminExportProduct>
|
||||
export const AdminExportProduct = z.object({})
|
||||
|
||||
// TODO: Handle in create and update product once ready
|
||||
// @IsOptional()
|
||||
// @Type(() => ProductProductCategoryReq)
|
||||
// @ValidateNested({ each: true })
|
||||
// @IsArray()
|
||||
// categories?: ProductProductCategoryReq[]
|
||||
|
||||
export const AdminCreateVariantInventoryItem = z.object({
|
||||
required_quantity: z.number(),
|
||||
inventory_item_id: z.string(),
|
||||
|
||||
@@ -2,6 +2,7 @@ import { z } from "zod"
|
||||
import {
|
||||
GetProductsParams,
|
||||
ProductStatusEnum,
|
||||
transformProductParams,
|
||||
} from "../../utils/common-validators"
|
||||
import {
|
||||
createFindParams,
|
||||
@@ -45,28 +46,30 @@ export type StoreGetProductsParamsType = z.infer<typeof StoreGetProductsParams>
|
||||
export const StoreGetProductsParams = createFindParams({
|
||||
offset: 0,
|
||||
limit: 50,
|
||||
}).merge(
|
||||
z
|
||||
.object({
|
||||
// These are used to populate the tax and pricing context
|
||||
region_id: z.string().optional(),
|
||||
country_code: z.string().optional(),
|
||||
province: z.string().optional(),
|
||||
cart_id: z.string().optional(),
|
||||
})
|
||||
.merge(
|
||||
z
|
||||
.object({
|
||||
// These are used to populate the tax and pricing context
|
||||
region_id: z.string().optional(),
|
||||
country_code: z.string().optional(),
|
||||
province: z.string().optional(),
|
||||
cart_id: z.string().optional(),
|
||||
|
||||
variants: z
|
||||
.object({
|
||||
status: ProductStatusEnum.array().optional(),
|
||||
options: z
|
||||
.object({ value: z.string(), option_id: z.string() })
|
||||
.optional(),
|
||||
$and: z.lazy(() => StoreGetProductsParams.array()).optional(),
|
||||
$or: z.lazy(() => StoreGetProductsParams.array()).optional(),
|
||||
})
|
||||
.optional(),
|
||||
$and: z.lazy(() => StoreGetProductsParams.array()).optional(),
|
||||
$or: z.lazy(() => StoreGetProductsParams.array()).optional(),
|
||||
})
|
||||
.merge(GetProductsParams)
|
||||
.strict()
|
||||
)
|
||||
variants: z
|
||||
.object({
|
||||
status: ProductStatusEnum.array().optional(),
|
||||
options: z
|
||||
.object({ value: z.string(), option_id: z.string() })
|
||||
.optional(),
|
||||
$and: z.lazy(() => StoreGetProductsParams.array()).optional(),
|
||||
$or: z.lazy(() => StoreGetProductsParams.array()).optional(),
|
||||
})
|
||||
.optional(),
|
||||
$and: z.lazy(() => StoreGetProductsParams.array()).optional(),
|
||||
$or: z.lazy(() => StoreGetProductsParams.array()).optional(),
|
||||
})
|
||||
.merge(GetProductsParams)
|
||||
.strict()
|
||||
)
|
||||
.transform(transformProductParams)
|
||||
|
||||
@@ -2,21 +2,58 @@ import { ProductStatus } from "@medusajs/utils"
|
||||
import { z } from "zod"
|
||||
import { createOperatorMap } from "../../validators"
|
||||
import { OptionalBooleanValidator } from "../common"
|
||||
import { FilterableProductProps } from "@medusajs/types"
|
||||
|
||||
export const ProductStatusEnum = z.nativeEnum(ProductStatus)
|
||||
|
||||
export const GetProductsParams = z.object({
|
||||
q: z.string().optional(),
|
||||
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
title: z.string().nullish(),
|
||||
handle: z.string().nullish(),
|
||||
title: z.string().optional(),
|
||||
handle: z.string().optional(),
|
||||
is_giftcard: OptionalBooleanValidator,
|
||||
category_id: z.union([z.string(), z.array(z.string())]).nullish(),
|
||||
sales_channel_id: z.union([z.string(), z.array(z.string())]).nullish(),
|
||||
collection_id: z.union([z.string(), z.array(z.string())]).nullish(),
|
||||
tags: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
category_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
sales_channel_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
collection_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
tag_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
type_id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
created_at: createOperatorMap().optional(),
|
||||
updated_at: createOperatorMap().optional(),
|
||||
deleted_at: createOperatorMap().optional(),
|
||||
})
|
||||
|
||||
type HttpProductFilters = FilterableProductProps & {
|
||||
tag_id?: string | string[]
|
||||
category_id?: string | string[]
|
||||
}
|
||||
|
||||
export const transformProductParams = (
|
||||
data: HttpProductFilters
|
||||
): FilterableProductProps => {
|
||||
const res = {
|
||||
...data,
|
||||
tags: normalizeArray(data, "tag_id"),
|
||||
categories: normalizeArray(data, "category_id"),
|
||||
}
|
||||
|
||||
delete res.tag_id
|
||||
delete res.category_id
|
||||
|
||||
return res as FilterableProductProps
|
||||
}
|
||||
|
||||
const normalizeArray = (filters: HttpProductFilters, key: string) => {
|
||||
if (filters[key]) {
|
||||
if (Array.isArray(filters[key])) {
|
||||
return {
|
||||
id: { $in: filters[key] },
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
id: filters[key] as string,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user