Skip to content

Commit ce3900a

Browse files
committed
fix device import again
1 parent bed3bb4 commit ce3900a

3 files changed

Lines changed: 12 additions & 51 deletions

File tree

plugins/NinjaOne/v1/dataStreams/devices.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"expandInnerObjects": true,
2525
"endpointPath": "/v2/devices",
2626
"postRequestScript": "devices.js",
27-
"pathToData": "results",
27+
"pathToData": "",
2828
"getArgs": [],
2929
"headers": []
3030
},
Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,31 @@
1-
// Handle NinjaOne array or paged response and convert numeric IDs to strings for SquaredUp compatibility
21
const items = (data && data.results ? data.results : (Array.isArray(data) ? data : []));
32

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-
*/
113
const convertTimestamps = (obj) => {
124
if (!obj || typeof obj !== 'object') return obj;
135
for (const key in obj) {
146
const val = obj[key];
157
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+
}
2314
} else if (typeof val === 'object' && val !== null) {
2415
convertTimestamps(val);
2516
}
2617
}
2718
return obj;
2819
};
2920

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 => {
3222
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,
3726
id: converted.id ? converted.id.toString() : null,
38-
// Ensure displayName is present for the import engine (maps to systemName if missing)
3927
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
4128
organizationId: converted.organizationId ? converted.organizationId.toString() : null,
4229
locationId: converted.locationId ? converted.locationId.toString() : null
4330
};
4431
});
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;

plugins/NinjaOne/v1/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ninja-one",
33
"displayName": "NinjaOne",
4-
"version": "1.1.2",
4+
"version": "1.1.3",
55
"author": {
66
"name": "SquaredUp Labs",
77
"type": "labs"

0 commit comments

Comments
 (0)