So I am creating an API endpoint where I send in two different types of data from the UI. One is a string and the other is an array containing upto 5 items. I need to compare the results across the queries and get data as the following.
This is a sample schema data for the above example: link
This is my implementation:
router.post('/compareRoles/', async (request, response, next) => {
const srcRoleName = request.body.srcRole // 'ENGINEER';
const tgtRoleNames = request.body.tgtRoles // ['ARCHITECT', 'TESTER'] array can have atleast one element and a maximum length of 5 chosen via a multi-select dropdown;
const querySrcRoleName = `select * from table roles where role_name = '${srcRoleName}'`;
const tgtRoleQueries = [];
_.forEach(tgtRoleNames, (item, index) => {
this['tgtQuery' + index] = `select * from table roles where role_name = '${item}'`;
tgtRoleQueries.push(this['tgtQuery' + index])
});
let result = {};
try {
const results = await Promise.all([compareRoles()]);
result = Object.assign({}, result, ...results);
} catch (err) {
result.error = err.message;
}
response.json(result);
async function compareRoles() {
const srcRoleValues = await pool.query(queryJobRoleName);
let tgtRoleValues0, tgtRoleValues1, tgtRoleValues2, tgtRoleValues3, tgtRoleValues4;
if (tgtRoleQueries[0]) {
tgtRoleValues0 = await pool.query(tgtRoleQueries[0]);
}
if (tgtRoleQueries[1]) {
tgtRoleValues1 = await pool.query(tgtRoleQueries[1]);
}
if (tgtRoleQueries[2]) {
tgtRoleValues2 = await pool.query(tgtRoleQueries[2]);
}
if (tgtRoleQueries[3]) {
tgtRoleValues3 = await pool.query(tgtRoleQueries[3]);
}
if (tgtRoleQueries[4]) {
tgtRoleValues4 = await pool.query(tgtRoleQueries[4]);
}
let compareRoles = _.uniqWith(_.concat(srcRoleValues.rows,
tgtRoleQueries[0] ? tgtRoleValues0.rows : [],
tgtRoleQueries[1] ? tgtRoleValues1.rows : [],
tgtRoleQueries[2] ? tgtRoleValues2.rows : [],
tgtRoleQueries[3] ? tgtRoleValues3.rows : [],
tgtRoleQueries[4] ? tgtRoleValues4.rows : []),
_.isEqual);
compareRoles = _.map(compareRoles, function (obj, index) {
const comparedObj = {
role_name: obj.role_name,
role_id: obj.role_id,
presentInSrc: _.find(
srcRoleValues.rows, (o) => o.role_name === obj.role_name && o.role_id === obj.role_id
) ? "true" : "false",
};
if (tgtRoleQueries[0]) {
comparedObj['presentInTgt0'] = _.find(
tgtRoleQueries[0].rows, (o) => o.role_name === obj.role_name && o.role_id === obj.role_id
) ? "true" : "false"
}
if (tgtRoleQueries[1]) {
comparedObj['presentInTgt1'] = _.find(
tgtRoleQueries[1].rows, (o) => o.role_name === obj.role_name && o.role_id === obj.role_id
) ? "true" : "false"
}
if (tgtRoleQueries[2]) {
comparedObj['presentInTgt2'] = _.find(
tgtRoleQueries[2].rows, (o) => o.role_name === obj.role_name && o.role_id === obj.role_id
) ? "true" : "false"
}
if (tgtRoleQueries[3]) {
comparedObj['presentInTgt3'] = _.find(
tgtRoleQueries[3].rows, (o) => o.role_name === obj.role_name && o.role_id === obj.role_id
) ? "true" : "false"
}
if (tgtRoleQueries[4]) {
comparedObj['presentInTgt4'] = _.find(
tgtRoleQueries[4].rows, (o) => o.bus_nm === obj.bus_nm && o.role_id === obj.role_id && o.rbf_rating === obj.rbf_rating && o.prvlg_grp === obj.prvlg_grp
) ? "true" : "false"
}
return comparedObj;
});
return {rows: compareRoles};
}
});
Is there a way I can simplify the way I compare data and mark them as present or not?