fix(users): Using the users entity in the use cases

This commit is contained in:
Daniel Gonzalez
2021-07-31 15:00:38 -04:00
parent d57085949c
commit c5a1eb6f81
2 changed files with 5 additions and 12 deletions
+4 -11
View File
@@ -3,7 +3,7 @@
functions are called by the /user REST API endpoints. functions are called by the /user REST API endpoints.
*/ */
// const UserModel = require('../adapters/localdb/models/users') const UserEntity = require('../entities/user')
const { wlogger } = require('../adapters/wlogger') const { wlogger } = require('../adapters/wlogger')
class UserLib { class UserLib {
@@ -17,6 +17,7 @@ class UserLib {
} }
// Encapsulate dependencies // Encapsulate dependencies
this.UserEntity = new UserEntity()
this.UserModel = this.adapters.localdb.Users this.UserModel = this.adapters.localdb.Users
} }
@@ -24,17 +25,9 @@ class UserLib {
async createUser (userObj) { async createUser (userObj) {
try { try {
// Input Validation // Input Validation
if (!userObj.email || typeof userObj.email !== 'string') {
throw new Error("Property 'email' must be a string!")
}
if (!userObj.password || typeof userObj.password !== 'string') {
throw new Error("Property 'password' must be a string!")
}
if (!userObj.name || typeof userObj.name !== 'string') {
throw new Error("Property 'name' must be a string!")
}
const user = new this.UserModel(userObj) const userEntity = this.UserEntity.validate(userObj)
const user = new this.UserModel(userEntity)
// Enforce default value of 'user' // Enforce default value of 'user'
user.type = 'user' user.type = 'user'
+1 -1
View File
@@ -57,7 +57,7 @@ describe('#users-use-case', () => {
} catch (err) { } catch (err) {
// console.log(err) // console.log(err)
// assert.equal(err.status, 422) // assert.equal(err.status, 422)
assert.include(err.message, 'Cannot read property') assert.include(err.message, "Property 'email' must be a string!")
} }
}) })