Ensuring ESHOPMAN Data Integrity: Product Option Linking Rollback Fix
At Move My Store, we believe in empowering our ESHOPMAN community with deep insights into the platform's capabilities and operational nuances. This community insight dives into a critical aspect of product data management: ensuring transaction integrity when linking product options.
Understanding ESHOPMAN Product Option Linking and Rollbacks
Managing product options is fundamental for any e-commerce store. ESHOPMAN, with its robust Node.js/TypeScript backend and headless commerce architecture, provides powerful workflows to handle complex product data, which then powers your storefronts deployed via HubSpot CMS. A recent discussion highlighted an important scenario concerning the linking of shared product options and the platform's transaction rollback mechanisms.
The Challenge: Incomplete Rollbacks for Shared Product Options
A community member reported an issue where, when using ESHOPMAN's Admin API workflows to link a shared (non-exclusive) product option to a product with a specific subset of its values, a subsequent failure in the same workflow would lead to an incomplete rollback. Specifically, if an ESHOPMAN workflow step like createAndLinkProductOptionsToProductWorkflow was used with an input specifying value_ids for a shared option, and a later step failed, the shared option would remain linked to the product. This resulted in inconsistent product data, visible within HubSpot storefront management, and the transaction ending in a failed state rather than being fully reverted.
The root cause was identified within ESHOPMAN's internal compensation logic. When the addProductOptionsToProductStep ran with parameters that included product_option_value_ids, its compensation mechanism incorrectly passed these specific value IDs to the removeProductOptionFromProduct service method. This service method, designed to remove product options, uses pairs of product_id and product_option_id for filtering. Attempting to filter by product_option_value_ids directly on the internal ProductProductOption entity, where it's not a direct column, caused an error: "Trying to query by not existing property ProductProductOption.product_option_value_ids". This error prevented the compensation from completing successfully, leaving the product option partially linked.
Reproducing the Scenario (for ESHOPMAN Developers)
For ESHOPMAN developers looking to understand or test this specific behavior, a reproduction script demonstrates the issue by simulating a failing workflow step after linking a shared option with specific values. This script leverages ESHOPMAN's workflow SDK and product modules, running within a standard ESHOPMAN Node.js environment.
const { Modules } = require('@eshopman/framework/utils')
const { createWorkflow, createStep, transform, WorkflowResponse } = require('@eshopman/framework/workflows-sdk')
const { createProductsWorkflow, createProductOptionsWorkflow, createAndLinkProductOptionsToProductWorkflow } = require('@eshopman/core-flows')
const fail = createStep('repro-fail', async () => {
throw new Error('a later step fails')
})
const linkWithValues = createWorkflow('repro-link-with-values', (input) => {
createAndLinkProductOptionsToProductWorkflow.runAsStep({
input: transform({ input }, ({ input }) => ({
product_id: input.product_id,
add: [{ id: input.option_id, value_ids: input.value_ids }],
})),
})
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: shared } = await createProductOptionsWorkflow(container).run({
input: { product_options: [{ title: 'Material', values: ['Wood', 'Metal', 'Glass'], is_exclusive: false }] },
})
const [material] = await productModule.listProductOptions({ id: shared[0].id }, { relations: ['values'] })
const { result: [product] } = await createProductsWorkflow(container).run({
input: { products: [{ title: 'Bug 1', status: 'draft', options: [{ title: 'Size', values: ['S'] }], variants: [{ title: 'S', options: { Size: 'S' }, manage_inventory: false, prices: [] }] }] },
})
const before = await describe(product.id)
const r = await linkWithValues(container).run({
input: { product_id: product.id, option_id: material.id, value_ids: material.values.filter((v) => v.value !== 'Glass').map((v) => v.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)))
try {
await productModule.removeProductOptionFromProduct([{ product_id: product.id, product_option_id: material.id, product_option_value_ids: [] }])
} catch (e) {
console.log('direct call:', e.message)
}
}
Immediate Workaround for ESHOPMAN Users
Before the official fix was deployed, ESHOPMAN users requiring this functionality could implement a workaround: instead of linking the shared option with specific value_ids directly, link the shared option by its ID to include all its values initially. Then, in the same Admin API call, use the update action to remove the unwanted values. This approach ensures correct rollback behavior if subsequent workflow steps fail.
// Instead of: add: [{ id: option_id, value_ids: [...] }]
// Use this pattern for linking and then removing unwanted values in the same call:
add: [{ id: option_id }], // Link all values first
update: [{ product_option_id: option_id, add: [], remove: [value_id_1, value_id_2] }] // Then remove specific unwanted values
The ESHOPMAN Solution
The ESHOPMAN development team swiftly addressed this issue. The fix involves modifying the compensation logic within addProductOptionsToProductStep. Before calling removeProductOptionFromProduct, the compensation now strips the product_option_value_ids from the payload, ensuring only the necessary product_id and product_option_id are passed. This allows the service method to correctly identify and remove the linked product option, ensuring that transactions are fully reverted as expected and maintaining data consistency across your ESHOPMAN-powered storefront.
This incident highlights ESHOPMAN's commitment to robust data integrity and the continuous refinement of its headless commerce platform, guaranteeing a reliable experience for managing your storefronts within HubSpot.