ofsc-utility 1.0.10 → 1.0.12

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.
@@ -30,11 +30,22 @@ async function fetchEventsPage(url, token, clientId, clientSecret, instanceUrl)
30
30
  data: res.data
31
31
  };
32
32
  }
33
+ function getNextDay(dateString) {
34
+ // Parse manually to avoid timezone issues
35
+ const [year, month, day] = dateString.split('-').map(Number);
36
+ const date = new Date(year, month - 1, day);
37
+ date.setDate(date.getDate() + 1);
38
+ const pad = (n) => n.toString().padStart(2, '0');
39
+ const y = date.getFullYear();
40
+ const m = pad(date.getMonth() + 1);
41
+ const d = pad(date.getDate());
42
+ return `${y}-${m}-${d}`;
43
+ }
33
44
  function processEventItems(items, sinceDate, output) {
34
45
  for (const item of items) {
35
46
  const eventTime = item.time;
36
47
  // Stop if date changes
37
- if (!eventTime.startsWith(sinceDate)) {
48
+ if (eventTime.startsWith(getNextDay(sinceDate))) {
38
49
  console.log("Stopping at different event date:", eventTime);
39
50
  return false;
40
51
  }
@@ -55,6 +66,7 @@ async function downloadAllEventsOfDayCSV(clientId, clientSecret, instanceUrl, su
55
66
  // Build initial request URL
56
67
  const baseUrl = `https://${instanceUrl}.fs.ocs.oraclecloud.com/rest/ofscCore/v1/events`;
57
68
  const initialUrl = `${baseUrl}?subscriptionId=${encodeURIComponent(subscriptionId)}&since=${encodeURIComponent(sinceDate + " 00:00:00")}`;
69
+ console.log("sinceDate", sinceDate);
58
70
  let token = await (0, index_1.getOAuthToken)(clientId, clientSecret, instanceUrl);
59
71
  // Get first page
60
72
  let firstPage = await fetchEventsPage(initialUrl, token, clientId, clientSecret, instanceUrl);
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * as Activity from './activities';
2
2
  export * as ActivityInventories from './activityInventories';
3
3
  export * as Events from './events';
4
+ export * as Inventory from './inventory';
4
5
  export * as InventoryType from './inventoryTypes';
5
6
  export * as OauthTokenService from './oauthTokenService';
6
7
  export * as Resource from './resources';
@@ -8,10 +9,11 @@ export * as User from './users';
8
9
  export * as CSV from './utilities';
9
10
  export * as WorkZone from './workZones';
10
11
  export * from './types';
12
+ export { generateAllOnHandInventoryOfAllResourcesCSV } from './inventory';
11
13
  export { getOAuthToken } from './oauthTokenService';
12
14
  export { downloadWorkZoneCSV } from './workZones';
13
15
  export { downloadAllResourcesCSV } from './resources';
14
- export { downloadAllUsersCSV } from './users';
16
+ export { downloadAllUsersCSV, generateUsersCollaborationCSV } from './users';
15
17
  export { downloadAllInventoryTypesCSV, getInventoryTypesDetail, updateCreateInventoryType } from './inventoryTypes';
16
18
  export { getAllActivities } from './activities';
17
19
  export { createActivityCustomerInventories, getActivityCustomerInventories } from './activityInventories';
@@ -29,5 +31,7 @@ declare const OfscUtility: {
29
31
  createActivityCustomerInventories: any;
30
32
  downloadAllEventsOfDayCSV: any;
31
33
  downloadAllEventsOfDay: any;
34
+ generateUsersCollaborationCSV: any;
35
+ generateAllOnHandInventoryOfAllResourcesCSV: any;
32
36
  };
33
37
  export default OfscUtility;
package/dist/index.js CHANGED
@@ -36,11 +36,12 @@ 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.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;
39
+ exports.downloadAllEventsOfDayCSV = exports.downloadAllEventsOfDay = exports.getActivityCustomerInventories = exports.createActivityCustomerInventories = exports.getAllActivities = exports.updateCreateInventoryType = exports.getInventoryTypesDetail = exports.downloadAllInventoryTypesCSV = exports.generateUsersCollaborationCSV = exports.downloadAllUsersCSV = exports.downloadAllResourcesCSV = exports.downloadWorkZoneCSV = exports.getOAuthToken = exports.generateAllOnHandInventoryOfAllResourcesCSV = exports.WorkZone = exports.CSV = exports.User = exports.Resource = exports.OauthTokenService = exports.InventoryType = exports.Inventory = exports.Events = exports.ActivityInventories = exports.Activity = void 0;
40
40
  // Export all methods grouped by category
41
41
  exports.Activity = __importStar(require("./activities"));
42
42
  exports.ActivityInventories = __importStar(require("./activityInventories"));
43
43
  exports.Events = __importStar(require("./events"));
44
+ exports.Inventory = __importStar(require("./inventory"));
44
45
  exports.InventoryType = __importStar(require("./inventoryTypes"));
45
46
  exports.OauthTokenService = __importStar(require("./oauthTokenService"));
46
47
  exports.Resource = __importStar(require("./resources"));
@@ -49,7 +50,8 @@ exports.CSV = __importStar(require("./utilities"));
49
50
  exports.WorkZone = __importStar(require("./workZones"));
50
51
  // Export types
51
52
  __exportStar(require("./types"), exports);
52
- // Export individual popular methods for convenience
53
+ var inventory_1 = require("./inventory");
54
+ Object.defineProperty(exports, "generateAllOnHandInventoryOfAllResourcesCSV", { enumerable: true, get: function () { return inventory_1.generateAllOnHandInventoryOfAllResourcesCSV; } });
53
55
  var oauthTokenService_1 = require("./oauthTokenService");
54
56
  Object.defineProperty(exports, "getOAuthToken", { enumerable: true, get: function () { return oauthTokenService_1.getOAuthToken; } });
55
57
  var workZones_1 = require("./workZones");
@@ -58,6 +60,7 @@ var resources_1 = require("./resources");
58
60
  Object.defineProperty(exports, "downloadAllResourcesCSV", { enumerable: true, get: function () { return resources_1.downloadAllResourcesCSV; } });
59
61
  var users_1 = require("./users");
60
62
  Object.defineProperty(exports, "downloadAllUsersCSV", { enumerable: true, get: function () { return users_1.downloadAllUsersCSV; } });
63
+ Object.defineProperty(exports, "generateUsersCollaborationCSV", { enumerable: true, get: function () { return users_1.generateUsersCollaborationCSV; } });
61
64
  var inventoryTypes_1 = require("./inventoryTypes");
62
65
  Object.defineProperty(exports, "downloadAllInventoryTypesCSV", { enumerable: true, get: function () { return inventoryTypes_1.downloadAllInventoryTypesCSV; } });
63
66
  Object.defineProperty(exports, "getInventoryTypesDetail", { enumerable: true, get: function () { return inventoryTypes_1.getInventoryTypesDetail; } });
@@ -83,6 +86,8 @@ const OfscUtility = {
83
86
  getActivityCustomerInventories: require('./activityInventories').getActivityCustomerInventories,
84
87
  createActivityCustomerInventories: require('./activityInventories').createActivityCustomerInventories,
85
88
  downloadAllEventsOfDayCSV: require('./events').downloadAllEventsOfDayCSV,
86
- downloadAllEventsOfDay: require('./events').downloadAllEventsOfDay
89
+ downloadAllEventsOfDay: require('./events').downloadAllEventsOfDay,
90
+ generateUsersCollaborationCSV: require('./users').generateUsersCollaborationCSV,
91
+ generateAllOnHandInventoryOfAllResourcesCSV: require('./inventory').generateAllOnHandInventoryOfAllResourcesCSV
87
92
  };
88
93
  exports.default = OfscUtility;
@@ -0,0 +1 @@
1
+ export declare function generateAllOnHandInventoryOfAllResourcesCSV(clientId: string, clientSecret: string, instanceUrl: string): Promise<void>;
@@ -0,0 +1,79 @@
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.generateAllOnHandInventoryOfAllResourcesCSV = generateAllOnHandInventoryOfAllResourcesCSV;
7
+ const path_1 = __importDefault(require("path"));
8
+ const utilities_1 = require("../utilities");
9
+ async function generateAllOnHandInventoryOfAllResourcesCSV(clientId, clientSecret, instanceUrl) {
10
+ let offset = 0;
11
+ const limit = 100;
12
+ const allUsers = [];
13
+ let token = "";
14
+ console.log("🚀 Starting all Resources Inventories export...");
15
+ console.log("--------------------------------------------------");
16
+ while (true) {
17
+ const usersUrl = `https://${instanceUrl}.fs.ocs.oraclecloud.com/rest/ofscCore/v1/resources/?offset=${offset}&limit=${limit}`;
18
+ console.log(`➡️ Fetching users offset=${offset}`);
19
+ const res = await (0, utilities_1.fetchWithRetry)(usersUrl, clientId, clientSecret, instanceUrl, token);
20
+ token = res.token;
21
+ const data = res.data;
22
+ if (!data?.items?.length) {
23
+ console.warn("⚠ No user items returned. Breaking.");
24
+ break;
25
+ }
26
+ allUsers.push(...data.items);
27
+ console.log(` ✔ Received ${data.items.length} resources (Total: ${allUsers.length})`);
28
+ if (offset + limit >= data.totalResults)
29
+ break;
30
+ offset += limit;
31
+ }
32
+ console.log("--------------------------------------------------");
33
+ console.log(`🧩 Total resources to process: ${allUsers.length}`);
34
+ console.log("--------------------------------------------------");
35
+ // 2. Fetch on hand inventories for each resource
36
+ const rows = [];
37
+ for (const [index, user] of allUsers.entries()) {
38
+ console.log(`${index} 👤 Fetching Inventories for ${user.resourceId}`);
39
+ if (user.status !== "active") {
40
+ console.log(` ⚠ Skipping inactive resource ${user.resourceId}`);
41
+ continue;
42
+ }
43
+ offset = 0;
44
+ while (true) {
45
+ const invUrl = `https://${instanceUrl}.fs.ocs.oraclecloud.com/rest/ofscCore/v1/resources/${encodeURIComponent(user.resourceId)}/inventories/?offset=${offset}&limit=${limit}`;
46
+ try {
47
+ const res = await (0, utilities_1.fetchWithRetry)(invUrl, clientId, clientSecret, instanceUrl, token);
48
+ token = res.token;
49
+ const itemsData = res.data;
50
+ const items = itemsData?.items ?? [];
51
+ if (!items || items.length === 0) {
52
+ console.log(` ⚠ No Inventories found for ${user.resourceId}`);
53
+ break;
54
+ }
55
+ for (const inv of items) {
56
+ delete inv.links;
57
+ delete inv.status;
58
+ rows.push({ resourceName: user.name, resourceType: user.resourceType, resourceTimeZone: user.timeZoneIANA, ...inv, });
59
+ }
60
+ console.log(` ✔ Found ${items.length} Inventories`);
61
+ if (offset + limit >= itemsData.totalResults)
62
+ break;
63
+ offset += limit;
64
+ }
65
+ catch (err) {
66
+ console.error(`❌ Error fetching Inventories for ${user.resourceId}:`, err);
67
+ console.log(` ⚠ Skipping invUrl ${invUrl}`);
68
+ }
69
+ }
70
+ }
71
+ console.log("--------------------------------------------------");
72
+ console.log(`📦 Total rows: ${rows.length}`);
73
+ console.log("--------------------------------------------------");
74
+ const ts = Math.floor(Date.now() / 1000);
75
+ const filename = `All_Resources_Inventories_${ts}.csv`;
76
+ const fullPath = path_1.default.resolve(filename);
77
+ (0, utilities_1.saveCsv)(rows, filename);
78
+ console.log(`📁 CSV saved: ${fullPath}`);
79
+ }
@@ -0,0 +1 @@
1
+ export declare function generateUsersCollaborationCSV(clientId: string, clientSecret: string, instanceUrl: string): Promise<void>;
@@ -0,0 +1,70 @@
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.generateUsersCollaborationCSV = generateUsersCollaborationCSV;
7
+ const path_1 = __importDefault(require("path"));
8
+ const utilities_1 = require("../utilities");
9
+ async function generateUsersCollaborationCSV(clientId, clientSecret, instanceUrl) {
10
+ let offset = 0;
11
+ const limit = 100;
12
+ const allUsers = [];
13
+ let token = "";
14
+ console.log("🚀 Starting Users Collaboration Groups export...");
15
+ console.log("--------------------------------------------------");
16
+ while (true) {
17
+ const usersUrl = `https://${instanceUrl}.fs.ocs.oraclecloud.com/rest/ofscCore/v1/users/?offset=${offset}&limit=${limit}`;
18
+ console.log(`➡️ Fetching users offset=${offset}`);
19
+ const res = await (0, utilities_1.fetchWithRetry)(usersUrl, clientId, clientSecret, instanceUrl, token);
20
+ token = res.token;
21
+ const data = res.data;
22
+ if (!data?.items?.length) {
23
+ console.warn("⚠ No user items returned. Breaking.");
24
+ break;
25
+ }
26
+ allUsers.push(...data.items);
27
+ console.log(` ✔ Received ${data.items.length} users (Total: ${allUsers.length})`);
28
+ if (offset + limit >= data.totalResults)
29
+ break;
30
+ offset += limit;
31
+ }
32
+ console.log("--------------------------------------------------");
33
+ console.log(`🧩 Total users to process: ${allUsers.length}`);
34
+ console.log("--------------------------------------------------");
35
+ // 2. Fetch collaboration groups for each user
36
+ const rows = [];
37
+ for (const user of allUsers) {
38
+ console.log(`👤 Fetching groups for ${user.login}`);
39
+ if (user.status !== "active") {
40
+ console.log(` ⚠ Skipping inactive user ${user.login}`);
41
+ continue;
42
+ }
43
+ try {
44
+ const groupUrl = `https://${instanceUrl}.fs.ocs.oraclecloud.com/rest/ofscCore/v1/users/${user.login}/collaborationGroups`;
45
+ const res = await (0, utilities_1.fetchWithRetry)(groupUrl, clientId, clientSecret, instanceUrl, token);
46
+ token = res.token;
47
+ const groupData = res.data;
48
+ const groups = groupData?.items ?? [];
49
+ if (groups.length === 0) {
50
+ console.log(` ⚠ No groups found for ${user.login}`);
51
+ continue;
52
+ }
53
+ for (const group of groups) {
54
+ rows.push({ ...group, login: user.login, userType: user.userType, timeZoneIANA: user.timeZoneIANA, userName: user.name });
55
+ }
56
+ console.log(` ✔ Found ${groups.length} groups`);
57
+ }
58
+ catch (err) {
59
+ console.error(`❌ Error fetching groups for ${user.login}:`, err);
60
+ }
61
+ }
62
+ console.log("--------------------------------------------------");
63
+ console.log(`📦 Total rows: ${rows.length}`);
64
+ console.log("--------------------------------------------------");
65
+ const ts = Math.floor(Date.now() / 1000);
66
+ const filename = `collaborationGroups_${ts}.csv`;
67
+ const fullPath = path_1.default.resolve(filename);
68
+ (0, utilities_1.saveCsv)(rows, filename);
69
+ console.log(`📁 CSV saved: ${fullPath}`);
70
+ }
@@ -1 +1,7 @@
1
1
  export declare function downloadAllUsersCSV(clientId: string, clientSecret: string, instanceUrl: string): Promise<void>;
2
+ export { generateUsersCollaborationCSV } from './collaborationGroups';
3
+ declare const OfscUserUtility: {
4
+ generateUsersCollaborationCSV: any;
5
+ downloadAllUsersCSV: typeof downloadAllUsersCSV;
6
+ };
7
+ export default OfscUserUtility;
@@ -36,6 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
36
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.generateUsersCollaborationCSV = void 0;
39
40
  exports.downloadAllUsersCSV = downloadAllUsersCSV;
40
41
  const fs = __importStar(require("fs"));
41
42
  const node_fetch_1 = __importDefault(require("node-fetch"));
@@ -113,3 +114,10 @@ async function downloadAllUsersCSV(clientId, clientSecret, instanceUrl) {
113
114
  console.log(`🧩 Date Time: ${new Date()}`);
114
115
  console.log("-------------------------------------");
115
116
  }
117
+ var collaborationGroups_1 = require("./collaborationGroups");
118
+ Object.defineProperty(exports, "generateUsersCollaborationCSV", { enumerable: true, get: function () { return collaborationGroups_1.generateUsersCollaborationCSV; } });
119
+ const OfscUserUtility = {
120
+ generateUsersCollaborationCSV: require('./collaborationGroups').generateUsersCollaborationCSV,
121
+ downloadAllUsersCSV
122
+ };
123
+ exports.default = OfscUserUtility;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ofsc-utility",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "A wrapper for Oracle Field Service REST API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/readme.md CHANGED
@@ -25,11 +25,15 @@ Please see the code snippet below.
25
25
 
26
26
  #### csv
27
27
 
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")
28
+ downloadWorkZoneCSV(process.env.clientID, process.env.clientSecret, process.env.instanceId)
29
+ downloadAllResourcesCSV(process.env.clientID, process.env.clientSecret, process.env.instanceId)
30
+ downloadAllUsersCSV(process.env.clientID, process.env.clientSecret, process.env.instanceId)
31
+ downloadAllInventoryTypesCSV(process.env.clientID, process.env.clientSecret, process.env.instanceId)
32
+ downloadAllEventsOfDayCSV(process.env.clientID, process.env.clientSecret, process.env.instanceId, process.env.subscriptionId,"2025-12-05")
33
+ // Download Collaboration groups assigned to users
34
+ generateUsersCollaborationCSV(process.env.clientID, process.env.clientSecret, process.env.instanceId)
35
+ // Download all resource's inventories
36
+ generateAllOnHandInventoryOfAllResourcesCSV(process.env.clientID, process.env.clientSecret, process.env.instanceId)
33
37
 
34
38
  #### records
35
39
 
@@ -39,7 +43,7 @@ Please see the code snippet below.
39
43
  getAllActivities("clientId", "clientSecret", "instanceId"."resources","dateFrom","dateTo","q","fields")
40
44
  getActivityCustomerInventories("clientId", "clientSecret", "instanceId"."activityId")
41
45
  createActivityCustomerInventories( "clientId", "clientSecret", "instanceId"."activityId","payload")
42
- downloadAllEventsOfDay(process.env.clientID, process.env.clientSecreat, process.env.instanceId, process.env.subscriptionId,"2025-12-05")
46
+ downloadAllEventsOfDay(process.env.clientID, process.env.clientSecret, process.env.instanceId, process.env.subscriptionId,"2025-12-05")
43
47
 
44
48
  ## Usage
45
49
 
@@ -47,6 +51,17 @@ downloadWorkZoneCSV("bot", "XXXXXXXXX", "compXXX.test")
47
51
 
48
52
  ### CommonJS
49
53
 
54
+ ```js
55
+ const ofs = require("ofsc-utility");
56
+
57
+ ofs.User.generateUsersCollaborationCSV(
58
+ process.env.clientID,
59
+ process.env.clientSecret,
60
+ process.env.instanceId,
61
+ process.env.subscriptionId
62
+ );
63
+ ```
64
+
50
65
  ```js
51
66
  async function run() {
52
67
  let data = await ofs.InventoryType.getInventoryTypesDetail("clientId", "clientSecret", "instanceId", "inventory_label");
@@ -174,7 +189,7 @@ ofs.getActivityCustomerInventories("CLIENT_ID", "CLIENT_SECRET", "INSTANCE_URL",
174
189
  const ofs = require("ofsc-utility");
175
190
  ofs.Events.downloadAllEventsOfDayCSV(
176
191
  process.env.clientID,
177
- process.env.clientSecreat,
192
+ process.env.clientSecret,
178
193
  process.env.instanceId,
179
194
  process.env.subscriptionId,
180
195
  "2025-12-05"