summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-06-01 14:43:00 +0200
committerRory& <root@rory.gay>2025-06-01 14:43:00 +0200
commita3869af4c616da1f8af900594cbad2b829d7644a (patch)
tree67245749d32373b199be7714b30b529715ce4614
parentAdd alarm endpoints, basic budget routes, spend history (diff)
downloadnodejs-final-assignment-a3869af4c616da1f8af900594cbad2b829d7644a.tar.xz
Partial progress on generating http files
-rw-r--r--endpoints.http2
-rw-r--r--plan.md3
-rw-r--r--src/api/routes.js24
-rw-r--r--src/api/routes/alarmRoutes.js12
-rw-r--r--src/api/routes/auth/adminAccountRoutes.js24
-rw-r--r--src/api/routes/auth/index.js1
-rw-r--r--src/db/schemas/user.js6
-rw-r--r--test.http59
8 files changed, 127 insertions, 4 deletions
diff --git a/endpoints.http b/endpoints.http

index cf0bf60..93a4b53 100644 --- a/endpoints.http +++ b/endpoints.http
@@ -2,6 +2,8 @@ @username={{$randomInt}} @email={{$randomInt}}@google.com +GET {{baseUri}} HTTP/1.1 + POST {{baseUri}}/auth/register HTTP/1.1 Content-Type: application/json diff --git a/plan.md b/plan.md
index 59edd66..7e54a64 100644 --- a/plan.md +++ b/plan.md
@@ -26,7 +26,8 @@ - [ ] Get current budget - [ ] Review spending history - [ ] Request additional budget in case of emergency -- [ ] Emergency alarm +- [-] Emergency alarm + - [ ] Get alarm list (monitor) - [ ] Silencing/clearing - [ ] Optional: integration with park's emergency services - [ ] Emergency contact & info card diff --git a/src/api/routes.js b/src/api/routes.js
index 09606f1..5151187 100644 --- a/src/api/routes.js +++ b/src/api/routes.js
@@ -1,6 +1,21 @@ import * as routes from './routes/index.js'; +function logHttpEntry(method, route, exampleBody) { + console.log('%', method, '{{baseUrl}}' + route, 'HTTP/1.1'); + if (exampleBody) { + console.log('%', 'Content-Type: application/json'); + console.log('% '); + console.log('%', exampleBody); + console.log('% '); + } + console.log('% '); + console.log('%', '###'); +} + export function registerRoutes(app) { + // http file header: + console.log('%', ''); + // app.get("/status", routes.statusRoute); let routeCount = 0; Object.values(routes).forEach(route => { @@ -16,19 +31,25 @@ export function registerRoutes(app) { app.get(route.route, route.onGetValidation, route.onGet); else app.get(route.route, route.onGet); routeCount++; + logHttpEntry('GET', route.route, route.exampleGetBody); } + if (route.onPost) { if (route.onPostValidation) app.post(route.route, route.onPostValidation, route.onPost); else app.post(route.route, route.onPost); routeCount++; + logHttpEntry('POST', route.route, route.examplePostBody); } + if (route.onPut) { if (route.onPutValidation) app.put(route.route, route.onPutValidation, route.onPut); else app.put(route.route, route.onPut); routeCount++; + logHttpEntry('PUT', route.route, route.examplePutBody); } + if (route.onDelete) { if (route.onDeleteValidation) app.delete( @@ -38,12 +59,15 @@ export function registerRoutes(app) { ); else app.delete(route.route, route.onDelete); routeCount++; + logHttpEntry('DELETE', route.route, route.exampleDeleteBody); } + if (route.onPatch) { if (route.onPatchValidation) app.patch(route.route, route.onPatchValidation, route.onPatch); else app.patch(route.route, route.onPatch); routeCount++; + logHttpEntry('PATCH', route.route, route.examplePatchBody); } }); console.log(`Registered ${routeCount} routes.`); diff --git a/src/api/routes/alarmRoutes.js b/src/api/routes/alarmRoutes.js
index 5170327..e50d4b2 100644 --- a/src/api/routes/alarmRoutes.js +++ b/src/api/routes/alarmRoutes.js
@@ -5,19 +5,27 @@ export const alarmByUserRoute = { route: '/alarm/:id', onGetValidation: validateAuth({ roles: [UserType.MONITOR] }), async onGet(req, res) { - const user = await getUserById(req.query.id); + const user = await getUserById(req.params.id); res.send(user.alarm); }, onDeleteValidation: validateAuth({ roles: [UserType.MONITOR] }), async onDelete(req, res) { - const user = await getUserById(req.query.id); + const user = await getUserById(req.params.id); user.alarm = null; await user.save(); res.status(204).send(); } }; +export const alarmListRoute = { + route: '/alarms', + onGetValidation: validateAuth({ roles: [UserType.MONITOR] }), + onGet(req, res) { + console.log(req.user.monitoredUsers); + } +}; + export const alarmRoute = { onGetValidation: validateAuth({ roles: [UserType.USER] }), async onGet(req, res) { diff --git a/src/api/routes/auth/adminAccountRoutes.js b/src/api/routes/auth/adminAccountRoutes.js new file mode 100644
index 0000000..f85cc73 --- /dev/null +++ b/src/api/routes/auth/adminAccountRoutes.js
@@ -0,0 +1,24 @@ +import { deleteUser, loginUser, registerUser, UserType } from '#db/index.js'; +import { AuthDto, RegisterDto } from '#dto/index.js'; +import { validateAuth } from '#api/middlewares/index.js'; + +export const adminGetUsersRoute = { + route: '/admin/users', + onGetValidation: validateAuth({ roles: [UserType.ADMIN] }), + async onGet(req, res) { + res.send(DbUser.find({}).exec()); + } +}; +export const adminUserRoute = { + route: '/admin/user/:id', + onGetValidation: validateAuth({ roles: [UserType.ADMIN] }), + async onGet(req, res) { + const user = await getUserById(req.params.id); + }, + + onDeleteValidation: validateAuth({ roles: [UserType.ADMIN] }), + async onDelete(req, res) { + await deleteUser(data); + res.status(204).send(); + } +}; diff --git a/src/api/routes/auth/index.js b/src/api/routes/auth/index.js
index e687911..2d2cc86 100644 --- a/src/api/routes/auth/index.js +++ b/src/api/routes/auth/index.js
@@ -1,2 +1,3 @@ export * from './accountRoutes.js'; export * from './deviceRoutes.js'; +export * from './adminAccountRoutes.js'; diff --git a/src/db/schemas/user.js b/src/db/schemas/user.js
index 063fddf..f8802d7 100644 --- a/src/db/schemas/user.js +++ b/src/db/schemas/user.js
@@ -1,4 +1,4 @@ -import { model, Schema } from 'mongoose'; +import { model, Schema, ObjectId } from 'mongoose'; import { hash, compare } from 'bcrypt'; export const UserType = Object.freeze({ @@ -80,6 +80,10 @@ export const userSchema = new Schema({ }, alarm: { type: alarmSchema + }, + monitoredUsers: { + type: [ObjectId], + ref: 'users' } }); diff --git a/test.http b/test.http new file mode 100644
index 0000000..66dfa1e --- /dev/null +++ b/test.http
@@ -0,0 +1,59 @@ +@base + +GET /budget/:id/add HTTP/1.1 + +### +GET /admin/users HTTP/1.1 + +### +GET /admin/user/:id HTTP/1.1 + +### +DELETE /admin/user/:id HTTP/1.1 + +### +GET /alarm/:id HTTP/1.1 + +### +DELETE /alarm/:id HTTP/1.1 + +### +GET /alarms HTTP/1.1 + +### +GET /alarm/@me HTTP/1.1 + +### +PUT /alarm/@me HTTP/1.1 + +### +DELETE /alarm/@me HTTP/1.1 + +### +DELETE /auth/delete HTTP/1.1 + +### +GET /budget/:id HTTP/1.1 + +### +GET /budget/@me HTTP/1.1 + +### +GET /auth/devices HTTP/1.1 + +### +GET / HTTP/1.1 + +### +POST /auth/login HTTP/1.1 + +### +POST /auth/logout HTTP/1.1 + +### +POST /auth/register HTTP/1.1 + +### +GET /status HTTP/1.1 + +###