Files
bodyshop/fortellis.js
2024-05-22 08:18:10 -07:00

267 lines
9.1 KiB
JavaScript

const path = require("path");
const Dinero = require("dinero.js");
const { gql } = require("graphql-request");
const queries = require("./server/graphql-client/queries");
const GraphQLClient = require("graphql-request").GraphQLClient;
const logger = require("./server/utils/logger");
const AxiosLib = require("axios").default;
const axios = AxiosLib.create();
const uuid = require("uuid").v4;
const FORTELLIS_KEY = "X1FxzLyOk3kjHvMbzdPQXFZShkdbgzuo";
const FORTELLIS_SECRET = "JPSAqenpF4CT2buD";
const FORTELLIS_AUTH_URL = "https://identity.fortellis.io/oauth2/aus1p1ixy7YL8cMq02p7/v1/token";
const FORTELLIS_URL = "https://api.fortellis.io";
const SubscriptionID = "5b527d7d-baf3-40bc-adae-e7a541e37363";
//const SubscriptionID = "cb59fa04-e53e-4b57-b071-80a48ebc346c";
function sleep(time, callback) {
var stop = new Date().getTime();
while (new Date().getTime() < stop + time) {}
callback();
}
async function GetAuthToken() {
const {
data: { access_token, expires_in, token_type }
} = await axios.post(
FORTELLIS_AUTH_URL,
{},
{
auth: {
username: FORTELLIS_KEY,
password: FORTELLIS_SECRET
},
params: {
grant_type: "client_credentials",
scope: "anonymous"
}
}
);
return access_token;
}
async function FetchSubscriptions() {
const access_token = await GetAuthToken();
try {
const subscriptions = await axios.get(`https://subscriptions.fortellis.io/v1/solution/subscriptions`, {
headers: { Authorization: `Bearer ${access_token}` }
});
return subscriptions.data.subscriptions;
} catch (error) {
console.log("🚀 ~ FetchSubscriptions ~ error:", error);
}
}
async function GetBulkVendors() {
const departmentIds = (await FetchSubscriptions())
.find((s) => s.subscriptionId === SubscriptionID) //Get the subscription object.
?.apiDmsInfo.find((info) => info.name === "CDK Drive Async Vendors")?.departments; //Departments are categorized by API name and have an array of departments.
const access_token = await GetAuthToken();
const ReqId = uuid();
try {
//TODO: This is pointing towards the test environment. Need to carefully watch for the production switch.
const Vendors = await axios.get(`https://api.fortellis.io/cdk-test/drive/vendor/v2/bulk`, {
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionID,
"Request-Id": ReqId,
"Department-Id": departmentIds[0].id
}
});
//Returns a long poll. Need to wait specified seconds until checking.
console.log("🚀 ~ GetBulkVendors ~ Vendors - waiting to execute callback:", Vendors.data.checkStatusAfterSeconds);
sleep(Vendors.data.checkStatusAfterSeconds * 1000, async () => {
const VendorsResult = await axios.get(Vendors.data._links.status.href, {
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionID,
"Request-Id": ReqId,
"Department-Id": departmentIds[0].id
}
});
//This may have to check again if it isn't ready.
const VendorsResult2 = await axios.get(VendorsResult.data._links.result.href, {
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionID,
"Request-Id": ReqId,
"Department-Id": departmentIds[0].id
}
});
console.log("🚀 ~ sleep ~ VendorsResult2:", VendorsResult2);
});
console.log("🚀 ~ GetBulkVendors ~ Vendors:", ReqId, Vendors.data);
} catch (error) {
console.log("🚀 ~ GetBulkVendors ~ error:", ReqId, error);
}
}
async function FetchVehicles({ SubscriptionID }) {
const access_token = await GetAuthToken();
try {
//This doesn't seem to work as it is for production only.
const Vehicles = await axios.get(`https://api.fortellis.io/cdkdrive/service/v1/vehicles/`, {
headers: { Authorization: `Bearer ${access_token}`, "Subscription-Id": SubscriptionID }
});
console.log("🚀 ~ FetchVehicles ~ Vehicles:", Vehicles);
return Vehicles.data;
} catch (error) {
console.log("🚀 ~ FetchVehicles ~ error:", error);
}
}
async function PostVehicleServiceHistory() {
const access_token = await GetAuthToken();
const ReqId = uuid();
const departmentIds = (await FetchSubscriptions())
.find((s) => s.subscriptionId === SubscriptionID) //Get the subscription object.
?.apiDmsInfo.find((info) => info.name === "CDK Drive Async Vendors")?.departments; //Departments are categorized by API name and have an array of departments.
//Need to get a vehicle ID from somewhere.
const vehicles = await FetchVehicles({ SubscriptionID });
try {
//TODO: This is pointing towards the test environment. Need to carefully watch for the production switch.
const Vendors = await axios.post(
`https://api.fortellis.io/cdk-test/drive/post/service-vehicle-history-mgmt/v2/
`,
{
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionID,
"Request-Id": ReqId,
"Department-Id": departmentIds[0].id
}
}
);
} catch (error) {
console.log("🚀 ~ PostVehicleServiceHistory ~ error:", ReqId, error);
}
}
//PostVehicleServiceHistory();
//GetBulkVendors();
async function GetDepartmentId() {
const departmentIds = (await FetchSubscriptions())
.find((s) => s.subscriptionId === SubscriptionID) //Get the subscription object.
?.apiDmsInfo.find((info) => info.name === "CDK Drive Async Vendors")?.departments; //Departments are categorized by API name and have an array of departments.
return departmentIds[0].id;
}
//////////////////GL WIP Section //////////////////////
async function OrgHelpers() {
console.log("Executing Org Helpers");
const ReqId = uuid();
const access_token = await GetAuthToken();
const DepartmentId = await GetDepartmentId();
try {
//This doesn't seem to work as it is for production only.
const OrgHelpers = await axios.get(`https://api.fortellis.io/cdk-test/drive/businessofficeglwippost/orgHelper`, {
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionID,
"Request-Id": ReqId,
"Department-Id": DepartmentId
}
});
console.log("🚀 ~ OrgHelpers ~ Data:", OrgHelpers);
return OrgHelpers.data;
} catch (error) {
console.log("🚀 ~ OrgHelpers ~ error:", error);
}
}
async function JournalHelpers({ glCompanyNumber }) {
console.log("Executing Journal Helpers");
const ReqId = uuid();
const access_token = await GetAuthToken();
const DepartmentId = await GetDepartmentId();
try {
//This doesn't seem to work as it is for production only.
const JournalHelpers = await axios.get(
`https://api.fortellis.io/cdk-test/drive/businessofficeglwippost/jrnlHelper/${glCompanyNumber}`,
{
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionID,
"Request-Id": ReqId,
"Department-Id": DepartmentId
}
}
);
console.log("🚀 ~ JournalHelpers ~ Data:", JournalHelpers);
return JournalHelpers.data;
} catch (error) {
console.log("🚀 ~ JournalHelpers ~ error:", error);
}
}
async function GlSalesChain() {
console.log("Executing GL Sales Chain");
const ReqId = uuid();
const access_token = await GetAuthToken();
const DepartmentId = await GetDepartmentId();
try {
//This doesn't seem to work as it is for production only.
const GlSalesChain = await axios.get(
`https://api.fortellis.io/cdk-test/drive/businessofficeglwippost/glSalesChain`,
{
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionID,
"Request-Id": ReqId,
"Department-Id": DepartmentId
}
}
);
console.log("🚀 ~ GlSalesChain ~ Data:", GlSalesChain);
return GlSalesChain.data;
} catch (error) {
console.log("🚀 ~ GlSalesChain ~ error:", error);
}
}
async function GlExpenseAllocation() {
console.log("Executing GL Expense Allocation");
const ReqId = uuid();
const access_token = await GetAuthToken();
const DepartmentId = await GetDepartmentId();
try {
//This doesn't seem to work as it is for production only.
const GlExpenseAllocation = await axios.get(
`https://api.fortellis.io/cdk-test/drive/businessofficeglwippost/glExpenseAllocation`,
{
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionID,
"Request-Id": ReqId,
"Department-Id": DepartmentId
}
}
);
console.log("🚀 ~ GlExpenseAllocation ~ Data:", GlExpenseAllocation);
return GlExpenseAllocation.data;
} catch (error) {
console.log("🚀 ~ GlSalesChain ~ error:", error);
}
}
///EXEC FUNCTIONS
async function PostAccountsGLWIP() {
//const orgHelpers = await OrgHelpers();
//const jrnlHelpers = await JournalHelpers({ glCompanyNumber: orgHelpers[0].coID });
//const glSalesChain = await GlSalesChain();
const glExpenseAllocation = await GlExpenseAllocation();
}
PostAccountsGLWIP();