Fortellis WIP
This commit is contained in:
131
fortellis.js
Normal file
131
fortellis.js
Normal file
@@ -0,0 +1,131 @@
|
||||
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 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.
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
//GetBulkVendors();
|
||||
Reference in New Issue
Block a user