IO-594 Add AH Settings
This commit is contained in:
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user