From bbd310b7eba5087b09b84a3a7a729361c6d617c0 Mon Sep 17 00:00:00 2001 From: Simon Dupree Date: Tue, 13 Aug 2024 19:57:42 +0200 Subject: [PATCH] fix weight field update by shopify update in ProductVariant --- .../products/use-cases/get-products.js | 77 ++++++++++--------- web/connector/productUtils.js | 36 +++++++-- .../products/controllers/shopify/products.js | 9 ++- 3 files changed, 79 insertions(+), 43 deletions(-) diff --git a/web/api-modules/products/use-cases/get-products.js b/web/api-modules/products/use-cases/get-products.js index cb8f41b..9b9991e 100644 --- a/web/api-modules/products/use-cases/get-products.js +++ b/web/api-modules/products/use-cases/get-products.js @@ -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)); diff --git a/web/connector/productUtils.js b/web/connector/productUtils.js index 282ebb3..6b8c9eb 100644 --- a/web/connector/productUtils.js +++ b/web/connector/productUtils.js @@ -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; @@ -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 @@ -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); @@ -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(); @@ -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; @@ -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, @@ -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, diff --git a/web/fdc-modules/products/controllers/shopify/products.js b/web/fdc-modules/products/controllers/shopify/products.js index 3f52854..76b1edc 100644 --- a/web/fdc-modules/products/controllers/shopify/products.js +++ b/web/fdc-modules/products/controllers/shopify/products.js @@ -33,7 +33,14 @@ const query = `query findProducts($ids: [ID!]!) { inventoryPolicy taxable inventoryQuantity - weight + inventoryItem { + measurement { + weight { + unit + value + } + } + } image { id altText