Skip to content

Commit

Permalink
refactor: add user specific db operations in the repository
Browse files Browse the repository at this point in the history
  • Loading branch information
nunocaseiro committed Dec 5, 2022
1 parent 98bffe5 commit 2b7c19b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { BaseInterfaceRepository } from 'src/libs/repositories/interfaces/base.repository.interface';
import User from '../entities/user.schema';

export type UserRepositoryInterface = BaseInterfaceRepository<User>;
export interface UserRepositoryInterface extends BaseInterfaceRepository<User> {
getById(userId: string): Promise<User>;
updateUserWithRefreshToken(refreshToken: string, userId: string): Promise<User>;
updateUserPassword(email: string, password: string): Promise<User>;
}
13 changes: 13 additions & 0 deletions backend/src/modules/users/repository/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ export class UserRepository
currentHashedRefreshToken: 0
});
}

updateUserWithRefreshToken(refreshToken: string, userId: string) {
return this.findOneByFieldAndUpdate({ _id: userId }, { $set: { refreshToken } });
}

updateUserPassword(email: string, password: string) {
return this.findOneByFieldAndUpdate(
{ email },
{
$set: { password }
}
);
}
}
5 changes: 1 addition & 4 deletions backend/src/modules/users/services/get.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ export default class GetUserServiceImpl implements GetUserService {
}

getById(_id: string) {
return this.userRepository.get(_id, {
password: 0,
currentHashedRefreshToken: 0
});
return this.userRepository.getById(_id);
}

async getUserIfRefreshTokenMatches(refreshToken: string, userId: string) {
Expand Down
12 changes: 2 additions & 10 deletions backend/src/modules/users/services/update.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,15 @@ export default class updateUserServiceImpl implements UpdateUserService {
async setCurrentRefreshToken(refreshToken: string, userId: string) {
const currentHashedRefreshToken = await encrypt(refreshToken);

return this.userRepository.findOneByFieldAndUpdate(
{ _id: userId },
{ $set: { currentHashedRefreshToken } }
);
return this.userRepository.updateUserWithRefreshToken(currentHashedRefreshToken, userId);
}

async setPassword(userEmail: string, newPassword: string, newPasswordConf: string) {
const password = await encrypt(newPassword);

if (newPassword !== newPasswordConf)
throw new HttpException('PASSWORDS_DO_NOT_MATCH', HttpStatus.BAD_REQUEST);
const user = await this.userRepository.findOneByFieldAndUpdate(
{ email: userEmail },
{
$set: { password }
}
);
const user = this.userRepository.updateUserPassword(userEmail, password);

if (!user) throw new HttpException('USER_NOT_FOUND', HttpStatus.NOT_FOUND);

Expand Down

0 comments on commit 2b7c19b

Please sign in to comment.