IO-256 QBO SS Realm ID
This commit is contained in:
@@ -34,10 +34,14 @@ exports.default = async (req, res) => {
|
||||
const response = await apiGqlClient.request(queries.GET_QBO_AUTH, {
|
||||
email: req.user.email,
|
||||
});
|
||||
const { qbo_realmId } = response.associations[0];
|
||||
|
||||
oauthClient.setToken(response.associations[0].qbo_auth);
|
||||
|
||||
await refreshOauthToken(oauthClient, req);
|
||||
if (!qbo_realmId) {
|
||||
res.status(401).json({ error: "No company associated." });
|
||||
return;
|
||||
}
|
||||
await refreshOauthToken(oauthClient, qbo_realmId, req);
|
||||
|
||||
const BearerToken = req.headers.authorization;
|
||||
const { bills: billsToQuery } = req.body;
|
||||
@@ -60,14 +64,26 @@ exports.default = async (req, res) => {
|
||||
for (const bill of bills) {
|
||||
try {
|
||||
let vendorRecord;
|
||||
vendorRecord = await QueryVendorRecord(oauthClient, req, bill);
|
||||
vendorRecord = await QueryVendorRecord(
|
||||
oauthClient,
|
||||
qbo_realmId,
|
||||
qbo_realmId,
|
||||
req,
|
||||
bill
|
||||
);
|
||||
|
||||
if (!vendorRecord) {
|
||||
vendorRecord = await InsertVendorRecord(oauthClient, req, bill);
|
||||
vendorRecord = await InsertVendorRecord(
|
||||
oauthClient,
|
||||
qbo_realmId,
|
||||
req,
|
||||
bill
|
||||
);
|
||||
}
|
||||
|
||||
const insertResults = await InsertBill(
|
||||
oauthClient,
|
||||
qbo_realmId,
|
||||
req,
|
||||
bill,
|
||||
vendorRecord
|
||||
@@ -93,11 +109,11 @@ exports.default = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
async function QueryVendorRecord(oauthClient, req, bill) {
|
||||
async function QueryVendorRecord(oauthClient, qbo_realmId, req, bill) {
|
||||
try {
|
||||
const result = await oauthClient.makeApiCall({
|
||||
url: urlBuilder(
|
||||
req.cookies.qbo_realmId,
|
||||
qbo_realmId,
|
||||
"query",
|
||||
`select * From vendor where DisplayName = '${bill.vendor.name}'`
|
||||
),
|
||||
@@ -123,13 +139,13 @@ async function QueryVendorRecord(oauthClient, req, bill) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
async function InsertVendorRecord(oauthClient, req, bill) {
|
||||
async function InsertVendorRecord(oauthClient, qbo_realmId, req, bill) {
|
||||
const Vendor = {
|
||||
DisplayName: bill.vendor.name,
|
||||
};
|
||||
try {
|
||||
const result = await oauthClient.makeApiCall({
|
||||
url: urlBuilder(req.cookies.qbo_realmId, "vendor"),
|
||||
url: urlBuilder(qbo_realmId, "vendor"),
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@@ -149,8 +165,12 @@ async function InsertVendorRecord(oauthClient, req, bill) {
|
||||
}
|
||||
}
|
||||
|
||||
async function InsertBill(oauthClient, req, bill, vendor) {
|
||||
const { accounts, taxCodes, classes } = await QueryMetaData(oauthClient, req);
|
||||
async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor) {
|
||||
const { accounts, taxCodes, classes } = await QueryMetaData(
|
||||
oauthClient,
|
||||
qbo_realmId,
|
||||
req
|
||||
);
|
||||
|
||||
const billQbo = {
|
||||
VendorRef: {
|
||||
@@ -182,7 +202,7 @@ async function InsertBill(oauthClient, req, bill, vendor) {
|
||||
try {
|
||||
const result = await oauthClient.makeApiCall({
|
||||
url: urlBuilder(
|
||||
req.cookies.qbo_realmId,
|
||||
qbo_realmId,
|
||||
bill.is_credit_memo ? "vendorcredit" : "bill"
|
||||
),
|
||||
method: "POST",
|
||||
@@ -248,10 +268,10 @@ const generateBillLine = (
|
||||
};
|
||||
};
|
||||
|
||||
async function QueryMetaData(oauthClient, req) {
|
||||
async function QueryMetaData(oauthClient, qbo_realmId, req) {
|
||||
const accounts = await oauthClient.makeApiCall({
|
||||
url: urlBuilder(
|
||||
req.cookies.qbo_realmId,
|
||||
qbo_realmId,
|
||||
"query",
|
||||
`select * From Account where AccountType = 'Cost of Goods Sold'`
|
||||
),
|
||||
@@ -262,7 +282,7 @@ async function QueryMetaData(oauthClient, req) {
|
||||
});
|
||||
setNewRefreshToken(req.user.email, accounts);
|
||||
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",
|
||||
@@ -270,7 +290,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",
|
||||
|
||||
Reference in New Issue
Block a user