Files
bodyshop/job-totals-testing-util.js
Patrick Fic 0a60f77bfc WIP America
2023-01-26 10:58:35 -08:00

104 lines
3.7 KiB
JavaScript

const path = require("path");
const Dinero = require("dinero.js");
const { gql } = require("graphql-request");
const queries = require("./server/graphql-client/queries");
const GraphQLClient = require("graphql-request").GraphQLClient;
const logger = require("./server/utils/logger");
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 = ["6c63a820-542c-497e-8c82-0cc38fb2bbca"];
const bearerToken = `Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImQwNTU5YzU5MDgzZDc3YWI2NDUxOThiNTIxZmM4ZmVmZmVlZmJkNjIiLCJ0eXAiOiJKV1QifQ.eyJuYW1lIjoiUGF0cmljayBGaWMgKERFVikiLCJodHRwczovL2hhc3VyYS5pby9qd3QvY2xhaW1zIjp7IngtaGFzdXJhLWRlZmF1bHQtcm9sZSI6InVzZXIiLCJ4LWhhc3VyYS1hbGxvd2VkLXJvbGVzIjpbInVzZXIiXSwieC1oYXN1cmEtdXNlci1pZCI6ImhOSjhBRHB0REhRQkRFcXNCOFFNWVRqaURuZjEifSwiaXNzIjoiaHR0cHM6Ly9zZWN1cmV0b2tlbi5nb29nbGUuY29tL2ltZXgtZGV2IiwiYXVkIjoiaW1leC1kZXYiLCJhdXRoX3RpbWUiOjE2NzQ1OTgwMTEsInVzZXJfaWQiOiJoTko4QURwdERIUUJERXFzQjhRTVlUamlEbmYxIiwic3ViIjoiaE5KOEFEcHRESFFCREVxc0I4UU1ZVGppRG5mMSIsImlhdCI6MTY3NDY3MzYyOSwiZXhwIjoxNjc0Njc3MjI5LCJlbWFpbCI6InBhdHJpY2tAaW1leC5kZXYiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsiZW1haWwiOlsicGF0cmlja0BpbWV4LmRldiJdfSwic2lnbl9pbl9wcm92aWRlciI6InBhc3N3b3JkIn19.UGFIwVz_B8Q2m7_lmpT8U4Kq0LG3N-yiv4eRhWybme46y1OCiUpw1dJct-vjunprGTrRXdRlWsEOBEmPULYYbKektcHs60boxQxPty9YghageMcknj14ujoLE98Gz3pN6De4mAQMtqYdWaC20OWrH8wIDGkoFMN7PHhY-c2QWdt-UQH_OUW0dqY0vSSJIlC9QNI3Rjn0N6if40j2jnTF9qgIClRjoWN9OBh1s1qIou6yNchnA2v9ni1Js_kGmMJfixmkTxUau3THCxVQAdPtuwURYRn1Xa8Eo5feo0FLoFbPtylHUZyaoogE7q6ZQkHA2A3ylIxWhYUspyPzkIyf_g`;
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 job of jobs) {
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
}
}
`,
{
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,
};
const calcTotal = newjob.job_totals.totals.total_repairs.amount;
const ttlTotal = newjob.cieca_ttl.data.g_ttl_amt * 100;
result.difference = Math.abs(calcTotal - ttlTotal) / 100;
if (Math.abs(calcTotal - ttlTotal) > 5) {
//Diff is greater than 5 cents. Fail it.
result.result = "***FAIL***";
} else {
result.result = "PASS";
}
console.log(`${result.result} => RO ${job.ro_number}`);
results.push(result);
} catch (error) {
results.push({
ro_number: job.ro_number,
id: job.id,
result: "**503 FAILURE**",
});
}
}
console.table(results);
}
RunTheTest();