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
2024-03-15 13:35:46 +01:00
committed by GitHub
parent a1b4aff127
commit 1956dce80a
40 changed files with 1517 additions and 1896 deletions
@@ -1,5 +1,5 @@
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
import { ProductTypes, UpdateProductVariantOnlyDTO } from "@medusajs/types"
import { ProductTypes, UpdateProductVariantDTO } from "@medusajs/types"
import { WorkflowArguments } from "@medusajs/workflows-sdk"
type HandlerInput = {
@@ -14,18 +14,13 @@ export async function updateProductVariants({
> {
const { productVariantsMap } = data
const productsVariants: ProductTypes.UpdateProductVariantDTO[] = []
const updateVariantsData: ProductTypes.UpdateProductVariantOnlyDTO[] = []
const updateVariantsData: ProductTypes.UpdateProductVariantDTO[] = []
const productModuleService: ProductTypes.IProductModuleService =
container.resolve(ModulesDefinition[Modules.PRODUCT].registrationName)
for (const [
product_id,
variantsUpdateData = [],
] of productVariantsMap) {
for (const [product_id, variantsUpdateData = []] of productVariantsMap) {
updateVariantsData.push(
...(variantsUpdateData as unknown as UpdateProductVariantOnlyDTO[]).map(
(update) => ({ ...update, product_id })
)
...variantsUpdateData.map((update) => ({ ...update, product_id }))
)
productsVariants.push(...variantsUpdateData)