Merge pull request #33 from veyselboybay/master
update on joi schema, added logger docs, and updated user model
This commit is contained in:
5 files changed
+34
-11
No files matched your search
@@ -1,6 +1,7 @@
|
||||
const express = require("express");
|
||||
const UserModel = require("../models/user");
|
||||
const { authValidation } = require("../validation/joi");
|
||||
const { registerValidation } = require("../validation/joi");
|
||||
const logger = require('../utils/logger')
|
||||
|
||||
/**
|
||||
* @function
|
||||
@@ -11,7 +12,7 @@ const { authValidation } = require("../validation/joi");
|
||||
const registerController = async (req, res) => {
|
||||
// joi validation
|
||||
try {
|
||||
await authValidation.validateAsync(req.body);
|
||||
await registerValidation.validateAsync(req.body);
|
||||
} catch (error) {
|
||||
return res
|
||||
.status(400)
|
||||
@@ -21,21 +22,27 @@ const registerController = async (req, res) => {
|
||||
// Check if the user exists
|
||||
try {
|
||||
const isUser = await req.db.getUserByEmail(req, res);
|
||||
if (isUser)
|
||||
if (isUser) {
|
||||
logger.warning("User already exists!", { "service": "auth", "userId": isUser._id });
|
||||
return res
|
||||
.status(400)
|
||||
.json({ success: false, msg: "User already exists!" });
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(error.message, { "service": "auth" });
|
||||
return res.status(500).json({ success: false, msg: error.message });
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
// Create a new user
|
||||
const newUser = await req.db.insertUser(req, res);
|
||||
return res.json({ success: true, msg: "User created}", data: newUser });
|
||||
// Send an email to user
|
||||
// TODO: Send an email to user
|
||||
// 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) {
|
||||
logger.error(error.message, { "service": "auth" });
|
||||
return res.status(500).json({ success: false, msg: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2,6 +2,7 @@ const {
|
||||
getMonitorsByIdValidation,
|
||||
getMonitorsByUserIdValidation,
|
||||
} = require("../validation/joi");
|
||||
const logger = require('../utils/logger')
|
||||
|
||||
// Gets all monitors
|
||||
const getAllMonitors = async (req, res) => {
|
||||
@@ -9,6 +10,7 @@ const getAllMonitors = async (req, res) => {
|
||||
const monitors = await req.db.getAllMonitors();
|
||||
return res.json({ success: true, msg: "Monitors found", data: monitors });
|
||||
} catch (error) {
|
||||
logger.error(error.message, { "service": "monitor" });
|
||||
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);
|
||||
return res.json({ success: true, msg: "Monitor found", data: monitor });
|
||||
} catch (error) {
|
||||
logger.error(error.message, { "service": "monitor" });
|
||||
return res.status(500).json({ success: false, msg: error.message });
|
||||
}
|
||||
};
|
||||
@@ -43,12 +46,14 @@ const getMonitorsByUserId = async (req, res) => {
|
||||
try {
|
||||
const userId = req.params.userId;
|
||||
const monitors = await req.db.getMonitorsByUserId(userId);
|
||||
logger.info(`Monitors for user ${userId} found`, { "service": "monitor", "userId":userId });
|
||||
return res.json({
|
||||
success: true,
|
||||
msg: `Monitors for user ${userId} found`,
|
||||
data: monitors,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error.message, { "service": "monitor" });
|
||||
return res.status(500).json({ success: false, msg: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -14,7 +14,7 @@ const connect = async () => {
|
||||
|
||||
const insertUser = async (req, res) => {
|
||||
try {
|
||||
const newUser = await UserModel.create({ ...req.body });
|
||||
const newUser = await UserModel.create({ ...req.body }).select('-password');
|
||||
return newUser;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
@@ -24,7 +24,7 @@ const insertUser = async (req, res) => {
|
||||
const getUserByEmail = async (req, res) => {
|
||||
try {
|
||||
// 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;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
|
||||
@@ -2,8 +2,13 @@ const mongoose = require("mongoose");
|
||||
const bcrypt = require("bcrypt");
|
||||
|
||||
const UserSchema = mongoose.Schema({
|
||||
username: {
|
||||
firstname: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
lastname: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
email: {
|
||||
type: String,
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
const joi = require("joi");
|
||||
const user = require("../models/user");
|
||||
|
||||
const authValidation = joi.object({
|
||||
username: joi.string(),
|
||||
const loginValidation = joi.object({
|
||||
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(),
|
||||
password: joi.string().min(8).required(),
|
||||
});
|
||||
@@ -16,7 +21,8 @@ const getMonitorsByUserIdValidation = joi.object({
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
authValidation,
|
||||
loginValidation,
|
||||
registerValidation,
|
||||
getMonitorsByIdValidation,
|
||||
getMonitorsByUserIdValidation,
|
||||
};
|
||||
Reference in new issue
Block a user