summary refs log tree commit diff
path: root/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/db')
-rw-r--r--src/db/dbAccess/user.js13
-rw-r--r--src/db/schemas/spendHistory.js29
-rw-r--r--src/db/schemas/user.js29
3 files changed, 70 insertions, 1 deletions
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 } });