Skip to content

Commit

Permalink
feat: syncerkhet sync products with weight
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhsaikhan committed Jan 23, 2024
1 parent 8b061e1 commit d86acf3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 31 deletions.
9 changes: 9 additions & 0 deletions packages/plugin-syncerkhet-api/src/messageBroker.ts
Expand Up @@ -209,6 +209,15 @@ export const sendProductsMessage = async (
});
};

export const sendFormsMessage = async (
args: MessageArgsOmitService,
): Promise<any> => {
return sendMessage({
serviceName: 'forms',
...args,
});
};

export const sendContactsMessage = async (
args: MessageArgsOmitService,
): Promise<any> => {
Expand Down
71 changes: 49 additions & 22 deletions 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) => {
Expand All @@ -7,15 +7,15 @@ 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') {
const productCategory = await sendProductsMessage({
subdomain,
action: 'categories.findOne',
data: { code: doc.category_code },
isRPC: true
isRPC: true,
});

const config = await getConfig(subdomain, 'ERKHET', {});
Expand All @@ -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;
Expand All @@ -59,22 +86,22 @@ 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) {
await sendProductsMessage({
subdomain,
action: 'removeProducts',
data: { _ids: [product._id] },
isRPC: true
isRPC: true,
});
}
};
Expand All @@ -83,27 +110,27 @@ 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') {
const parentCategory = await sendProductsMessage({
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) {
Expand All @@ -116,10 +143,10 @@ export const consumeInventoryCategory = async (
...document,
parentId: parentCategory
? parentCategory._id
: productCategory.parentId
}
: productCategory.parentId,
},
},
isRPC: true
isRPC: true,
});
} else {
await sendProductsMessage({
Expand All @@ -128,20 +155,20 @@ export const consumeInventoryCategory = async (
data: {
doc: {
...document,
parentId: parentCategory ? parentCategory._id : ''
}
parentId: parentCategory ? parentCategory._id : '',
},
},
isRPC: true
isRPC: true,
});
}
} else if (action === 'delete' && productCategory) {
await sendProductsMessage({
subdomain,
action: 'categories.removeProductCategory',
data: {
_id: productCategory._id
_id: productCategory._id,
},
isRPC: true
isRPC: true,
});
}
};
39 changes: 30 additions & 9 deletions packages/plugin-syncerkhet-api/src/utils/productToErkhet.ts
@@ -1,12 +1,12 @@
import { sendProductsMessage } from '../messageBroker';
import { sendFormsMessage, sendProductsMessage } from '../messageBroker';
import { getConfig, toErkhet } from './utils';

export const productCategoryToErkhet = async (
subdomain,
models,
syncLog,
params,
action
action,
) => {
const productCategory = params.updatedDocument || params.object;
const oldProductCategory = params.object;
Expand All @@ -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', {});
Expand All @@ -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');
Expand All @@ -38,7 +38,7 @@ export const productToErkhet = async (
models,
syncLog,
params,
action
action,
) => {
const product = params.updatedDocument || params.object;
const oldProduct = params.object;
Expand All @@ -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;

Expand All @@ -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');
Expand Down

0 comments on commit d86acf3

Please sign in to comment.