diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 1ec9de04c..7a10b8fac 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -31,21 +31,42 @@ export class UsersService { */ async createUser(userName: string, displayName: string): Promise { const user = User.create(userName, displayName); - return this.userRepository.save(user); + try { + return await this.userRepository.save(user); + } catch { + this.logger.debug( + `A user with the username '${userName}' already exists.`, + 'createUser', + ); + throw new AlreadyInDBError( + `A user with the username '${userName}' already exists.`, + ); + } } /** * @async * Delete the user with the specified userName - * @param userName - the username of the user to be delete + * @param {User} user - the username of the user to be delete * @throws {NotInDBError} the userName has no user associated with it. */ - async deleteUser(userName: string): Promise { - // TODO: Handle owned notes and edits - const user = await this.userRepository.findOne({ - where: { userName: userName }, - }); - await this.userRepository.delete(user); + async deleteUser(user: User): Promise { + await this.userRepository.remove(user); + this.logger.debug( + `Successfully deleted user with username ${user.userName}`, + 'deleteUser', + ); + } + + /** + * @async + * Change the displayName of the specified user + * @param {User} user - the user to be changed + * @param displayName - the new displayName + */ + async changeDisplayName(user: User, displayName: string): Promise { + user.displayName = displayName; + await this.userRepository.save(user); } /** @@ -56,9 +77,13 @@ export class UsersService { * @return {User} the specified user */ async getUserByUsername(userName: string, withTokens = false): Promise { + const relations: string[] = []; + if (withTokens) { + relations.push('authTokens'); + } const user = await this.userRepository.findOne({ where: { userName: userName }, - relations: withTokens ? ['authTokens'] : null, + relations: relations, }); if (user === undefined) { throw new NotInDBError(`User with username '${userName}' not found`);