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:
@@ -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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
}))
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user