Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions transformers/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ const transformCustomAttributes = attributes => {
* @param {string | number} variantValue - The variant value
* @returns {string} The variant reference ID
*/
function buildVariantReferenceId(masterSku, variantCode, variantValue) {
const buildVariantReferenceId = (masterSku, variantCode, variantValue) => {
return `${masterSku}-${variantCode}-${variantValue.toString()}`;
}
};

/**
* Transforms variationAttributes and masterSku to configurations format
Expand All @@ -92,7 +92,7 @@ function buildVariantReferenceId(masterSku, variantCode, variantValue) {
* @param {Array} variationAttributes - The variation attributes array
* @returns {Array} Array of configuration objects
*/
function transformVariantAttributes(masterSku, variationAttributes) {
const transformVariantAttributes = (masterSku, variationAttributes) => {
if (!Array.isArray(variationAttributes)) return [];
return variationAttributes.map(attr => ({
attributeCode: attr.id,
Expand All @@ -105,7 +105,7 @@ function transformVariantAttributes(masterSku, variationAttributes) {
}))
: [],
}));
}
};

/**
* Transforms variationValues to ACO attribute format
Expand All @@ -114,14 +114,37 @@ function transformVariantAttributes(masterSku, variationAttributes) {
* @param {object} variationValues - The variation values object
* @returns {Array} Array of ACO attribute objects
*/
function transformVariantValues(masterSku, variationValues) {
const transformVariantValues = (masterSku, variationValues) => {
if (!variationValues || typeof variationValues !== 'object') return [];
return Object.entries(variationValues).map(([key, value]) => ({
code: key,
values: [value.toString()],
variantReferenceId: buildVariantReferenceId(masterSku, key, value),
}));
}
};

/**
* Builds the bundles array for an ACO parent bundle product.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the comment for transformProduct was removed during this update. Can you add it back?

*
* @param {{ id: string; name: string; quantity: number }[]} bundledProducts - Array of bundled product objects
* @returns {object[]} Array of bundle group objects for ACO
*/
const buildAcoBundles = bundledProducts => {
if (!Array.isArray(bundledProducts) || bundledProducts.length === 0) return [];
return bundledProducts.map(bp => ({
group: bp.name,
required: true,
multiSelect: false,
defaultItemSkus: [bp.id],
items: [
{
sku: bp.id,
qty: bp.quantity,
userDefinedQty: false,
},
],
}));
};

/**
* Transforms a Salesforce product to an ACO product.
Expand Down Expand Up @@ -166,7 +189,6 @@ const transformProduct = product => {
...transformCustomAttributes(product.customAttributes),
],
images: transformImages(product.images),
// TODO: Add bundle product support
};

//add configurations
Expand All @@ -182,11 +204,23 @@ const transformProduct = product => {
sku: masterSku,
},
];

//attributes
acoProduct.attributes.push(...transformVariantValues(masterSku, product.variationValues));
}

// Handle parent bundle product (type BUNDLE) using correct bundles array structure
if (product.type === 'BUNDLE' && product.bundledProducts?.length) {
acoProduct.bundles = buildAcoBundles(product.bundledProducts);
}

// Handle bundled products (child) for BUNDLED type
if (product.type === 'BUNDLE' && product.bundles?.length) {
acoProduct.links = product.bundles.map(bundleSku => ({
type: 'in_bundle',
sku: bundleSku,
}));
}

// @ts-ignore
return acoProduct;
};
Expand Down
8 changes: 8 additions & 0 deletions types/salesforce.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ declare interface SalesforceProduct {
variants?: SalesforceProductVariant[];
variationValues?: SalesforceProductVariationValues;
master?: SalesforceProductMaster;
bundles?: string[]; // Array of bundled product IDs for BUNDLE type
bundledProducts?: BundledProduct[]; // Array of bundled products with details for a parent bundle product
}

declare interface SalesforceProductPrice {
Expand Down Expand Up @@ -164,3 +166,9 @@ declare interface SalesforceProductVariationValues {
declare interface SalesforceProductMaster {
id: string;
}

declare interface BundledProduct {
id: string;
name: string;
quantity: number;
}