Skip to content

Commit 8ec98c9

Browse files
committed
Merge branch 'dev' of https://github.com/oslabs-beta/Docketeer into dev
2 parents 6e8d74b + 0b50f0c commit 8ec98c9

File tree

4 files changed

+81
-64
lines changed

4 files changed

+81
-64
lines changed

server/controllers/configController.js renamed to server/controllers/configController.ts

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
* @description Contains middleware that updates a user's contact preference, CPU threshold, memory threshold, and container stop preference in database
44
*/
55

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';
99

10+
const configController: ConfigController = {
1011
// update configuration thresholds
11-
configController.configureThresholds = (req, res, next) => {
12+
configureThresholds: (req: Request, res: Response, next: NextFunction ) => {
1213
if (res.locals.error) return next();
1314

1415
const { contact_pref, mem_threshold, cpu_threshold, container_stops, _id } = req.body;
@@ -17,21 +18,21 @@ configController.configureThresholds = (req, res, next) => {
1718
const thresholdDetails = [contact_pref, mem_threshold, cpu_threshold, container_stops, _id];
1819

1920
db.query(inputThresholds, thresholdDetails)
20-
.then((data) => {
21+
.then((data: any) => {
2122
res.locals.user = data.rows[0];
2223
return next();
2324
})
24-
.catch((err) => {
25+
.catch((err: ServerError) => {
2526
return next({
2627
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.' },
2829
});
2930
});
3031

31-
};
32+
},
3233

3334
// configure contact preference
34-
configController.updateContactPref = (req, res, next) => {
35+
updateContactPref: (req: Request, res: Response, next: NextFunction) => {
3536
if (res.locals.error) return next();
3637

3738
const { contact_pref, _id } = req.body;
@@ -40,20 +41,20 @@ configController.updateContactPref = (req, res, next) => {
4041
const prefDetails = [contact_pref, _id];
4142

4243
db.query(inputPref, prefDetails)
43-
.then((data) => {
44+
.then((data: any) => {
4445
res.locals.user = data.rows[0];
4546
return next();
4647
})
47-
.catch((err) => {
48+
.catch((err: ServerError) => {
4849
return next({
4950
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.' },
5152
});
5253
});
53-
};
54+
},
5455

5556
// configure CPU threshold
56-
configController.updateCPUThreshold = (req, res, next) => {
57+
updateCPUThreshold: (req: Request, res: Response, next: NextFunction) => {
5758
if (res.locals.error) return next();
5859

5960
const { cpu_threshold, _id } = req.body;
@@ -62,20 +63,20 @@ configController.updateCPUThreshold = (req, res, next) => {
6263
const CPUDetails = [cpu_threshold, _id];
6364

6465
db.query(inputCPU, CPUDetails)
65-
.then((data) => {
66+
.then((data: any) => {
6667
res.locals.user = data.rows[0];
6768
return next();
6869
})
69-
.catch((err) => {
70+
.catch((err: ServerError) => {
7071
return next({
7172
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.' },
7374
});
7475
});
75-
};
76+
},
7677

7778
// configure memory threshold
78-
configController.updateMemThreshold = (req, res, next) => {
79+
updateMemThreshold: (req: Request, res: Response, next: NextFunction) => {
7980
if (res.locals.error) return next();
8081

8182
const { mem_threshold, _id } = req.body;
@@ -84,20 +85,20 @@ configController.updateMemThreshold = (req, res, next) => {
8485
const memDetails = [mem_threshold, _id];
8586

8687
db.query(inputMem, memDetails)
87-
.then((data) => {
88+
.then((data: any) => {
8889
res.locals.user = data.rows[0];
8990
return next();
9091
})
91-
.catch((err) => {
92+
.catch((err: ServerError) => {
9293
return next({
9394
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.' },
9596
});
9697
});
97-
};
98+
},
9899

99100
// configure preference to receive notification when a container stops running
100-
configController.updateStopPref = (req, res, next) => {
101+
updateStopPref: (req: Request, res: Response, next: NextFunction) => {
101102
if (res.locals.error) return next();
102103

103104
const { container_stops, _id } = req.body;
@@ -106,16 +107,18 @@ configController.updateStopPref = (req, res, next) => {
106107
const stopPrefDetails = [container_stops, _id];
107108

108109
db.query(inputStopPref, stopPrefDetails)
109-
.then((data) => {
110+
.then((data: any) => {
110111
res.locals.user = data.rows[0];
111112
return next();
112113
})
113-
.catch((err) => {
114+
.catch((err: ServerError) => {
114115
return next({
115116
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.' },
117118
});
118119
});
119-
};
120+
},
121+
122+
}
120123

121-
module.exports = configController;
124+
export default configController;

server/controllers/cookieController.js

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @module Cookie Controller
3+
* @description Contains middleware that stores the user id in a HTTP-only cookie and sets HTTP-only cookie specifically for admins
4+
*/
5+
6+
import { Request, Response, NextFunction } from 'express';
7+
import { CookieController, ServerError } from '../../types'
8+
9+
const cookieController: CookieController = {
10+
// store the user id in a cookie
11+
setSSIDCookie: (req: Request, res: Response, next: NextFunction) => {
12+
if (res.locals.error) return next();
13+
14+
res.cookie('ssid', res.locals.user._id, { httpOnly: true });
15+
return next();
16+
},
17+
// set admin cookie for users with admin privileges
18+
setAdminCookie: (req: Request, res: Response, next: NextFunction) => {
19+
if (res.locals.error) return next();
20+
21+
const { role_id } = res.locals.user;
22+
23+
if (role_id === 1) {
24+
res.cookie('adminType', 'system admin', { httpOnly: true });
25+
res.locals.cookie = 'system admin';
26+
}
27+
if (role_id === 2) {
28+
res.cookie('adminType', 'admin', { httpOnly: true });
29+
res.locals.cookie = 'admin';
30+
}
31+
return next();
32+
}
33+
};
34+
35+
export default cookieController;

types.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,20 @@ export interface CommandController{
522522
composeStacks: (req: Request, res: Response, next: NextFunction) => void,
523523
composeDown: (req: Request, res: Response, next: NextFunction) => void,
524524
getAllDockerVolumes: (req: Request, res: Response, next: NextFunction) => void,
525-
getVolumeContainers: (req: Request, res: Response, next: NextFunction) => void,getLogs: (req: Request, res: Response, next: NextFunction) => void,
525+
getVolumeContainers: (req: Request, res: Response, next: NextFunction) => void,
526+
getLogs: (req: Request, res: Response, next: NextFunction) => void,
527+
}
528+
export interface CookieController {
529+
setSSIDCookie: (req: Request, res: Response, next: NextFunction) => void,
530+
setAdminCookie: (req: Request, res: Response, next: NextFunction) => void,
531+
}
532+
533+
export interface ConfigController{
534+
configureThresholds: (req: Request, res: Response, next: NextFunction) => void,
535+
updateContactPref: (req: Request, res: Response, next: NextFunction) => void,
536+
updateCPUThreshold: (req: Request, res: Response, next: NextFunction) => void,
537+
updateMemThreshold: (req: Request, res: Response, next: NextFunction) => void,
538+
updateStopPref: (req: Request, res: Response, next: NextFunction) => void,
526539
}
527540

528541
export interface DbController{
@@ -540,7 +553,6 @@ export interface InitController{
540553
gitUrl: (req: Request, res: Response, next: NextFunction) => void,
541554
addMetrics: (req: Request, res: Response, next: NextFunction) => void,
542555
getMetrics:(req: Request, res: Response, next: NextFunction) => void,
543-
544556
}
545557

546558
export interface SettingsController {

0 commit comments

Comments
 (0)