feat: Update the product options model and refactor the product module (#6685)

The changes in this PR are:
1. Change how product options are created and stored. The relationship changed from
`options --> option values <-- variants`
to
`options --> option values --> variant options <-- variants`

Now we can enforce non-duplicate option values, easier creation and updates of options, and more.

2. Refactors the product module. The product module did a lot of things in a non-ideal approach, and this is a step towards a more consistent usage of the base repository and methods exposed by a module. There is still work left to improve the module, but a large chunk of the changes are included in this PR


Things to do as a follow-up:
1. Remove many-to-many relationships  if an empty list is passed in the base repository.
2. Improve the typings of the module
3. Further cleanup and improvements (there are few questions that I need answered before I can improve the API)
This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-03-15 12:35:46 +00:00
1 parent a1b4aff127
commit 1956dce80a
40 files changed
+1517 -1896

No files matched your search

@@ -48,7 +48,9 @@ export const buildProductAndRelationsData = ({
variants,
collection_id,
}: Partial<ProductTypes.CreateProductDTO>) => {
const defaultOptionTitle = faker.commerce.productName()
const defaultOptionTitle = "test-option"
const defaultOptionValue = "test-value"
return {
title: title ?? faker.commerce.productName(),
description: description ?? faker.commerce.productName(),
@@ -64,17 +66,16 @@ export const buildProductAndRelationsData = ({
options: options ?? [
{
title: defaultOptionTitle,
values: [defaultOptionValue],
},
],
variants: variants ?? [
{
title: faker.commerce.productName(),
sku: faker.commerce.productName(),
options: [
{
value: defaultOptionTitle + faker.commerce.productName(),
},
],
options: {
[defaultOptionTitle]: defaultOptionValue,
},
},
],
// TODO: add categories, must be created first
@@ -219,7 +219,7 @@ moduleIntegrationTestRunner({
}
expect(error.message).toEqual(
"ProductOption with id: does-not-exist was not found"
`ProductOption with id: does-not-exist was not found`
)
})
})
@@ -268,7 +268,7 @@ moduleIntegrationTestRunner({
}
expect(error.message).toEqual(
'ProductOption with id "does-not-exist" not found'
`Option with id "does-not-exist" does not exist, but was referenced in the update request`
)
})
})
@@ -278,6 +278,7 @@ moduleIntegrationTestRunner({
const res = await service.createOptions([
{
title: "test",
values: [],
product_id: productOne.id,
},
])
@@ -174,11 +174,7 @@ moduleIntegrationTestRunner({
...data.variants,
]
productBefore.type = { value: "new-type" }
// TODO: Change test (this one and below) to not require setting product ID here.
productBefore.options = data.options.map((o) => ({
...o,
product_id: productBefore.id,
}))
productBefore.options = data.options
productBefore.images = data.images
productBefore.thumbnail = data.thumbnail
productBefore.tags = data.tags
@@ -230,7 +226,7 @@ moduleIntegrationTestRunner({
values: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
value: createdVariant.options?.[0].value,
value: data.options[0].values[0],
}),
]),
}),
@@ -257,7 +253,9 @@ moduleIntegrationTestRunner({
options: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
value: createdVariant.options?.[0].value,
option_value: expect.objectContaining({
value: data.options[0].values[0],
}),
}),
]),
}),
@@ -275,10 +273,7 @@ moduleIntegrationTestRunner({
const updateData = {
...data,
options: data.options.map((o) => ({
...o,
product_id: productOne.id,
})),
options: data.options,
id: productOne.id,
title: "updated title",
}
@@ -426,7 +421,8 @@ moduleIntegrationTestRunner({
)
})
it("should remove relationships of a product", async () => {
// TODO: Currently the base repository doesn't remove relationships if an empty array is passed, we need to fix that in the base repo.
it.skip("should remove relationships of a product", async () => {
const updateData = {
id: productTwo.id,
categories: [],
@@ -535,7 +531,7 @@ moduleIntegrationTestRunner({
})
expect(error.message).toEqual(
`ProductVariant with id "does-not-exist" not found`
`Variant with id "does-not-exist" does not exist, but was referenced in the update request`
)
})
})
@@ -597,7 +593,7 @@ moduleIntegrationTestRunner({
values: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
value: data.variants[0].options?.[0].value,
value: data.options[0].values[0],
}),
]),
}),
@@ -624,7 +620,9 @@ moduleIntegrationTestRunner({
options: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
value: data.variants[0].options?.[0].value,
option_value: expect.objectContaining({
value: data.options[0].values[0],
}),
}),
]),
}),