fix: Correctly associate options to variants (#8285)

This commit is contained in:
Stevche Radevski
2024-07-25 17:56:13 +02:00
committed by GitHub
parent eb64ae75a6
commit 639ecdfd74
2 changed files with 67 additions and 1 deletions
@@ -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", () => {
@@ -1710,7 +1710,10 @@ export default class ProductModuleService
const variantsWithOptions = variants.map((variant: any) => {
const variantOptions = Object.entries(variant.options ?? {}).map(
([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(
(v: any) => (v.value?.value ?? v.value) === val
)