feature/IO-3255-simplified-parts-management - Checkpoint
This commit is contained in:
@@ -16,20 +16,6 @@ const INSERT_JOB_WITH_LINES = `
|
||||
}
|
||||
`;
|
||||
|
||||
const INSERT_PARTS_ORDERS = `
|
||||
mutation InsertPartsOrders($po: [parts_orders_insert_input!]!) {
|
||||
insert_parts_orders(objects: $po) {
|
||||
returning { id order_number }
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
/**
|
||||
* Handles incoming VehicleDamageEstimateAddRq XML,
|
||||
* parses every known field (including estimator, adjuster,
|
||||
* repair facility), nests Vehicle and JobLines inserts,
|
||||
* then any PartsOrders (grouped per SupplierRefNum).
|
||||
*/
|
||||
const partsManagementVehicleDamageEstimateAddRq = async (req, res) => {
|
||||
const { logger } = req;
|
||||
const xml = req.body;
|
||||
@@ -91,10 +77,7 @@ const partsManagementVehicleDamageEstimateAddRq = async (req, res) => {
|
||||
const clm_total = parseFloat(ci.Cieca_ttl || 0);
|
||||
|
||||
// ── ADMIN / OWNER INFO ──────────────────────────────────────────────────────
|
||||
const admin = rq.AdminInfo || {};
|
||||
|
||||
// Owner
|
||||
const ownerParty = admin.Owner?.Party || {};
|
||||
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;
|
||||
@@ -123,135 +106,32 @@ const partsManagementVehicleDamageEstimateAddRq = async (req, res) => {
|
||||
});
|
||||
const preferred_contact = ownerParty.PreferredContactMethod || null;
|
||||
|
||||
// Estimator → map to est_… fields
|
||||
const estParty = admin.Estimator?.Party || {};
|
||||
const est_fn = estParty.PersonInfo?.PersonName?.FirstName || null;
|
||||
const est_ln = estParty.PersonInfo?.PersonName?.LastName || null;
|
||||
const est_aff = admin.Estimator?.Affiliation || null;
|
||||
const estComms = estParty.ContactInfo?.Communications
|
||||
? Array.isArray(estParty.ContactInfo.Communications)
|
||||
? estParty.ContactInfo.Communications
|
||||
: [estParty.ContactInfo.Communications]
|
||||
: [];
|
||||
const est_ea = estComms.find((c) => c.CommQualifier === "EM")?.CommEmail || null;
|
||||
|
||||
// Adjuster → map to agt_… fields
|
||||
const adjParty = admin.Adjuster?.Party || {};
|
||||
const agt_ct_fn = adjParty.PersonInfo?.PersonName?.FirstName || null;
|
||||
const agt_ct_ln = adjParty.PersonInfo?.PersonName?.LastName || null;
|
||||
const adjComms = adjParty.ContactInfo?.Communications
|
||||
? Array.isArray(adjParty.ContactInfo.Communications)
|
||||
? adjParty.ContactInfo.Communications
|
||||
: [adjParty.ContactInfo.Communications]
|
||||
: [];
|
||||
const agt_ct_ph = adjComms.find((c) => c.CommQualifier === "CP")?.CommPhone || null;
|
||||
const agt_ea = adjComms.find((c) => c.CommQualifier === "EM")?.CommEmail || null;
|
||||
|
||||
// Repair Facility → map to servicing_dealer, servicing_dealer_contact
|
||||
const rfParty = admin.RepairFacility?.Party || {};
|
||||
const servicing_dealer = rfParty.OrgInfo?.CompanyName || null;
|
||||
const rfAdr = rfParty.OrgInfo?.Communications?.Address || {};
|
||||
const servicing_dealer_addr1 = rfAdr.Address1 || null;
|
||||
const servicing_dealer_city = rfAdr.City || null;
|
||||
const servicing_dealer_st = rfAdr.StateProvince || null;
|
||||
// const servicing_dealer_zip = rfAdr.PostalCode || null;
|
||||
// const servicing_dealer_ctry = rfAdr.Country || null;
|
||||
const rfComms = rfParty.ContactInfo?.Communications
|
||||
? Array.isArray(rfParty.ContactInfo.Communications)
|
||||
? rfParty.ContactInfo.Communications
|
||||
: [rfParty.ContactInfo.Communications]
|
||||
: [];
|
||||
const servicing_dealer_contact =
|
||||
rfComms.find((c) => c.CommQualifier === "WP")?.CommPhone ||
|
||||
rfComms.find((c) => c.CommQualifier === "FX")?.CommPhone ||
|
||||
null;
|
||||
|
||||
// ── VEHICLE INFO (nested relationship) ──────────────────────────────────────
|
||||
const vin = rq.VehicleInfo?.VINInfo?.VINNum || null;
|
||||
const plate_no = rq.VehicleInfo?.License?.LicensePlateNum || null;
|
||||
const plate_st = rq.VehicleInfo?.License?.LicensePlateStateProvince || null;
|
||||
const desc = rq.VehicleInfo?.VehicleDesc || {};
|
||||
const v_model_yr = desc.ModelYear || null;
|
||||
const v_make_desc = desc.MakeDesc || null;
|
||||
const v_model_desc = desc.ModelName || null;
|
||||
const v_color = rq.VehicleInfo?.Paint?.Exterior?.ColorName || null;
|
||||
const body_style = desc.BodyStyle || null;
|
||||
const engine_desc = desc.EngineDesc || null;
|
||||
const production_date = desc.ProductionDate || null;
|
||||
const v_options = desc.SubModelDesc || null;
|
||||
const v_type = desc.FuelType || null;
|
||||
const driveable = rq.VehicleInfo?.Condition?.DrivableInd === "Y";
|
||||
|
||||
const vehicleData = {
|
||||
shopid: shopId,
|
||||
// vin,
|
||||
plate_no,
|
||||
plate_st
|
||||
// year: v_model_yr,
|
||||
// make: v_make_desc
|
||||
// model: v_model_desc
|
||||
// color: v_color,
|
||||
// bstyle: body_style,
|
||||
// engine: engine_desc
|
||||
// prod_dt: production_date,
|
||||
// options: v_options,
|
||||
// type: v_type,
|
||||
// condition: driveable
|
||||
plate_no: rq.VehicleInfo?.License?.LicensePlateNum || null,
|
||||
plate_st: rq.VehicleInfo?.License?.LicensePlateStateProvince || null
|
||||
};
|
||||
|
||||
// ── PROFILE & RATES ─────────────────────────────────────────────────────────
|
||||
const rateInfos = rq.ProfileInfo?.RateInfo
|
||||
? Array.isArray(rq.ProfileInfo.RateInfo)
|
||||
? rq.ProfileInfo.RateInfo
|
||||
: [rq.ProfileInfo.RateInfo]
|
||||
: [];
|
||||
const rates = {},
|
||||
rateTier = {},
|
||||
materialCalc = {};
|
||||
rateInfos.forEach((r) => {
|
||||
if (!r.RateType) return;
|
||||
const t = r.RateType.toLowerCase();
|
||||
if (r.Rate) rates[`rate_${t}`] = parseFloat(r.Rate) || 0;
|
||||
if (r.RateTierInfo) {
|
||||
const tiers = Array.isArray(r.RateTierInfo) ? r.RateTierInfo : [r.RateTierInfo];
|
||||
rateTier[t] = tiers.map((ti) => ({
|
||||
tier: ti.TierNum,
|
||||
pct: parseFloat(ti.Percentage) || 0
|
||||
}));
|
||||
}
|
||||
if (r.MaterialCalcSettings) {
|
||||
materialCalc[t] = r.MaterialCalcSettings;
|
||||
}
|
||||
});
|
||||
|
||||
// ── 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),
|
||||
manual_line: line.ManualLineInd === "1",
|
||||
// automated_entry: line.AutomatedEntry === "1",
|
||||
// desc_judgment_ind: line.DescJudgmentInd === "1",
|
||||
status: line.LineStatusCode || null,
|
||||
line_desc: line.LineDesc || null,
|
||||
|
||||
// parts
|
||||
// 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,
|
||||
|
||||
// non-OEM → not persisted at jobline level
|
||||
|
||||
// after_market_usage: line.PartInfo?.AfterMarketUsage || null,
|
||||
// certification_type: line.PartInfo?.CertificationType || null,
|
||||
// certification_type: line.PartInfo?.CertificationType || null,
|
||||
tax_part: line.PartInfo?.TaxableInd === "1",
|
||||
glass_flag: line.PartInfo?.GlassPartInd === "1",
|
||||
price_j: line.PriceJudgmentInd === "1",
|
||||
price_inc: line.PriceInclInd === "1",
|
||||
// order_by_application_ind: String(line.PartInfo?.OrderByApplicationInd).toLowerCase() === "true",
|
||||
|
||||
// labor
|
||||
mod_lbr_ty: line.LaborInfo?.LaborType || null,
|
||||
@@ -259,7 +139,6 @@ const partsManagementVehicleDamageEstimateAddRq = async (req, res) => {
|
||||
lbr_op: line.LaborInfo?.LaborOperation || null,
|
||||
lbr_amt: parseFloat(line.LaborInfo?.LaborAmt || 0),
|
||||
|
||||
// parent_line_no: line.ParentLineNum ? parseInt(line.ParentLineNum, 10) : null,
|
||||
notes: line.LineMemo || null
|
||||
}));
|
||||
|
||||
@@ -267,13 +146,11 @@ const partsManagementVehicleDamageEstimateAddRq = async (req, res) => {
|
||||
const jobInput = {
|
||||
shopid: shopId,
|
||||
ro_number: RefClaimNum,
|
||||
|
||||
clm_no,
|
||||
status,
|
||||
clm_total,
|
||||
policy_no,
|
||||
ded_amt,
|
||||
|
||||
comment,
|
||||
date_exported,
|
||||
asgn_no,
|
||||
@@ -297,34 +174,13 @@ const partsManagementVehicleDamageEstimateAddRq = async (req, res) => {
|
||||
ownr_ea,
|
||||
// preferred_contact,
|
||||
|
||||
// est_co_id: est_aff,
|
||||
est_ct_fn: est_fn,
|
||||
est_ct_ln: est_ln,
|
||||
est_ea,
|
||||
|
||||
agt_ct_fn,
|
||||
agt_ct_ln,
|
||||
agt_ct_ph,
|
||||
agt_ea,
|
||||
|
||||
servicing_dealer,
|
||||
// servicing_dealer_addr1,
|
||||
// servicing_dealer_city,
|
||||
// servicing_dealer_st,
|
||||
// servicing_dealer_zip,
|
||||
// servicing_dealer_ctry,
|
||||
servicing_dealer_contact,
|
||||
|
||||
// ...rates,
|
||||
// nest vehicle & joblines
|
||||
vehicle: { data: vehicleData },
|
||||
joblines: { data: joblinesData },
|
||||
|
||||
production_vars: {
|
||||
documentVersions,
|
||||
rateTier,
|
||||
materialCalc
|
||||
},
|
||||
|
||||
vehicle: { data: vehicleData },
|
||||
joblines: { data: joblinesData }
|
||||
documentVersions
|
||||
}
|
||||
};
|
||||
|
||||
logger.log("parts-insert-job", "debug", null, null, { jobInput });
|
||||
@@ -332,69 +188,18 @@ const partsManagementVehicleDamageEstimateAddRq = async (req, res) => {
|
||||
const jobId = newJob.id;
|
||||
logger.log("parts-job-created", "info", jobId, null);
|
||||
|
||||
// ── BUILD & INSERT PARTS ORDERS ────────────────────────────────────────────
|
||||
/*
|
||||
// ── 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;
|
||||
}, {});
|
||||
|
||||
const currentDate = new Date().toISOString().slice(0, 10);
|
||||
// TODO: we should not need to capture the order status from the XML,
|
||||
const defaultStatus = process.env.DEFAULT_PARTS_ORDER_STATUS || "";
|
||||
|
||||
// TODO: This will be the username of the bodyshop user used for the integration.
|
||||
const userEmail = req.headers["x-hasura-user-id"] || "system@example.com";
|
||||
|
||||
// group by vendor
|
||||
const poGroups = {};
|
||||
damageLines.forEach((line) => {
|
||||
const pt = line.PartInfo?.PartType;
|
||||
const qty = parseInt(line.PartInfo?.Quantity || 0, 10);
|
||||
if (["PAN", "PAC", "PAM", "PAA"].includes(pt) && qty > 0) {
|
||||
// TODO: We should not need to capture the vendor ID from the XML
|
||||
const vid = line.PartInfo?.NonOEM?.SupplierRefNum || process.env.DEFAULT_VENDOR_ID || null;
|
||||
(poGroups[vid] = poGroups[vid] || []).push({
|
||||
line,
|
||||
unq_seq: parseInt(line.UniqueSequenceNum, 10)
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const partsOrders = Object.entries(poGroups).map(([vendorid, entries]) => ({
|
||||
jobid: jobId,
|
||||
vendorid,
|
||||
order_number: `${entries[0].line.LineNum}`,
|
||||
order_date: currentDate,
|
||||
orderedby: "XML-API",
|
||||
user_email: userEmail,
|
||||
status: defaultStatus,
|
||||
parts_order_lines: {
|
||||
data: entries.map(({ line, unq_seq }) => {
|
||||
const pi = line.PartInfo || {};
|
||||
const nonOEM = pi.NonOEM || {};
|
||||
return {
|
||||
job_line_id: seqToId[unq_seq],
|
||||
line_desc: line.LineDesc || null,
|
||||
oem_partno: pi.OEMPartNum || null,
|
||||
db_price: parseFloat(pi.PartPrice || 0),
|
||||
act_price: parseFloat(pi.PartPrice || 0),
|
||||
line_remarks: line.LineMemo || null,
|
||||
quantity: parseInt(pi.Quantity || 1, 10),
|
||||
part_type: pi.PartType || null,
|
||||
cost: nonOEM.NonOEMPartPrice ? parseFloat(nonOEM.NonOEMPartPrice) : null,
|
||||
cm_received: null
|
||||
};
|
||||
})
|
||||
}
|
||||
}));
|
||||
|
||||
if (partsOrders.length) {
|
||||
logger.log("parts-insert-orders", "debug", null, null, {
|
||||
partsOrders
|
||||
});
|
||||
await client.request(INSERT_PARTS_ORDERS, { po: partsOrders });
|
||||
logger.log("parts-orders-created", "info", jobId, null);
|
||||
}
|
||||
// ... grouping logic and mutation calls removed ...
|
||||
*/
|
||||
|
||||
return res.status(200).json({ success: true, jobId });
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user