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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 10x 10x 10x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 9x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 3x 3x 3x 2x 1x 1x 1x 3x 14x 14x | import {authenticate, AuthenticationBindings} from '@loopback/authentication'; import {inject} from '@loopback/core'; import { repository, Where } from '@loopback/repository'; import { del, get, param, patch, requestBody, response, Response, RestBindings } from '@loopback/rest'; import _ from 'lodash'; import {roles} from '../config'; import {PasswordHasherBindings, TokenServiceBindings, UserServiceBindings} from '../keys'; import {User} from '../models'; import {LeadsRepository, UserRepository} from '../repositories'; import {checkValidPassword, userFormValid} from '../services'; import {BcryptHasher} from '../services/hash.password'; import {JWTService} from '../services/jwt-service'; import {MyUserService} from '../services/user-service'; import errorMessages from '../utils/errorMessages'; import {log} from "../utils/logMethod"; import successMessages from '../utils/successMessages'; import {failureHandler, isAdmin, successHandler, validatePayload} from './components'; import {baseResponse, changePasswordReq, myProfileReq, patchUserReq} from './requestSpecs'; import {changePasswordType as changePasswordType, Credentials, currentUserType, userObjectTypes} from './types'; let {confirmPasswordMsg, emailAlreadyExist, passwordOldPasswordIsSame, payloadEmpty, permissionDenied, phoneNumberAssignedToLead, invalidEntry, userNotFound} = errorMessages @authenticate("jwt") export class UserController { constructor( @repository(UserRepository) public userRepository: UserRepository, @inject(PasswordHasherBindings.PASSWORD_HASHER) public hasher: BcryptHasher, @inject(UserServiceBindings.USER_SERVICE) public userService: MyUserService, @inject(TokenServiceBindings.TOKEN_SERVICE) public jwtService: JWTService, @inject(AuthenticationBindings.CURRENT_USER) public currentUser: currentUserType, @inject(RestBindings.Http.RESPONSE) protected res: Response, @repository(LeadsRepository) public leadsRepository: LeadsRepository, ) { } @get('/users/count') @log() async count(@param.where(User) where?: Where<User>,): Promise<Response> { try { const users = await this.userRepository.count(where) return successHandler(this.res, 200, users) } catch (error) { return this.failureHandler(this.res, 403, error) } } @get('/users') @log() async userList(): Promise<Response> { try { let methodName = "userList" Iif (this.currentUser.role.roleType === roles.admin) { const users = await this.userRepository.find({include: ['role', 'userDetails']}); return successHandler(this.res, 200, users) } else { throw {code: 403, message: permissionDenied, methodName} } } catch (error) { return this.failureHandler(this.res, 403, error) } } @get('/users/{UID}', baseResponse) @log() async getUserDetail(@param.path.string('UID') UID: string): Promise<Response> { let methodName = "getUserDetail" try { const user = await this.userRepository.findOne({where: {UID}, include: ['role']}) Iif (user) { return successHandler(this.res, 200, user) } else { throw {code: 406, message: userNotFound, methodName} } } catch (error) { return this.failureHandler(this.res, 403, error) } } @patch('/users/changePassword', baseResponse) @log() async changePassword(@requestBody(changePasswordReq) payload: changePasswordType): Promise<Response> { let methodName = 'changePassword' try { validatePayload(payload, changePasswordReq) const foundUser = await this.userRepository.findOne({where: {UID: this.currentUser.UID}}); Iif (!foundUser) { throw {code: 400, message: userNotFound, methodName} } let body: Credentials = { password: payload.password, email: foundUser.email, confirmPassword: payload.confirmPassword, oldPassword: payload.oldPassword } await this.changePasswordValidation(body) const obj = { password: await this.hasher.hashPassword(payload.password) } await this.userRepository.updateById(this.currentUser.UID, obj); return successHandler(this.res, 200, successMessages.passwordChanged) } catch (error) { return this.failureHandler(this.res, 403, error) } } @patch('/users') @response(200, patchUserReq) @log() async updateUserDetails(@requestBody(patchUserReq) user: userObjectTypes): Promise<Response> { let methodName = "updateUserDetails" try { validatePayload(user, patchUserReq) await userFormValid(user) let errorMessage = "" let UID = this.currentUser.UID Iif (user.phone === "") { let type: any = "Phone" const foundLead = await this.leadsRepository.findOne({where: {preferredContact: type, userUID: this.currentUser.UID}}); if (foundLead) { errorMessage = phoneNumberAssignedToLead } } errorMessage = await this.checkEmailExist(user) Eif (errorMessage === "") { // role Id should not update if current user is not a admin Iif (isAdmin(this.currentUser) || user.UID) { throw {code: 403, message: permissionDenied, methodName} } Iif ((user.closeAgentUID || user.leadAssigned || user.isVerified)) { throw {code: 403, message: invalidEntry, methodName} } delete user.password if (_.isEmpty(user)) { throw {code: 422, message: payloadEmpty, methodName} } else { const result = await this.userRepository.updateAll(user, {UID}); Eif (result.count) { return successHandler(this.res, 200, successMessages.userUpdatedSuccessfully) } else { throw {code: 422, message: userNotFound, methodName} } } } else { throw {code: 422, message: errorMessage, methodName} } } catch (error) { return this.failureHandler(this.res, 403, error) } } @del('/users/{UID}') @log() async deleteById(@param.path.string('UID') UID: string): Promise<Response> { try { await this.userRepository.deleteAll({UID}); return successHandler(this.res, 200, successMessages.userDeleted) } catch (error) { return this.failureHandler(this.res, 403, error) } } @get('/getIntegrationLink') @log() @response(200, baseResponse) async getIntegrationLink(): Promise<Response> { let methodName = "getIntegrationLink" try { if (this.currentUser.role.roleType === roles.registrar) { let url = process.env.APP_URL + `/registrar-message?registrarId=${this.currentUser.UID}&domainName=` return successHandler(this.res, 200, url) } else { throw {code: 403, message: errorMessages.permissionDenied, methodName} } } catch (error) { return this.failureHandler(this.res, 403, error) } } @get('/myProfile') @log() @response(200, myProfileReq) async me(): Promise<Response> { return successHandler(this.res, 200, this.currentUser) } /** * checkEmailExist */ public async checkEmailExist(user: userObjectTypes) { const foundUser = await this.userRepository.findOne({where: {or: [{email: user.email}], and: [{UID: {neq: this.currentUser.UID}}]}}); Iif (foundUser && foundUser.UID !== this.currentUser.UID && user.email && foundUser.email.toLocaleLowerCase() === user.email.toLocaleLowerCase()) { return emailAlreadyExist } return "" } public async changePasswordValidation(payload: Credentials) { try { let methodName = 'changePasswordValidation' await checkValidPassword(payload.password) if (payload.password === payload.oldPassword) { throw {code: 422, message: passwordOldPasswordIsSame, methodName} } Eif (payload.password !== payload.confirmPassword) { throw {code: 422, message: confirmPasswordMsg, methodName} } await this.userService.verifyCredentials(payload); } catch (error) { throw error } } private failureHandler(res: Response, code: number, error: any): Promise<Response> { let className = error.className || UserController.name return failureHandler(res, code, error, className) } } |