From 098754125bf051a956df1018c613ce0a016244f4 Mon Sep 17 00:00:00 2001 From: Patrick Fic <> Date: Wed, 21 Jul 2021 13:11:51 -0700 Subject: [PATCH] IO-594 Add AH Settings --- server/data/autohouse.js | 163 +++++++++++++++++++++------------------ 1 file changed, 89 insertions(+), 74 deletions(-) diff --git a/server/data/autohouse.js b/server/data/autohouse.js index bed1e2a1f..f8873c684 100644 --- a/server/data/autohouse.js +++ b/server/data/autohouse.js @@ -30,90 +30,105 @@ const ftpSetup = { exports.default = async (req, res) => { //Query for the List of Bodyshop Clients. + console.log("Starting Autohouse datapump request."); const { bodyshops } = await client.request(queries.GET_AUTOHOUSE_SHOPS); const allxmlsToUpload = []; const allErrors = []; - - for (const bodyshop of bodyshops) { - const erroredJobs = []; - try { - const { jobs } = await client.request(queries.AUTOHOUSE_QUERY, { - bodyshopid: bodyshop.id, - }); - - const autoHouseObject = { - AutoHouseExport: { - RepairOrder: jobs.map((j) => - CreateRepairOrderTag({ ...j, bodyshop }, function ({ job, error }) { - erroredJobs.push({ job: job, error: error.toString() }); - }) - ), - }, - }; - - console.log( - "***Number of Failed jobs***: ", - erroredJobs.length, - JSON.stringify(erroredJobs.map((j) => j.job.ro_number)) - ); - - var ret = builder - .create(autoHouseObject, { - version: "1.0", - encoding: "UTF-8", - }) - .end({ pretty: true, allowEmptyTags: true }); - - allxmlsToUpload.push({ - xml: ret, - filename: `IM_${bodyshop.imexshopid}_${moment().format( - "DDMMYYYY_HHMMSS" - )}.xml`, - }); - } catch (error) { - //Error at the shop level. - allErrors.push({ - bodyshopid: bodyshop.id, - imexshopid: bodyshop.imexshopid, - fatal: true, - errors: [error.toString()], - }); - } finally { - allErrors.push({ - bodyshopid: bodyshop.id, - imexshopid: bodyshop.imexshopid, - errors: erroredJobs, - }); - } - } - - let sftp = new Client(); try { - //Connect to the FTP and upload all. + for (const bodyshop of bodyshops) { + console.log("Starting extract for ", bodyshop.shopname); + const erroredJobs = []; + try { + const { jobs } = await client.request(queries.AUTOHOUSE_QUERY, { + bodyshopid: bodyshop.id, + }); - await sftp.connect(ftpSetup); + const autoHouseObject = { + AutoHouseExport: { + RepairOrder: jobs.map((j) => + CreateRepairOrderTag( + { ...j, bodyshop }, + function ({ job, error }) { + erroredJobs.push({ job: job, error: error.toString() }); + } + ) + ), + }, + }; - for (const xmlObj of allxmlsToUpload) { - console.log("Uploading", xmlObj.filename); - const uploadResult = await sftp.put( - Buffer.from(xmlObj.xml), - `/${xmlObj.filename}` - ); - console.log( - "🚀 ~ file: autohouse.js ~ line 94 ~ uploadResult", - uploadResult - ); + console.log( + bodyshop.shopname, + "***Number of Failed jobs***: ", + erroredJobs.length, + JSON.stringify(erroredJobs.map((j) => j.job.ro_number)) + ); + + var ret = builder + .create(autoHouseObject, { + version: "1.0", + encoding: "UTF-8", + }) + .end({ pretty: true, allowEmptyTags: true }); + + allxmlsToUpload.push({ + xml: ret, + filename: `IM_${bodyshop.imexshopid}_${moment().format( + "DDMMYYYY_HHMMSS" + )}.xml`, + }); + + console.log("Finished extract for shop ", bodyshop.shopname); + } catch (error) { + //Error at the shop level. + console.log("Error at shop level", bodyshop.shopname, error); + allErrors.push({ + bodyshopid: bodyshop.id, + imexshopid: bodyshop.imexshopid, + fatal: true, + errors: [error.toString()], + }); + } finally { + allErrors.push({ + bodyshopid: bodyshop.id, + imexshopid: bodyshop.imexshopid, + errors: erroredJobs, + }); + } } - //***TODO Change filing naming when creating the cron job. IM_ShopInternalName_DDMMYYYY_HHMMSS.xml - } catch (error) { - console.log("Error when connecting to FTP", error); - } finally { - sftp.end(); - } + let sftp = new Client(); + sftp.on("error", (errors) => + console.log("Error in FTP client", JSON.stringify(errors)) + ); + try { + //Connect to the FTP and upload all. - res.sendStatus(200); + await sftp.connect(ftpSetup); + + for (const xmlObj of allxmlsToUpload) { + console.log("Uploading", xmlObj.filename); + const uploadResult = await sftp.put( + Buffer.from(xmlObj.xml), + `/${xmlObj.filename}` + ); + console.log( + "🚀 ~ file: autohouse.js ~ line 94 ~ uploadResult", + uploadResult + ); + } + + //***TODO Change filing naming when creating the cron job. IM_ShopInternalName_DDMMYYYY_HHMMSS.xml + } catch (error) { + console.log("Error when connecting to FTP", error); + } finally { + sftp.end(); + } + + res.sendStatus(200); + } catch (error) { + res.JSON(error).sendStatus(500); + } }; const CreateRepairOrderTag = (job, errorCallback) => {