IO-256 Authorization and Basic Calls

This commit is contained in:
Patrick Fic
2021-08-27 15:42:32 -07:00
parent 724c097d52
commit 5284ee2ef9
13 changed files with 6559 additions and 126 deletions

View File

@@ -1,3 +1,4 @@
const urlBuilder = require("./qbo").urlBuilder;
const path = require("path");
require("dotenv").config({
path: path.resolve(
@@ -5,39 +6,102 @@ require("dotenv").config({
`.env.${process.env.NODE_ENV || "development"}`
),
});
const client = require("../../graphql-client/graphql-client").client;
const queries = require("../../graphql-client/queries");
const {
refresh: refreshOauthToken,
setNewRefreshToken,
} = require("./qbo-callback");
const OAuthClient = require("intuit-oauth");
var QuickBooks = require("node-quickbooks");
const oauthClient = new OAuthClient({
clientId: process.env.QBO_CLIENT_ID,
clientSecret: process.env.QBO_SECRET,
environment: process.env.NODE_ENV === "production" ? "production" : "sandbox",
redirectUri: process.env.QBO_REDIRECT_URI,
logging: true,
});
exports.default = async (req, res) => {
try {
oauthClient.setToken(req.cookies.qbo_access_token);
const response = await client.request(queries.GET_QBO_AUTH, {
email: req.user.email,
});
response.associations[0].qbo_auth;
var qbo = new QuickBooks(
process.env.QBO_CLIENT_ID,
process.env.QBO_SECRET,
req.cookies.qbo_access_token,
false, // no token secret for oAuth 2.0
req.cookies.qbo_realmId,
process.env.NODE_ENV !== "production", // use the sandbox?, // use the sandbox?
true, // enable debugging?
null, // set minorversion, or null for the latest version
"2.0", //oAuth version
req.cookies.qbo_refresh_token
);
oauthClient.setToken(response.associations[0].qbo_auth);
qbo.findInvoices({ fetchAll: true }, (errors, invoices) =>
console.log(errors, invoices)
);
if (!oauthClient.token.isAccessTokenValid()) {
await refreshOauthToken(oauthClient, req);
if (!oauthClient.token.isAccessTokenValid()) {
res.sendStatus(401);
}
}
res.send({});
const customerCreate = {
FullyQualifiedName: "A Test Customer",
DisplayName: "A test Customer",
};
// const ret = await oauthClient.makeApiCall({
// url: urlBuilder(req.cookies.qbo_realmId, "customer"),
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify(customerCreate),
// });
// const invoice = {
// Line: [
// {
// DetailType: "SalesItemLineDetail",
// Amount: 100,
// SalesItemLineDetail: {
// ItemRef: {
// name: "Services",
// value: "1",
// },
// TaxCodeRef: {
// value: "2",
// },
// Qty: 1,
// UnitPrice: 100,
// },
// },
// ],
// CustomerRef: {
// name: "A test Customer",
// },
// };
// const ret2 = await oauthClient.makeApiCall({
// url: urlBuilder(req.cookies.qbo_realmId, "invoice"),
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify(invoice),
// });
const ret2 = await oauthClient.makeApiCall({
url: urlBuilder(
req.cookies.qbo_realmId,
"query",
`select * From TaxCode where Active = true`
),
method: "POST",
headers: {
"Content-Type": "application/json",
},
// body: JSON.stringify(invoice),
});
setNewRefreshToken(req.user.email, ret2);
console.log(ret2);
res.send(ret2);
} catch (error) {
console.log(error);
res.sendStatus(500);
res.status(400).json(error);
}
};