|
1 | | -// Handle NinjaOne array or paged response and convert numeric IDs to strings for SquaredUp compatibility |
2 | 1 | const items = (data && data.results ? data.results : (Array.isArray(data) ? data : [])); |
3 | 2 |
|
4 | | -/** |
5 | | - * Recursively converts NinjaOne Unix timestamps (seconds) to ISO strings. |
6 | | - * NinjaOne typically returns timestamps as floats/integers in seconds. |
7 | | - * SquaredUp's date shape/JS Date expects milliseconds or ISO strings. |
8 | | - * @param {any} obj |
9 | | - * @returns {any} |
10 | | - */ |
11 | 3 | const convertTimestamps = (obj) => { |
12 | 4 | if (!obj || typeof obj !== 'object') return obj; |
13 | 5 | for (const key in obj) { |
14 | 6 | const val = obj[key]; |
15 | 7 | if (typeof val === 'number') { |
16 | | - // Heuristic: check if key name suggests a timestamp and value is in seconds range (1e9 to 1e10) |
17 | | - const lowerKey = key.toLowerCase(); |
18 | | - if (lowerKey.includes('time') || lowerKey.includes('date') || lowerKey.includes('contact') || lowerKey.includes('update') || lowerKey.includes('start') || lowerKey.includes('end')) { |
19 | | - if (val > 1000000000 && val < 10000000000) { |
20 | | - obj[key] = new Date(val * 1000).toISOString(); |
21 | | - } |
22 | | - } |
| 8 | + const lowerKey = key.toLowerCase(); |
| 9 | + if (lowerKey.includes('time') || lowerKey.includes('date') || lowerKey.includes('contact') || lowerKey.includes('update') || lowerKey.includes('start') || lowerKey.includes('end')) { |
| 10 | + if (val > 1000000000 && val < 10000000000) { |
| 11 | + obj[key] = new Date(val * 1000).toISOString(); |
| 12 | + } |
| 13 | + } |
23 | 14 | } else if (typeof val === 'object' && val !== null) { |
24 | 15 | convertTimestamps(val); |
25 | 16 | } |
26 | 17 | } |
27 | 18 | return obj; |
28 | 19 | }; |
29 | 20 |
|
30 | | -const processedItems = items.map(item => { |
31 | | - // Recursively convert timestamps in the item (modifies in place but we are mapping to a new object) |
| 21 | +result = items.map(item => { |
32 | 22 | const converted = convertTimestamps(item); |
33 | | - |
34 | | - return { |
35 | | - ...converted, |
36 | | - deviceId: converted.id ? converted.id.toString() : null, |
| 23 | + return { |
| 24 | + ...converted, |
| 25 | + deviceId: converted.id ? converted.id.toString() : null, |
37 | 26 | id: converted.id ? converted.id.toString() : null, |
38 | | - // Ensure displayName is present for the import engine (maps to systemName if missing) |
39 | 27 | displayName: converted.displayName || converted.systemName || converted.dnsName || (converted.id ? `Device ${converted.id}` : 'Unknown Device'), |
40 | | - // Add organizationId and locationId as strings if they are there |
41 | 28 | organizationId: converted.organizationId ? converted.organizationId.toString() : null, |
42 | 29 | locationId: converted.locationId ? converted.locationId.toString() : null |
43 | 30 | }; |
44 | 31 | }); |
45 | | - |
46 | | -// Always return a consistent wrapped object for SquaredUp compatibility |
47 | | -// This ensures that pathToData: "results" always works, and paging metadata is consistently located. |
48 | | -result = { |
49 | | - results: processedItems, |
50 | | - metadata: (data && data.metadata) ? { ...data.metadata } : {} |
51 | | -}; |
52 | | - |
53 | | -// Capture next_page_token from root if present (some endpoints put it there) |
54 | | -if (data && data.next_page_token) { |
55 | | - result.metadata.next_page_token = data.next_page_token; |
56 | | -} |
57 | | - |
58 | | -// Capture cursor from root if present (used by some endpoints) |
59 | | -if (data && data.cursor) { |
60 | | - result.metadata.cursor = data.cursor; |
61 | | -} |
62 | | - |
63 | | -// If the response was a flat array, we can still support paging by providing a next_page_token |
64 | | -// based on the last item's ID (which is what NinjaOne often expects in 'after' param) |
65 | | -if (Array.isArray(data) && !result.metadata.next_page_token) { |
66 | | - result.metadata.next_page_token = processedItems.length > 0 ? processedItems[processedItems.length - 1].id : null; |
67 | | -} |
68 | | - |
69 | | -// Return the result object |
70 | | -result; |
0 commit comments