diff --git a/packages/plugin-syncerkhet-api/src/messageBroker.ts b/packages/plugin-syncerkhet-api/src/messageBroker.ts index 197e11fc9e2..b0dafb315c1 100644 --- a/packages/plugin-syncerkhet-api/src/messageBroker.ts +++ b/packages/plugin-syncerkhet-api/src/messageBroker.ts @@ -209,6 +209,15 @@ export const sendProductsMessage = async ( }); }; +export const sendFormsMessage = async ( + args: MessageArgsOmitService, +): Promise => { + return sendMessage({ + serviceName: 'forms', + ...args, + }); +}; + export const sendContactsMessage = async ( args: MessageArgsOmitService, ): Promise => { diff --git a/packages/plugin-syncerkhet-api/src/utils/consumeInventory.ts b/packages/plugin-syncerkhet-api/src/utils/consumeInventory.ts index e8623c312a3..f5042c4a2c7 100644 --- a/packages/plugin-syncerkhet-api/src/utils/consumeInventory.ts +++ b/packages/plugin-syncerkhet-api/src/utils/consumeInventory.ts @@ -1,4 +1,4 @@ -import { sendProductsMessage } from '../messageBroker'; +import { sendFormsMessage, sendProductsMessage } from '../messageBroker'; import { getConfig } from './utils'; export const consumeInventory = async (subdomain, doc, old_code, action) => { @@ -7,7 +7,7 @@ export const consumeInventory = async (subdomain, doc, old_code, action) => { action: 'findOne', data: { code: old_code }, isRPC: true, - defaultValue: {} + defaultValue: {}, }); if ((action === 'update' && old_code) || action === 'create') { @@ -15,7 +15,7 @@ export const consumeInventory = async (subdomain, doc, old_code, action) => { subdomain, action: 'categories.findOne', data: { code: doc.category_code }, - isRPC: true + isRPC: true, }); const config = await getConfig(subdomain, 'ERKHET', {}); @@ -36,19 +36,46 @@ export const consumeInventory = async (subdomain, doc, old_code, action) => { description: eval('`' + config.consumeDescription + '`'), status: 'active', taxType: doc.vat_type || '', - taxCode: doc.vat_type_code || '' + taxCode: doc.vat_type_code || '', }; + const weightField = await sendFormsMessage({ + subdomain, + action: 'fields.findOne', + data: { query: { code: 'weight' } }, + isRPC: true, + defaultValue: null, + }); + + if (weightField && weightField._id) { + const weightData = { + field: weightField._id, + value: doc.weight, + stringValue: doc.weight.toString(), + numberValue: Number(doc.weight), + }; + + if (product && product.customFieldsData) { + const otherFieldsData = (product.customFieldsData || []).filter( + (cfd) => cfd.field !== weightField._id, + ); + otherFieldsData.push(weightData); + document.customFieldsData = otherFieldsData; + } else { + document.customFieldsData = [weightData]; + } + } + if (doc.sub_measure_unit_code && doc.ratio_measure_unit) { let subUoms = (product || {}).subUoms || []; - const subUomCodes = subUoms.map(u => u.uom); + const subUomCodes = subUoms.map((u) => u.uom); if (subUomCodes.includes(doc.sub_measure_unit_code)) { - subUoms = subUoms.filter(u => u.uom !== doc.sub_measure_unit_code); + subUoms = subUoms.filter((u) => u.uom !== doc.sub_measure_unit_code); } subUoms.unshift({ uom: doc.sub_measure_unit_code, - ratio: doc.ratio_measure_unit + ratio: doc.ratio_measure_unit, }); document.subUoms = subUoms; @@ -59,14 +86,14 @@ export const consumeInventory = async (subdomain, doc, old_code, action) => { subdomain, action: 'updateProduct', data: { _id: product._id, doc: { ...document } }, - isRPC: true + isRPC: true, }); } else { await sendProductsMessage({ subdomain, action: 'createProduct', data: { doc: { ...document } }, - isRPC: true + isRPC: true, }); } } else if (action === 'delete' && product) { @@ -74,7 +101,7 @@ export const consumeInventory = async (subdomain, doc, old_code, action) => { subdomain, action: 'removeProducts', data: { _ids: [product._id] }, - isRPC: true + isRPC: true, }); } }; @@ -83,13 +110,13 @@ export const consumeInventoryCategory = async ( subdomain, doc, old_code, - action + action, ) => { const productCategory = await sendProductsMessage({ subdomain, action: 'categories.findOne', data: { code: old_code }, - isRPC: true + isRPC: true, }); if ((action === 'update' && old_code) || action === 'create') { @@ -97,13 +124,13 @@ export const consumeInventoryCategory = async ( subdomain, action: 'categories.findOne', data: { code: doc.parent_code }, - isRPC: true + isRPC: true, }); const document = { code: doc.code, name: doc.name, - order: doc.order + order: doc.order, }; if (productCategory) { @@ -116,10 +143,10 @@ export const consumeInventoryCategory = async ( ...document, parentId: parentCategory ? parentCategory._id - : productCategory.parentId - } + : productCategory.parentId, + }, }, - isRPC: true + isRPC: true, }); } else { await sendProductsMessage({ @@ -128,10 +155,10 @@ export const consumeInventoryCategory = async ( data: { doc: { ...document, - parentId: parentCategory ? parentCategory._id : '' - } + parentId: parentCategory ? parentCategory._id : '', + }, }, - isRPC: true + isRPC: true, }); } } else if (action === 'delete' && productCategory) { @@ -139,9 +166,9 @@ export const consumeInventoryCategory = async ( subdomain, action: 'categories.removeProductCategory', data: { - _id: productCategory._id + _id: productCategory._id, }, - isRPC: true + isRPC: true, }); } }; diff --git a/packages/plugin-syncerkhet-api/src/utils/productToErkhet.ts b/packages/plugin-syncerkhet-api/src/utils/productToErkhet.ts index dbf3e04325a..471528a988b 100644 --- a/packages/plugin-syncerkhet-api/src/utils/productToErkhet.ts +++ b/packages/plugin-syncerkhet-api/src/utils/productToErkhet.ts @@ -1,4 +1,4 @@ -import { sendProductsMessage } from '../messageBroker'; +import { sendFormsMessage, sendProductsMessage } from '../messageBroker'; import { getConfig, toErkhet } from './utils'; export const productCategoryToErkhet = async ( @@ -6,7 +6,7 @@ export const productCategoryToErkhet = async ( models, syncLog, params, - action + action, ) => { const productCategory = params.updatedDocument || params.object; const oldProductCategory = params.object; @@ -15,7 +15,7 @@ export const productCategoryToErkhet = async ( subdomain, action: 'categories.findOne', data: { _id: productCategory.parentId }, - isRPC: true + isRPC: true, }); const config = await getConfig(subdomain, 'ERKHET', {}); @@ -26,8 +26,8 @@ export const productCategoryToErkhet = async ( object: { code: productCategory.code || '', name: productCategory.name || '', - parentCode: parentProductCategory ? parentProductCategory.code : '' - } + parentCode: parentProductCategory ? parentProductCategory.code : '', + }, }; toErkhet(models, syncLog, config, sendData, 'product-change'); @@ -38,7 +38,7 @@ export const productToErkhet = async ( models, syncLog, params, - action + action, ) => { const product = params.updatedDocument || params.object; const oldProduct = params.object; @@ -47,9 +47,29 @@ export const productToErkhet = async ( subdomain, action: 'categories.findOne', data: { _id: product.categoryId }, - isRPC: true + isRPC: true, + }); + + let weight = 1; + + const weightField = await sendFormsMessage({ + subdomain, + action: 'fields.findOne', + data: { query: { code: 'weight' } }, + isRPC: true, + defaultValue: null, }); + if (weightField && weightField._id) { + const weightData = (product.customFieldsData || []).find( + (cfd) => cfd.field === weightField._id, + ); + + if (weightData && weightData.value) { + weight = Number(weightData.value) || 1; + } + } + let subMeasureUnit; let ratioMeasureUnit; @@ -76,9 +96,10 @@ export const productToErkhet = async ( saleAccount: config.saleAccount, categoryCode: productCategory ? productCategory.code : '', defaultCategory: config.productCategoryCode, + weight, taxType: product.taxType, - taxCode: product.taxCode - } + taxCode: product.taxCode, + }, }; toErkhet(models, syncLog, config, sendData, 'product-change');