@@ -449,6 +449,9 @@ const processCohortIds = async (cohort_ids, request) => {
449
449
( result ) => ! ( result . success === false )
450
450
) ;
451
451
452
+ // join the array of arrays into a single array
453
+ const flattened = [ ] . concat ( ...validDeviceIdResults ) ;
454
+
452
455
if ( isEmpty ( invalidDeviceIdResults ) && validDeviceIdResults . length > 0 ) {
453
456
request . query . device_id = validDeviceIdResults . join ( "," ) ;
454
457
}
@@ -2975,6 +2978,168 @@ const createEvent = {
2975
2978
return ;
2976
2979
}
2977
2980
} ,
2981
+ getWorstReadingForSites : async ( req , res , next ) => {
2982
+ try {
2983
+ const errors = extractErrorsFromRequest ( req ) ;
2984
+ if ( errors ) {
2985
+ next (
2986
+ new HttpError ( "bad request errors" , httpStatus . BAD_REQUEST , errors )
2987
+ ) ;
2988
+ return ;
2989
+ }
2990
+
2991
+ const request = req ;
2992
+ const defaultTenant = constants . DEFAULT_TENANT || "airqo" ;
2993
+ request . query . tenant = isEmpty ( req . query . tenant )
2994
+ ? defaultTenant
2995
+ : req . query . tenant ;
2996
+
2997
+ const { site_id, grid_id } = { ...req . query , ...req . params } ;
2998
+ let locationErrors = 0 ;
2999
+
3000
+ let siteIds = [ ] ;
3001
+ if ( Array . isArray ( site_id ) ) {
3002
+ siteIds = site_id . map ( String ) ;
3003
+ } else if ( site_id ) {
3004
+ siteIds = [ String ( site_id ) ] ;
3005
+ }
3006
+
3007
+ if ( isEmpty ( siteIds ) && ! isEmpty ( grid_id ) ) {
3008
+ await processGridIds ( grid_id , request ) ;
3009
+ if ( isEmpty ( request . query . site_id ) ) {
3010
+ locationErrors ++ ;
3011
+ } else {
3012
+ siteIds = request . query . site_id . split ( "," ) ;
3013
+ }
3014
+ }
3015
+
3016
+ if ( locationErrors === 0 ) {
3017
+ const result = await createEventUtil . getWorstReadingForSites ( {
3018
+ siteIds,
3019
+ next,
3020
+ } ) ;
3021
+
3022
+ if ( isEmpty ( result ) || res . headersSent ) {
3023
+ return ;
3024
+ }
3025
+
3026
+ if ( result . success === true ) {
3027
+ const status = result . status || httpStatus . OK ;
3028
+ res . status ( status ) . json ( {
3029
+ success : true ,
3030
+ message : result . message ,
3031
+ data : result . data ,
3032
+ } ) ;
3033
+ } else {
3034
+ const errorStatus = result . status || httpStatus . INTERNAL_SERVER_ERROR ;
3035
+ res . status ( errorStatus ) . json ( {
3036
+ success : false ,
3037
+ errors : result . errors || { message : "" } ,
3038
+ message : result . message ,
3039
+ } ) ;
3040
+ }
3041
+ } else {
3042
+ res . status ( httpStatus . BAD_REQUEST ) . json ( {
3043
+ success : false ,
3044
+ errors : {
3045
+ message : `Unable to process measurements for the provided site IDs` ,
3046
+ } ,
3047
+ message : "Bad Request Error" ,
3048
+ } ) ;
3049
+ }
3050
+ } catch ( error ) {
3051
+ logger . error ( `🐛🐛 Internal Server Error ${ error . message } ` ) ;
3052
+ next (
3053
+ new HttpError (
3054
+ "Internal Server Error" ,
3055
+ httpStatus . INTERNAL_SERVER_ERROR ,
3056
+ { message : error . message }
3057
+ )
3058
+ ) ;
3059
+ return ;
3060
+ }
3061
+ } ,
3062
+ getWorstReadingForDevices : async ( req , res , next ) => {
3063
+ try {
3064
+ const errors = extractErrorsFromRequest ( req ) ;
3065
+ if ( errors ) {
3066
+ next (
3067
+ new HttpError ( "bad request errors" , httpStatus . BAD_REQUEST , errors )
3068
+ ) ;
3069
+ return ;
3070
+ }
3071
+
3072
+ const request = req ;
3073
+ const defaultTenant = constants . DEFAULT_TENANT || "airqo" ;
3074
+ request . query . tenant = isEmpty ( req . query . tenant )
3075
+ ? defaultTenant
3076
+ : req . query . tenant ;
3077
+
3078
+ const { device_id, cohort_id } = { ...req . query , ...req . params } ;
3079
+ let locationErrors = 0 ;
3080
+
3081
+ let deviceIds = [ ] ;
3082
+ if ( Array . isArray ( device_id ) ) {
3083
+ deviceIds = device_id . map ( String ) ;
3084
+ } else if ( device_id ) {
3085
+ deviceIds = [ String ( device_id ) ] ;
3086
+ }
3087
+
3088
+ if ( isEmpty ( deviceIds ) && ! isEmpty ( cohort_id ) ) {
3089
+ await processCohortIds ( cohort_id , request ) ;
3090
+ if ( isEmpty ( request . query . device_id ) ) {
3091
+ locationErrors ++ ;
3092
+ } else {
3093
+ deviceIds = request . query . device_id . split ( "," ) ;
3094
+ }
3095
+ }
3096
+ logObject ( "deviceIds" , deviceIds ) ;
3097
+ if ( locationErrors === 0 ) {
3098
+ const result = await createEventUtil . getWorstReadingForDevices ( {
3099
+ deviceIds,
3100
+ next,
3101
+ } ) ;
3102
+
3103
+ if ( isEmpty ( result ) || res . headersSent ) {
3104
+ return ;
3105
+ }
3106
+
3107
+ if ( result . success === true ) {
3108
+ const status = result . status || httpStatus . OK ;
3109
+ res . status ( status ) . json ( {
3110
+ success : true ,
3111
+ message : result . message ,
3112
+ data : result . data ,
3113
+ } ) ;
3114
+ } else {
3115
+ const errorStatus = result . status || httpStatus . INTERNAL_SERVER_ERROR ;
3116
+ res . status ( errorStatus ) . json ( {
3117
+ success : false ,
3118
+ errors : result . errors || { message : "" } ,
3119
+ message : result . message ,
3120
+ } ) ;
3121
+ }
3122
+ } else {
3123
+ res . status ( httpStatus . BAD_REQUEST ) . json ( {
3124
+ success : false ,
3125
+ errors : {
3126
+ message : `Unable to process measurements for the provided device IDs` ,
3127
+ } ,
3128
+ message : "Bad Request Error" ,
3129
+ } ) ;
3130
+ }
3131
+ } catch ( error ) {
3132
+ logger . error ( `🐛🐛 Internal Server Error ${ error . message } ` ) ;
3133
+ next (
3134
+ new HttpError (
3135
+ "Internal Server Error" ,
3136
+ httpStatus . INTERNAL_SERVER_ERROR ,
3137
+ { message : error . message }
3138
+ )
3139
+ ) ;
3140
+ return ;
3141
+ }
3142
+ } ,
2978
3143
} ;
2979
3144
2980
3145
module . exports = createEvent ;
0 commit comments