feature/IO-3205-Paint-Scale-Integrations: Fix output query

This commit is contained in:
Dave Richer
2025-04-30 12:37:22 -04:00
parent a2511685ac
commit 593f01f5dc

View File

@@ -52,6 +52,8 @@ interface GraphQLResponse {
shopname: string; shopname: string;
}; };
jobs?: Array<{ jobs?: Array<{
labhrs: any;
larhrs: any;
ro_number: string; ro_number: string;
ownr_ln: string; ownr_ln: string;
ownr_fn: string; ownr_fn: string;
@@ -232,80 +234,68 @@ const outputTypeHandlers: Record<
try { try {
log.info(`Generating PPG output for config ${config.id}: ${config.path}`); log.info(`Generating PPG output for config ${config.id}: ${config.path}`);
// Ensure output directory exists
await fs.mkdir(config.path!, { recursive: true }); await fs.mkdir(config.path!, { recursive: true });
// GraphQL query for jobs
const query = ` const query = `
query PpgData($today: date!, $todayplus5: date!, $shopid: uuid!) { query PpgData($today: timestamptz!, $todayplus5: timestamptz!, $shopid: uuid!) {
bodyshops_by_pk(id:$shopid) { bodyshops_by_pk(id:$shopid) {
imexshopid id
shopname shopname
imexshopid
} }
jobs(where: { jobs(where: {
_or: [
{
_and: [ _and: [
{ shop_id: { _eq: $shopid } }, { scheduled_in: { _lte: $todayplus5 } },
{ _or: [ { scheduled_in: { _gte: $today } }
{ status: { _eq: "Active" } }, ]
{ scheduled_date: { _gte: $today, _lte: $todayplus5 } } },
] } { inproduction: { _eq: true } }
] ]
}) { }) {
id
ro_number ro_number
ownr_ln status
ownr_fn ownr_fn
plate_no ownr_ln
ownr_co_nm
v_vin v_vin
v_model_yr v_model_yr
v_make_desc v_make_desc
v_model_desc v_model_desc
vehicle { v_color
v_paint_codes { plate_no
paint_cd1
}
}
larhrs_aggregate: estimate_lines_aggregate(where: { line_type: { _eq: "Paint" } }) {
aggregate {
sum {
mod_lb_hrs
}
}
}
ins_co_nm ins_co_nm
est_ct_ln
est_ct_fn est_ct_fn
job_totals { est_ct_ln
rates {
mapa {
total {
amount
}
}
}
totals {
subtotal {
amount
}
}
}
rate_mapa rate_mapa
labhrs_aggregate: estimate_lines_aggregate(where: { line_type: { _eq: "Body" } }) { rate_lab
job_totals
vehicle {
v_paint_codes
}
labhrs: joblines_aggregate(where: { mod_lbr_ty: { _neq: "LAR" }, removed: { _eq: false } }) {
aggregate {
sum {
mod_lb_hrs
}
}
}
larhrs: joblines_aggregate(where: { mod_lbr_ty: { _eq: "LAR" }, removed: { _eq: false } }) {
aggregate { aggregate {
sum { sum {
mod_lb_hrs mod_lb_hrs
} }
} }
} }
rate_lab
} }
} }
`; `;
const variables = { const variables = {
today: new Date().toISOString().split("T")[0], today: new Date().toISOString(),
todayplus5: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000) todayplus5: new Date(Date.now() + 5 * 86400000).toISOString(),
.toISOString()
.split("T")[0],
shopid: (store.get("app.bodyshop") as BodyShop)?.id, shopid: (store.get("app.bodyshop") as BodyShop)?.id,
}; };
@@ -314,8 +304,9 @@ const outputTypeHandlers: Record<
variables, variables,
)) as GraphQLResponse; )) as GraphQLResponse;
// Build JSON structure const jobs = response.jobs ?? [];
const jsonData = {
const header = {
PPG: { PPG: {
Header: { Header: {
Protocol: { Protocol: {
@@ -342,9 +333,8 @@ const outputTypeHandlers: Record<
ShopName: response.bodyshops_by_pk?.shopname || "", ShopName: response.bodyshops_by_pk?.shopname || "",
}, },
RepairOrders: { RepairOrders: {
ROCount: (response.jobs?.length || 0).toString(), ROCount: jobs.length.toString(),
RO: RO: jobs.map((job) => ({
response.jobs?.map((job) => ({
RONumber: job.ro_number || "", RONumber: job.ro_number || "",
ROStatus: "Open", ROStatus: "Open",
Customer: `${job.ownr_ln || ""}, ${job.ownr_fn || ""}`, Customer: `${job.ownr_ln || ""}, ${job.ownr_fn || ""}`,
@@ -356,38 +346,35 @@ const outputTypeHandlers: Record<
ModelName: job.v_model_desc || "", ModelName: job.v_model_desc || "",
OEMColorCode: job.vehicle?.v_paint_codes?.paint_cd1 || "", OEMColorCode: job.vehicle?.v_paint_codes?.paint_cd1 || "",
RefinishLaborHours: RefinishLaborHours:
job.larhrs_aggregate?.aggregate?.sum?.mod_lb_hrs || 0, job.larhrs?.aggregate?.sum?.mod_lb_hrs || 0,
InsuranceCompanyName: job.ins_co_nm || "", InsuranceCompanyName: job.ins_co_nm || "",
EstimatorName: `${job.est_ct_ln || ""}, ${job.est_ct_fn || ""}`, EstimatorName: `${job.est_ct_ln || ""}, ${job.est_ct_fn || ""}`,
PaintMaterialsRevenue: ( PaintMaterialsRevenue: (
(job.job_totals?.rates?.mapa?.total?.amount || 0) / 100 (job.job_totals?.rates?.mapa?.total?.amount || 0) / 100
).toFixed(2), ).toFixed(2),
PaintMaterialsRate: job.rate_mapa || 0, PaintMaterialsRate: job.rate_mapa || 0,
BodyHours: BodyHours: job.labhrs?.aggregate?.sum?.mod_lb_hrs || 0,
job.labhrs_aggregate?.aggregate?.sum?.mod_lb_hrs || 0,
BodyLaborRate: job.rate_lab || 0, BodyLaborRate: job.rate_lab || 0,
TotalCostOfRepairs: ( TotalCostOfRepairs: (
(job.job_totals?.totals?.subtotal?.amount || 0) / 100 (job.job_totals?.totals?.subtotal?.amount || 0) / 100
).toFixed(2), ).toFixed(2),
})) || [], })),
}, },
}, },
}, },
}, },
}; };
// Convert JSON to XML const xml = create({ version: "1.0" }, header).end({ prettyPrint: true });
const doc = create({ version: "1.0" }, jsonData);
const xmlString = doc.end({ prettyPrint: true });
// Save XML to file
const outputPath = path.join(config.path!, `PPGPaint.xml`); const outputPath = path.join(config.path!, `PPGPaint.xml`);
await fs.writeFile(outputPath, xmlString); await fs.writeFile(outputPath, xml);
log.info(`Saved PPG output XML to ${outputPath}`); log.info(`Saved PPG output XML to ${outputPath}`);
} catch (error) { } catch (error) {
log.error(`Error generating PPG output for config ${config.id}:`, error); log.error(`Error generating PPG output for config ${config.id}:`, error);
} }
}, // Add other output type handlers as needed },
}; };
// Default handler for unsupported types // Default handler for unsupported types