diff --git a/src/group/group.controller.ts b/src/group/group.controller.ts index fd5f28b..dba0315 100644 --- a/src/group/group.controller.ts +++ b/src/group/group.controller.ts @@ -120,7 +120,7 @@ export class GroupController { @Param('uuid') uuid: string, @GetUser() user: User, ): Promise { - this.groupService.deleteGroup(uuid, user.uuid); + return this.groupService.deleteGroup(uuid, user.uuid); } @ApiOperation({ diff --git a/src/group/group.repository.ts b/src/group/group.repository.ts index 9a01f40..86791ff 100644 --- a/src/group/group.repository.ts +++ b/src/group/group.repository.ts @@ -255,16 +255,38 @@ export class GroupRepository { }); } - async deleteGroup(uuid: string): Promise { + async deleteGroup(uuid: string, userUuid: string): Promise { this.logger.log(`deleteGroup: ${uuid}`); - await this.prismaService.group.update({ - where: { - uuid, - }, - data: { - deletedAt: new Date(), - }, - }); + + await this.prismaService.group + .update({ + where: { + uuid, + UserRole: { + some: { + userUuid, + Role: { + authorities: { + has: Authority.GROUP_DELETE, + }, + }, + }, + }, + }, + data: { + deletedAt: new Date(), + }, + }) + .catch((error) => { + if (error instanceof PrismaClientKnownRequestError) { + if (error.code === 'P2025') { + throw new ForbiddenException(); + } + this.logger.log(error); + throw new InternalServerErrorException('unknown database error'); + } + throw new InternalServerErrorException('unknown error'); + }); } async addUserToGroup(uuid: string, userUuid: string): Promise { diff --git a/src/group/group.service.ts b/src/group/group.service.ts index 7f8120d..d07381e 100644 --- a/src/group/group.service.ts +++ b/src/group/group.service.ts @@ -72,18 +72,15 @@ export class GroupService { async deleteGroup(uuid: string, userUuid: string): Promise { this.logger.log(`deleteGroup: ${uuid}`); - if ( - !(await this.groupRepository.validateAuthority( - uuid, - [Authority.GROUP_DELETE], - userUuid, - )) - ) { - throw new ForbiddenException( - 'You do not have permission to delete group', - ); + + const checkGroupExistence = + await this.groupRepository.checkGroupExistenceByUuid(uuid); + + if (!checkGroupExistence) { + throw new NotFoundException('Group not found'); } - await this.groupRepository.deleteGroup(uuid); + + await this.groupRepository.deleteGroup(uuid, userUuid); } /**