ofsc-utility 1.0.11 → 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,6 +9,7 @@ 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';
@@ -30,5 +32,6 @@ declare const OfscUtility: {
30
32
  downloadAllEventsOfDayCSV: any;
31
33
  downloadAllEventsOfDay: any;
32
34
  generateUsersCollaborationCSV: any;
35
+ generateAllOnHandInventoryOfAllResourcesCSV: any;
33
36
  };
34
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.generateUsersCollaborationCSV = 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");
@@ -85,6 +87,7 @@ const OfscUtility = {
85
87
  createActivityCustomerInventories: require('./activityInventories').createActivityCustomerInventories,
86
88
  downloadAllEventsOfDayCSV: require('./events').downloadAllEventsOfDayCSV,
87
89
  downloadAllEventsOfDay: require('./events').downloadAllEventsOfDay,
88
- generateUsersCollaborationCSV: require('./users').generateUsersCollaborationCSV
90
+ generateUsersCollaborationCSV: require('./users').generateUsersCollaborationCSV,
91
+ generateAllOnHandInventoryOfAllResourcesCSV: require('./inventory').generateAllOnHandInventoryOfAllResourcesCSV
89
92
  };
90
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ofsc-utility",
3
- "version": "1.0.11",
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,12 +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")
33
- generateUsersCollaborationCSV(process.env.clientID, process.env.clientSecreat, process.env.instanceId) // all Collaboration groups of a user
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)
34
37
 
35
38
  #### records
36
39
 
@@ -40,7 +43,7 @@ Please see the code snippet below.
40
43
  getAllActivities("clientId", "clientSecret", "instanceId"."resources","dateFrom","dateTo","q","fields")
41
44
  getActivityCustomerInventories("clientId", "clientSecret", "instanceId"."activityId")
42
45
  createActivityCustomerInventories( "clientId", "clientSecret", "instanceId"."activityId","payload")
43
- 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")
44
47
 
45
48
  ## Usage
46
49
 
@@ -53,7 +56,7 @@ const ofs = require("ofsc-utility");
53
56
 
54
57
  ofs.User.generateUsersCollaborationCSV(
55
58
  process.env.clientID,
56
- process.env.clientSecreat,
59
+ process.env.clientSecret,
57
60
  process.env.instanceId,
58
61
  process.env.subscriptionId
59
62
  );
@@ -186,7 +189,7 @@ ofs.getActivityCustomerInventories("CLIENT_ID", "CLIENT_SECRET", "INSTANCE_URL",
186
189
  const ofs = require("ofsc-utility");
187
190
  ofs.Events.downloadAllEventsOfDayCSV(
188
191
  process.env.clientID,
189
- process.env.clientSecreat,
192
+ process.env.clientSecret,
190
193
  process.env.instanceId,
191
194
  process.env.subscriptionId,
192
195
  "2025-12-05"