IO-3498 QBO Fix for Returned Data from oauthClient

Signed-off-by: Allan Carr <allan@imexsystems.ca>
This commit is contained in:
Allan Carr
2026-01-16 16:37:29 -08:00
parent f40af8cba4
commit 6a521c0f46
3 changed files with 118 additions and 216 deletions

View File

@@ -103,7 +103,7 @@ exports.default = async (req, res) => {
if (!req.body.custDataOnly) {
await InsertInvoice(oauthClient, qbo_realmId, req, job, bodyshop, jobTier);
if (job.qb_multiple_payers && job.qb_multiple_payers.length > 0) {
if (job.qb_multiple_payers?.length > 0) {
for (const [index, payer] of job.qb_multiple_payers.entries()) {
//do the thing.
@@ -150,23 +150,21 @@ exports.default = async (req, res) => {
// //No error. Mark the job exported & insert export log.
if (elgen) {
await client
.setHeaders({ Authorization: BearerToken })
.request(queries.QBO_MARK_JOB_EXPORTED, {
jobId: job.id,
job: {
status: bodyshop.md_ro_statuses.default_exported || "Exported*",
date_exported: moment().tz(bodyshop.timezone)
},
logs: [
{
bodyshopid: bodyshop.id,
jobid: job.id,
successful: true,
useremail: req.user.email
}
]
});
await client.setHeaders({ Authorization: BearerToken }).request(queries.QBO_MARK_JOB_EXPORTED, {
jobId: job.id,
job: {
status: bodyshop.md_ro_statuses.default_exported || "Exported*",
date_exported: moment().tz(bodyshop.timezone)
},
logs: [
{
bodyshopid: bodyshop.id,
jobid: job.id,
successful: true,
useremail: req.user.email
}
]
});
}
}
ret.push({ jobid: job.id, success: true });
@@ -193,9 +191,7 @@ exports.default = async (req, res) => {
bodyshopid: bodyshop.id,
jobid: job.id,
successful: false,
message: JSON.stringify([
(error && error.authResponse && error.authResponse.body) || (error && error.message)
]),
message: JSON.stringify([error?.authResponse?.body || error?.message]),
useremail: req.user.email
}
]
@@ -232,18 +228,13 @@ async function QueryInsuranceCo(oauthClient, qbo_realmId, req, job) {
platform: "QBO",
method: "POST",
name: "QueryCustomer",
status: result.response?.status,
status: result.status,
bodyshopid: job.shopid,
jobid: job.id,
email: req.user.email
});
setNewRefreshToken(req.user.email, result);
return (
result.json &&
result.json.QueryResponse &&
result.json.QueryResponse.Customer &&
result.json.QueryResponse.Customer[0]
);
return result.json?.QueryResponse?.Customer?.[0];
} catch (error) {
logger.log("qbo-receivables-error", "DEBUG", req.user.email, job.id, {
error,
@@ -287,13 +278,13 @@ async function InsertInsuranceCo(oauthClient, qbo_realmId, req, job, bodyshop) {
platform: "QBO",
method: "POST",
name: "InsertCustomer",
status: result.response.status,
status: result.status,
bodyshopid: job.shopid,
jobid: job.id,
email: req.user.email
});
setNewRefreshToken(req.user.email, result);
return result && result.json.Customer;
return result.json?.Customer;
} catch (error) {
logger.log("qbo-receivables-error", "DEBUG", req.user.email, job.id, {
error,
@@ -322,18 +313,13 @@ async function QueryOwner(oauthClient, qbo_realmId, req, job, parentTierRef) {
platform: "QBO",
method: "POST",
name: "QueryCustomer",
status: result.response?.status,
status: result.status,
bodyshopid: job.shopid,
jobid: job.id,
email: req.user.email
});
setNewRefreshToken(req.user.email, result);
return (
result.json &&
result.json.QueryResponse &&
result.json.QueryResponse.Customer &&
result.json.QueryResponse.Customer.find((x) => x.ParentRef?.value === parentTierRef?.Id)
);
return result.json?.QueryResponse?.Customer?.find((x) => x.ParentRef?.value === parentTierRef?.Id);
}
exports.QueryOwner = QueryOwner;
@@ -373,13 +359,13 @@ async function InsertOwner(oauthClient, qbo_realmId, req, job, isThreeTier, pare
platform: "QBO",
method: "POST",
name: "InsertCustomer",
status: result.response?.status,
status: result.status,
bodyshopid: job.shopid,
jobid: job.id,
email: req.user.email
});
setNewRefreshToken(req.user.email, result);
return result && result.json.Customer;
return result.json?.Customer;
} catch (error) {
logger.log("qbo-receivables-error", "DEBUG", req.user.email, job.id, {
error,
@@ -407,20 +393,14 @@ async function QueryJob(oauthClient, qbo_realmId, req, job, parentTierRef) {
platform: "QBO",
method: "POST",
name: "QueryCustomer",
status: result.response?.status,
status: result.status,
bodyshopid: job.shopid,
jobid: job.id,
email: req.user.email
});
setNewRefreshToken(req.user.email, result);
return (
result.json &&
result.json.QueryResponse &&
result.json.QueryResponse.Customer &&
(parentTierRef
? result.json.QueryResponse.Customer.find((x) => x.ParentRef.value === parentTierRef.Id)
: result.json.QueryResponse.Customer[0])
);
const customers = result.json?.QueryResponse?.Customer;
return customers && (parentTierRef ? customers.find((x) => x.ParentRef.value === parentTierRef.Id) : customers[0]);
}
exports.QueryJob = QueryJob;
@@ -464,7 +444,7 @@ async function InsertJob(oauthClient, qbo_realmId, req, job, parentTierRef) {
if (result.status >= 400) {
throw new Error(JSON.stringify(result.json.Fault));
}
if (result.status === 200) return result?.json;
if (result.status === 200) return result?.json.Customer;
} catch (error) {
logger.log("qbo-receivables-error", "DEBUG", req.user.email, job.id, {
method: "InsertOwner",
@@ -479,13 +459,7 @@ exports.InsertJob = InsertJob;
async function QueryMetaData(oauthClient, qbo_realmId, req, bodyshopid, jobid) {
const items = await oauthClient.makeApiCall({
url: urlBuilder(
qbo_realmId,
"query",
`select *
From Item
where active = true maxresults 1000`
),
url: urlBuilder(qbo_realmId, "query", `select * From Item where active = true maxresults 1000`),
method: "POST",
headers: {
"Content-Type": "application/json"
@@ -502,13 +476,7 @@ async function QueryMetaData(oauthClient, qbo_realmId, req, bodyshopid, jobid) {
});
setNewRefreshToken(req.user.email, items);
const taxCodes = await oauthClient.makeApiCall({
url: urlBuilder(
qbo_realmId,
"query",
`select *
From TaxCode
where active = true`
),
url: urlBuilder(qbo_realmId, "query", `select * From TaxCode where active = true`),
method: "POST",
headers: {
"Content-Type": "application/json"
@@ -524,12 +492,7 @@ async function QueryMetaData(oauthClient, qbo_realmId, req, bodyshopid, jobid) {
email: req.user.email
});
const classes = await oauthClient.makeApiCall({
url: urlBuilder(
qbo_realmId,
"query",
`select *
From Class`
),
url: urlBuilder(qbo_realmId, "query", `select * From Class`),
method: "POST",
headers: {
"Content-Type": "application/json"
@@ -544,31 +507,21 @@ async function QueryMetaData(oauthClient, qbo_realmId, req, bodyshopid, jobid) {
jobid: jobid,
email: req.user.email
});
const taxCodeMapping = {};
taxCodes.json &&
taxCodes.json.QueryResponse &&
taxCodes.json.QueryResponse.TaxCode &&
taxCodes.json.QueryResponse.TaxCode.forEach((t) => {
taxCodeMapping[t.Name] = t.Id;
});
const taxCodeMapping = {};
taxCodes.json?.QueryResponse?.TaxCode?.forEach((t) => {
taxCodeMapping[t.Name] = t.Id;
});
const itemMapping = {};
items.json &&
items.json.QueryResponse &&
items.json.QueryResponse.Item &&
items.json.QueryResponse.Item.forEach((t) => {
itemMapping[t.Name] = t.Id;
});
items.json?.QueryResponse?.Item?.forEach((t) => {
itemMapping[t.Name] = t.Id;
});
const classMapping = {};
classes.json &&
classes.json.QueryResponse &&
classes.json.QueryResponse.Class &&
classes.json.QueryResponse.Class.forEach((t) => {
classMapping[t.Name] = t.Id;
});
classes.json?.QueryResponse?.Class?.forEach((t) => {
classMapping[t.Name] = t.Id;
});
return {
items: itemMapping,
@@ -601,12 +554,11 @@ async function InsertInvoice(oauthClient, qbo_realmId, req, job, bodyshop, paren
} ${job.v_vin || ""} ${job.plate_no || ""} `.trim()
},
CustomerRef: {
value: parentTierRef.Id
value: parentTierRef?.Id
},
...(bodyshop.accountingconfig.qbo_departmentid &&
bodyshop.accountingconfig.qbo_departmentid.trim() !== "" && {
DepartmentRef: { value: bodyshop.accountingconfig.qbo_departmentid }
}),
...(bodyshop.accountingconfig.qbo_departmentid?.trim() !== "" && {
DepartmentRef: { value: bodyshop.accountingconfig.qbo_departmentid }
}),
CustomField: [
...(bodyshop.accountingconfig.ReceivableCustomField1
? [
@@ -636,9 +588,8 @@ async function InsertInvoice(oauthClient, qbo_realmId, req, job, bodyshop, paren
]
: [])
],
...(bodyshop.accountingconfig &&
bodyshop.accountingconfig.qbo &&
bodyshop.accountingconfig.qbo_usa && {
...(bodyshop.accountingconfig?.qbo &&
bodyshop.accountingconfig?.qbo_usa && {
TxnTaxDetail: {
TxnTaxCodeRef: {
value: taxCodes[bodyshop.md_responsibility_centers.taxes.state.accountitem]
@@ -732,10 +683,9 @@ async function InsertInvoiceMultiPayerInvoice(
CustomerRef: {
value: parentTierRef.Id
},
...(bodyshop.accountingconfig.qbo_departmentid &&
bodyshop.accountingconfig.qbo_departmentid.trim() !== "" && {
DepartmentRef: { value: bodyshop.accountingconfig.qbo_departmentid }
}),
...(bodyshop.accountingconfig.qbo_departmentid?.trim() !== "" && {
DepartmentRef: { value: bodyshop.accountingconfig.qbo_departmentid }
}),
CustomField: [
...(bodyshop.accountingconfig.ReceivableCustomField1
? [
@@ -765,9 +715,8 @@ async function InsertInvoiceMultiPayerInvoice(
]
: [])
],
...(bodyshop.accountingconfig &&
bodyshop.accountingconfig.qbo &&
bodyshop.accountingconfig.qbo_usa &&
...(bodyshop.accountingconfig?.qbo &&
bodyshop.accountingconfig?.qbo_usa &&
bodyshop.region_config.includes("CA_") && {
TxnTaxDetail: {
TxnTaxCodeRef: {