Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 15x 15x 15x 15x 15x 9x 9x 5x 3x 3x 2x 4x 2x 2x 2x 13x 3x 10x 9x 7x 2x 5x 5x 4x 4x 3x 3x 4x 3x 3x 1x 2x 2x 2x 2x 1x 1x 1x 4x | class UserService { constructor(userRepository, { id, name, email, password } ) { this.userRepository = userRepository this.id = id || null this.name = name this.email = email this.password = password } isSafePassword() { const STRONG_PASS_REGEX = /((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/ return STRONG_PASS_REGEX.test(this.password) } async avaibleEmail() { if(this.userRepository.data.length > 0) { const userExists = await this.userRepository.find(this.email,"email") return !userExists.email } return true } async avaibleName() { if(this.userRepository.data.length > 0) { const userExists = await this.userRepository.find(this.name,"name") return !userExists.name } return true } validateBody() { if (!this.name || !this.email || !this.password) { throw new Error("Name, Email, and Password are required"); } return true } async save() { this.validateBody() if (!this.isSafePassword()) { throw new Error("Password is not strong enough"); } const isAvaibleEmail = await this.avaibleEmail() if(!isAvaibleEmail) throw new Error(`Email "${this.email}" is not avaible`) const isAvaibleName = await this.avaibleName() if(!isAvaibleName) throw new Error(`Name "${this.name}" is not avaible`) const newUser = this.userRepository.save({ name: this.name, email: this.email, password: this.password, }); return newUser; } async update() { this.validateBody() const user = await this.userRepository.find(this.id, "id") if(!user.id) { throw new Error(`User with id "${this.id}" Not Found!!`) } const { password, ...updatedUser} = await this.userRepository.update(this.id, { name: this.name, email: user.email, password: this.password }) return updatedUser } async delete() { const userExists = await this.userRepository.find(this.id, "id") if(!userExists.id) { throw new Error(`User with id "${this.id}" Not Found!!`) } this.userRepository.delete(this.id) return } } module.exports = UserService |