1+ // Handle NinjaOne paged response for device health
2+ const items = ( data && data . results ? data . results : ( Array . isArray ( data ) ? data : [ ] ) ) ;
3+
4+ const convertTimestamps = ( obj ) => {
5+ if ( ! obj || typeof obj !== 'object' ) return obj ;
6+ for ( const key in obj ) {
7+ const val = obj [ key ] ;
8+ if ( typeof val === 'number' ) {
9+ const lowerKey = key . toLowerCase ( ) ;
10+ if ( lowerKey . includes ( 'time' ) || lowerKey . includes ( 'date' ) || lowerKey . includes ( 'contact' ) || lowerKey . includes ( 'update' ) || lowerKey . includes ( 'start' ) || lowerKey . includes ( 'end' ) || lowerKey . includes ( 'expires' ) ) {
11+ if ( val > 1000000000 && val < 10000000000 ) {
12+ obj [ key ] = new Date ( val * 1000 ) . toISOString ( ) ;
13+ }
14+ }
15+ } else if ( typeof val === 'object' && val !== null ) {
16+ convertTimestamps ( val ) ;
17+ }
18+ }
19+ return obj ;
20+ } ;
21+
22+ const formatProductsInstallationStatuses = ( statuses ) => {
23+ if ( ! statuses || typeof statuses !== 'object' ) return statuses ;
24+ const parts = [ ] ;
25+ for ( const [ product , status ] of Object . entries ( statuses ) ) {
26+ parts . push ( `${ product } : ${ status } ` ) ;
27+ }
28+ return parts . join ( ', ' ) ;
29+ } ;
30+
31+ result = items . map ( item => {
32+ const converted = convertTimestamps ( item ) ;
33+ const transformed = { ...converted } ;
34+ if ( transformed . deviceId !== undefined ) transformed . deviceId = transformed . deviceId . toString ( ) ;
35+ if ( transformed . parentDeviceId !== undefined ) transformed . parentDeviceId = transformed . parentDeviceId . toString ( ) ;
36+ if ( transformed . productsInstallationStatuses !== undefined ) {
37+ transformed . productsInstallationStatuses = formatProductsInstallationStatuses ( transformed . productsInstallationStatuses ) ;
38+ }
39+ return transformed ;
40+ } ) ;
0 commit comments