Format and test fixes

This commit is contained in:
2020-11-18 21:17:29 -06:00
parent 95d7360ee4
commit 172e208edd
8 changed files with 55 additions and 27 deletions

View File

@@ -80,6 +80,9 @@ app.use(function (err, req, res, next) {
if (res.headersSent) { if (res.headersSent) {
return next(err); return next(err);
} }
if (err.code && err.code === 500) {
console.log(err);
}
res.status(err.code || 500); res.status(err.code || 500);
res.json({ res.json({
error: err.message || `Internal Server Error - "${err.message}"`, error: err.message || `Internal Server Error - "${err.message}"`,

View File

@@ -3,8 +3,10 @@
*/ */
const jwt = require(`jsonwebtoken`); const jwt = require(`jsonwebtoken`);
const users = require(`../models/users`); const users = require(`../../models/users`);
const { comparePassword } = require(`../utils`); const { comparePassword } = require(`../../utils`);
const validation = require(`../../validation`);
const schemas = require(`./schemas`);
/** /**
* Save * Save
@@ -36,6 +38,8 @@ const register = async (req, res) => {
* @param {*} res * @param {*} res
*/ */
const login = async (req, res) => { const login = async (req, res) => {
validation.validateOrThrow(schemas.login, res.body);
let user = await users.getByEmail(req.body.email); let user = await users.getByEmail(req.body.email);
if (!user) { if (!user) {

View File

@@ -0,0 +1,19 @@
const commonSchemas = require(`../../validation/commonSchemas`);
const login = {
$schema: "http://json-schema.org/draft-07/schema#",
title: "Login User",
description: "An object to log-in a new user",
type: "object",
properties: {
email: commonSchemas.email,
password: {
type: "string",
},
},
required: ["email", "password"],
};
module.exports = {
login,
};

View File

@@ -1,12 +1,11 @@
"use strict";
/** /**
* Model: Users * Model: Users
*/ */
const utils = require(`../../utils`); const utils = require(`../../utils`);
const db = require(`../db`); const db = require(`../db`);
const schemas = require(`./schemas`); const schemas = require(`./schemas`);
const sharedSchemas = require(`../schemas`); const commonSchemas = require(`../../validation/commonSchemas`);
const validation = require(`../validation`); const validation = require(`../../validation`);
/** /**
* Create a new user * Create a new user
@@ -19,7 +18,8 @@ const create = async (user = {}) => {
const existingUser = await getByEmail(user.email); const existingUser = await getByEmail(user.email);
if (existingUser) { if (existingUser) {
throw utils.customError( throw utils.customError(
`A user with email "${user.email}" is already registered` `A user with email "${user.email}" is already registered`,
400
); );
} }
@@ -36,7 +36,7 @@ const create = async (user = {}) => {
* @param {string} email Email address of user to retrieve * @param {string} email Email address of user to retrieve
*/ */
const getByEmail = async (email) => { const getByEmail = async (email) => {
validation.validateOrThrow(sharedSchemas.email, email); validation.validateOrThrow(commonSchemas.email, email);
let user = await db.getByKey(email); let user = await db.getByKey(email);
@@ -49,7 +49,7 @@ const getByEmail = async (email) => {
* @param {string} id * @param {string} id
*/ */
const getById = async (id) => { const getById = async (id) => {
validation.validateOrThrow(sharedSchemas.id, id); validation.validateOrThrow(commonSchemas.id, id);
let user = await db.getById(`user`, id); let user = await db.getById(`user`, id);

View File

@@ -1,19 +1,21 @@
const sharedSchemas = require(`../schemas`); const commonSchemas = require(`../../validation/commonSchemas`);
const create = {
$schema: "http://json-schema.org/draft-07/schema#",
title: "Create User",
description: "An object to create a new user",
type: "object",
properties: {
email: commonSchemas.email,
password: {
type: "string",
minLength: 10,
maxLength: 50,
},
},
required: ["email", "password"],
};
module.exports = { module.exports = {
"create": { create,
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Create User",
"description": "An object to create a new user",
"type": "object",
"properties": {
"email": sharedSchemas.email,
"password": {
"type": "string",
"minLength": 10,
"maxLength": 50
}
},
"required": ["email", "password"]
}
}; };

View File

@@ -9,7 +9,7 @@ const bcrypt = require(`bcryptjs`);
* @param {string} message Error message * @param {string} message Error message
* @param {number} code Error code, should be HTTP status code * @param {number} code Error code, should be HTTP status code
*/ */
const customError = (message, code) => { const customError = (message = "An error occurred.", code = 500) => {
const error = new Error(message); const error = new Error(message);
error.code = code; error.code = code;
return error; return error;
@@ -19,7 +19,7 @@ const customError = (message, code) => {
* Build a custom validation error * Build a custom validation error
* @param {array[string]} validationErrors Validation errors * @param {array[string]} validationErrors Validation errors
*/ */
const validationError = (validationErrors) => { const validationError = (validationErrors = []) => {
let error = new Error("Validation error(s)"); let error = new Error("Validation error(s)");
error.validationErrors = validationErrors; error.validationErrors = validationErrors;
error.code = 400; error.code = 400;