feat(medusa,product,types): product type & tag store missing endpoints (#11057)
* wip: tag endpoints * feat: types, product types * feat: tests * fix: update product type schema
This commit is contained in:
@@ -54,6 +54,8 @@ import { storePaymentCollectionsMiddlewares } from "./store/payment-collections/
|
||||
import { storePaymentProvidersMiddlewares } from "./store/payment-providers/middlewares"
|
||||
import { storeProductCategoryRoutesMiddlewares } from "./store/product-categories/middlewares"
|
||||
import { storeProductRoutesMiddlewares } from "./store/products/middlewares"
|
||||
import { storeProductTagRoutesMiddlewares } from "./store/product-tags/middlewares"
|
||||
import { storeProductTypeRoutesMiddlewares } from "./store/product-types/middlewares"
|
||||
import { storeRegionRoutesMiddlewares } from "./store/regions/middlewares"
|
||||
import { storeReturnReasonRoutesMiddlewares } from "./store/return-reasons/middlewares"
|
||||
import { storeShippingOptionRoutesMiddlewares } from "./store/shipping-options/middlewares"
|
||||
@@ -69,6 +71,8 @@ export default defineMiddlewares([
|
||||
...storeCartRoutesMiddlewares,
|
||||
...storeCollectionRoutesMiddlewares,
|
||||
...storeProductCategoryRoutesMiddlewares,
|
||||
...storeProductTagRoutesMiddlewares,
|
||||
...storeProductTypeRoutesMiddlewares,
|
||||
...storePaymentProvidersMiddlewares,
|
||||
...storeShippingOptionRoutesMiddlewares,
|
||||
...storePaymentCollectionsMiddlewares,
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { StoreProductTagResponse } from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
|
||||
import { StoreProductTagParamsType } from "../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<StoreProductTagParamsType>,
|
||||
res: MedusaResponse<StoreProductTagResponse>
|
||||
) => {
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data } = await query.graph({
|
||||
entity: "product_tag",
|
||||
filters: {
|
||||
id: req.params.id,
|
||||
},
|
||||
fields: req.queryConfig.fields,
|
||||
})
|
||||
|
||||
if (!data.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Product tag with id: ${req.params.id} was not found`
|
||||
)
|
||||
}
|
||||
res.json({ product_tag: data[0] })
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { MiddlewareRoute } from "@medusajs/framework/http"
|
||||
import { validateAndTransformQuery } from "@medusajs/framework"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import { StoreProductTagsParams, StoreProductTagParams } from "./validators"
|
||||
|
||||
export const storeProductTagRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/store/product-tags",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
StoreProductTagsParams,
|
||||
QueryConfig.listProductTagConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/store/product-tags/:id",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
StoreProductTagParams,
|
||||
QueryConfig.retrieveProductTagConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,19 @@
|
||||
export const defaults = [
|
||||
"id",
|
||||
"value",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"metadata",
|
||||
"*products",
|
||||
]
|
||||
|
||||
export const retrieveProductTagConfig = {
|
||||
defaults,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listProductTagConfig = {
|
||||
defaults,
|
||||
defaultLimit: 50,
|
||||
isList: true,
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { StoreProductTagListResponse } from "@medusajs/framework/types"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
import { StoreProductTagsParamsType } from "./validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<StoreProductTagsParamsType>,
|
||||
res: MedusaResponse<StoreProductTagListResponse>
|
||||
) => {
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data: product_tags, metadata } = await query.graph({
|
||||
entity: "product_tag",
|
||||
filters: req.filterableFields,
|
||||
pagination: req.queryConfig.pagination,
|
||||
fields: req.queryConfig.fields,
|
||||
})
|
||||
|
||||
res.json({
|
||||
product_tags,
|
||||
count: metadata?.count ?? 0,
|
||||
offset: metadata?.skip ?? 0,
|
||||
limit: metadata?.take ?? 0,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { z } from "zod"
|
||||
import { applyAndAndOrOperators } from "../../utils/common-validators"
|
||||
import {
|
||||
createFindParams,
|
||||
createOperatorMap,
|
||||
createSelectParams,
|
||||
} from "../../utils/validators"
|
||||
|
||||
export type StoreProductTagParamsType = z.infer<typeof StoreProductTagParams>
|
||||
|
||||
export const StoreProductTagParams = createSelectParams().merge(z.object({}))
|
||||
|
||||
export const StoreProductTagsParamsFields = z.object({
|
||||
q: z.string().optional(),
|
||||
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
value: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
created_at: createOperatorMap().optional(),
|
||||
updated_at: createOperatorMap().optional(),
|
||||
deleted_at: createOperatorMap().optional(),
|
||||
})
|
||||
|
||||
export type StoreProductTagsParamsType = z.infer<typeof StoreProductTagsParams>
|
||||
export const StoreProductTagsParams = createFindParams({
|
||||
offset: 0,
|
||||
limit: 50,
|
||||
})
|
||||
.merge(StoreProductTagsParamsFields)
|
||||
.merge(applyAndAndOrOperators(StoreProductTagsParamsFields))
|
||||
@@ -0,0 +1,34 @@
|
||||
import { StoreProductTypeResponse } from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
|
||||
import { StoreProductTypeParamsType } from "../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<StoreProductTypeParamsType>,
|
||||
res: MedusaResponse<StoreProductTypeResponse>
|
||||
) => {
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data } = await query.graph({
|
||||
entity: "product_type",
|
||||
filters: {
|
||||
id: req.params.id,
|
||||
},
|
||||
fields: req.queryConfig.fields,
|
||||
})
|
||||
|
||||
if (!data.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Product type with id: ${req.params.id} was not found`
|
||||
)
|
||||
}
|
||||
res.json({ product_type: data[0] })
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { MiddlewareRoute } from "@medusajs/framework/http"
|
||||
import { validateAndTransformQuery } from "@medusajs/framework"
|
||||
import * as QueryConfig from "./query-config"
|
||||
|
||||
import { StoreProductTypesParams } from "./validators"
|
||||
|
||||
export const storeProductTypeRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/store/product-types",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
StoreProductTypesParams,
|
||||
QueryConfig.listProductTypeConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/store/product-types/:id",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
StoreProductTypesParams,
|
||||
QueryConfig.retrieveProductTypeConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,19 @@
|
||||
export const defaults = [
|
||||
"id",
|
||||
"value",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"metadata",
|
||||
"*products",
|
||||
]
|
||||
|
||||
export const retrieveProductTypeConfig = {
|
||||
defaults,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listProductTypeConfig = {
|
||||
defaults,
|
||||
defaultLimit: 50,
|
||||
isList: true,
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { StoreProductTypeListResponse } from "@medusajs/framework/types"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
import { StoreProductTypesParamsType } from "./validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<StoreProductTypesParamsType>,
|
||||
res: MedusaResponse<StoreProductTypeListResponse>
|
||||
) => {
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data: product_types, metadata } = await query.graph({
|
||||
entity: "product_type",
|
||||
filters: req.filterableFields,
|
||||
pagination: req.queryConfig.pagination,
|
||||
fields: req.queryConfig.fields,
|
||||
})
|
||||
|
||||
res.json({
|
||||
product_types,
|
||||
count: metadata?.count ?? 0,
|
||||
offset: metadata?.skip ?? 0,
|
||||
limit: metadata?.take ?? 0,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { z } from "zod"
|
||||
import { applyAndAndOrOperators } from "../../utils/common-validators"
|
||||
import {
|
||||
createFindParams,
|
||||
createOperatorMap,
|
||||
createSelectParams,
|
||||
} from "../../utils/validators"
|
||||
|
||||
export type StoreProductTypeParamsType = z.infer<typeof StoreProductTypeParams>
|
||||
|
||||
export const StoreProductTypeParams = createSelectParams().merge(z.object({}))
|
||||
|
||||
export const StoreProductTypesParamsFields = z.object({
|
||||
q: z.string().optional(),
|
||||
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
value: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
created_at: createOperatorMap().optional(),
|
||||
updated_at: createOperatorMap().optional(),
|
||||
deleted_at: createOperatorMap().optional(),
|
||||
})
|
||||
|
||||
export type StoreProductTypesParamsType = z.infer<
|
||||
typeof StoreProductTypesParams
|
||||
>
|
||||
export const StoreProductTypesParams = createFindParams({
|
||||
offset: 0,
|
||||
limit: 50,
|
||||
})
|
||||
.merge(StoreProductTypesParamsFields)
|
||||
.merge(applyAndAndOrOperators(StoreProductTypesParamsFields))
|
||||
Reference in New Issue
Block a user