3
3
* @description Contains middleware that updates a user's contact preference, CPU threshold, memory threshold, and container stop preference in database
4
4
*/
5
5
6
- const db = require ( '../models/cloudModel' ) ;
7
-
8
- const configController = { } ;
6
+ import db from '../models/cloudModel' ;
7
+ import { Request , Response , NextFunction } from 'express' ;
8
+ import { ConfigController , ServerError } from '../../types' ;
9
9
10
+ const configController : ConfigController = {
10
11
// update configuration thresholds
11
- configController . configureThresholds = ( req , res , next ) => {
12
+ configureThresholds : ( req : Request , res : Response , next : NextFunction ) => {
12
13
if ( res . locals . error ) return next ( ) ;
13
14
14
15
const { contact_pref, mem_threshold, cpu_threshold, container_stops, _id } = req . body ;
@@ -17,21 +18,21 @@ configController.configureThresholds = (req, res, next) => {
17
18
const thresholdDetails = [ contact_pref , mem_threshold , cpu_threshold , container_stops , _id ] ;
18
19
19
20
db . query ( inputThresholds , thresholdDetails )
20
- . then ( ( data ) => {
21
+ . then ( ( data : any ) => {
21
22
res . locals . user = data . rows [ 0 ] ;
22
23
return next ( ) ;
23
24
} )
24
- . catch ( ( err ) => {
25
+ . catch ( ( err : ServerError ) => {
25
26
return next ( {
26
27
log : `Error in userController newUser: ${ err } ` ,
27
- message : { err : 'An error occured creating new user in database. See userController.newUser.' } ,
28
+ message : { err : 'An error occured creating new user in database. See userController .newUser.' } ,
28
29
} ) ;
29
30
} ) ;
30
31
31
- } ;
32
+ } ,
32
33
33
34
// configure contact preference
34
- configController . updateContactPref = ( req , res , next ) => {
35
+ updateContactPref : ( req : Request , res : Response , next : NextFunction ) => {
35
36
if ( res . locals . error ) return next ( ) ;
36
37
37
38
const { contact_pref, _id } = req . body ;
@@ -40,20 +41,20 @@ configController.updateContactPref = (req, res, next) => {
40
41
const prefDetails = [ contact_pref , _id ] ;
41
42
42
43
db . query ( inputPref , prefDetails )
43
- . then ( ( data ) => {
44
+ . then ( ( data : any ) => {
44
45
res . locals . user = data . rows [ 0 ] ;
45
46
return next ( ) ;
46
47
} )
47
- . catch ( ( err ) => {
48
+ . catch ( ( err : ServerError ) => {
48
49
return next ( {
49
50
log : `Error in configController updateContactPref: ${ err } ` ,
50
- message : { err : 'An error occured updating contact preferences in database. See configController.updateContactPref.' } ,
51
+ message : { err : 'An error occured updating contact preferences in database. See configController .updateContactPref.' } ,
51
52
} ) ;
52
53
} ) ;
53
- } ;
54
+ } ,
54
55
55
56
// configure CPU threshold
56
- configController . updateCPUThreshold = ( req , res , next ) => {
57
+ updateCPUThreshold : ( req : Request , res : Response , next : NextFunction ) => {
57
58
if ( res . locals . error ) return next ( ) ;
58
59
59
60
const { cpu_threshold, _id } = req . body ;
@@ -62,20 +63,20 @@ configController.updateCPUThreshold = (req, res, next) => {
62
63
const CPUDetails = [ cpu_threshold , _id ] ;
63
64
64
65
db . query ( inputCPU , CPUDetails )
65
- . then ( ( data ) => {
66
+ . then ( ( data : any ) => {
66
67
res . locals . user = data . rows [ 0 ] ;
67
68
return next ( ) ;
68
69
} )
69
- . catch ( ( err ) => {
70
+ . catch ( ( err : ServerError ) => {
70
71
return next ( {
71
72
log : `Error in configController updateCPUThreshold: ${ err } ` ,
72
- message : { err : 'An error occured updating CPU threshold in database. See configController.updateCPUThreshold.' } ,
73
+ message : { err : 'An error occured updating CPU threshold in database. See configController .updateCPUThreshold.' } ,
73
74
} ) ;
74
75
} ) ;
75
- } ;
76
+ } ,
76
77
77
78
// configure memory threshold
78
- configController . updateMemThreshold = ( req , res , next ) => {
79
+ updateMemThreshold : ( req : Request , res : Response , next : NextFunction ) => {
79
80
if ( res . locals . error ) return next ( ) ;
80
81
81
82
const { mem_threshold, _id } = req . body ;
@@ -84,20 +85,20 @@ configController.updateMemThreshold = (req, res, next) => {
84
85
const memDetails = [ mem_threshold , _id ] ;
85
86
86
87
db . query ( inputMem , memDetails )
87
- . then ( ( data ) => {
88
+ . then ( ( data : any ) => {
88
89
res . locals . user = data . rows [ 0 ] ;
89
90
return next ( ) ;
90
91
} )
91
- . catch ( ( err ) => {
92
+ . catch ( ( err : ServerError ) => {
92
93
return next ( {
93
94
log : `Error in configController updateMemThreshold: ${ err } ` ,
94
- message : { err : 'An error occured updating memory threshold in database. See configController.updateMemThreshold.' } ,
95
+ message : { err : 'An error occured updating memory threshold in database. See configController .updateMemThreshold.' } ,
95
96
} ) ;
96
97
} ) ;
97
- } ;
98
+ } ,
98
99
99
100
// configure preference to receive notification when a container stops running
100
- configController . updateStopPref = ( req , res , next ) => {
101
+ updateStopPref : ( req : Request , res : Response , next : NextFunction ) => {
101
102
if ( res . locals . error ) return next ( ) ;
102
103
103
104
const { container_stops, _id } = req . body ;
@@ -106,16 +107,18 @@ configController.updateStopPref = (req, res, next) => {
106
107
const stopPrefDetails = [ container_stops , _id ] ;
107
108
108
109
db . query ( inputStopPref , stopPrefDetails )
109
- . then ( ( data ) => {
110
+ . then ( ( data : any ) => {
110
111
res . locals . user = data . rows [ 0 ] ;
111
112
return next ( ) ;
112
113
} )
113
- . catch ( ( err ) => {
114
+ . catch ( ( err : ServerError ) => {
114
115
return next ( {
115
116
log : `Error in configController updateStopPref: ${ err } ` ,
116
- message : { err : 'An error occured updating container stop preference in database. See configController.updateStopPref.' } ,
117
+ message : { err : 'An error occured updating container stop preference in database. See configController .updateStopPref.' } ,
117
118
} ) ;
118
119
} ) ;
119
- } ;
120
+ } ,
121
+
122
+ }
120
123
121
- module . exports = configController ;
124
+ export default configController ;
0 commit comments