feat: Refactor the product module definitions and implementation (#6866)

There are several things done in this PR, namely:

Unify the service endpoints API to always work with a model rather than allowing to pass both ID and model (eg. both type_id and type being available in the request to create).
Start using upsertWithReplace to simplify the code and fix some deassociation bugs
Apply some changes to tests to deal with the pricing breaking changes
Correctly define the model relationships (with both ID and entity fields available)
All tests for the product are passing, which should bring us back to a great baseline.
This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-03-29 09:03:41 +00:00
1 parent e603726985
commit cbb5e6bd99
35 files changed
+1318 -1374

No files matched your search

@@ -1,4 +1,9 @@
import { MedusaContainer, ProductDTO, ProductVariantDTO } from "@medusajs/types"
import {
CreateProductDTO,
MedusaContainer,
ProductDTO,
ProductVariantDTO,
} from "@medusajs/types"
import { remoteQueryObjectFromString } from "@medusajs/utils"
const isPricing = (fieldName: string) =>
@@ -45,8 +50,14 @@ export const remapVariant = (v: ProductVariantDTO) => {
return {
...v,
prices: (v as any).price_set?.prices?.map((price) => ({
...price,
id: price.id,
amount: price.amount,
currency_code: price.currency_code,
min_quantity: price.min_quantity,
max_quantity: price.max_quantity,
variant_id: v.id,
created_at: price.created_at,
updated_at: price.updated_at,
})),
price_set: undefined,
}
@@ -41,11 +41,7 @@ export const POST = async (
req: AuthenticatedMedusaRequest<CreateProductDTO>,
res: MedusaResponse
) => {
const input = [
{
...req.validatedBody,
},
]
const input = [req.validatedBody]
const { result, errors } = await createProductsWorkflow(req.scope).run({
input: { products: input },
@@ -287,9 +287,8 @@ export class AdminPostProductsReq {
status?: ProductStatus = ProductStatus.DRAFT
@IsOptional()
@Type(() => ProductTypeReq)
@ValidateNested()
type?: ProductTypeReq
@IsString()
type_id?: string
@IsOptional()
@IsString()
@@ -400,9 +399,8 @@ export class AdminPostProductsProductReq {
status?: ProductStatus
@IsOptional()
@Type(() => ProductTypeReq)
@ValidateNested()
type?: ProductTypeReq
@IsString()
type_id?: string
@IsOptional()
@IsString()