IO-594 Create schedulable AH export.
This commit is contained in:
@@ -11,42 +11,109 @@ require("dotenv").config({
|
||||
`.env.${process.env.NODE_ENV || "development"}`
|
||||
),
|
||||
});
|
||||
let Client = require("ssh2-sftp-client");
|
||||
|
||||
const client = require("../graphql-client/graphql-client").client;
|
||||
const AHDineroFormat = "0.00";
|
||||
const AhDateFormat = "MMDDYYYY";
|
||||
|
||||
const repairOpCodes = ["OP4", "OP9", "OP10"];
|
||||
const replaceOpCodes = ["OP2", "OP5", "OP11", "OP12"];
|
||||
|
||||
const ftpSetup = {
|
||||
host: process.env.AUTOHOUSE_HOST,
|
||||
port: process.env.AUTOHOUSE_PORT,
|
||||
username: process.env.AUTOHOUSE_USER,
|
||||
password: process.env.AUTOHOUSE_PASSWORD,
|
||||
//debug: console.log,
|
||||
};
|
||||
|
||||
exports.default = async (req, res) => {
|
||||
//Get Client Dataset.
|
||||
const { jobs } = await client.request(queries.AUTOHOUSE_QUERY);
|
||||
//Query for the List of Bodyshop Clients.
|
||||
const { bodyshops } = await client.request(queries.GET_AUTOHOUSE_SHOPS);
|
||||
|
||||
const erroredJobs = [];
|
||||
const allxmlsToUpload = [];
|
||||
const allErrors = [];
|
||||
|
||||
const autoHouseObject = {
|
||||
AutoHouseExport: {
|
||||
RepairOrder: jobs.map((j) =>
|
||||
CreateRepairOrderTag(j, (job, error) => {
|
||||
erroredJobs.push({ job, error });
|
||||
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 });
|
||||
|
||||
console.log(
|
||||
"***Number of Failed jobs***: ",
|
||||
erroredJobs.length,
|
||||
JSON.stringify(erroredJobs.map((x) => x.error))
|
||||
);
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//***TODO Change filing naming when creating the cron job. IM_ShopInternalName_DDMMYYYY_HHMMSS.xml
|
||||
res.type("application/xml");
|
||||
//res.sendFile(ret);
|
||||
res.send(ret);
|
||||
let sftp = new Client();
|
||||
try {
|
||||
//Connect to the FTP and upload all.
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
const CreateRepairOrderTag = (job, errorCallback) => {
|
||||
@@ -410,7 +477,7 @@ const CreateRepairOrderTag = (job, errorCallback) => {
|
||||
return ret;
|
||||
} catch (error) {
|
||||
console.log("Error calculating job", error);
|
||||
errorCallback(job, error);
|
||||
errorCallback({ job, error });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -611,6 +678,3 @@ const generateNullDetailLine = () => {
|
||||
EstimateAmount: null,
|
||||
};
|
||||
};
|
||||
|
||||
const repairOpCodes = ["OP4", "OP9", "OP10"];
|
||||
const replaceOpCodes = ["OP2", "OP5", "OP11", "OP12"];
|
||||
|
||||
Reference in New Issue
Block a user