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
2024-03-29 10:03:41 +01:00
committed by GitHub
parent e603726985
commit cbb5e6bd99
35 changed files with 1318 additions and 1374 deletions

View File

@@ -11,6 +11,10 @@ interface Input {
export function prepareLineItemData(data: Input) {
const { variant, unitPrice, quantity, metadata, cartId } = data
if (!variant.product) {
throw new Error("Variant does not have a product")
}
const lineItem: any = {
quantity,
title: variant.title,

View File

@@ -75,13 +75,13 @@ export async function updateProductVariantsPrepareData({
}
const variantsData: ProductWorkflow.UpdateProductVariantsInputDTO[] =
productVariantsMap.get(variantWithProductID.product_id) || []
productVariantsMap.get(variantWithProductID.product_id!) || []
if (variantData) {
variantsData.push(variantData)
}
productVariantsMap.set(variantWithProductID.product_id, variantsData)
productVariantsMap.set(variantWithProductID.product_id!, variantsData)
}
return {

View File

@@ -40,6 +40,13 @@ export const updateProductOptionsStep = createStep(
ModuleRegistrationName.PRODUCT
)
await service.upsertOptions(prevData)
await service.upsertOptions(
prevData.map((o) => ({
...o,
values: o.values?.map((v) => v.value),
product: undefined,
product_id: o.product_id ?? undefined,
}))
)
}
)

View File

@@ -36,7 +36,7 @@ export const createProductsWorkflow = createWorkflow(
const createdProducts = createProductsStep(productWithoutPrices)
// Note: We rely on the same order of input and output when creating products here, make sure that assumption holds
// Note: We rely on the same order of input and output when creating products here, ensure this always holds true
const variantsWithAssociatedPrices = transform(
{ input, createdProducts },
(data) => {