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

@@ -145,7 +145,7 @@ exports.default = async (req, res) => {
ret.push({ paymentid: payment.id, success: true });
} catch (error) {
logger.log("qbo-payment-create-error", "ERROR", req.user.email, null, {
error: (error && error.authResponse && error.authResponse.body) || (error && error.message)
error: error?.authResponse?.body || error?.message
});
//Add the export log error.
if (elgen) {
@@ -155,9 +155,7 @@ exports.default = async (req, res) => {
bodyshopid: bodyshop.id,
paymentid: payment.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
}
]
@@ -167,7 +165,7 @@ exports.default = async (req, res) => {
ret.push({
paymentid: payment.id,
success: false,
errorMessage: (error && error.authResponse && error.authResponse.body) || (error && error.message)
errorMessage: error?.authResponse?.body || error?.message
});
}
}
@@ -201,9 +199,7 @@ async function InsertPayment(oauthClient, qbo_realmId, req, payment, parentRef)
CustomerRef: {
value: parentRef.Id
},
TxnDate: moment(payment.date) //.tz(bodyshop.timezone)
.format("YYYY-MM-DD"),
//DueDate: bill.due_date && moment(bill.due_date).format("YYYY-MM-DD"),
TxnDate: moment(payment.date).format("YYYY-MM-DD"),
DocNumber: payment.paymentnum,
TotalAmt: Dinero({
amount: Math.round(payment.amount * 100)
@@ -211,19 +207,13 @@ async function InsertPayment(oauthClient, qbo_realmId, req, payment, parentRef)
PaymentMethodRef: {
value: paymentMethods[payment.type]
},
PrivateNote: payment.memo
? payment.memo.length > 4000
? payment.memo.substring(0, 4000).trim()
: payment.memo.trim()
: "",
PrivateNote: payment.memo?.substring(0, 4000)?.trim() ?? "",
PaymentRefNum: payment.transactionid,
...(invoices && invoices.length === 1 && invoices[0]
...(invoices?.length === 1 && invoices[0]
? {
Line: [
{
Amount: Dinero({
amount: Math.round(payment.amount * 100)
}).toFormat(DineroQbFormat),
Amount: Dinero({ amount: Math.round(payment.amount * 100) }).toFormat(DineroQbFormat),
LinkedTxn: [
{
TxnId: invoices[0].Id,
@@ -260,7 +250,7 @@ async function InsertPayment(oauthClient, qbo_realmId, req, payment, parentRef)
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-payables-error", "DEBUG", req.user.email, payment.id, {
method: "InsertPayment",
@@ -273,11 +263,7 @@ async function InsertPayment(oauthClient, qbo_realmId, req, payment, parentRef)
async function QueryMetaData(oauthClient, qbo_realmId, req, ro_number, isCreditMemo, parentTierRef, bodyshopid) {
const invoice = await oauthClient.makeApiCall({
url: urlBuilder(
qbo_realmId,
"query",
`select * From Invoice where DocNumber like '${ro_number}%'`
),
url: urlBuilder(qbo_realmId, "query", `select * From Invoice where DocNumber like '${ro_number}%'`),
method: "POST",
headers: {
"Content-Type": "application/json"
@@ -292,11 +278,7 @@ async function QueryMetaData(oauthClient, qbo_realmId, req, ro_number, isCreditM
email: req.user.email
});
const paymentMethods = await oauthClient.makeApiCall({
url: urlBuilder(
qbo_realmId,
"query",
`select * From PaymentMethod`
),
url: urlBuilder(qbo_realmId, "query", `select * From PaymentMethod`),
method: "POST",
headers: {
"Content-Type": "application/json"
@@ -322,12 +304,9 @@ async function QueryMetaData(oauthClient, qbo_realmId, req, ro_number, isCreditM
const paymentMethodMapping = {};
paymentMethods.json &&
paymentMethods.json.QueryResponse &&
paymentMethods.json.QueryResponse.PaymentMethod &&
paymentMethods.json.QueryResponse.PaymentMethod.forEach((t) => {
paymentMethodMapping[t.Name] = t.Id;
});
paymentMethods.json?.QueryResponse?.PaymentMethod?.forEach((t) => {
paymentMethodMapping[t.Name] = t.Id;
});
// const accountMapping = {};
@@ -347,11 +326,7 @@ async function QueryMetaData(oauthClient, qbo_realmId, req, ro_number, isCreditM
if (isCreditMemo) {
const taxCodes = await oauthClient.makeApiCall({
url: urlBuilder(
qbo_realmId,
"query",
`select * From TaxCode`
),
url: urlBuilder(qbo_realmId, "query", `select * From TaxCode`),
method: "POST",
headers: {
"Content-Type": "application/json"
@@ -366,11 +341,7 @@ async function QueryMetaData(oauthClient, qbo_realmId, req, ro_number, isCreditM
email: req.user.email
});
const items = await oauthClient.makeApiCall({
url: urlBuilder(
qbo_realmId,
"query",
`select * From Item`
),
url: urlBuilder(qbo_realmId, "query", `select * From Item`),
method: "POST",
headers: {
"Content-Type": "application/json"
@@ -388,20 +359,15 @@ async function QueryMetaData(oauthClient, qbo_realmId, req, ro_number, isCreditM
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 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;
});
ret = {
...ret,
items: itemMapping,
@@ -413,12 +379,10 @@ async function QueryMetaData(oauthClient, qbo_realmId, req, ro_number, isCreditM
...ret,
paymentMethods: paymentMethodMapping,
invoices:
invoice.json &&
invoice.json.QueryResponse &&
invoice.json.QueryResponse.Invoice &&
invoice.json?.QueryResponse?.Invoice &&
(parentTierRef
? [invoice.json.QueryResponse.Invoice.find((x) => x.CustomerRef.value === parentTierRef.Id)]
: [invoice.json.QueryResponse.Invoice[0]])
? [invoice.json?.QueryResponse?.Invoice.find((x) => x.CustomerRef?.value === parentTierRef?.Id)]
: [invoice.json?.QueryResponse?.Invoice?.[0]])
};
}
@@ -433,7 +397,7 @@ async function InsertCreditMemo(oauthClient, qbo_realmId, req, payment, parentRe
payment.job.shopid
);
if (invoices && invoices.length !== 1) {
if (invoices?.length !== 1) {
throw new Error(`More than 1 invoice with DocNumber ${payment.ro_number} found.`);
}
@@ -441,11 +405,9 @@ async function InsertCreditMemo(oauthClient, qbo_realmId, req, payment, parentRe
CustomerRef: {
value: parentRef.Id
},
TxnDate: moment(payment.date)
//.tz(bodyshop.timezone)
.format("YYYY-MM-DD"),
TxnDate: moment(payment.date).format("YYYY-MM-DD"),
DocNumber: payment.paymentnum,
...(invoices && invoices[0] ? { InvoiceRef: { value: invoices[0].Id } } : {}),
...(invoices?.[0] ? { InvoiceRef: { value: invoices[0].Id } } : {}),
PaymentRefNum: payment.transactionid,
Line: [
{