feature/IO-3255-simplified-parts-management - Checkpoint
This commit is contained in:
@@ -5,6 +5,8 @@ const client = require("../../../graphql-client/graphql-client").client;
|
||||
const { parseXml, normalizeXmlObject } = require("../partsManagementUtils");
|
||||
const { extractPartsTaxRates } = require("./lib/extractPartsTaxRates");
|
||||
const opCodes = require("./lib/opCodes.json");
|
||||
const { uploadFileToS3 } = require("../../../utils/s3");
|
||||
const InstanceMgr = require("../../../utils/instanceMgr").default;
|
||||
|
||||
const {
|
||||
GET_JOB_BY_ID,
|
||||
@@ -214,6 +216,22 @@ const extractDeletions = (deletions = {}) => {
|
||||
return Array.from(new Set(allSeqs));
|
||||
};
|
||||
|
||||
// S3 bucket + key builder (mirrors AddRq but with changeRequest prefix)
|
||||
const ESTIMATE_XML_BUCKET =
|
||||
process.env?.NODE_ENV === "development"
|
||||
? "parts-estimates"
|
||||
: InstanceMgr({
|
||||
imex: `imex-webest-xml`,
|
||||
rome: `rome-webest-xml`
|
||||
});
|
||||
|
||||
const buildEstimateXmlKey = (rq) => {
|
||||
const shopId = rq.ShopID;
|
||||
const jobId = rq.JobID;
|
||||
const ts = new Date().toISOString().replace(/:/g, "-");
|
||||
return `changeRequest/${shopId}/${jobId}/${ts}.xml`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles VehicleDamageEstimateChgRq requests.
|
||||
* @param req
|
||||
@@ -222,19 +240,26 @@ const extractDeletions = (deletions = {}) => {
|
||||
*/
|
||||
const partsManagementVehicleDamageEstimateChgRq = async (req, res) => {
|
||||
const { logger } = req;
|
||||
|
||||
const rawXml = typeof req.body === "string" ? req.body : Buffer.isBuffer(req.body) ? req.body.toString("utf8") : "";
|
||||
try {
|
||||
const payload = await parseXml(req.body, logger);
|
||||
const rq = normalizeXmlObject(payload.VehicleDamageEstimateChgRq);
|
||||
if (!rq) return res.status(400).send("Missing <VehicleDamageEstimateChgRq>");
|
||||
|
||||
const shopId = rq.ShopID;
|
||||
|
||||
const jobId = rq.JobID;
|
||||
|
||||
if (!shopId || !jobId) return res.status(400).send("Missing ShopID or JobID");
|
||||
const shopId = rq.ShopID;
|
||||
|
||||
// Fire-and-forget archival on valid request
|
||||
(async () => {
|
||||
try {
|
||||
const key = buildEstimateXmlKey(rq);
|
||||
await uploadFileToS3({ bucketName: ESTIMATE_XML_BUCKET, key, content: rawXml || "", contentType: "application/xml" });
|
||||
logger.log("parts-estimate-xml-uploaded", "info", jobId, null, { key, bytes: rawXml?.length || 0 });
|
||||
} catch (e) {
|
||||
logger.log("parts-estimate-xml-upload-failed", "warn", jobId, null, { error: e?.message });
|
||||
}
|
||||
})();
|
||||
|
||||
const job = await findJob(shopId, jobId, logger);
|
||||
|
||||
if (!job) return res.status(404).send("Job not found");
|
||||
|
||||
// --- Get updated lines and their unq_seq ---
|
||||
@@ -290,3 +315,5 @@ const partsManagementVehicleDamageEstimateChgRq = async (req, res) => {
|
||||
};
|
||||
|
||||
module.exports = partsManagementVehicleDamageEstimateChgRq;
|
||||
|
||||
// Remove any duplicate S3 constants that might have been appended previously (none expected now)
|
||||
|
||||
Reference in New Issue
Block a user