Skip to content

Commit

Permalink
fix weight field update by shopify update in ProductVariant
Browse files Browse the repository at this point in the history
  • Loading branch information
dupreesi committed Aug 13, 2024
1 parent 9bf5b72 commit bbd310b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 43 deletions.
77 changes: 42 additions & 35 deletions web/api-modules/products/use-cases/get-products.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,57 @@ const toProduct = (product) => ({
});

async function findProducts(client) {
const response = await client.request(
`{
products(first: 250) {
edges {
node {
id
title
descriptionHtml
productType
status
images(first: 10) {
edges {
node {
id
altText
src
}
const response = await client.request(`
{
products(first: 250) {
edges {
node {
id
title
descriptionHtml
productType
status
images(first: 10) {
edges {
node {
id
altText
src
}
}
variants(first: 250) {
edges {
node {
id
title
price
sku
position
inventoryPolicy
taxable
inventoryQuantity
weight
image {
id
altText
src
}
variants(first: 250) {
edges {
node {
id
title
price
sku
position
inventoryPolicy
taxable
inventoryQuantity
inventoryItem {
measurement {
weight {
unit
value
}
}
}
image {
id
altText
src
}
}
}
}
}
}
}`
);
}
}
`);

if (response.errors) {
console.error('Failed to load Products', JSON.stringify(response.errors));
Expand Down
36 changes: 29 additions & 7 deletions web/connector/productUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ const createCatalogItem = (
stockLimitation
});

async function createVariantSuppliedProduct(parentProduct, variant, enterpriseName) {
async function createVariantSuppliedProduct(
parentProduct,
variant,
enterpriseName
) {
try {
const connector = await loadConnectorWithResources();
const kilogram = connector.MEASURES.UNIT.QUANTITYUNIT.KILOGRAM;
Expand All @@ -47,7 +51,7 @@ async function createVariantSuppliedProduct(parentProduct, variant, enterpriseNa

const quantity = createQuantitativeValue(
connector,
variant.weight,
variant?.inventoryItem?.measurement?.weight?.value || 0,
kilogram
);
const hasVat = variant.taxable ? 1.0 : 0.0; // TODO check how the vat rate can be added
Expand Down Expand Up @@ -75,7 +79,7 @@ async function createVariantSuppliedProduct(parentProduct, variant, enterpriseNa
productType: productTypes[parentProduct.productType] ?? null
});

const image = variant.image?.src || parentProduct.images[0]?.src
const image = variant.image?.src || parentProduct.images[0]?.src;

if (image) {
suppliedProduct.addImage(image);
Expand All @@ -98,7 +102,9 @@ async function createSuppliedProducts(productsFromShopify, enterpriseName) {
}

const productsPromises = productsFromShopify.flatMap((product) =>
product.fdcVariants.filter(({enabled}) => enabled).map(variant => createVariants(product, variant, enterpriseName))
product.fdcVariants
.filter(({ enabled }) => enabled)
.map((variant) => createVariants(product, variant, enterpriseName))
);

return (await Promise.all(productsPromises)).flat();
Expand All @@ -107,7 +113,11 @@ async function createSuppliedProducts(productsFromShopify, enterpriseName) {
}
}

const createVariants = async (shopifyProduct, variantMapping, enterpriseName) => {
const createVariants = async (
shopifyProduct,
variantMapping,
enterpriseName
) => {
const { wholesaleVariantId, retailVariantId, noOfItemsPerPackage } =
variantMapping;

Expand All @@ -126,7 +136,13 @@ const createVariants = async (shopifyProduct, variantMapping, enterpriseName) =>
const wholesaleVariant = shopifyProduct.variants.find(
({ id }) => id == wholesaleVariantId
);
return await createMappedVariant(shopifyProduct, retailVariant, wholesaleVariant, noOfItemsPerPackage, enterpriseName);
return await createMappedVariant(
shopifyProduct,
retailVariant,
wholesaleVariant,
noOfItemsPerPackage,
enterpriseName
);
} else {
return await createVariantSuppliedProduct(
shopifyProduct,
Expand All @@ -136,7 +152,13 @@ const createVariants = async (shopifyProduct, variantMapping, enterpriseName) =>
}
};

async function createMappedVariant(shopifyProduct, retailVariant, wholesaleVariant, noOfItemsPerPackage, enterpriseName) {
async function createMappedVariant(
shopifyProduct,
retailVariant,
wholesaleVariant,
noOfItemsPerPackage,
enterpriseName
) {
const [retailSuppliedProduct, ...retailOthers] =
await createVariantSuppliedProduct(
shopifyProduct,
Expand Down
9 changes: 8 additions & 1 deletion web/fdc-modules/products/controllers/shopify/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ const query = `query findProducts($ids: [ID!]!) {
inventoryPolicy
taxable
inventoryQuantity
weight
inventoryItem {
measurement {
weight {
unit
value
}
}
}
image {
id
altText
Expand Down

0 comments on commit bbd310b

Please sign in to comment.