Skip to content

Commit

Permalink
fix: delete group
Browse files Browse the repository at this point in the history
  • Loading branch information
GanghyeonSeo committed Aug 29, 2024
1 parent 28014a7 commit 21a7150
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/group/group.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class GroupController {
@Param('uuid') uuid: string,
@GetUser() user: User,
): Promise<void> {
this.groupService.deleteGroup(uuid, user.uuid);
return this.groupService.deleteGroup(uuid, user.uuid);
}

@ApiOperation({
Expand Down
40 changes: 31 additions & 9 deletions src/group/group.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,38 @@ export class GroupRepository {
});
}

async deleteGroup(uuid: string): Promise<void> {
async deleteGroup(uuid: string, userUuid: string): Promise<void> {
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<void> {
Expand Down
19 changes: 8 additions & 11 deletions src/group/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,15 @@ export class GroupService {

async deleteGroup(uuid: string, userUuid: string): Promise<void> {
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);
}

/**
Expand Down

0 comments on commit 21a7150

Please sign in to comment.