IO-1855 Change QBO changes to server side.

This commit is contained in:
Patrick Fic
2022-05-02 15:52:05 -07:00
parent 2d9de4703b
commit 4f6d1d27d5
18 changed files with 567 additions and 337 deletions

View File

@@ -71,7 +71,7 @@ exports.default = async (req, res) => {
exports.refresh = async (oauthClient, req) => {
try {
logger.log("qbo-token-refresh", "DEBUG", req.user.email, null, null);
// logger.log("qbo-token-refresh", "DEBUG", req.user.email, null, null);
const authResponse = await oauthClient.refresh();
await client.request(queries.SET_QBO_AUTH, {
email: req.user.email,
@@ -85,7 +85,7 @@ exports.refresh = async (oauthClient, req) => {
};
exports.setNewRefreshToken = async (email, apiResponse) => {
logger.log("qbo-token-updated", "DEBUG", email, null, null);
//logger.log("qbo-token-updated", "DEBUG", email, null, null);
await client.request(queries.SET_QBO_AUTH, {
email,

View File

@@ -59,8 +59,9 @@ exports.default = async (req, res) => {
bills: billsToQuery,
});
const { bills } = result;
const { bills, bodyshops } = result;
const ret = [];
const bodyshop = bodyshops[0];
for (const bill of bills) {
try {
@@ -86,9 +87,29 @@ exports.default = async (req, res) => {
qbo_realmId,
req,
bill,
vendorRecord
vendorRecord,
bodyshop
);
// //No error. Mark the job exported & insert export log.
const result = await client
.setHeaders({ Authorization: BearerToken })
.request(queries.QBO_MARK_BILL_EXPORTED, {
billId: bill.id,
bill: {
exported: true,
exported_at: moment().tz(bodyshop.timezone),
},
logs: [
{
bodyshopid: bodyshop.id,
billid: bill.id,
successful: true,
useremail: req.user.email,
},
],
});
ret.push({ billid: bill.id, success: true });
} catch (error) {
ret.push({
@@ -98,6 +119,24 @@ exports.default = async (req, res) => {
(error && error.authResponse && error.authResponse.body) ||
(error && error.message),
});
//Add the export log error.
const result = await client
.setHeaders({ Authorization: BearerToken })
.request(queries.INSERT_EXPORT_LOG, {
logs: [
{
bodyshopid: bodyshop.id,
billid: bill.id,
successful: false,
message: JSON.stringify([
(error && error.authResponse && error.authResponse.body) ||
(error && error.message),
]),
useremail: req.user.email,
},
],
});
}
}
@@ -167,7 +206,7 @@ async function InsertVendorRecord(oauthClient, qbo_realmId, req, bill) {
}
}
async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor) {
async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor, bodyshop) {
const { accounts, taxCodes, classes } = await QueryMetaData(
oauthClient,
qbo_realmId,
@@ -179,20 +218,20 @@ async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor) {
il,
accounts,
bill.job.class,
bill.job.bodyshop.md_responsibility_centers.sales_tax_codes,
bodyshop.md_responsibility_centers.sales_tax_codes,
classes,
taxCodes,
bill.job.bodyshop.md_responsibility_centers.costs
bodyshop.md_responsibility_centers.costs
)
);
//QB USA with GST
//This was required for the No. 1 Collision Group.
if (
bill.job.bodyshop.accountingconfig &&
bill.job.bodyshop.accountingconfig.qbo &&
bill.job.bodyshop.accountingconfig.qbo_usa &&
bill.job.bodyshop.region_config.includes("CA_")
bodyshop.accountingconfig &&
bodyshop.accountingconfig.qbo &&
bodyshop.accountingconfig.qbo_usa &&
bodyshop.region_config.includes("CA_")
) {
lines.push({
DetailType: "AccountBasedExpenseLineDetail",
@@ -204,7 +243,7 @@ async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor) {
AccountRef: {
value:
accounts[
bill.job.bodyshop.md_responsibility_centers.taxes.federal
bodyshop.md_responsibility_centers.taxes.federal
.accountdesc
],
},
@@ -239,6 +278,14 @@ async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor) {
}),
DocNumber: bill.invoice_number,
//...(bill.job.class ? { ClassRef: { Id: classes[bill.job.class] } } : {}),
...(!(
bodyshop.accountingconfig &&
bodyshop.accountingconfig.qbo &&
bodyshop.accountingconfig.qbo_usa &&
bodyshop.region_config.includes("CA_")
)
? { GlobalTaxCalculation: "TaxExcluded" }
: {}),
PrivateNote: `RO ${bill.job.ro_number || ""}`,
Line: lines,

View File

@@ -155,6 +155,25 @@ exports.default = async (req, res) => {
bodyshop
);
}
// //No error. Mark the payment exported & insert export log.
const result = await client
.setHeaders({ Authorization: BearerToken })
.request(queries.QBO_MARK_PAYMENT_EXPORTED, {
paymentId: payment.id,
payment: {
exportedat: moment().tz(bodyshop.timezone),
},
logs: [
{
bodyshopid: bodyshop.id,
paymentid: payment.id,
successful: true,
useremail: req.user.email,
},
],
});
ret.push({ paymentid: payment.id, success: true });
} catch (error) {
logger.log("qbo-payment-create-error", "ERROR", req.user.email, {
@@ -162,7 +181,24 @@ exports.default = async (req, res) => {
(error && error.authResponse && error.authResponse.body) ||
(error && error.message),
});
//Add the export log error.
const result = await client
.setHeaders({ Authorization: BearerToken })
.request(queries.INSERT_EXPORT_LOG, {
logs: [
{
bodyshopid: bodyshop.id,
paymentid: payment.id,
successful: false,
message: JSON.stringify([
(error && error.authResponse && error.authResponse.body) ||
(error && error.message),
]),
useremail: req.user.email,
},
],
});
ret.push({
paymentid: payment.id,
success: false,

View File

@@ -139,6 +139,25 @@ exports.default = async (req, res) => {
bodyshop,
jobTier
);
// //No error. Mark the job exported & insert export log.
const result = 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 });
} catch (error) {
@@ -149,6 +168,23 @@ exports.default = async (req, res) => {
(error && error.authResponse && error.authResponse.body) ||
(error && error.message),
});
//Add the export log error.
const result = await client
.setHeaders({ Authorization: BearerToken })
.request(queries.INSERT_EXPORT_LOG, {
logs: [
{
bodyshopid: bodyshop.id,
jobid: job.id,
successful: false,
message: JSON.stringify([
(error && error.authResponse && error.authResponse.body) ||
(error && error.message),
]),
useremail: req.user.email,
},
],
});
}
}

View File

@@ -37,14 +37,15 @@ exports.default = async (req, res) => {
.request(queries.QUERY_BILLS_FOR_PAYABLES_EXPORT, {
bills: billsToQuery,
});
const { bills } = result;
const { bills, bodyshops } = result;
const bodyshop = bodyshops[0];
const QbXmlToExecute = [];
bills.map((i) => {
QbXmlToExecute.push({
id: i.id,
okStatusCodes: ["0"],
qbxml: generateBill(i),
qbxml: generateBill(i, bodyshop),
});
});
@@ -62,7 +63,7 @@ exports.default = async (req, res) => {
}
};
const generateBill = (bill) => {
const generateBill = (bill, bodyshop) => {
const billQbxmlObj = {
QBXML: {
QBXMLMsgsRq: {
@@ -87,7 +88,7 @@ const generateBill = (bill) => {
ExpenseLineAdd: bill.billlines.map((il) =>
generateBillLine(
il,
bill.job.bodyshop.md_responsibility_centers,
bodyshop.md_responsibility_centers,
bill.job.class
)
),

View File

@@ -217,6 +217,7 @@ query QUERY_JOBS_FOR_RECEIVABLES_EXPORT($ids: [uuid!]!) {
accountingconfig
md_ins_cos
timezone
md_ro_statuses
}
}
`;
@@ -408,6 +409,13 @@ query QUERY_JOBS_FOR_PBS_EXPORT($id: uuid!) {
exports.QUERY_BILLS_FOR_PAYABLES_EXPORT = `
query QUERY_BILLS_FOR_PAYABLES_EXPORT($bills: [uuid!]!) {
bodyshops(where: {associations: {active: {_eq: true}}}) {
id
md_responsibility_centers
timezone
region_config
accountingconfig
}
bills(where: {id: {_in: $bills}}) {
id
date
@@ -424,12 +432,6 @@ query QUERY_BILLS_FOR_PAYABLES_EXPORT($bills: [uuid!]!) {
ownr_ln
ownr_co_nm
class
bodyshop{
md_responsibility_centers
timezone
region_config
accountingconfig
}
}
billlines{
id
@@ -1482,6 +1484,7 @@ mutation MARK_JOB_EXPORTED($jobId: uuid!, $job: jobs_set_input!, $log: exportlog
}
}
`;
exports.INSERT_EXPORT_LOG = `
mutation INSERT_EXPORT_LOG($log: exportlog_insert_input!) {
insert_exportlog_one(object: $log) {
@@ -1534,4 +1537,52 @@ exports.QUERY_JOB_ID_MIXDATA = `query QUERY_JOB_ID_MIXDATA($roNumbers: [String!]
}
}
`;
`;
exports.QBO_MARK_JOB_EXPORTED = `
mutation QBO_MARK_JOB_EXPORTED($jobId: uuid!, $job: jobs_set_input!, $logs: [exportlog_insert_input!]!) {
insert_exportlog(objects: $logs) {
affected_rows
}
update_jobs(where: {id: {_eq: $jobId}}, _set: $job) {
returning {
id
}
}
}
`;
exports.QBO_MARK_BILL_EXPORTED = `
mutation QBO_MARK_BILL_EXPORTED($billId: uuid!, $bill: bills_set_input!, $logs: [exportlog_insert_input!]!) {
insert_exportlog(objects: $logs) {
affected_rows
}
update_bills(where: { id: { _eq: $billId } }, _set: $bill) {
returning {
id
}
}
}
`;
exports.QBO_MARK_PAYMENT_EXPORTED = `
mutation QBO_MARK_PAYMENT_EXPORTED($paymentId: uuid!, $payment: payments_set_input!, $logs: [exportlog_insert_input!]!) {
insert_exportlog(objects: $logs) {
affected_rows
}
update_payments(where: {id: {_eq: $paymentId}}, _set: $payment) {
returning {
id
}
}
}`;
exports.INSERT_EXPORT_LOG = `
mutation INSERT_EXPORT_LOG($logs: [exportlog_insert_input!]!) {
insert_exportlog(objects: $logs) {
affected_rows
}
}
`;