Basic posting up to fortellis-select-customer.

This commit is contained in:
Patrick Fic
2025-04-16 15:55:22 -07:00
parent 567171c722
commit e2b4b408ed
9 changed files with 433 additions and 317 deletions

View File

@@ -12,6 +12,17 @@ const logger = require("../utils/logger");
const uuid = require("uuid").v4;
const AxiosLib = require("axios").default;
const axios = AxiosLib.create();
const axiosCurlirize = require('axios-curlirize').default;
axiosCurlirize(axios, (result, err) => {
const { command } = result;
console.log("*** ~ axiosCurlirize ~ command:", command);
// if (err) {
// use your logger here
// } else {
// }
});
const getTransactionType = (jobid) => `fortellis:${jobid}`;
const defaultFortellisTTL = 60 * 60;
@@ -100,10 +111,11 @@ async function GetDepartmentId({ apiName, debug = false, SubscriptionMeta }) {
);
console.log("===========");
}
//TODO: Verify how to select the correct department.
const departmentIds2 = SubscriptionMeta.apiDmsInfo //Get the subscription object.
.find((info) => info.name === apiName)?.departments; //Departments are categorized by API name and have an array of departments.
return departmentIds2[0].id; //TODO: This makes the assumption that there is only 1 department.
return departmentIds2 && departmentIds2[0] && departmentIds2[0].id; //TODO: This makes the assumption that there is only 1 department.
}
//Highest level function call to make a call to fortellis. This should be the only call required, and it will handle all the logic for making the call.
@@ -114,13 +126,17 @@ async function MakeFortellisCall({
body = {},
type = "post",
debug = true,
requestPathParams,
requestSearchParams = [], //Array of key/value strings like [["key", "value"]]
jobid,
redisHelpers,
socket
socket,
}) {
const { setSessionTransactionData, getSessionTransactionData } = redisHelpers;
if (debug) logger.log(`Executing ${type} to ${url}`);
const fullUrl = constructFullUrl({ url, pathParams: requestPathParams, requestSearchParams });
if (debug) logger.log(`Executing ${type} to ${fullUrl}`);
const ReqId = uuid();
const access_token = await GetAuthToken();
const SubscriptionMeta = await FetchSubscriptions({ redisHelpers, socket, jobid });
@@ -138,18 +154,18 @@ async function MakeFortellisCall({
switch (type) {
case "post":
default:
result = await axios.post(url, body, {
result = await axios.post(fullUrl, body, {
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionMeta.subscriptionId,
"Request-Id": ReqId,
"Department-Id": DepartmentId,
...DepartmentId && { "Department-Id": DepartmentId },
...headers
}
});
break;
case "get":
result = await axios.get(url, {
result = await axios.get(fullUrl, {
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionMeta.subscriptionId,
@@ -229,10 +245,31 @@ const FortellisActions = {
type: "get",
apiName: "Service Vehicle - Query Vehicles"
},
GetVehicleId: {
url: isProduction
? "https://api.fortellis.io/cdk/drive/service-vehicle-mgmt/v2/vehicle-ids/" //Request path params of vins
: "https://api.fortellis.io/cdk-test/drive/service-vehicle-mgmt/v2/vehicle-ids/",
type: "get",
apiName: "CDK Drive Post Service Vehicle",
},
GetVehicleById: {
url: isProduction
? "https://api.fortellis.io/cdk/drive/service-vehicle-mgmt/v2/" //Request path params of vehicleId
: "https://api.fortellis.io/cdk-test/drive/service-vehicle-mgmt/v2/",
type: "get",
apiName: "CDK Drive Post Service Vehicle",
},
QueryCustomerByName: {
url: isProduction
? "https://api.fortellis.io/cdk/drive/customerpost/v1/search"
: "https://api.fortellis.io/cdk-test/drive/customerpost/v1/search",
type: "get",
apiName: "CDK Drive Post Customer",
},
GetCOA: {
type: "get",
apiName: "CDK Drive Post Accounts GL WIP",
url: `https://api.fortellis.io/cdk-test/drive/chartofaccounts/v2/bulk`,
url: `https://api.fortellis.io/cdk-test/drive/chartofaccounts/v2/bulk/`,
waitForResult: true
}
};
@@ -243,6 +280,17 @@ const FortellisCacheEnums = {
DepartmentId: "DepartmentId"
};
function constructFullUrl({ url, pathParams = "", requestSearchParams = [] }) {
// Ensure the base URL ends with a single "/"
url = url.replace(/\/+$/, "/");
const fullPath = pathParams ? `${url}${pathParams}` : url;
const searchParams = new URLSearchParams(requestSearchParams).toString();
const fullUrl = searchParams ? `${fullPath}?${searchParams}` : fullPath;
return fullUrl;
}
module.exports = {
GetAuthToken,
FortellisCacheEnums,