Guest User

Untitled

a guest
Nov 13th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. async function(req, res, next) {
  2.  
  3. const ACTIVATION = 1;
  4. const EXPIRATION = 2;
  5. const OFFER = 3;
  6. const CHARGE = 4;
  7. const EXTERNAL = 5;
  8.  
  9. const getAutoGroup = ({ groupId, name }) => {
  10. if (groupId) return groupId;
  11. const groupMap = new Map([
  12. [/activation/i, ACTIVATION],
  13. [/expiration/i, EXPIRATION],
  14. [/offer/i, OFFER],
  15. [/charg/i, CHARGE],
  16. [/external/i, EXTERNAL],
  17. ]);
  18. const foundMapEntry = Array.from(
  19. groupMap.entries()
  20. ).find(([regex]) => (
  21. typeof name === 'string' && regex.test(name)
  22. ));
  23. if (foundMapEntry) {
  24. return foundMapEntry[1];
  25. }
  26. };
  27.  
  28. try {
  29. const rows = await ProductOfferingVersionAttributeType.find();
  30. res.json(map(rows, row => ({
  31. ...row,
  32. groupId: getAutoGroup(row)
  33. })));
  34. } catch (e) {
  35. next(e);
  36. }
  37.  
  38. }
Add Comment
Please, Sign In to add comment