Skip to content

Commit

Permalink
perf: products similarity group config filter by any field (#5054)
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhsaikhan committed Mar 11, 2024
1 parent 6061faa commit c08436a
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 129 deletions.
Expand Up @@ -392,14 +392,34 @@ const productQueries = {
(cf) => cf.field,
);

const matchedMasks = codeMasks.filter(
(cm) =>
product.code.match(getRegex(cm)) &&
const matchedMasks = codeMasks.filter((cm) => {
const mask = similarityGroups[cm];
const filterFieldDef = mask.filterField || 'code';
const regexer = getRegex(cm);

if (filterFieldDef.includes('customFieldsData.')) {
if (
!(product.customFieldsData || []).find(
(cfd) =>
cfd.field === filterFieldDef.replace('customFieldsData.', '') &&
cfd.stringValue?.match(regexer),
)
) {
return false;
}
} else {
if (!product[filterFieldDef].match(regexer)) {
return false;
}
}

return (
(similarityGroups[cm].rules || [])
.map((sg) => sg.fieldId)
.filter((sgf) => customFieldIds.includes(sgf)).length ===
(similarityGroups[cm].rules || []).length,
);
(similarityGroups[cm].rules || []).length
);
});

if (!matchedMasks.length) {
return {
Expand All @@ -411,7 +431,30 @@ const productQueries = {
const fieldIds: string[] = [];
const groups: { title: string; fieldId: string }[] = [];
for (const matchedMask of matchedMasks) {
codeRegexs.push({ code: { $in: [getRegex(matchedMask)] } });
const matched = similarityGroups[matchedMask];
const filterFieldDef = matched.filterField || 'code';

if (filterFieldDef.includes('customFieldsData.')) {
codeRegexs.push({
$and: [
{
'customFieldsData.field': filterFieldDef.replace(
'customFieldsData.',
'',
),
},
{
'customFieldsData.stringValue': {
$in: [getRegex(matchedMask)],
},
},
],
});
} else {
codeRegexs.push({
[filterFieldDef]: { $in: [getRegex(matchedMask)] },
});
}

for (const rule of similarityGroups[matchedMask].rules || []) {
const { fieldId, title } = rule;
Expand All @@ -426,13 +469,21 @@ const productQueries = {
$and: [
{
$or: codeRegexs,
},
{
'customFieldsData.field': { $in: fieldIds },
},
],
};

let products = await models.Products.find(filters)
.sort({ code: 1 })
.lean();
if (!products.length) {
products = [product];
}
return {
products: await models.Products.find(filters).sort({ code: 1 }),
products,
groups,
};
}
Expand Down
Expand Up @@ -5,7 +5,11 @@ const configMutations = {
/**
* Create or update config object
*/
async productsConfigsUpdate(_root, { configsMap }, { models }: IContext) {
async productsConfigsUpdate(
_root,
{ configsMap },
{ models }: IContext,
) {
const codes = Object.keys(configsMap);

for (const code of codes) {
Expand All @@ -20,23 +24,39 @@ const configMutations = {

if (code === 'similarityGroup') {
const masks = Object.keys(value);

await models.Products.updateMany({}, { $unset: { sameMasks: '' } });
for (const mask of masks) {
const maskValue = value[mask];

const codeRegex = new RegExp(
`^${mask
.replace(/\./g, '\\.')
.replace(/\*/g, '.')
.replace(/_/g, '.')}.*`,
'igu'
'igu',
);
const fieldFilter = (maskValue.filterField || 'code').includes(
'customFieldsData.',
)
? {
'customFieldsData.field': maskValue.filterField.replace(
'customFieldsData.',
'',
),
'customFieldsData.stringValue': { $in: [codeRegex] },
}
: { [maskValue.filterField || 'code']: { $in: [codeRegex] } };

const fieldIds = (value[mask].rules || []).map(r => r.fieldId);
const fieldIds = (maskValue.rules || []).map((r) => r.fieldId);
await models.Products.updateMany(
{
code: { $in: [codeRegex] },
'customFieldsData.field': { $in: fieldIds }
$and: [
{ ...fieldFilter },
{ 'customFieldsData.field': { $in: fieldIds } },
],
},
{ $addToSet: { sameMasks: mask } }
{ $addToSet: { sameMasks: mask } },
);
}
}
Expand All @@ -55,13 +75,13 @@ const configMutations = {
if (isRequireUOM && defaultUOM) {
await models.Products.updateMany(
{
$or: [{ uom: { $exists: false } }, { uom: '' }]
$or: [{ uom: { $exists: false } }, { uom: '' }],
},
{ $set: { uom: defaultUOM } }
{ $set: { uom: defaultUOM } },
);
}
return ['success'];
}
},
};

checkPermission(configMutations, 'productsConfigsUpdate', 'manageProducts');
Expand Down
Expand Up @@ -94,16 +94,13 @@ const generateFilter = async (
if (searchValue) {
const regex = new RegExp(`.*${escapeRegExp(searchValue)}.*`, 'i');
const codeRegex = new RegExp(
`^${searchValue
.replace(/\./g, '\\.')
.replace(/\*/g, '.')
.replace(/_/g, '.')}.*`,
`^${searchValue.replace(/\*/g, '.').replace(/_/g, '.')}$`,
'igu',
);

filter.$or = [
{
$or: [{ code: { $in: [regex] } }, { code: { $in: [codeRegex] } }],
$or: [{ code: { $in: [codeRegex] } }],
},
{ name: { $in: [regex] } },
{ barcodes: { $in: [searchValue] } },
Expand Down

0 comments on commit c08436a

Please sign in to comment.