Merge pull request #33 from veyselboybay/master

update on joi schema, added logger docs, and updated user model
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-05-14 21:44:55 -07:00
commit 4f3bbb2f88
5 files changed
+34 -11

No files matched your search

+12 -5
View File
@@ -1,6 +1,7 @@
const express = require("express"); const express = require("express");
const UserModel = require("../models/user"); const UserModel = require("../models/user");
const { authValidation } = require("../validation/joi"); const { registerValidation } = require("../validation/joi");
const logger = require('../utils/logger')
/** /**
* @function * @function
@@ -11,7 +12,7 @@ const { authValidation } = require("../validation/joi");
const registerController = async (req, res) => { const registerController = async (req, res) => {
// joi validation // joi validation
try { try {
await authValidation.validateAsync(req.body); await registerValidation.validateAsync(req.body);
} catch (error) { } catch (error) {
return res return res
.status(400) .status(400)
@@ -21,21 +22,27 @@ const registerController = async (req, res) => {
// Check if the user exists // Check if the user exists
try { try {
const isUser = await req.db.getUserByEmail(req, res); const isUser = await req.db.getUserByEmail(req, res);
if (isUser) if (isUser) {
logger.warning("User already exists!", { "service": "auth", "userId": isUser._id });
return res return res
.status(400) .status(400)
.json({ success: false, msg: "User already exists!" }); .json({ success: false, msg: "User already exists!" });
}
} catch (error) { } catch (error) {
logger.error(error.message, { "service": "auth" });
return res.status(500).json({ success: false, msg: error.message }); return res.status(500).json({ success: false, msg: error.message });
} }
try { try {
// Create a new user // Create a new user
const newUser = await req.db.insertUser(req, res); const newUser = await req.db.insertUser(req, res);
return res.json({ success: true, msg: "User created}", data: newUser }); // TODO: Send an email to user
// Send an email to user
// Will add this later // Will add this later
logger.info("New user created!", { "service": "auth", "userId": newUser._id });
return res.json({ success: true, msg: "User created}", data: newUser });
} catch (error) { } catch (error) {
logger.error(error.message, { "service": "auth" });
return res.status(500).json({ success: false, msg: error.message }); return res.status(500).json({ success: false, msg: error.message });
} }
}; };
+5
View File
@@ -2,6 +2,7 @@ const {
getMonitorsByIdValidation, getMonitorsByIdValidation,
getMonitorsByUserIdValidation, getMonitorsByUserIdValidation,
} = require("../validation/joi"); } = require("../validation/joi");
const logger = require('../utils/logger')
// Gets all monitors // Gets all monitors
const getAllMonitors = async (req, res) => { const getAllMonitors = async (req, res) => {
@@ -9,6 +10,7 @@ const getAllMonitors = async (req, res) => {
const monitors = await req.db.getAllMonitors(); const monitors = await req.db.getAllMonitors();
return res.json({ success: true, msg: "Monitors found", data: monitors }); return res.json({ success: true, msg: "Monitors found", data: monitors });
} catch (error) { } catch (error) {
logger.error(error.message, { "service": "monitor" });
return res.status(500).json({ success: false, msg: error.message }); return res.status(500).json({ success: false, msg: error.message });
} }
}; };
@@ -27,6 +29,7 @@ const getMonitorById = async (req, res) => {
const monitor = await req.db.getMonitorById(monitorId); const monitor = await req.db.getMonitorById(monitorId);
return res.json({ success: true, msg: "Monitor found", data: monitor }); return res.json({ success: true, msg: "Monitor found", data: monitor });
} catch (error) { } catch (error) {
logger.error(error.message, { "service": "monitor" });
return res.status(500).json({ success: false, msg: error.message }); return res.status(500).json({ success: false, msg: error.message });
} }
}; };
@@ -43,12 +46,14 @@ const getMonitorsByUserId = async (req, res) => {
try { try {
const userId = req.params.userId; const userId = req.params.userId;
const monitors = await req.db.getMonitorsByUserId(userId); const monitors = await req.db.getMonitorsByUserId(userId);
logger.info(`Monitors for user ${userId} found`, { "service": "monitor", "userId":userId });
return res.json({ return res.json({
success: true, success: true,
msg: `Monitors for user ${userId} found`, msg: `Monitors for user ${userId} found`,
data: monitors, data: monitors,
}); });
} catch (error) { } catch (error) {
logger.error(error.message, { "service": "monitor" });
return res.status(500).json({ success: false, msg: error.message }); return res.status(500).json({ success: false, msg: error.message });
} }
}; };
+2 -2
View File
@@ -14,7 +14,7 @@ const connect = async () => {
const insertUser = async (req, res) => { const insertUser = async (req, res) => {
try { try {
const newUser = await UserModel.create({ ...req.body }); const newUser = await UserModel.create({ ...req.body }).select('-password');
return newUser; return newUser;
} catch (error) { } catch (error) {
throw error; throw error;
@@ -24,7 +24,7 @@ const insertUser = async (req, res) => {
const getUserByEmail = async (req, res) => { const getUserByEmail = async (req, res) => {
try { try {
// Returns null if no user is found // Returns null if no user is found
const user = await UserModel.findOne({ email: req.body.email }); const user = await UserModel.findOne({ email: req.body.email }).select('-password');
return user; return user;
} catch (error) { } catch (error) {
throw error; throw error;
+6 -1
View File
@@ -2,8 +2,13 @@ const mongoose = require("mongoose");
const bcrypt = require("bcrypt"); const bcrypt = require("bcrypt");
const UserSchema = mongoose.Schema({ const UserSchema = mongoose.Schema({
username: { firstname: {
type: String, type: String,
required: true,
},
lastname: {
type: String,
required: true,
}, },
email: { email: {
type: String, type: String,
+9 -3
View File
@@ -1,8 +1,13 @@
const joi = require("joi"); const joi = require("joi");
const user = require("../models/user"); const user = require("../models/user");
const authValidation = joi.object({ const loginValidation = joi.object({
username: joi.string(), email: joi.string().email().required(),
password: joi.string().min(8).required(),
});
const registerValidation = joi.object({
firstname: joi.string().required(),
lastname: joi.string().required(),
email: joi.string().email().required(), email: joi.string().email().required(),
password: joi.string().min(8).required(), password: joi.string().min(8).required(),
}); });
@@ -16,7 +21,8 @@ const getMonitorsByUserIdValidation = joi.object({
}); });
module.exports = { module.exports = {
authValidation, loginValidation,
registerValidation,
getMonitorsByIdValidation, getMonitorsByIdValidation,
getMonitorsByUserIdValidation, getMonitorsByUserIdValidation,
}; };