feat: Implement missing methods in product module and make more tests pass (#6650)
The 2 bigger remaining tasks are: 1. handling prices for variants 2. Handling options (breaking change) After that all tests should pass on both v1 and v2
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import {
|
||||
deleteCollectionsWorkflow,
|
||||
updateCollectionsWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
|
||||
import { UpdateProductCollectionDTO } from "@medusajs/types"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const variables = { id: req.params.id }
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "product_collection",
|
||||
variables,
|
||||
fields: req.retrieveConfig.select as string[],
|
||||
})
|
||||
|
||||
const [collection] = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ collection })
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<UpdateProductCollectionDTO>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { result, errors } = await updateCollectionsWorkflow(req.scope).run({
|
||||
input: {
|
||||
selector: { id: req.params.id },
|
||||
update: req.validatedBody,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ collection: result[0] })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
|
||||
const { errors } = await deleteCollectionsWorkflow(req.scope).run({
|
||||
input: { ids: [id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "collection",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import * as QueryConfig from "./query-config"
|
||||
|
||||
import {
|
||||
AdminGetCollectionsCollectionParams,
|
||||
AdminGetCollectionsParams,
|
||||
AdminPostCollectionsCollectionReq,
|
||||
AdminPostCollectionsReq,
|
||||
} from "./validators"
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
|
||||
export const adminCollectionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["ALL"],
|
||||
matcher: "/admin/collections*",
|
||||
middlewares: [authenticate("admin", ["bearer", "session", "api-key"])],
|
||||
},
|
||||
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/collections",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetCollectionsParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/collections/:id",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetCollectionsCollectionParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/collections",
|
||||
middlewares: [transformBody(AdminPostCollectionsReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/collections/:id",
|
||||
middlewares: [transformBody(AdminPostCollectionsCollectionReq)],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/admin/collections/:id",
|
||||
middlewares: [],
|
||||
},
|
||||
// TODO: There were two batch methods, they need to be handled
|
||||
]
|
||||
@@ -0,0 +1,25 @@
|
||||
export const allowedAdminCollectionRelations = ["products.profiles"]
|
||||
|
||||
// TODO: See how these should look when expanded
|
||||
export const defaultAdminCollectionRelations = ["products.profiles"]
|
||||
|
||||
export const defaultAdminCollectionFields = [
|
||||
"id",
|
||||
"title",
|
||||
"handle",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultAdminCollectionFields,
|
||||
defaultRelations: defaultAdminCollectionRelations,
|
||||
allowedRelations: allowedAdminCollectionRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
...retrieveTransformQueryConfig,
|
||||
defaultLimit: 10,
|
||||
isList: true,
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
|
||||
import { CreateProductCollectionDTO } from "@medusajs/types"
|
||||
import { createCollectionsWorkflow } from "@medusajs/core-flows"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "product_collection",
|
||||
variables: {
|
||||
filters: req.filterableFields,
|
||||
order: req.listConfig.order,
|
||||
skip: req.listConfig.skip,
|
||||
take: req.listConfig.take,
|
||||
},
|
||||
fields: req.listConfig.select as string[],
|
||||
})
|
||||
|
||||
const { rows: collections, metadata } = await remoteQuery(queryObject)
|
||||
|
||||
res.json({
|
||||
collections,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<CreateProductCollectionDTO>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const input = [
|
||||
{
|
||||
...req.validatedBody,
|
||||
},
|
||||
]
|
||||
|
||||
const { result, errors } = await createCollectionsWorkflow(req.scope).run({
|
||||
input: { collections: input },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ collection: result[0] })
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import { OperatorMap } from "@medusajs/types"
|
||||
import { Type } from "class-transformer"
|
||||
import {
|
||||
IsNotEmpty,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
||||
|
||||
// TODO: Ensure these match the DTOs in the types
|
||||
export class AdminGetCollectionsCollectionParams extends FindParams {}
|
||||
|
||||
/**
|
||||
* Parameters used to filter and configure the pagination of the retrieved regions.
|
||||
*/
|
||||
export class AdminGetCollectionsParams extends extendedFindParamsMixin({
|
||||
limit: 10,
|
||||
offset: 0,
|
||||
}) {
|
||||
/**
|
||||
* Term to search product collections by their title and handle.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
q?: string
|
||||
|
||||
/**
|
||||
* Title to filter product collections by.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
title?: string | string[]
|
||||
|
||||
/**
|
||||
* Handle to filter product collections by.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
handle?: string | string[]
|
||||
|
||||
/**
|
||||
* Date filters to apply on the product collections' `created_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => OperatorMapValidator)
|
||||
created_at?: OperatorMap<string>
|
||||
|
||||
/**
|
||||
* Date filters to apply on the product collections' `updated_at` date.
|
||||
*/
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => OperatorMapValidator)
|
||||
updated_at?: OperatorMap<string>
|
||||
|
||||
/**
|
||||
* Date filters to apply on the product collections' `deleted_at` date.
|
||||
*/
|
||||
@ValidateNested()
|
||||
@IsOptional()
|
||||
@Type(() => OperatorMapValidator)
|
||||
deleted_at?: OperatorMap<string>
|
||||
|
||||
// TODO: To be added in next iteration
|
||||
// /**
|
||||
// * Filter product collections by their associated discount condition's ID.
|
||||
// */
|
||||
// @IsString()
|
||||
// @IsOptional()
|
||||
// discount_condition_id?: string
|
||||
|
||||
// Note: These are new in v2
|
||||
// Additional filters from BaseFilterable
|
||||
@IsOptional()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => AdminGetCollectionsParams)
|
||||
$and?: AdminGetCollectionsParams[]
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => AdminGetCollectionsParams)
|
||||
$or?: AdminGetCollectionsParams[]
|
||||
}
|
||||
|
||||
export class AdminPostCollectionsReq {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
title: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
handle?: string
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export class AdminPostCollectionsCollectionReq {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
title?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
handle?: string
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
@@ -39,9 +39,11 @@ export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<CreateProductOptionDTO>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const productId = req.params.id
|
||||
const input = [
|
||||
{
|
||||
...req.validatedBody,
|
||||
product_id: productId,
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@@ -24,13 +24,13 @@ export const GET = async (
|
||||
const variables = { id: variantId, product_id: productId }
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "product_variant",
|
||||
entryPoint: "variant",
|
||||
variables,
|
||||
fields: req.retrieveConfig.select as string[],
|
||||
})
|
||||
|
||||
const [product_variant] = await remoteQuery(queryObject)
|
||||
res.status(200).json({ product_variant })
|
||||
const [variant] = await remoteQuery(queryObject)
|
||||
res.status(200).json({ variant })
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
@@ -55,7 +55,7 @@ export const POST = async (
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ product_variant: result[0] })
|
||||
res.status(200).json({ variant: result[0] })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
@@ -78,7 +78,7 @@ export const DELETE = async (
|
||||
|
||||
res.status(200).json({
|
||||
id: variantId,
|
||||
object: "product_variant",
|
||||
object: "variant",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ export const GET = async (
|
||||
const productId = req.params.id
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "product_variant",
|
||||
entryPoint: "variant",
|
||||
variables: {
|
||||
filters: { ...req.filterableFields, product_id: productId },
|
||||
order: req.listConfig.order,
|
||||
@@ -25,10 +25,10 @@ export const GET = async (
|
||||
fields: req.listConfig.select as string[],
|
||||
})
|
||||
|
||||
const { rows: product_variants, metadata } = await remoteQuery(queryObject)
|
||||
const { rows: variants, metadata } = await remoteQuery(queryObject)
|
||||
|
||||
res.json({
|
||||
product_variants,
|
||||
variants,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
@@ -39,9 +39,11 @@ export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<CreateProductVariantDTO>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const productId = req.params.id
|
||||
const input = [
|
||||
{
|
||||
...req.validatedBody,
|
||||
product_id: productId,
|
||||
},
|
||||
]
|
||||
|
||||
@@ -56,5 +58,5 @@ export const POST = async (
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({ product_variant: result[0] })
|
||||
res.status(200).json({ variant: result[0] })
|
||||
}
|
||||
|
||||
@@ -66,9 +66,9 @@ export const allowedAdminProductRelations = [
|
||||
// TODO: See how this should be handled
|
||||
// "options.values",
|
||||
// TODO: Handle in next iteration
|
||||
// "tags",
|
||||
// "type",
|
||||
// "collection",
|
||||
"tags",
|
||||
"type",
|
||||
"collection",
|
||||
]
|
||||
|
||||
// TODO: This is what we had in the v1 list. Do we still want to expand that much by default? Also this doesn't work in v2 it seems.
|
||||
@@ -110,6 +110,12 @@ export const defaultAdminProductFields = [
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
"metadata",
|
||||
"type.id",
|
||||
"type.value",
|
||||
"type.metadata",
|
||||
"type.created_at",
|
||||
"type.updated_at",
|
||||
"type.deleted_at",
|
||||
"collection.id",
|
||||
"collection.title",
|
||||
"collection.handle",
|
||||
|
||||
@@ -17,7 +17,6 @@ import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
||||
import { ProductStatus } from "@medusajs/utils"
|
||||
import { IsType } from "../../../utils"
|
||||
import { optionalBooleanMapper } from "../../../utils/validators/is-boolean"
|
||||
import { ProductTagReq, ProductTypeReq } from "../../../types/product"
|
||||
|
||||
export class AdminGetProductsProductParams extends FindParams {}
|
||||
export class AdminGetProductsProductVariantsVariantParams extends FindParams {}
|
||||
@@ -66,14 +65,6 @@ export class AdminGetProductsParams extends extendedFindParamsMixin({
|
||||
@IsOptional()
|
||||
handle?: string
|
||||
|
||||
// TODO: Should we remove this? It makes sense for search, but not for equality comparison
|
||||
/**
|
||||
* Description to filter products by.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string
|
||||
|
||||
/**
|
||||
* Filter products by whether they're gift cards.
|
||||
*/
|
||||
@@ -402,11 +393,10 @@ export class AdminPostProductsProductReq {
|
||||
@ValidateIf((_, value) => value !== undefined)
|
||||
status?: ProductStatus
|
||||
|
||||
// TODO: Deal with in next iteration
|
||||
// @IsOptional()
|
||||
// @Type(() => ProductTypeReq)
|
||||
// @ValidateNested()
|
||||
// type?: ProductTypeReq
|
||||
@IsOptional()
|
||||
@Type(() => ProductTypeReq)
|
||||
@ValidateNested()
|
||||
type?: ProductTypeReq
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@@ -432,12 +422,11 @@ export class AdminPostProductsProductReq {
|
||||
// ])
|
||||
// sales_channels?: ProductSalesChannelReq[] | null
|
||||
|
||||
// TODO: Should we remove this on update?
|
||||
// @IsOptional()
|
||||
// @Type(() => ProductVariantReq)
|
||||
// @ValidateNested({ each: true })
|
||||
// @IsArray()
|
||||
// variants?: ProductVariantReq[]
|
||||
@IsOptional()
|
||||
@Type(() => ProductVariantReq)
|
||||
@ValidateNested({ each: true })
|
||||
@IsArray()
|
||||
variants?: ProductVariantReq[]
|
||||
|
||||
@IsNumber()
|
||||
@IsOptional()
|
||||
@@ -544,11 +533,13 @@ export class AdminPostProductsProductVariantsReq {
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
|
||||
// TODO: Add on next iteration
|
||||
// TODO: Add on next iteration, adding temporary field for now
|
||||
// @IsArray()
|
||||
// @ValidateNested({ each: true })
|
||||
// @Type(() => ProductVariantPricesCreateReq)
|
||||
// prices: ProductVariantPricesCreateReq[]
|
||||
@IsArray()
|
||||
prices: any[]
|
||||
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
@@ -651,3 +642,36 @@ export class AdminPostProductsProductOptionsOptionReq {
|
||||
@IsArray()
|
||||
values: string[]
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductVariantReq extends AdminPostProductsProductVariantsVariantReq {
|
||||
@IsString()
|
||||
id: string
|
||||
}
|
||||
|
||||
export class ProductTagReq {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
id?: string
|
||||
|
||||
@IsString()
|
||||
value: string
|
||||
}
|
||||
|
||||
/**
|
||||
* The details of a product type, used to create or update an existing product type.
|
||||
*/
|
||||
export class ProductTypeReq {
|
||||
/**
|
||||
* The ID of the product type. It's only required when referring to an existing product type.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
id?: string
|
||||
|
||||
/**
|
||||
* The value of the product type.
|
||||
*/
|
||||
@IsString()
|
||||
value: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user