Maintaining Product Data Consistency: Addressing Variant Option Rollback Issues in ESHOPMAN

Ensuring Product Variant Data Integrity During Option Management in ESHOPMAN

At Move My Store, we understand that maintaining impeccable product data integrity is crucial for any successful headless commerce operation, especially when managing your storefront through ESHOPMAN's HubSpot application. ESHOPMAN, built on Node.js/TypeScript and leveraging HubSpot CMS for storefront deployment, relies on robust data management for its Admin API and Store API.

Recently, our community identified a specific scenario where product data consistency can be compromised during the unassignment of product options from variants. This insight delves into a critical bug related to workflow rollbacks that can leave your ESHOPMAN product variants in an inconsistent state, despite a transaction reporting a 'reverted' status.

The Challenge: Inconsistent Variant Data After Workflow Rollback

The issue arises when an option is unassigned from an ESHOPMAN product that has existing variants. The process involves a workflow (specifically, the createAndLinkProductOptionsToProductWorkflow with the remove action, which executes removeProductOptionsFromProductStep). During this removal, the detachOptionFromVariants_ step correctly strips the option's values from the associated variants.

However, if a subsequent step within the same workflow fails, the compensation mechanism is triggered. While the compensation successfully restores the soft-deleted option and its link to the product, it critically fails to re-attach the previously detached option values to the variants. This leaves the product with an option that none of its variants now hold a value for, creating a discrepancy that can impact your storefront display on HubSpot CMS or data retrieved via the Store API.

For instance, if you unassign a 'Color' option from a product with 'Red/S' and 'Red/M' variants, and the workflow rolls back, the 'Color' option is restored to the product, but the variants might appear as 'S' and 'M' without their 'Red' color value.

Technical Breakdown and Reproduction

The core of the problem lies in the compensation logic within the ESHOPMAN core flows. While restoreProductOptions and addProductOptionToProduct are called to re-establish the option and its product link, there's no corresponding step to re-associate the variant values. This behavior has been observed in ESHOPMAN versions 2.22.2 and 2.22.7.

Here's a reproduction script that demonstrates this behavior using the ESHOPMAN CLI:

Package.json file

{
"dependencies": {
"@eshopman/framework": "2.22.2",
"@eshopman/core": "2.22.2"
}
}

Node.js version

v24.18.0

Database and its version

PostgreSQL 17.10

Operating system name and version

macOS 27.0

Reproduction Script (repro.js)

const { Modules } = require('@eshopman/framework/utils')
const { createWorkflow, createStep, transform, WorkflowResponse } = require('@eshopman/framework/workflows-sdk')
const { createProductsWorkflow, createAndLinkProductOptionsToProductWorkflow } = require('@eshopman/core-flows')

const fail = createStep('repro-fail', async () => {
throw new Error('a later step fails')
})

const unassign = createWorkflow('repro-unassign', (input) => {
createAndLinkProductOptionsToProductWorkflow.runAsStep({
input: transform({ input }, ({ input }) => ({ product_id: input.product_id, remove: [input.option_id] })),
})
fail()
return new WorkflowResponse(undefined)
})

exports.default = async function repro({ container }) {
const productModule = container.resolve(Modules.PRODUCT)
const describe = async (id) => {
const p = await productModule.retrieveProduct(id, { relations: ['options', 'variants', 'variants.options'] })
return {
options: p.options.map((o) => `${o.title} (${o.is_exclusive ? 'exclusive' : 'shared'})`).sort(),
variants: p.variants.map((v) => v.options.map((o) => o.value).join('/')).sort(),
}
}
const { result: [product] } = await createProductsWorkflow(container).run({
input: {
products: [{
title: 'Bug 2', status: 'draft',
options: [{ title: 'Color', values: ['Red'] }, { title: 'Size', values: ['S', 'M'] }],
variants: [
{ title: 'Red / S', options: { Color: 'Red', Size: 'S' }, manage_inventory: false, prices: [] },
{ title: 'Red / M', options: { Color: 'Red', Size: 'M' }, manage_inventory: false, prices: [] },
],
}],
},
})
const color = (await productModule.retrieveProduct(product.id, { relations: ['options'] })).options.find((o) => o.title === 'Color')
const before = await describe(product.id)
const r = await unassign(container).run({ input: { product_id: product.id, option_id: color.id }, throwOnError: false })
console.log('state:', r.transaction.getState())
console.log('errors:', r.transaction.getErrors().map((e) => `${e.action} [${e.handlerType}]: ${e.error?.message ?? e.error}`))
console.log('before:', JSON.stringify(before))
console.log('after: ', JSON.stringify(await describe(product.id)))
}

To run this, save it as repro.js in a fresh ESHOPMAN project (version 2.22.2 or 2.22.7) and execute npx eshopman exec ./repro.js.

Proposed Solution or Workaround

A potential fix involves enhancing the compensation logic. Before an option's values are stripped from variants (i.e., before detachOptionFromVariants_), the system should capture the variant/value pairs for the options being removed. In the event of a workflow rollback, this captured data could then be used during the compensation phase to correctly re-attach the values to the variants after the option itself has been restored.

This would ensure that after a rollback, the ESHOPMAN product variants correctly reflect their original state, maintaining data integrity across your headless commerce platform and ensuring a seamless experience for your HubSpot-powered storefront.

Conclusion

Understanding and addressing such nuances in workflow management is vital for ESHOPMAN developers. By focusing on robust data integrity, especially in complex operations involving product options and variants, we ensure that your ESHOPMAN platform provides a reliable foundation for your headless commerce strategy and HubSpot integration. Stay tuned to Move My Store for more ESHOPMAN community insights and best practices!

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools