diff --git a/server/data/autohouse.js b/server/data/autohouse.js index 506ba53d3..9ea3a415a 100644 --- a/server/data/autohouse.js +++ b/server/data/autohouse.js @@ -45,12 +45,14 @@ exports.default = async (req, res) => { res.sendStatus(401); return; } - try { - //Query for the List of Bodyshop Clients. - logger.log("autohouse-start", "DEBUG", "api", null, null); - const { bodyshops } = await client.request(queries.GET_AUTOHOUSE_SHOPS); - const specificShopIds = req.body.bodyshopIds; // ['uuid]; + // Send immediate response and continue processing. + res.status(200).send(); + + try { + logger.log("autohouse-start", "DEBUG", "api", null, null); + const { bodyshops } = await client.request(queries.GET_AUTOHOUSE_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; @@ -61,26 +63,28 @@ exports.default = async (req, res) => { if (shopsToProcess.length === 0) { logger.log("autohouse-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); + const batchPromise = (async () => { + await processBatch(batch, start, end); - if (skipUpload) { - for (const xmlObj of allxmlsToUpload) { - fs.writeFileSync(`./logs/${xmlObj.filename}`, xmlObj.xml); + if (skipUpload) { + for (const xmlObj of allxmlsToUpload) { + fs.writeFileSync(`./logs/${xmlObj.filename}`, xmlObj.xml); + } + } else { + await uploadViaSFTP(allxmlsToUpload); } - } else { - await uploadViaSFTP(allxmlsToUpload); - } + })(); + batchPromises.push(batchPromise); } - sendServerEmail({ + await Promise.all(batchPromises); + await sendServerEmail({ subject: `Autohouse 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( allxmlsToUpload.map((x) => ({ filename: x.filename, count: x.count, result: x.result })), null, 2 @@ -88,10 +92,8 @@ exports.default = async (req, res) => { }); logger.log("autohouse-end", "DEBUG", "api", null, null); - res.sendStatus(200); } catch (error) { - logger.log("autohouse-shopsToProcess-error", "ERROR", "api", null, { error: error.message, stack: error.stack }); - res.status(500).json({ error: error.message, stack: error.stack }); + logger.log("autohouse-error", "ERROR", "api", null, { error: error.message, stack: error.stack }); } };