Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. module.exports.publish = async (event, context, callback) => {
  2. var p = downloadFile(bucket, "prices/", file_prefix, ".csv", "Non-site", results);
  3. var c = downloadFile(bucket, "contacts/", file_prefix, "-contact.csv", "Contacts", contacts);
  4. var h = downloadText(bucket, "header-text/", file_prefix, "-header.txt", "Header");
  5. var promises = [];
  6.  
  7. try {
  8. await Promise.all([p, c, h]).then(() => {
  9. if (results.length !== 0) {
  10. // Get a distinct list of groups
  11. var groups = [...new Set(results.map(x => x.GRP_NAME))];
  12. if (groups != undefined && groups.length > 0) {
  13. groups.forEach(grp => {
  14. // Get a distinct list of sold-to customers
  15. var customers = [...new Set(results.filter(x => (x.GRP_NAME == grp).map(x => x.KUNNR + " " + x.CUST_NAME))];
  16. logMessage(customers.length.toString() + ' customers will be processed for this publishing run in group ' + grp + eol);
  17. if (customers != undefined && customers.length > 0) {
  18. promises = customers.map(async (i) => {
  19. var customer_data = results.filter(o => o.KUNNR == i.substring(0, i.indexOf(' ')) && o.GRP_NAME.trim() == grp);
  20. if (customer_data != undefined && customer_data.length > 0) {
  21. var build = await buildPriceFile(i.substring(0, i.indexOf(' ')),
  22. i.substring(i.indexOf(' ') + 1),
  23. file_prefix,
  24. customer_data,
  25. grp);
  26. counter += 1;
  27. var sent = await sendEmail(build,
  28. file_prefix,
  29. i.substring(i.indexOf(' ') + 1),
  30. counter);
  31. };
  32. });
  33. };
  34. });
  35. groups = [];
  36. };
  37. };
  38. });
  39. await Promise.all(promises).then(() => {
  40. logMessage(counter.toString() + ' of emails were sent.');
  41. logMessage('Emails successfully sent.');
  42. sendSNS(logger);
  43. callback(null, 'Emails successfully sent.');
  44. });
  45. } catch(e) {
  46. errHandler(e);
  47. logMessage(e.toString());
  48. sendSNS(logger);
  49. callback(e);
  50. };
  51. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement