ofsc-utility 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/activityInventories/index.d.ts +23 -0
- package/dist/activityInventories/index.js +71 -0
- package/dist/events/index.d.ts +7 -0
- package/dist/events/index.js +114 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +15 -3
- package/dist/types.d.ts +5 -0
- package/dist/utilities/index.d.ts +16 -0
- package/dist/utilities/index.js +85 -0
- package/package.json +1 -1
- package/readme.md +57 -50
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetches all customer inventories related to the given activity.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} clientId - The OFSC client ID.
|
|
5
|
+
* @param {string} clientSecret - The OFSC client secret.
|
|
6
|
+
* @param {string} instanceUrl - The OFSC instance URL.
|
|
7
|
+
* @param {string} activityId - The ID of the activity to fetch customer inventories for.
|
|
8
|
+
*
|
|
9
|
+
* @returns {Promise<any[]>} A promise which resolves to an array of customer inventory objects.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getActivityCustomerInventories(clientId: string, clientSecret: string, instanceUrl: string, activityId: string): Promise<any[]>;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new customer inventory related to the given activity.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} clientId - The OFSC client ID.
|
|
16
|
+
* @param {string} clientSecret - The OFSC client secret.
|
|
17
|
+
* @param {string} instanceUrl - The OFSC instance URL.
|
|
18
|
+
* @param {string} activityId - The ID of the activity to create a customer inventory for.
|
|
19
|
+
* @param {object} payload - The customer inventory payload.
|
|
20
|
+
*
|
|
21
|
+
* @returns {Promise<object>} A promise which resolves to the created customer inventory object.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createActivityCustomerInventories(clientId: string, clientSecret: string, instanceUrl: string, activityId: string, payload: {}): Promise<{}>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getActivityCustomerInventories = getActivityCustomerInventories;
|
|
4
|
+
exports.createActivityCustomerInventories = createActivityCustomerInventories;
|
|
5
|
+
const oauthTokenService_1 = require("../oauthTokenService");
|
|
6
|
+
const utilities_1 = require("../utilities");
|
|
7
|
+
/**
|
|
8
|
+
* Fetches all customer inventories related to the given activity.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} clientId - The OFSC client ID.
|
|
11
|
+
* @param {string} clientSecret - The OFSC client secret.
|
|
12
|
+
* @param {string} instanceUrl - The OFSC instance URL.
|
|
13
|
+
* @param {string} activityId - The ID of the activity to fetch customer inventories for.
|
|
14
|
+
*
|
|
15
|
+
* @returns {Promise<any[]>} A promise which resolves to an array of customer inventory objects.
|
|
16
|
+
*/
|
|
17
|
+
async function getActivityCustomerInventories(clientId, clientSecret, instanceUrl, activityId) {
|
|
18
|
+
const limit = 100;
|
|
19
|
+
let offset = 0;
|
|
20
|
+
let token = await (0, oauthTokenService_1.getOAuthToken)(clientId, clientSecret, instanceUrl);
|
|
21
|
+
const allItems = [];
|
|
22
|
+
const fetchCustomerInventories = async (offset) => {
|
|
23
|
+
const params = new URLSearchParams({
|
|
24
|
+
offset: offset.toString(),
|
|
25
|
+
limit: limit.toString()
|
|
26
|
+
});
|
|
27
|
+
const url = `https://${instanceUrl}.fs.ocs.oraclecloud.com/rest/ofscCore/v1/activities/${activityId}/customerInventories?${params}`;
|
|
28
|
+
console.log(`➡️ Fetching offset=${offset}, limit=${limit}`);
|
|
29
|
+
const response = await (0, utilities_1.fetchWithRetry)(url, clientId, clientSecret, instanceUrl, token);
|
|
30
|
+
token = response.token;
|
|
31
|
+
const data = response.data;
|
|
32
|
+
if (!data.items || data.items.length === 0) {
|
|
33
|
+
// console.log("✔ No more items found. Stopping pagination.");
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
allItems.push(...data.items);
|
|
37
|
+
console.log(` ✔ Received ${data.items.length} items (Total: ${allItems.length}) for activity ${activityId}`);
|
|
38
|
+
await fetchCustomerInventories(offset + limit);
|
|
39
|
+
};
|
|
40
|
+
await fetchCustomerInventories(offset);
|
|
41
|
+
return allItems;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new customer inventory related to the given activity.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} clientId - The OFSC client ID.
|
|
47
|
+
* @param {string} clientSecret - The OFSC client secret.
|
|
48
|
+
* @param {string} instanceUrl - The OFSC instance URL.
|
|
49
|
+
* @param {string} activityId - The ID of the activity to create a customer inventory for.
|
|
50
|
+
* @param {object} payload - The customer inventory payload.
|
|
51
|
+
*
|
|
52
|
+
* @returns {Promise<object>} A promise which resolves to the created customer inventory object.
|
|
53
|
+
*/
|
|
54
|
+
async function createActivityCustomerInventories(clientId, clientSecret, instanceUrl, activityId, payload) {
|
|
55
|
+
const url = `https://${instanceUrl}.fs.ocs.oraclecloud.com/rest/ofscCore/v1/activities/${activityId}/customerInventories`;
|
|
56
|
+
const token = await (0, oauthTokenService_1.getOAuthToken)(clientId, clientSecret, instanceUrl);
|
|
57
|
+
const res = await fetch(url, {
|
|
58
|
+
method: "POST",
|
|
59
|
+
headers: {
|
|
60
|
+
Authorization: `Bearer ${token}`,
|
|
61
|
+
Accept: "application/json",
|
|
62
|
+
"Content-Type": "application/json"
|
|
63
|
+
},
|
|
64
|
+
body: JSON.stringify(payload)
|
|
65
|
+
});
|
|
66
|
+
// if (!res.ok) {
|
|
67
|
+
// return await res.json();
|
|
68
|
+
// throw new Error(`❌ POST failed: ${res.status} ${res.statusText}`);
|
|
69
|
+
// }
|
|
70
|
+
return await res.json();
|
|
71
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
type AnyObject = {
|
|
2
|
+
[key: string]: any;
|
|
3
|
+
};
|
|
4
|
+
export declare function flattenObject(obj: AnyObject, parentKey?: string, result?: AnyObject): AnyObject;
|
|
5
|
+
export declare function downloadAllEventsOfDay(clientId: string, clientSecret: string, instanceUrl: string, subscriptionId: string, sinceDate: string, onlyData: boolean): Promise<any[]>;
|
|
6
|
+
export declare function downloadAllEventsOfDayCSV(clientId: string, clientSecret: string, instanceUrl: string, subscriptionId: string, sinceDate: string, onlyData: boolean): Promise<any[]>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.flattenObject = flattenObject;
|
|
7
|
+
exports.downloadAllEventsOfDay = downloadAllEventsOfDay;
|
|
8
|
+
exports.downloadAllEventsOfDayCSV = downloadAllEventsOfDayCSV;
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const index_1 = require("../oauthTokenService/index");
|
|
11
|
+
const utilities_1 = require("../utilities");
|
|
12
|
+
function flattenObject(obj, parentKey = "", result = {}) {
|
|
13
|
+
for (const key in obj) {
|
|
14
|
+
const newKey = parentKey ? `${parentKey}_${key}` : key;
|
|
15
|
+
if (typeof obj[key] === "object" &&
|
|
16
|
+
obj[key] !== null &&
|
|
17
|
+
!Array.isArray(obj[key])) {
|
|
18
|
+
flattenObject(obj[key], newKey, result);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
result[newKey] = obj[key];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
async function fetchEventsPage(url, token, clientId, clientSecret, instanceUrl) {
|
|
27
|
+
const res = await (0, utilities_1.fetchWithRetry)(url, clientId, clientSecret, instanceUrl, token);
|
|
28
|
+
return {
|
|
29
|
+
token: res.token,
|
|
30
|
+
data: res.data
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function processEventItems(items, sinceDate, output) {
|
|
34
|
+
for (const item of items) {
|
|
35
|
+
const eventTime = item.time;
|
|
36
|
+
// Stop if date changes
|
|
37
|
+
if (!eventTime.startsWith(sinceDate)) {
|
|
38
|
+
console.log("Stopping at different event date:", eventTime);
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
// Add activityId as first level field
|
|
42
|
+
const activityId = item.activityDetails?.activityId ?? null;
|
|
43
|
+
output.push({ activityId, ...item });
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
async function downloadAllEventsOfDay(clientId, clientSecret, instanceUrl, subscriptionId, sinceDate, onlyData) {
|
|
48
|
+
console.log("Downloading events...OnlyData:", onlyData);
|
|
49
|
+
let data = await downloadAllEventsOfDayCSV(clientId, clientSecret, instanceUrl, subscriptionId, sinceDate, onlyData);
|
|
50
|
+
return data;
|
|
51
|
+
}
|
|
52
|
+
async function downloadAllEventsOfDayCSV(clientId, clientSecret, instanceUrl, subscriptionId, sinceDate, onlyData) {
|
|
53
|
+
// All collected events
|
|
54
|
+
const events = [];
|
|
55
|
+
// Build initial request URL
|
|
56
|
+
const baseUrl = `https://${instanceUrl}.fs.ocs.oraclecloud.com/rest/ofscCore/v1/events`;
|
|
57
|
+
const initialUrl = `${baseUrl}?subscriptionId=${encodeURIComponent(subscriptionId)}&since=${encodeURIComponent(sinceDate + " 00:00:00")}`;
|
|
58
|
+
let token = await (0, index_1.getOAuthToken)(clientId, clientSecret, instanceUrl);
|
|
59
|
+
// Get first page
|
|
60
|
+
let firstPage = await fetchEventsPage(initialUrl, token, clientId, clientSecret, instanceUrl);
|
|
61
|
+
token = firstPage.token;
|
|
62
|
+
let nextPage = firstPage.data.nextPage;
|
|
63
|
+
let found = firstPage.data.found;
|
|
64
|
+
// Controls infinite loop
|
|
65
|
+
let lastSeenPage = nextPage;
|
|
66
|
+
let repeatedPageCount = 0;
|
|
67
|
+
// Loop through pages
|
|
68
|
+
while (found && nextPage) {
|
|
69
|
+
const pageUrl = new URL(baseUrl);
|
|
70
|
+
pageUrl.search = new URLSearchParams({
|
|
71
|
+
subscriptionId,
|
|
72
|
+
page: nextPage,
|
|
73
|
+
limit: "1000",
|
|
74
|
+
}).toString();
|
|
75
|
+
const finalUrl = pageUrl.toString();
|
|
76
|
+
const result = await fetchEventsPage(finalUrl, token, clientId, clientSecret, instanceUrl);
|
|
77
|
+
token = result.token;
|
|
78
|
+
const page = result.data;
|
|
79
|
+
found = page.found;
|
|
80
|
+
nextPage = page.nextPage;
|
|
81
|
+
console.error("nextPage", nextPage, "Records:", page.items?.length, "Time:", page.items?.[0]?.time);
|
|
82
|
+
// Prevent infinite looping
|
|
83
|
+
if (nextPage === lastSeenPage) {
|
|
84
|
+
repeatedPageCount++;
|
|
85
|
+
if (repeatedPageCount > 10) {
|
|
86
|
+
console.warn("⚠️ Pagination repeating same page more than 10 times. Stopping.");
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
lastSeenPage = nextPage;
|
|
92
|
+
repeatedPageCount = 0;
|
|
93
|
+
}
|
|
94
|
+
// Add events
|
|
95
|
+
if (page.items) {
|
|
96
|
+
if (!processEventItems(page.items, sinceDate, events)) {
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
console.warn("⚠️ No items found in page. Stopping.");
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// Save CSV
|
|
106
|
+
const ts = Math.floor(Date.now() / 1000);
|
|
107
|
+
const filename = `events-${sinceDate}_${ts}.csv`;
|
|
108
|
+
const fullPath = path_1.default.resolve(filename);
|
|
109
|
+
if (!onlyData) {
|
|
110
|
+
(0, utilities_1.saveCsv)(events, fullPath);
|
|
111
|
+
console.log(`✅ Saved ${events.length} events to: ${fullPath}`);
|
|
112
|
+
}
|
|
113
|
+
return events;
|
|
114
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
export * as Activity from './activities';
|
|
2
|
+
export * as ActivityInventories from './activityInventories';
|
|
3
|
+
export * as Events from './events';
|
|
2
4
|
export * as InventoryType from './inventoryTypes';
|
|
3
5
|
export * as OauthTokenService from './oauthTokenService';
|
|
4
6
|
export * as Resource from './resources';
|
|
5
7
|
export * as User from './users';
|
|
8
|
+
export * as CSV from './utilities';
|
|
6
9
|
export * as WorkZone from './workZones';
|
|
7
10
|
export * from './types';
|
|
8
11
|
export { getOAuthToken } from './oauthTokenService';
|
|
@@ -11,6 +14,8 @@ export { downloadAllResourcesCSV } from './resources';
|
|
|
11
14
|
export { downloadAllUsersCSV } from './users';
|
|
12
15
|
export { downloadAllInventoryTypesCSV, getInventoryTypesDetail, updateCreateInventoryType } from './inventoryTypes';
|
|
13
16
|
export { getAllActivities } from './activities';
|
|
17
|
+
export { createActivityCustomerInventories, getActivityCustomerInventories } from './activityInventories';
|
|
18
|
+
export { downloadAllEventsOfDay, downloadAllEventsOfDayCSV } from './events';
|
|
14
19
|
declare const OfscUtility: {
|
|
15
20
|
getOAuthToken: any;
|
|
16
21
|
downloadWorkZoneCSV: any;
|
|
@@ -20,5 +25,9 @@ declare const OfscUtility: {
|
|
|
20
25
|
getInventoryTypesDetail: any;
|
|
21
26
|
updateInventoryType: any;
|
|
22
27
|
getAllActivities: any;
|
|
28
|
+
getActivityCustomerInventories: any;
|
|
29
|
+
createActivityCustomerInventories: any;
|
|
30
|
+
downloadAllEventsOfDayCSV: any;
|
|
31
|
+
downloadAllEventsOfDay: any;
|
|
23
32
|
};
|
|
24
33
|
export default OfscUtility;
|
package/dist/index.js
CHANGED
|
@@ -36,13 +36,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
36
36
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.getAllActivities = exports.updateCreateInventoryType = exports.getInventoryTypesDetail = exports.downloadAllInventoryTypesCSV = exports.downloadAllUsersCSV = exports.downloadAllResourcesCSV = exports.downloadWorkZoneCSV = exports.getOAuthToken = exports.WorkZone = exports.User = exports.Resource = exports.OauthTokenService = exports.InventoryType = exports.Activity = void 0;
|
|
39
|
+
exports.downloadAllEventsOfDayCSV = exports.downloadAllEventsOfDay = exports.getActivityCustomerInventories = exports.createActivityCustomerInventories = exports.getAllActivities = exports.updateCreateInventoryType = exports.getInventoryTypesDetail = exports.downloadAllInventoryTypesCSV = exports.downloadAllUsersCSV = exports.downloadAllResourcesCSV = exports.downloadWorkZoneCSV = exports.getOAuthToken = exports.WorkZone = exports.CSV = exports.User = exports.Resource = exports.OauthTokenService = exports.InventoryType = exports.Events = exports.ActivityInventories = exports.Activity = void 0;
|
|
40
40
|
// Export all methods grouped by category
|
|
41
41
|
exports.Activity = __importStar(require("./activities"));
|
|
42
|
+
exports.ActivityInventories = __importStar(require("./activityInventories"));
|
|
43
|
+
exports.Events = __importStar(require("./events"));
|
|
42
44
|
exports.InventoryType = __importStar(require("./inventoryTypes"));
|
|
43
45
|
exports.OauthTokenService = __importStar(require("./oauthTokenService"));
|
|
44
46
|
exports.Resource = __importStar(require("./resources"));
|
|
45
47
|
exports.User = __importStar(require("./users"));
|
|
48
|
+
exports.CSV = __importStar(require("./utilities"));
|
|
46
49
|
exports.WorkZone = __importStar(require("./workZones"));
|
|
47
50
|
// Export types
|
|
48
51
|
__exportStar(require("./types"), exports);
|
|
@@ -61,9 +64,14 @@ Object.defineProperty(exports, "getInventoryTypesDetail", { enumerable: true, ge
|
|
|
61
64
|
Object.defineProperty(exports, "updateCreateInventoryType", { enumerable: true, get: function () { return inventoryTypes_1.updateCreateInventoryType; } });
|
|
62
65
|
var activities_1 = require("./activities");
|
|
63
66
|
Object.defineProperty(exports, "getAllActivities", { enumerable: true, get: function () { return activities_1.getAllActivities; } });
|
|
67
|
+
var activityInventories_1 = require("./activityInventories");
|
|
68
|
+
Object.defineProperty(exports, "createActivityCustomerInventories", { enumerable: true, get: function () { return activityInventories_1.createActivityCustomerInventories; } });
|
|
69
|
+
Object.defineProperty(exports, "getActivityCustomerInventories", { enumerable: true, get: function () { return activityInventories_1.getActivityCustomerInventories; } });
|
|
70
|
+
var events_1 = require("./events");
|
|
71
|
+
Object.defineProperty(exports, "downloadAllEventsOfDay", { enumerable: true, get: function () { return events_1.downloadAllEventsOfDay; } });
|
|
72
|
+
Object.defineProperty(exports, "downloadAllEventsOfDayCSV", { enumerable: true, get: function () { return events_1.downloadAllEventsOfDayCSV; } });
|
|
64
73
|
// Default export with all functionality
|
|
65
74
|
const OfscUtility = {
|
|
66
|
-
// Case converters
|
|
67
75
|
getOAuthToken: require('./oauthTokenService').getOAuthToken,
|
|
68
76
|
downloadWorkZoneCSV: require('./workZones').downloadWorkZoneCSV,
|
|
69
77
|
downloadAllResourcesCSV: require('./resources').downloadAllResourcesCSV,
|
|
@@ -71,6 +79,10 @@ const OfscUtility = {
|
|
|
71
79
|
downloadAllInventoryTypesCSV: require('./inventoryTypes').downloadAllInventoryTypesCSV,
|
|
72
80
|
getInventoryTypesDetail: require('./inventoryTypes').getInventoryTypesDetail,
|
|
73
81
|
updateInventoryType: require('./inventoryTypes').updateInventoryType,
|
|
74
|
-
getAllActivities: require('./activities').getAllActivities
|
|
82
|
+
getAllActivities: require('./activities').getAllActivities,
|
|
83
|
+
getActivityCustomerInventories: require('./activityInventories').getActivityCustomerInventories,
|
|
84
|
+
createActivityCustomerInventories: require('./activityInventories').createActivityCustomerInventories,
|
|
85
|
+
downloadAllEventsOfDayCSV: require('./events').downloadAllEventsOfDayCSV,
|
|
86
|
+
downloadAllEventsOfDay: require('./events').downloadAllEventsOfDay
|
|
75
87
|
};
|
|
76
88
|
exports.default = OfscUtility;
|
package/dist/types.d.ts
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetches a URL with retry logic for expired tokens.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} url - The URL to fetch.
|
|
5
|
+
* @param {string} clientId - The OFSC client ID.
|
|
6
|
+
* @param {string} clientSecret - The OFSC client secret.
|
|
7
|
+
* @param {string} instanceUrl - The OFSC instance URL.
|
|
8
|
+
* @param {string} token - The current OAuth token.
|
|
9
|
+
*
|
|
10
|
+
* @returns {Promise<{ data: any; token: string }>} A promise which resolves to an object containing the parsed JSON data and the latest OAuth token.
|
|
11
|
+
*/
|
|
12
|
+
export declare const fetchWithRetry: (url: string, clientId: string, clientSecret: string, instanceUrl: string, token: string) => Promise<{
|
|
13
|
+
data: any;
|
|
14
|
+
token: string;
|
|
15
|
+
}>;
|
|
16
|
+
export declare function saveCsv<T extends Record<string, any>>(rows: T[], filePath: string): void;
|
package/dist/utilities/index.js
CHANGED
|
@@ -1 +1,86 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.fetchWithRetry = void 0;
|
|
7
|
+
exports.saveCsv = saveCsv;
|
|
8
|
+
const oauthTokenService_1 = require("../oauthTokenService");
|
|
9
|
+
/**
|
|
10
|
+
* Fetches a URL with retry logic for expired tokens.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} url - The URL to fetch.
|
|
13
|
+
* @param {string} clientId - The OFSC client ID.
|
|
14
|
+
* @param {string} clientSecret - The OFSC client secret.
|
|
15
|
+
* @param {string} instanceUrl - The OFSC instance URL.
|
|
16
|
+
* @param {string} token - The current OAuth token.
|
|
17
|
+
*
|
|
18
|
+
* @returns {Promise<{ data: any; token: string }>} A promise which resolves to an object containing the parsed JSON data and the latest OAuth token.
|
|
19
|
+
*/
|
|
20
|
+
const fetchWithRetry = async (url, clientId, clientSecret, instanceUrl, token) => {
|
|
21
|
+
const doFetch = async (bearer) => {
|
|
22
|
+
return fetch(url, {
|
|
23
|
+
method: "GET",
|
|
24
|
+
headers: {
|
|
25
|
+
Authorization: `Bearer ${bearer}`,
|
|
26
|
+
Accept: "application/json"
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
// Try with the current token
|
|
31
|
+
let res = await doFetch(token);
|
|
32
|
+
// If 401 → renew and retry
|
|
33
|
+
if (res.status === 401) {
|
|
34
|
+
console.warn("⚠️ Token expired — renewing token…");
|
|
35
|
+
token = await (0, oauthTokenService_1.getOAuthToken)(clientId, clientSecret, instanceUrl);
|
|
36
|
+
res = await doFetch(token);
|
|
37
|
+
}
|
|
38
|
+
// If still not OK → fail
|
|
39
|
+
if (!res.ok) {
|
|
40
|
+
const body = await res.text();
|
|
41
|
+
throw new Error(`❌ Request failed: ${res.status} ${res.statusText}\n${body}`);
|
|
42
|
+
}
|
|
43
|
+
// Return parsed JSON + latest token
|
|
44
|
+
return {
|
|
45
|
+
data: await res.json(),
|
|
46
|
+
token
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
exports.fetchWithRetry = fetchWithRetry;
|
|
50
|
+
const fs_1 = __importDefault(require("fs"));
|
|
51
|
+
const path_1 = __importDefault(require("path"));
|
|
52
|
+
function saveCsv(rows, filePath) {
|
|
53
|
+
if (!rows || rows.length === 0) {
|
|
54
|
+
throw new Error("CSV creation failed: no rows provided.");
|
|
55
|
+
}
|
|
56
|
+
// Extract headers from the first row
|
|
57
|
+
const headers = Object.keys(rows[0]);
|
|
58
|
+
// Build CSV content
|
|
59
|
+
const csvLines = [
|
|
60
|
+
headers.join(","), // header row
|
|
61
|
+
...rows.map(row => headers.map(h => escapeCsvValue(row[h])).join(","))
|
|
62
|
+
];
|
|
63
|
+
const csvContent = csvLines.join("\n");
|
|
64
|
+
// Ensure directory exists
|
|
65
|
+
const dir = path_1.default.dirname(filePath);
|
|
66
|
+
if (!fs_1.default.existsSync(dir)) {
|
|
67
|
+
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
68
|
+
}
|
|
69
|
+
// Write the file
|
|
70
|
+
fs_1.default.writeFileSync(filePath, csvContent);
|
|
71
|
+
console.log(`CSV saved: ${filePath}`);
|
|
72
|
+
}
|
|
73
|
+
// Escape CSV fields
|
|
74
|
+
function escapeCsvValue(value) {
|
|
75
|
+
if (value == null)
|
|
76
|
+
return "";
|
|
77
|
+
if (typeof value === "object") {
|
|
78
|
+
value = JSON.stringify(value);
|
|
79
|
+
}
|
|
80
|
+
const str = String(value);
|
|
81
|
+
// Wrap in quotes if needed
|
|
82
|
+
if (str.includes(",") || str.includes('"') || str.includes("\n")) {
|
|
83
|
+
return `"${str.replace(/"/g, '""')}"`;
|
|
84
|
+
}
|
|
85
|
+
return str;
|
|
86
|
+
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -21,41 +21,38 @@ npm install ofsc-utility
|
|
|
21
21
|
|
|
22
22
|
### Download
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
Please see the code snippet below.
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
#### csv
|
|
27
27
|
|
|
28
|
-
downloadWorkZoneCSV(
|
|
29
|
-
downloadAllResourcesCSV(
|
|
30
|
-
downloadAllUsersCSV(
|
|
31
|
-
downloadAllInventoryTypesCSV(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
28
|
+
downloadWorkZoneCSV(process.env.clientID, process.env.clientSecreat, process.env.instanceId)
|
|
29
|
+
downloadAllResourcesCSV(process.env.clientID, process.env.clientSecreat, process.env.instanceId)
|
|
30
|
+
downloadAllUsersCSV(process.env.clientID, process.env.clientSecreat, process.env.instanceId)
|
|
31
|
+
downloadAllInventoryTypesCSV(process.env.clientID, process.env.clientSecreat, process.env.instanceId)
|
|
32
|
+
downloadAllEventsOfDayCSV(process.env.clientID, process.env.clientSecreat, process.env.instanceId, process.env.subscriptionId,"2025-12-05")
|
|
33
|
+
|
|
34
|
+
#### records
|
|
35
35
|
|
|
36
36
|
getOAuthToken("clientId", "clientSecret", "instanceId")
|
|
37
37
|
getInventoryTypesDetail("clientId", "clientSecret", "instanceId"."inventory_label")
|
|
38
38
|
updateCreateInventoryType("clientId", "clientSecret", "instanceId"."inventory_label")
|
|
39
39
|
getAllActivities("clientId", "clientSecret", "instanceId"."resources","dateFrom","dateTo","q","fields")
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
getActivityCustomerInventories("clientId", "clientSecret", "instanceId"."activityId")
|
|
41
|
+
createActivityCustomerInventories( "clientId", "clientSecret", "instanceId"."activityId","payload")
|
|
42
|
+
downloadAllEventsOfDay(process.env.clientID, process.env.clientSecreat, process.env.instanceId, process.env.subscriptionId,"2025-12-05")
|
|
42
43
|
|
|
43
44
|
## Usage
|
|
44
45
|
|
|
45
|
-
|
|
46
|
+
downloadWorkZoneCSV("bot", "XXXXXXXXX", "compXXX.test")
|
|
46
47
|
|
|
47
48
|
### CommonJS
|
|
48
49
|
|
|
49
50
|
```js
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
"clientId", "clientSecret", "instanceId", "inventory_label");
|
|
54
|
-
console.error(data);
|
|
51
|
+
async function run() {
|
|
52
|
+
let data = await ofs.InventoryType.getInventoryTypesDetail("clientId", "clientSecret", "instanceId", "inventory_label");
|
|
53
|
+
console.error(data);
|
|
55
54
|
}
|
|
56
55
|
run();
|
|
57
|
-
|
|
58
|
-
|
|
59
56
|
```
|
|
60
57
|
|
|
61
58
|
```js
|
|
@@ -110,44 +107,37 @@ ofs
|
|
|
110
107
|
```
|
|
111
108
|
|
|
112
109
|
```js
|
|
113
|
-
const ofs = require(
|
|
110
|
+
const ofs = require("ofsc-utility");
|
|
114
111
|
|
|
115
112
|
async function run() {
|
|
116
|
-
|
|
117
|
-
|
|
113
|
+
const payload = {
|
|
114
|
+
label: "inventory_label",
|
|
115
|
+
name: "Ordered Part",
|
|
116
|
+
unitOfMeasurement: "ea",
|
|
117
|
+
active: true,
|
|
118
|
+
nonSerialized: true,
|
|
119
|
+
modelProperty: "part_item_number_rev",
|
|
120
|
+
quantityPrecision: 0,
|
|
121
|
+
translations: [
|
|
122
|
+
{
|
|
123
|
+
language: "en",
|
|
118
124
|
name: "Ordered Part",
|
|
119
125
|
unitOfMeasurement: "ea",
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
try {
|
|
135
|
-
const result = await updateCreateInventoryType(
|
|
136
|
-
"CLIENT_ID",
|
|
137
|
-
"CLIENT_SECRET",
|
|
138
|
-
"INSTANCE_URL",
|
|
139
|
-
"inventory_label",
|
|
140
|
-
payload
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
console.log("Updated:", result);
|
|
144
|
-
} catch (err) {
|
|
145
|
-
console.error(err);
|
|
146
|
-
}
|
|
126
|
+
languageISO: "en-US",
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
const result = await updateCreateInventoryType("CLIENT_ID", "CLIENT_SECRET", "INSTANCE_URL", "inventory_label", payload);
|
|
133
|
+
|
|
134
|
+
console.log("Updated:", result);
|
|
135
|
+
} catch (err) {
|
|
136
|
+
console.error(err);
|
|
137
|
+
}
|
|
147
138
|
}
|
|
148
139
|
|
|
149
140
|
run();
|
|
150
|
-
|
|
151
141
|
```
|
|
152
142
|
|
|
153
143
|
```js
|
|
@@ -174,6 +164,23 @@ async function run() {
|
|
|
174
164
|
run();
|
|
175
165
|
```
|
|
176
166
|
|
|
167
|
+
```js
|
|
168
|
+
ofs.getActivityCustomerInventories("CLIENT_ID", "CLIENT_SECRET", "INSTANCE_URL", "activityId").then((data) => {
|
|
169
|
+
console.log(data);
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
```js
|
|
174
|
+
const ofs = require("ofsc-utility");
|
|
175
|
+
ofs.Events.downloadAllEventsOfDayCSV(
|
|
176
|
+
process.env.clientID,
|
|
177
|
+
process.env.clientSecreat,
|
|
178
|
+
process.env.instanceId,
|
|
179
|
+
process.env.subscriptionId,
|
|
180
|
+
"2025-12-05"
|
|
181
|
+
);
|
|
182
|
+
```
|
|
183
|
+
|
|
177
184
|
## License
|
|
178
185
|
|
|
179
186
|
MIT
|