Created
November 13, 2018 13:49
-
-
Save fedulovivan/421f5af8e9e1721507f4e1ada696245e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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