Files
bodyshop/job-totals-testing-util.js
2024-10-30 14:06:00 -07:00

137 lines
4.8 KiB
JavaScript

const path = require("path");
const Dinero = require("dinero.js");
const { gql } = require("graphql-request");
const AxiosLib = require("axios").default;
const axios = AxiosLib.create();
// Dinero.defaultCurrency = "USD";
// Dinero.globalLocale = "en-CA";
Dinero.globalRoundingMode = "HALF_EVEN";
const client = require("./server/graphql-client/graphql-client").client;
require("dotenv").config({
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
});
async function RunTheTest() {
const bodyshopids = ["71f8494c-89f0-43e0-8eb2-820b52d723bc"];
const bearerToken = `Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImU2YWMzNTcyNzY3ZGUyNjE0ZmM1MTA4NjMzMDg3YTQ5MjMzMDNkM2IiLCJ0eXAiOiJKV1QifQ.eyJuYW1lIjoiUGF0cmljayBGaWMgKERFVikiLCJodHRwczovL2hhc3VyYS5pby9qd3QvY2xhaW1zIjp7IngtaGFzdXJhLWRlZmF1bHQtcm9sZSI6InVzZXIiLCJ4LWhhc3VyYS1hbGxvd2VkLXJvbGVzIjpbInVzZXIiXSwieC1oYXN1cmEtdXNlci1pZCI6ImhOSjhBRHB0REhRQkRFcXNCOFFNWVRqaURuZjEifSwiaXNzIjoiaHR0cHM6Ly9zZWN1cmV0b2tlbi5nb29nbGUuY29tL2ltZXgtZGV2IiwiYXVkIjoiaW1leC1kZXYiLCJhdXRoX3RpbWUiOjE3MzAxMzIwMjksInVzZXJfaWQiOiJoTko4QURwdERIUUJERXFzQjhRTVlUamlEbmYxIiwic3ViIjoiaE5KOEFEcHRESFFCREVxc0I4UU1ZVGppRG5mMSIsImlhdCI6MTczMDE1MjI5NiwiZXhwIjoxNzMwMTU1ODk2LCJlbWFpbCI6InBhdHJpY2tAaW1leC5kZXYiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsiZW1haWwiOlsicGF0cmlja0BpbWV4LmRldiJdfSwic2lnbl9pbl9wcm92aWRlciI6InBhc3N3b3JkIn19.Fgf_itlA2-CMd2sTYBNjD-g8Jt3wPLW_YWbDtMn8tm-doSPhU7-RHSHON7Vz6o1m6S3x88RODt2uxSifxfgjqXYC_yJiUVEnHp7xokI1X-7EIbv6S_jfD1gKq7gNehkKkm0QETvAX_wmL0hEZyZnBacxjkSZzDZgbfKfj8U2CAU1nY6O5vk90q0HdTrt98RL37Uiz62ftAdBZCLSpZ1AS1hGa1S3NDMhWbWvBVCMY59bhM8lreH-Q1znlSe9jNXbElvrKofSc3Jz2WeTIj3Ifq5Ev-p4rIOoILww8kE9ZKp4s28JXMdrpRnqFM7tufggjZaKx5g1_rwRnzl-dk50RQ`;
const { jobs } = await client.request(
gql`
query GET_JOBS($bodyshopids: [uuid!]!) {
jobs(where: { shopid: { _in: $bodyshopids } }, order_by: { created_at: desc }) {
id
ro_number
job_totals
cieca_ttl
}
}
`,
{
bodyshopids
}
);
const results = [];
for (const [index, job] of jobs.entries()) {
process.stdout.cursorTo(0);
process.stdout.write(
`Processing job ${index + 1} of ${jobs.length}. Failed jobs: ${results.filter((r) => r.result !== "PASS").length}`
);
try {
await axios.post(
`http://localhost:4000/job/totalsssu`,
{ id: job.id },
{ headers: { Authorization: bearerToken } }
);
const { jobs_by_pk: newjob } = await client.request(
gql`
query GET_JOBS($id: uuid!) {
jobs_by_pk(id: $id) {
id
ro_number
cieca_ttl
job_totals
ownr_fn
ownr_ln
ownr_co_nm
ins_co_nm
comment
}
}
`,
{
id: job.id
}
);
const result = {
id: newjob.id,
owner: `${newjob.ownr_fn} ${newjob.ownr_ln} ${job.ownr_co_nm || ""}`,
ins_co: newjob.ins_co_nm,
comment: newjob.comment
};
const calcTotal = newjob.job_totals.totals.total_repairs.amount;
const ttlTotal = newjob.cieca_ttl.data.g_ttl_amt * 100;
result.difference = (calcTotal - ttlTotal) / 100;
if (Math.abs(calcTotal - ttlTotal) > 3) {
//Diff is greater than 5 cents. Fail it.
result.result = "***FAIL***";
} else {
result.result = "PASS";
}
const subcalcTotal = newjob.job_totals.totals.subtotal.amount;
const subttlTotal = newjob.cieca_ttl.data.n_ttl_amt * 100;
result.subdifference = (subcalcTotal - subttlTotal) / 100;
if (Math.abs(subcalcTotal - subttlTotal) > 3) {
//Diff is greater than 5 cents. Fail it.
result.subresult = "***FAIL***";
} else {
result.subresult = "PASS";
}
// console.log(`${result.result} => RO ${job.ro_number} - ${job.id} `);
results.push(result);
} catch (error) {
results.push({
ro_number: job.ro_number,
id: job.id,
result: "**503 FAILURE**"
});
}
}
console.table(results.filter((r) => r.result !== "PASS"));
const summary = results.reduce(
(acc, val) => {
if (val.result === "PASS") {
return { ...acc, pass: acc.pass + 1 };
} else {
return { ...acc, fail: acc.fail + 1 };
}
},
{ pass: 0, fail: 0 }
);
console.log("Pass Rate: ", ((summary.pass / (summary.fail + summary.pass)) * 100).toFixed(1));
}
RunTheTest();
// mutation {
// delete_jobs(where: {shopid: {_eq: "a7ee1503-ee05-4a02-b80e-bdb11d1cc8ac"}}) {
// affected_rows
// }
// delete_owners(where: {shopid: {_eq: "a7ee1503-ee05-4a02-b80e-bdb11d1cc8ac"}}) {
// affected_rows
// }
// delete_vehicles(where: {shopid: {_eq: "a7ee1503-ee05-4a02-b80e-bdb11d1cc8ac"}}) {
// affected_rows
// }
// }