IO-256 QBO SS Realm ID

This commit is contained in:
Patrick Fic
2021-10-20 14:33:31 -07:00
parent f3c44f8dd1
commit 14af45baf0
17 changed files with 4270 additions and 131 deletions

View File

@@ -34,7 +34,11 @@ exports.default = async (req, res) => {
const response = await apiGqlClient.request(queries.GET_QBO_AUTH, {
email: req.user.email,
});
const { qbo_realmId } = response.associations[0];
if (!qbo_realmId) {
res.status(401).json({ error: "No company associated." });
return;
}
oauthClient.setToken(response.associations[0].qbo_auth);
await refreshOauthToken(oauthClient, req);
@@ -69,11 +73,17 @@ exports.default = async (req, res) => {
if (isThreeTier || (!isThreeTier && twoTierPref === "source")) {
//Insert the insurance company tier.
//Query for top level customer, the insurance company name.
insCoCustomerTier = await QueryInsuranceCo(oauthClient, req, job);
insCoCustomerTier = await QueryInsuranceCo(
oauthClient,
qbo_realmId,
req,
job
);
if (!insCoCustomerTier) {
//Creating the Insurance Customer.
insCoCustomerTier = await InsertInsuranceCo(
oauthClient,
qbo_realmId,
req,
job,
bodyshop
@@ -83,11 +93,17 @@ exports.default = async (req, res) => {
if (isThreeTier || (!isThreeTier && twoTierPref === "name")) {
//Insert the name/owner and account for whether the source should be the ins co in 3 tier..
ownerCustomerTier = await QueryOwner(oauthClient, req, job);
ownerCustomerTier = await QueryOwner(
oauthClient,
qbo_realmId,
req,
job
);
//Query for the owner itself.
if (!ownerCustomerTier) {
ownerCustomerTier = await InsertOwner(
oauthClient,
qbo_realmId,
req,
job,
isThreeTier,
@@ -97,13 +113,14 @@ exports.default = async (req, res) => {
}
//Query for the Job or Create it.
jobTier = await QueryJob(oauthClient, req, job);
jobTier = await QueryJob(oauthClient, qbo_realmId, req, job);
// Need to validate that the job tier is associated to the right individual?
if (!jobTier) {
jobTier = await InsertJob(
oauthClient,
qbo_realmId,
req,
job,
@@ -112,7 +129,14 @@ exports.default = async (req, res) => {
}
if (!req.body.custDataOnly) {
await InsertInvoice(oauthClient, req, job, bodyshop, jobTier);
await InsertInvoice(
oauthClient,
qbo_realmId,
req,
job,
bodyshop,
jobTier
);
}
ret.push({ jobid: job.id, success: true });
} catch (error) {
@@ -136,11 +160,11 @@ exports.default = async (req, res) => {
}
};
async function QueryInsuranceCo(oauthClient, req, job) {
async function QueryInsuranceCo(oauthClient, qbo_realmId, req, job) {
try {
const result = await oauthClient.makeApiCall({
url: urlBuilder(
req.cookies.qbo_realmId,
qbo_realmId,
"query",
`select * From Customer where DisplayName = '${job.ins_co_nm}'`
),
@@ -165,7 +189,7 @@ async function QueryInsuranceCo(oauthClient, req, job) {
}
}
exports.QueryInsuranceCo = QueryInsuranceCo;
async function InsertInsuranceCo(oauthClient, req, job, bodyshop) {
async function InsertInsuranceCo(oauthClient, qbo_realmId, req, job, bodyshop) {
const insCo = bodyshop.md_ins_cos.find((i) => i.name === job.ins_co_nm);
const Customer = {
@@ -180,7 +204,7 @@ async function InsertInsuranceCo(oauthClient, req, job, bodyshop) {
};
try {
const result = await oauthClient.makeApiCall({
url: urlBuilder(req.cookies.qbo_realmId, "customer"),
url: urlBuilder(qbo_realmId, "customer"),
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -198,11 +222,11 @@ async function InsertInsuranceCo(oauthClient, req, job, bodyshop) {
}
}
exports.InsertInsuranceCo = InsertInsuranceCo;
async function QueryOwner(oauthClient, req, job) {
async function QueryOwner(oauthClient, qbo_realmId, req, job) {
const ownerName = generateOwnerTier(job, true, null);
const result = await oauthClient.makeApiCall({
url: urlBuilder(
req.cookies.qbo_realmId,
qbo_realmId,
"query",
`select * From Customer where DisplayName = '${ownerName}'`
),
@@ -220,7 +244,14 @@ async function QueryOwner(oauthClient, req, job) {
);
}
exports.QueryOwner = QueryOwner;
async function InsertOwner(oauthClient, req, job, isThreeTier, parentTierRef) {
async function InsertOwner(
oauthClient,
qbo_realmId,
req,
job,
isThreeTier,
parentTierRef
) {
const ownerName = generateOwnerTier(job, true, null);
const Customer = {
DisplayName: ownerName,
@@ -242,7 +273,7 @@ async function InsertOwner(oauthClient, req, job, isThreeTier, parentTierRef) {
};
try {
const result = await oauthClient.makeApiCall({
url: urlBuilder(req.cookies.qbo_realmId, "customer"),
url: urlBuilder(qbo_realmId, "customer"),
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -260,10 +291,10 @@ async function InsertOwner(oauthClient, req, job, isThreeTier, parentTierRef) {
}
}
exports.InsertOwner = InsertOwner;
async function QueryJob(oauthClient, req, job) {
async function QueryJob(oauthClient, qbo_realmId, req, job) {
const result = await oauthClient.makeApiCall({
url: urlBuilder(
req.cookies.qbo_realmId,
qbo_realmId,
"query",
`select * From Customer where DisplayName = '${job.ro_number}'`
),
@@ -281,7 +312,7 @@ async function QueryJob(oauthClient, req, job) {
);
}
exports.QueryJob = QueryJob;
async function InsertJob(oauthClient, req, job, parentTierRef) {
async function InsertJob(oauthClient, qbo_realmId, req, job, parentTierRef) {
const Customer = {
DisplayName: job.ro_number,
BillAddr: {
@@ -299,7 +330,7 @@ async function InsertJob(oauthClient, req, job, parentTierRef) {
};
try {
const result = await oauthClient.makeApiCall({
url: urlBuilder(req.cookies.qbo_realmId, "customer"),
url: urlBuilder(qbo_realmId, "customer"),
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -317,9 +348,9 @@ async function InsertJob(oauthClient, req, job, parentTierRef) {
}
}
exports.InsertJob = InsertJob;
async function QueryMetaData(oauthClient, req) {
async function QueryMetaData(oauthClient, qbo_realmId, req) {
const items = await oauthClient.makeApiCall({
url: urlBuilder(req.cookies.qbo_realmId, "query", `select * From Item`),
url: urlBuilder(qbo_realmId, "query", `select * From Item`),
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -327,7 +358,7 @@ async function QueryMetaData(oauthClient, req) {
});
setNewRefreshToken(req.user.email, items);
const taxCodes = await oauthClient.makeApiCall({
url: urlBuilder(req.cookies.qbo_realmId, "query", `select * From TaxCode`),
url: urlBuilder(qbo_realmId, "query", `select * From TaxCode`),
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -335,7 +366,7 @@ async function QueryMetaData(oauthClient, req) {
});
const classes = await oauthClient.makeApiCall({
url: urlBuilder(req.cookies.qbo_realmId, "query", `select * From Class`),
url: urlBuilder(qbo_realmId, "query", `select * From Class`),
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -375,8 +406,19 @@ async function QueryMetaData(oauthClient, req) {
};
}
async function InsertInvoice(oauthClient, req, job, bodyshop, parentTierRef) {
const { items, taxCodes, classes } = await QueryMetaData(oauthClient, req);
async function InsertInvoice(
oauthClient,
qbo_realmId,
req,
job,
bodyshop,
parentTierRef
) {
const { items, taxCodes, classes } = await QueryMetaData(
oauthClient,
qbo_realmId,
req
);
const InvoiceLineAdd = CreateInvoiceLines({
bodyshop,
jobs_by_pk: job,
@@ -407,7 +449,7 @@ async function InsertInvoice(oauthClient, req, job, bodyshop, parentTierRef) {
try {
const result = await oauthClient.makeApiCall({
url: urlBuilder(req.cookies.qbo_realmId, "invoice"),
url: urlBuilder(qbo_realmId, "invoice"),
method: "POST",
headers: {