diff --git a/apps/backend/apps/client/src/notice/notice.controller.ts b/apps/backend/apps/client/src/notice/notice.controller.ts index 969038ca5..0dec24412 100644 --- a/apps/backend/apps/client/src/notice/notice.controller.ts +++ b/apps/backend/apps/client/src/notice/notice.controller.ts @@ -3,8 +3,6 @@ import { Get, Query, Param, - InternalServerErrorException, - Logger, DefaultValuePipe, ParseBoolPipe } from '@nestjs/common' @@ -15,8 +13,6 @@ import { NoticeService } from './notice.service' @Controller('notice') @AuthNotNeededIfOpenSpace() export class NoticeController { - private readonly logger = new Logger(NoticeController.name) - constructor(private readonly noticeService: NoticeService) {} @Get() @@ -28,18 +24,13 @@ export class NoticeController { @Query('fixed', new DefaultValuePipe(false), ParseBoolPipe) fixed: boolean, @Query('search') search?: string ) { - try { - return await this.noticeService.getNotices({ - cursor, - take, - fixed, - search, - groupId - }) - } catch (error) { - this.logger.error(error) - throw new InternalServerErrorException() - } + return await this.noticeService.getNotices({ + cursor, + take, + fixed, + search, + groupId + }) } @Get(':id') diff --git a/apps/backend/apps/client/src/notice/notice.service.ts b/apps/backend/apps/client/src/notice/notice.service.ts index 4acb4b86b..09080f076 100644 --- a/apps/backend/apps/client/src/notice/notice.service.ts +++ b/apps/backend/apps/client/src/notice/notice.service.ts @@ -20,7 +20,6 @@ export class NoticeService { groupId?: number }) { const paginator = this.prisma.getPaginator(cursor) - const notices = await this.prisma.notice.findMany({ ...paginator, where: { @@ -53,7 +52,6 @@ export class NoticeService { createdBy: notice.createdBy?.username } }) - const total = await this.prisma.notice.count({ where: { groupId, @@ -70,28 +68,26 @@ export class NoticeService { } async getNoticeByID(id: number, groupId = OPEN_SPACE_ID) { - const current = await this.prisma.notice - .findUniqueOrThrow({ - where: { - id, - groupId, - isVisible: true - }, - select: { - title: true, - content: true, - createTime: true, - updateTime: true, - createdBy: { - select: { - username: true - } + const notice = await this.prisma.notice.findUniqueOrThrow({ + where: { + id, + groupId, + isVisible: true + }, + select: { + title: true, + content: true, + createTime: true, + updateTime: true, + createdBy: { + select: { + username: true } } - }) - .then((notice) => { - return { ...notice, createdBy: notice.createdBy?.username } - }) + } + }) + + const current = { ...notice, createdBy: notice.createdBy?.username } const navigate = (pos: 'prev' | 'next') => { type Order = 'asc' | 'desc' diff --git a/apps/backend/apps/client/src/user/user.controller.ts b/apps/backend/apps/client/src/user/user.controller.ts index 68f75ab13..39df92950 100644 --- a/apps/backend/apps/client/src/user/user.controller.ts +++ b/apps/backend/apps/client/src/user/user.controller.ts @@ -6,7 +6,6 @@ import { Req, Res, Controller, - Logger, Delete, Query } from '@nestjs/common' @@ -25,8 +24,6 @@ import { UserService } from './user.service' @Controller('user') export class UserController { - private readonly logger = new Logger(UserController.name) - constructor(private readonly userService: UserService) {} @Patch('password-reset') @@ -95,8 +92,6 @@ export class UserController { @Controller('email-auth') @AuthNotNeededIfOpenSpace() export class EmailAuthenticationController { - private readonly logger = new Logger(EmailAuthenticationController.name) - constructor(private readonly userService: UserService) {} setJwtInHeader(res: Response, jwt: string) { diff --git a/apps/backend/apps/client/src/user/user.service.spec.ts b/apps/backend/apps/client/src/user/user.service.spec.ts index 681b12e16..80cb5b652 100644 --- a/apps/backend/apps/client/src/user/user.service.spec.ts +++ b/apps/backend/apps/client/src/user/user.service.spec.ts @@ -141,7 +141,7 @@ describe('UserService', () => { describe('getUsernameByEmail', () => { const { email, username } = user it('return username', async () => { - db.user.findUnique.resolves({ username }) + db.user.findUniqueOrThrow.resolves({ username }) expect(await service.getUsernameByEmail({ email })).to.be.deep.equal({ username @@ -149,7 +149,7 @@ describe('UserService', () => { }) it('should not return username', async () => { - db.user.findUnique.resolves(null) + db.user.findUniqueOrThrow.throws(new EntityNotExistException('user')) await expect(service.getUsernameByEmail({ email })).to.be.rejectedWith( EntityNotExistException @@ -400,6 +400,13 @@ describe('UserService', () => { createUserProfileSpy = spy(service, 'createUserProfile') registerUserToPublicGroupSpy = spy(service, 'registerUserToPublicGroup') deletePinFromCacheSpy = stub(service, 'deletePinFromCache') + + db.user.findUniqueOrThrow.resolves({ username: signUpDto.username }) + }) + + after(() => { + db.user.findUnique = stub() + db.user.findUniqueOrThrow = stub() }) it('carry sign up process', async () => { diff --git a/apps/backend/apps/client/src/user/user.service.ts b/apps/backend/apps/client/src/user/user.service.ts index 3e1485b82..b83349a22 100644 --- a/apps/backend/apps/client/src/user/user.service.ts +++ b/apps/backend/apps/client/src/user/user.service.ts @@ -55,7 +55,7 @@ export class UserService { ) {} async getUsernameByEmail({ email }: UserEmailDto) { - const username = await this.prisma.user.findUnique({ + const username = await this.prisma.user.findUniqueOrThrow({ where: { email }, @@ -63,9 +63,6 @@ export class UserService { username: true } }) - if (!username) { - throw new EntityNotExistException('User') - } this.logger.debug(username, 'getUsernameByEmail') return username @@ -180,26 +177,17 @@ export class UserService { email: string, newPassword: string ): Promise { - try { - const user = await this.prisma.user.update({ - where: { - email - }, - data: { - password: await hash(newPassword, ARGON2_HASH_OPTION) - } - }) - this.logger.debug(user, 'updateUserPasswordInPrisma') + const user = await this.prisma.user.update({ + where: { + email + }, + data: { + password: await hash(newPassword, ARGON2_HASH_OPTION) + } + }) + this.logger.debug(user, 'updateUserPasswordInPrisma') - return user - } catch (error) { - if ( - error instanceof PrismaClientKnownRequestError && - error.code == 'P2025' - ) - throw new EntityNotExistException('User') - throw error - } + return user } async verifyPinAndIssueJwt({ @@ -583,6 +571,7 @@ export class UserService { if (!this.isValidPassword(updateUserDto.newPassword)) { throw new UnprocessableDataException('Bad new password') } + encryptedNewPassword = await hash( updateUserDto.newPassword, ARGON2_HASH_OPTION @@ -598,7 +587,7 @@ export class UserService { } } - const updatedUser = await this.prisma.user.update({ + return await this.prisma.user.update({ where: { id: req.user.id }, data: updateData, select: { @@ -612,6 +601,5 @@ export class UserService { } } }) - return updatedUser } }