summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-06-01 11:13:55 +0200
committerRory& <root@rory.gay>2025-06-01 11:13:55 +0200
commit4e12e02bc805170e6b03d33e0ef894b2a3021fb3 (patch)
treea525a35cfcc28f80bbe33d152fe483d14d8b23d1 /src
parentUpdate test client (diff)
downloadnodejs-final-assignment-4e12e02bc805170e6b03d33e0ef894b2a3021fb3.tar.xz
Add alarm endpoints, basic budget routes, spend history
Diffstat (limited to 'src')
-rw-r--r--src/api/middlewares/authMiddleware.js8
-rw-r--r--src/api/routes/alarmRoutes.js41
-rw-r--r--src/api/routes/budgetRoutes.js20
-rw-r--r--src/api/routes/index.js2
-rw-r--r--src/db/dbAccess/user.js13
-rw-r--r--src/db/schemas/spendHistory.js29
-rw-r--r--src/db/schemas/user.js29
7 files changed, 140 insertions, 2 deletions
diff --git a/src/api/middlewares/authMiddleware.js b/src/api/middlewares/authMiddleware.js

index 1187112..8553517 100644 --- a/src/api/middlewares/authMiddleware.js +++ b/src/api/middlewares/authMiddleware.js
@@ -1,5 +1,5 @@ import { validateJwtToken } from '#util/jwtUtils.js'; -import { DbUser } from '#db/schemas/index.js'; +import { DbUser, UserType } from '#db/schemas/index.js'; /** * @param options {AuthValidationOptions} @@ -15,6 +15,12 @@ export function validateAuth(options) { const user = (req.user = await DbUser.findById(auth.id).exec()); + // admin can do everything + if (user.type == UserType.ADMIN) { + next(); + return; + } + if (options.roles && !options.roles.includes(user.type)) { res.status(401).send('Unauthorized'); return; diff --git a/src/api/routes/alarmRoutes.js b/src/api/routes/alarmRoutes.js new file mode 100644
index 0000000..5170327 --- /dev/null +++ b/src/api/routes/alarmRoutes.js
@@ -0,0 +1,41 @@ +import { validateAuth } from '#api/middlewares/index.js'; +import { UserType } from '#db/schemas/index.js'; + +export const alarmByUserRoute = { + route: '/alarm/:id', + onGetValidation: validateAuth({ roles: [UserType.MONITOR] }), + async onGet(req, res) { + const user = await getUserById(req.query.id); + res.send(user.alarm); + }, + + onDeleteValidation: validateAuth({ roles: [UserType.MONITOR] }), + async onDelete(req, res) { + const user = await getUserById(req.query.id); + user.alarm = null; + await user.save(); + res.status(204).send(); + } +}; + +export const alarmRoute = { + onGetValidation: validateAuth({ roles: [UserType.USER] }), + async onGet(req, res) { + res.send(req.user.alarm); + }, + + route: '/alarm/@me', + onPutValidation: validateAuth({ roles: [UserType.USER] }), + async onPut(req, res) { + req.user.alarm = req.body; + await req.user.save(); + res.status(204).send(); + }, + + onDeleteValidation: validateAuth({ roles: [UserType.USER] }), + async onDelete(req, res) { + req.user.alarm = null; + await req.user.save(); + res.status(204).send(); + } +}; diff --git a/src/api/routes/budgetRoutes.js b/src/api/routes/budgetRoutes.js new file mode 100644
index 0000000..ed827e8 --- /dev/null +++ b/src/api/routes/budgetRoutes.js
@@ -0,0 +1,20 @@ +import { validateAuth } from '#api/middlewares/index.js'; +import { UserType } from '#db/schemas/index.js'; + +export const getBudgetByUserRoute = { + route: '/budget/:id', + onGetValidation: validateAuth({ roles: [UserType.MONITOR] }), + onGet(req, res) {} +}; + +export const addBudgetByUserRoute = { + route: '/budget/:id/add', + onGetValidation: validateAuth({ roles: [UserType.MONITOR] }), + onGet(req, res) {} +}; + +export const getBudgetRoute = { + route: '/budget/@me', + onGetValidation: validateAuth({ roles: [UserType.USER] }), + onGet(req, res) {} +}; diff --git a/src/api/routes/index.js b/src/api/routes/index.js
index 745dd27..4feeb11 100644 --- a/src/api/routes/index.js +++ b/src/api/routes/index.js
@@ -2,3 +2,5 @@ export * from './statusRoute.js'; export * from './indexRoute.js'; export * from './auth/index.js'; +export * from './budgetRoutes.js'; +export * from './alarmRoutes.js'; diff --git a/src/db/dbAccess/user.js b/src/db/dbAccess/user.js
index fad5ba3..4ab70fd 100644 --- a/src/db/dbAccess/user.js +++ b/src/db/dbAccess/user.js
@@ -7,6 +7,19 @@ import { generateJwtToken } from '#util/jwtUtils.js'; async function whoAmI(token) {} +async function getUserById(id) { + const user = await DbUser.findById(id); + if (!user) { + throw new SafeNSoundError({ + errCode: 'ENTITY_NOT_FOUND', + message: 'No such user!' + }); + } + + console.log(user); + return user; +} + async function getUserByAuth(data) { if (!(data instanceof AuthDto)) throw new Error('Invalid data type. Expected AuthDto.'); diff --git a/src/db/schemas/spendHistory.js b/src/db/schemas/spendHistory.js new file mode 100644
index 0000000..b12bcc3 --- /dev/null +++ b/src/db/schemas/spendHistory.js
@@ -0,0 +1,29 @@ +import { model, Schema } from 'mongoose'; +import { hash, compare } from 'bcrypt'; +import {ref} from "joi"; + +/** + * User schema for MongoDB. + * @type {module:mongoose.Schema} + */ +export const spendHistorySchema = new Schema({ + spentBy: { + type: ObjectId, + ref: "users" + } + createdAt: { + type: Date, + default: Date.now, + immutable: true + } +}); + +export const UserType = Object.freeze({ + USER: 'user', + MONITOR: 'monitor', + ADMIN: 'admin' +}); + +export const DbUser = model('user', userSchema); + +console.log('[MONGODB] User schema initialized successfully!'); diff --git a/src/db/schemas/user.js b/src/db/schemas/user.js
index f490966..063fddf 100644 --- a/src/db/schemas/user.js +++ b/src/db/schemas/user.js
@@ -1,6 +1,17 @@ import { model, Schema } from 'mongoose'; import { hash, compare } from 'bcrypt'; +export const UserType = Object.freeze({ + USER: 'user', + MONITOR: 'monitor', + ADMIN: 'admin' +}); + +export const AlarmType = Object.freeze({ + FALL: 'fall', + TOILET: 'toilet' +}); + export const deviceSchema = new Schema({ name: { type: String, @@ -19,6 +30,19 @@ export const deviceSchema = new Schema({ } }); +export const alarmSchema = new Schema({ + createdAt: { + type: Date, + default: Date.now, + immutable: true + }, + reason: { + type: String, + enum: Object.values(AlarmType), + required: true + } +}); + /** * User schema for MongoDB. * @type {module:mongoose.Schema} @@ -42,7 +66,7 @@ export const userSchema = new Schema({ }, type: { type: String, - enum: ['user', 'monitor', 'admin'], + enum: Object.values(UserType), default: 'user' }, createdAt: { @@ -53,6 +77,9 @@ export const userSchema = new Schema({ devices: { type: [deviceSchema], default: [] + }, + alarm: { + type: alarmSchema } });