diff --git a/server/data/chatter.js b/server/data/chatter.js index 056e5ba22..ea8851e94 100644 --- a/server/data/chatter.js +++ b/server/data/chatter.js @@ -37,12 +37,14 @@ exports.default = async (req, res) => { res.sendStatus(401); return; } - try { - //Query for the List of Bodyshop Clients. - logger.log("chatter-start", "DEBUG", "api", null, null); - const { bodyshops } = await client.request(queries.GET_CHATTER_SHOPS); - const specificShopIds = req.body.bodyshopIds; // ['uuid]; + // Send immediate response and continue processing. + res.status(200).send(); + + try { + logger.log("chatter-start", "DEBUG", "api", null, null); + const { bodyshops } = await client.request(queries.GET_CHATTER_SHOPS); //Query for the List of Bodyshop Clients. + const specificShopIds = req.body.bodyshopIds; // ['uuid]; const { start, end, skipUpload } = req.body; //YYYY-MM-DD const batchSize = 10; @@ -53,26 +55,28 @@ exports.default = async (req, res) => { if (shopsToProcess.length === 0) { logger.log("chatter-shopsToProcess-empty", "DEBUG", "api", null, null); - res.sendStatus(200); return; } + const batchPromises = []; for (let i = 0; i < shopsToProcess.length; i += batchSize) { const batch = shopsToProcess.slice(i, i + batchSize); - await processBatch(batch, start, end); - - if (skipUpload) { - for (const csvObj of allcsvsToUpload) { - fs.writeFile(`./logs/${csvObj.filename}`, csvObj.csv); + const batchPromise = (async () => { + await processBatch(batch, start, end); + if (skipUpload) { + for (const csvObj of allcsvsToUpload) { + await fs.promises.writeFile(`./logs/${csvObj.filename}`, csvObj.csv); + } + } else { + await uploadViaSFTP(allcsvsToUpload); } - } else { - await uploadViaSFTP(allcsvsToUpload); - } + })(); + batchPromises.push(batchPromise); } - sendServerEmail({ + await Promise.all(batchPromises); + await sendServerEmail({ subject: `Chatter Report ${moment().format("MM-DD-YY")}`, - text: `Errors: ${allErrors.map((e) => JSON.stringify(e, null, 2))} - Uploaded: ${JSON.stringify( + text: `Errors:\n${JSON.stringify(allErrors, null, 2)}\n\nUploaded:\n${JSON.stringify( allcsvsToUpload.map((x) => ({ filename: x.filename, count: x.count, result: x.result })), null, 2 @@ -80,10 +84,8 @@ exports.default = async (req, res) => { }); logger.log("chatter-end", "DEBUG", "api", null, null); - res.sendStatus(200); } catch (error) { - logger.log("chatter-shopsToProcess-error", "ERROR", "api", null, { error: error.message, stack: error.stack }); - res.status(500).json({ error: error.message, stack: error.stack }); + logger.log("chatter-error", "ERROR", "api", null, { error: error.message, stack: error.stack }); } }; @@ -152,9 +154,7 @@ async function getPrivateKey() { try { const { SecretString, SecretBinary } = await client.send(command); if (SecretString || SecretBinary) logger.log("chatter-retrieved-private-key", "DEBUG", "api", null, null); - const chatterPrivateKey = SecretString - ? SecretString - : Buffer.from(SecretBinary, "base64").toString("ascii"); + const chatterPrivateKey = SecretString ? SecretString : Buffer.from(SecretBinary, "base64").toString("ascii"); return chatterPrivateKey; } catch (error) { logger.log("chatter-get-private-key", "ERROR", "api", null, { error: error.message, stack: error.stack });