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

@@ -87,17 +87,17 @@ exports.default = async (req, res) => {
} catch (error) {
logger.log("qbo-paybles-create-error", "ERROR", req.user.email, null, {
error:
(error?.authResponse && error.authResponse.body) ||
error.response?.data?.Fault?.Error.map((e) => e.Detail).join(", ") ||
error?.authResponse?.body ||
error?.response?.data?.Fault?.Error.map((e) => e.Detail).join(", ") ||
error?.message
});
ret.push({
billid: bill.id,
success: false,
errorMessage:
(error && error.authResponse && error.authResponse.body) ||
error.response?.data?.Fault?.Error.map((e) => e.Detail).join(", ") ||
(error && error.message)
error?.authResponse?.body ||
error?.response?.data?.Fault?.Error.map((e) => e.Detail).join(", ") ||
error?.message
});
//Add the export log error.
@@ -108,9 +108,7 @@ exports.default = async (req, res) => {
bodyshopid: bodyshop.id,
billid: bill.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
}
]
@@ -153,7 +151,7 @@ async function QueryVendorRecord(oauthClient, qbo_realmId, req, bill) {
email: req.user.email
});
setNewRefreshToken(req.user.email, result);
return result.json?.QueryResponse?.Vendor[0];
return result.json?.QueryResponse?.Vendor?.[0];
} catch (error) {
logger.log("qbo-payables-error", "DEBUG", req.user.email, bill.id, {
method: "QueryVendorRecord",
@@ -190,7 +188,7 @@ async function InsertVendorRecord(oauthClient, qbo_realmId, req, bill) {
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.Vendor;
} catch (error) {
logger.log("qbo-payables-error", "DEBUG", req.user.email, bill.id, {
method: "InsertVendorRecord",
@@ -248,12 +246,7 @@ async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor, bodyshop)
}
//QB USA with GST
//This was required for the No. 1 Collision Group.
if (
bodyshop.accountingconfig &&
bodyshop.accountingconfig.qbo &&
bodyshop.accountingconfig.qbo_usa &&
bodyshop.region_config.includes("CA_")
) {
if (bodyshop.accountingconfig?.qbo && bodyshop.accountingconfig?.qbo_usa && bodyshop.region_config.includes("CA_")) {
lines.push({
DetailType: "AccountBasedExpenseLineDetail",
@@ -276,7 +269,8 @@ async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor, bodyshop)
});
}
const billQbo = {
let billQbo, VendorCredit;
const billObject = {
VendorRef: {
value: vendor.Id
},
@@ -298,22 +292,30 @@ async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor, bodyshop)
DocNumber: bill.invoice_number,
//...(bill.job.class ? { ClassRef: { Id: classes[bill.job.class] } } : {}),
...(!(
bodyshop.accountingconfig &&
bodyshop.accountingconfig.qbo &&
bodyshop.accountingconfig.qbo_usa &&
bodyshop.accountingconfig?.qbo &&
bodyshop.accountingconfig?.qbo_usa &&
bodyshop.region_config.includes("CA_")
)
? { GlobalTaxCalculation: "TaxExcluded" }
: {}),
...(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 }
}),
PrivateNote: `RO ${bill.job.ro_number || ""}`,
Line: lines
};
if (bill.is_credit_memo) {
VendorCredit = billObject;
} else {
billQbo = billObject;
}
const logKey = bill.is_credit_memo ? "VendorCredit" : "billQbo";
const logValue = bill.is_credit_memo ? VendorCredit : billQbo;
logger.log("qbo-payable-objectlog", "DEBUG", req.user.email, bill.id, {
billQbo
[logKey]: logValue
});
try {
const result = await oauthClient.makeApiCall({
@@ -322,7 +324,7 @@ async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor, bodyshop)
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(billQbo)
body: JSON.stringify(bill.is_credit_memo ? VendorCredit : billQbo)
});
logger.LogIntegrationCall({
platform: "QBO",
@@ -450,30 +452,19 @@ async function QueryMetaData(oauthClient, qbo_realmId, req, bodyshopid) {
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;
});
taxCodes.json?.QueryResponse?.TaxCode?.forEach((t) => {
taxCodeMapping[t.Name] = t.Id;
});
const accountMapping = {};
accounts.json &&
accounts.json.QueryResponse &&
accounts.json.QueryResponse.Account &&
accounts.json.QueryResponse.Account.forEach((t) => {
accountMapping[t.FullyQualifiedName] = t.Id;
});
accounts.json?.QueryResponse?.Account?.forEach((t) => {
accountMapping[t.FullyQualifiedName] = 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 {
accounts: accountMapping,