IO-2921 Adjust for Promise and change processing

Signed-off-by: Allan Carr <allan.carr@thinkimex.com>
This commit is contained in:
Allan Carr
2024-11-08 23:43:18 -08:00
parent f0717b8b36
commit 655aeb86fc

View File

@@ -37,12 +37,14 @@ exports.default = async (req, res) => {
res.sendStatus(401); res.sendStatus(401);
return; 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 { start, end, skipUpload } = req.body; //YYYY-MM-DD
const batchSize = 10; const batchSize = 10;
@@ -53,26 +55,28 @@ exports.default = async (req, res) => {
if (shopsToProcess.length === 0) { if (shopsToProcess.length === 0) {
logger.log("chatter-shopsToProcess-empty", "DEBUG", "api", null, null); logger.log("chatter-shopsToProcess-empty", "DEBUG", "api", null, null);
res.sendStatus(200);
return; return;
} }
const batchPromises = [];
for (let i = 0; i < shopsToProcess.length; i += batchSize) { for (let i = 0; i < shopsToProcess.length; i += batchSize) {
const batch = shopsToProcess.slice(i, i + batchSize); const batch = shopsToProcess.slice(i, i + batchSize);
await processBatch(batch, start, end); const batchPromise = (async () => {
await processBatch(batch, start, end);
if (skipUpload) { if (skipUpload) {
for (const csvObj of allcsvsToUpload) { for (const csvObj of allcsvsToUpload) {
fs.writeFile(`./logs/${csvObj.filename}`, csvObj.csv); 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")}`, subject: `Chatter Report ${moment().format("MM-DD-YY")}`,
text: `Errors: ${allErrors.map((e) => JSON.stringify(e, null, 2))} text: `Errors:\n${JSON.stringify(allErrors, null, 2)}\n\nUploaded:\n${JSON.stringify(
Uploaded: ${JSON.stringify(
allcsvsToUpload.map((x) => ({ filename: x.filename, count: x.count, result: x.result })), allcsvsToUpload.map((x) => ({ filename: x.filename, count: x.count, result: x.result })),
null, null,
2 2
@@ -80,10 +84,8 @@ exports.default = async (req, res) => {
}); });
logger.log("chatter-end", "DEBUG", "api", null, null); logger.log("chatter-end", "DEBUG", "api", null, null);
res.sendStatus(200);
} catch (error) { } catch (error) {
logger.log("chatter-shopsToProcess-error", "ERROR", "api", null, { error: error.message, stack: error.stack }); logger.log("chatter-error", "ERROR", "api", null, { error: error.message, stack: error.stack });
res.status(500).json({ error: error.message, stack: error.stack });
} }
}; };
@@ -152,9 +154,7 @@ async function getPrivateKey() {
try { try {
const { SecretString, SecretBinary } = await client.send(command); const { SecretString, SecretBinary } = await client.send(command);
if (SecretString || SecretBinary) logger.log("chatter-retrieved-private-key", "DEBUG", "api", null, null); if (SecretString || SecretBinary) logger.log("chatter-retrieved-private-key", "DEBUG", "api", null, null);
const chatterPrivateKey = SecretString const chatterPrivateKey = SecretString ? SecretString : Buffer.from(SecretBinary, "base64").toString("ascii");
? SecretString
: Buffer.from(SecretBinary, "base64").toString("ascii");
return chatterPrivateKey; return chatterPrivateKey;
} catch (error) { } catch (error) {
logger.log("chatter-get-private-key", "ERROR", "api", null, { error: error.message, stack: error.stack }); logger.log("chatter-get-private-key", "ERROR", "api", null, { error: error.message, stack: error.stack });