Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports.publish = async (event, context, callback) => {
- var p = downloadFile(bucket, "prices/", file_prefix, ".csv", "Non-site", results);
- var c = downloadFile(bucket, "contacts/", file_prefix, "-contact.csv", "Contacts", contacts);
- var h = downloadText(bucket, "header-text/", file_prefix, "-header.txt", "Header");
- var promises = [];
- try {
- await Promise.all([p, c, h]).then(() => {
- if (results.length !== 0) {
- // Get a distinct list of groups
- var groups = [...new Set(results.map(x => x.GRP_NAME))];
- if (groups != undefined && groups.length > 0) {
- groups.forEach(grp => {
- // Get a distinct list of sold-to customers
- var customers = [...new Set(results.filter(x => (x.GRP_NAME == grp).map(x => x.KUNNR + " " + x.CUST_NAME))];
- logMessage(customers.length.toString() + ' customers will be processed for this publishing run in group ' + grp + eol);
- if (customers != undefined && customers.length > 0) {
- promises = customers.map(async (i) => {
- var customer_data = results.filter(o => o.KUNNR == i.substring(0, i.indexOf(' ')) && o.GRP_NAME.trim() == grp);
- if (customer_data != undefined && customer_data.length > 0) {
- var build = await buildPriceFile(i.substring(0, i.indexOf(' ')),
- i.substring(i.indexOf(' ') + 1),
- file_prefix,
- customer_data,
- grp);
- counter += 1;
- var sent = await sendEmail(build,
- file_prefix,
- i.substring(i.indexOf(' ') + 1),
- counter);
- };
- });
- };
- });
- groups = [];
- };
- };
- });
- await Promise.all(promises).then(() => {
- logMessage(counter.toString() + ' of emails were sent.');
- logMessage('Emails successfully sent.');
- sendSNS(logger);
- callback(null, 'Emails successfully sent.');
- });
- } catch(e) {
- errHandler(e);
- logMessage(e.toString());
- sendSNS(logger);
- callback(e);
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement