Skip to content

Instantly share code, notes, and snippets.

@fedulovivan
Created November 13, 2018 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedulovivan/421f5af8e9e1721507f4e1ada696245e to your computer and use it in GitHub Desktop.
Save fedulovivan/421f5af8e9e1721507f4e1ada696245e to your computer and use it in GitHub Desktop.
async function(req, res, next) {
const ACTIVATION = 1;
const EXPIRATION = 2;
const OFFER = 3;
const CHARGE = 4;
const EXTERNAL = 5;
const getAutoGroup = ({ groupId, name }) => {
if (groupId) return groupId;
const groupMap = new Map([
[/activation/i, ACTIVATION],
[/expiration/i, EXPIRATION],
[/offer/i, OFFER],
[/charg/i, CHARGE],
[/external/i, EXTERNAL],
]);
const foundMapEntry = Array.from(
groupMap.entries()
).find(([regex]) => (
typeof name === 'string' && regex.test(name)
));
if (foundMapEntry) {
return foundMapEntry[1];
}
};
try {
const rows = await ProductOfferingVersionAttributeType.find();
res.json(map(rows, row => ({
...row,
groupId: getAutoGroup(row)
})));
} catch (e) {
next(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment