fix: Correctly associate options to variants (#8285)
This commit is contained in:
+63
@@ -328,6 +328,69 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
|||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should correctly associate variants with own product options", async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
const productThree = await service.createProducts({
|
||||||
|
id: "product-3",
|
||||||
|
title: "product 3",
|
||||||
|
status: ProductStatus.PUBLISHED,
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
title: "size",
|
||||||
|
values: ["large", "small"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "color",
|
||||||
|
values: ["red", "blue"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
} as CreateProductDTO)
|
||||||
|
|
||||||
|
const data: CreateProductVariantDTO[] = [
|
||||||
|
{
|
||||||
|
title: "new variant",
|
||||||
|
product_id: productOne.id,
|
||||||
|
options: { size: "small" },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "new variant",
|
||||||
|
product_id: productThree.id,
|
||||||
|
options: { size: "small" },
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
const variants = await service.createProductVariants(data)
|
||||||
|
|
||||||
|
expect(variants).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
title: "new variant",
|
||||||
|
product_id: productOne.id,
|
||||||
|
options: expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
id: productOne.options
|
||||||
|
.find((o) => o.title === "size")
|
||||||
|
?.values?.find((v) => v.value === "small")?.id,
|
||||||
|
value: "small",
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
title: "new variant",
|
||||||
|
product_id: productThree.id,
|
||||||
|
options: expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
id: productThree.options
|
||||||
|
.find((o) => o.title === "size")
|
||||||
|
?.values?.find((v) => v.value === "small")?.id,
|
||||||
|
value: "small",
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("softDelete variant", () => {
|
describe("softDelete variant", () => {
|
||||||
|
|||||||
@@ -1710,7 +1710,10 @@ export default class ProductModuleService
|
|||||||
const variantsWithOptions = variants.map((variant: any) => {
|
const variantsWithOptions = variants.map((variant: any) => {
|
||||||
const variantOptions = Object.entries(variant.options ?? {}).map(
|
const variantOptions = Object.entries(variant.options ?? {}).map(
|
||||||
([key, val]) => {
|
([key, val]) => {
|
||||||
const option = options.find((o) => o.title === key)
|
const option = options.find(
|
||||||
|
(o) => o.title === key && o.product_id === variant.product_id
|
||||||
|
)
|
||||||
|
|
||||||
const optionValue = option?.values?.find(
|
const optionValue = option?.values?.find(
|
||||||
(v: any) => (v.value?.value ?? v.value) === val
|
(v: any) => (v.value?.value ?? v.value) === val
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user