212 lines
8.6 KiB
JavaScript
212 lines
8.6 KiB
JavaScript
// no-dd-sa:javascript-code-style/assignment-name
|
|
// The above disables camel case inspection for the file,
|
|
// CamelCase is used for GraphQL and database fields, and it is easier
|
|
// to maintain consistency with the existing codebase.
|
|
|
|
const xml2js = require("xml2js");
|
|
const client = require("../../graphql-client/graphql-client").client;
|
|
|
|
// GraphQL statements
|
|
const INSERT_JOB_WITH_LINES = `
|
|
mutation InsertJob($job: jobs_insert_input!) {
|
|
insert_jobs_one(object: $job) {
|
|
id
|
|
joblines { id unq_seq }
|
|
}
|
|
}
|
|
`;
|
|
|
|
const partsManagementVehicleDamageEstimateAddRq = async (req, res) => {
|
|
const { logger } = req;
|
|
const xml = req.body;
|
|
|
|
// ── PARSE XML ────────────────────────────────────────────────────────────────
|
|
let payload;
|
|
try {
|
|
payload = await xml2js.parseStringPromise(xml, {
|
|
explicitArray: false,
|
|
tagNameProcessors: [xml2js.processors.stripPrefix]
|
|
});
|
|
logger.log("parts-xml-parse", "debug", null, null, { success: true });
|
|
} catch (err) {
|
|
logger.log("parts-xml-parse-error", "error", null, null, { error: err });
|
|
console.dir(err);
|
|
return res.status(400).send("Invalid XML");
|
|
}
|
|
|
|
const rq = payload.VehicleDamageEstimateAddRq;
|
|
if (!rq) {
|
|
logger.log("parts-missing-root", "error");
|
|
return res.status(400).send("Missing <VehicleDamageEstimateAddRq>");
|
|
}
|
|
|
|
try {
|
|
// ── SHOP ID ────────────────────────────────────────────────────────────────
|
|
const shopId = rq.ShopID || rq.shopId;
|
|
if (!shopId) {
|
|
throw { status: 400, message: "Missing <ShopID> in XML" };
|
|
}
|
|
|
|
// ── DOCUMENT INFO ──────────────────────────────────────────────────────────
|
|
const { RefClaimNum } = rq;
|
|
const doc = rq.DocumentInfo || {};
|
|
const comment = doc.Comment || null;
|
|
const date_exported = doc.TransmitDateTime || null;
|
|
|
|
const docVers = doc.DocumentVer ? (Array.isArray(doc.DocumentVer) ? doc.DocumentVer : [doc.DocumentVer]) : [];
|
|
const documentVersions = docVers.map((dv) => ({
|
|
code: dv.DocumentVerCode,
|
|
num: dv.DocumentVerNum
|
|
}));
|
|
|
|
// ── EVENT INFO ──────────────────────────────────────────────────────────────
|
|
const ev = rq.EventInfo || {};
|
|
const asgn = ev.AssignmentEvent || {};
|
|
const asgn_no = asgn.AssignmentNumber || null;
|
|
const asgn_type = asgn.AssignmentType || null;
|
|
const asgn_date = asgn.AssignmentDate || null;
|
|
const scheduled_completion = ev.RepairEvent?.TargetCompletionDateTime || null;
|
|
const scheduled_in = ev.RepairEvent?.RequestedPickUpDateTime || null;
|
|
|
|
// ── CLAIM INFO ──────────────────────────────────────────────────────────────
|
|
const ci = rq.ClaimInfo || {};
|
|
const clm_no = ci.ClaimNum || null;
|
|
const status = ci.ClaimStatus || null;
|
|
const policy_no = ci.PolicyInfo?.PolicyNum || null;
|
|
const ded_amt = parseFloat(ci.PolicyInfo?.CoverageInfo?.Coverage?.DeductibleInfo?.DeductibleAmt || 0);
|
|
const clm_total = parseFloat(ci.Cieca_ttl || 0);
|
|
|
|
// ── ADMIN / OWNER INFO ──────────────────────────────────────────────────────
|
|
const ownerParty = rq.AdminInfo?.Owner?.Party || {};
|
|
const ownr_fn = ownerParty.PersonInfo?.PersonName?.FirstName || null;
|
|
const ownr_ln = ownerParty.PersonInfo?.PersonName?.LastName || null;
|
|
const ownr_co_nm = ownerParty.OrgInfo?.CompanyName || null;
|
|
const adr = ownerParty.PersonInfo?.Communications?.Address || {};
|
|
const ownr_addr1 = adr.Address1 || null;
|
|
const ownr_addr2 = adr.Address2 || null;
|
|
const ownr_city = adr.City || null;
|
|
const ownr_st = adr.StateProvince || null;
|
|
const ownr_zip = adr.PostalCode || null;
|
|
const ownr_ctry = adr.Country || null;
|
|
|
|
const ownrComms = ownerParty.ContactInfo?.Communications
|
|
? Array.isArray(ownerParty.ContactInfo.Communications)
|
|
? ownerParty.ContactInfo.Communications
|
|
: [ownerParty.ContactInfo.Communications]
|
|
: [];
|
|
let ownr_ph1 = null,
|
|
ownr_ph2 = null,
|
|
ownr_fax = null,
|
|
ownr_ea = null;
|
|
ownrComms.forEach((c) => {
|
|
if (c.CommQualifier === "CP") ownr_ph1 = c.CommPhone;
|
|
if (c.CommQualifier === "WP") ownr_ph2 = c.CommPhone;
|
|
if (c.CommQualifier === "FX") ownr_fax = c.CommPhone;
|
|
if (c.CommQualifier === "EM") ownr_ea = c.CommEmail;
|
|
});
|
|
const preferred_contact = ownerParty.PreferredContactMethod || null;
|
|
|
|
// ── VEHICLE INFO (nested relationship) ──────────────────────────────────────
|
|
const desc = rq.VehicleInfo?.VehicleDesc || {};
|
|
const vehicleData = {
|
|
shopid: shopId,
|
|
plate_no: rq.VehicleInfo?.License?.LicensePlateNum || null,
|
|
plate_st: rq.VehicleInfo?.License?.LicensePlateStateProvince || null
|
|
};
|
|
|
|
// ── DAMAGE LINES → joblinesData ────────────────────────────────────────────
|
|
const damageLines = Array.isArray(rq.DamageLineInfo) ? rq.DamageLineInfo : [rq.DamageLineInfo];
|
|
const joblinesData = damageLines.map((line) => ({
|
|
line_no: parseInt(line.LineNum, 10),
|
|
unq_seq: parseInt(line.UniqueSequenceNum, 10),
|
|
status: line.LineStatusCode || null,
|
|
line_desc: line.LineDesc || null,
|
|
|
|
// parts (only fields for jobline table)
|
|
part_type: line.PartInfo?.PartType || null,
|
|
part_qty: parseInt(line.PartInfo?.Quantity || 0, 10),
|
|
db_price: parseFloat(line.PartInfo?.PartPrice || 0),
|
|
act_price: parseFloat(line.PartInfo?.PartPrice || 0),
|
|
oem_partno: line.PartInfo?.OEMPartNum || null,
|
|
tax_part: line.PartInfo?.TaxableInd === "1",
|
|
glass_flag: line.PartInfo?.GlassPartInd === "1",
|
|
price_j: line.PriceJudgmentInd === "1",
|
|
price_inc: line.PriceInclInd === "1",
|
|
|
|
// labor
|
|
mod_lbr_ty: line.LaborInfo?.LaborType || null,
|
|
mod_lb_hrs: parseFloat(line.LaborInfo?.LaborHours || 0),
|
|
lbr_op: line.LaborInfo?.LaborOperation || null,
|
|
lbr_amt: parseFloat(line.LaborInfo?.LaborAmt || 0),
|
|
|
|
notes: line.LineMemo || null
|
|
}));
|
|
|
|
// ── BUILD & INSERT THE JOB ──────────────────────────────────────────────────
|
|
const jobInput = {
|
|
shopid: shopId,
|
|
ro_number: RefClaimNum,
|
|
clm_no,
|
|
status,
|
|
clm_total,
|
|
policy_no,
|
|
ded_amt,
|
|
comment,
|
|
date_exported,
|
|
asgn_no,
|
|
asgn_type,
|
|
asgn_date,
|
|
scheduled_completion,
|
|
scheduled_in,
|
|
|
|
ownr_fn,
|
|
ownr_ln,
|
|
ownr_co_nm,
|
|
ownr_addr1,
|
|
ownr_addr2,
|
|
ownr_city,
|
|
ownr_st,
|
|
ownr_zip,
|
|
ownr_ctry,
|
|
ownr_ph1,
|
|
ownr_ph2,
|
|
ownr_fax,
|
|
ownr_ea,
|
|
// preferred_contact,
|
|
|
|
// nest vehicle & joblines
|
|
vehicle: { data: vehicleData },
|
|
joblines: { data: joblinesData },
|
|
|
|
production_vars: {
|
|
documentVersions
|
|
}
|
|
};
|
|
|
|
logger.log("parts-insert-job", "debug", null, null, { jobInput });
|
|
const { insert_jobs_one: newJob } = await client.request(INSERT_JOB_WITH_LINES, { job: jobInput });
|
|
const jobId = newJob.id;
|
|
logger.log("parts-job-created", "info", jobId, null);
|
|
|
|
/*
|
|
// ── PARTS ORDERS ─────────────────────────────────────────────────────────
|
|
// The integration no longer requires parts_orders,
|
|
// all related logic is commented out.
|
|
|
|
const seqToId = newJob.joblines.reduce((map, ln) => {
|
|
map[ln.unq_seq] = ln.id;
|
|
return map;
|
|
}, {});
|
|
|
|
// ... grouping logic and mutation calls removed ...
|
|
*/
|
|
|
|
return res.status(200).json({ success: true, jobId });
|
|
} catch (err) {
|
|
logger.log("parts-route-error", "error", null, null, { error: err });
|
|
return res.status(err.status || 500).json({ error: err.message || "Internal error" });
|
|
}
|
|
};
|
|
|
|
module.exports = partsManagementVehicleDamageEstimateAddRq;
|