Skip to content

Commit

Permalink
feat: update group info
Browse files Browse the repository at this point in the history
  • Loading branch information
GanghyeonSeo committed Aug 29, 2024
1 parent cdf5acc commit 28014a7
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/group/dto/req/updateGroup.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';

export class UpdateGroupDto {
@IsString()
@IsNotEmpty()
uuid: string;

@IsString()
@IsOptional()
name?: string;

@IsString()
@IsOptional()
description?: string;

@IsString()
@IsOptional()
notionPageId?: string;
}
16 changes: 16 additions & 0 deletions src/group/group.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { GroupListResDto, GroupResDto } from './dto/res/groupRes.dto';
import { InviteCodeResDto } from './dto/res/inviteCodeRes.dto';
import { ExpandedGroupResDto } from './dto/res/ExpandedGroupRes.dto';
import { JoinDto } from './dto/req/join.dto';
import { UpdateGroupDto } from './dto/req/updateGroup.dto';

@ApiTags('group')
@ApiOAuth2(['openid', 'email', 'profile'])
Expand Down Expand Up @@ -91,6 +92,21 @@ export class GroupController {
return this.groupService.createGroup(body, user.uuid);
}

@ApiOperation({
summary: 'Update group info',
description: '그룹의 정보를 업데이트하는 API입니다.',
})
@ApiOkResponse()
@ApiForbiddenResponse()
@ApiInternalServerErrorResponse()
@Patch()
async updateGroup(
@Body() body: UpdateGroupDto,
@GetUser() user: User,
): Promise<void> {
return this.groupService.updateGroup(body, user.uuid);
}

@ApiOperation({
summary: 'Delete a group',
description:
Expand Down
63 changes: 63 additions & 0 deletions src/group/group.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ export class GroupRepository {
});
}

async checkGroupExistenceByUuid(uuid: string): Promise<Group | null> {
this.logger.log(`checkGroupExistenceByUuid ${uuid}`);

return this.prismaService.group
.findUnique({
where: {
deletedAt: null,
uuid,
},
})
.catch((error) => {
if (error instanceof PrismaClientKnownRequestError) {
throw new InternalServerErrorException('unknown database error');
}
throw new InternalServerErrorException('unknown error');
});
}

async getGroupByName(name: string): Promise<Group | null> {
this.logger.log(`getGroupByName ${name}`);
return this.prismaService.group
Expand Down Expand Up @@ -192,6 +210,51 @@ export class GroupRepository {
});
}

async updateGroup(
{
uuid,
name,
description,
notionPageId,
}: Pick<Group, 'uuid'> &
Partial<Pick<Group, 'name' | 'description' | 'notionPageId'>>,
userUuid: string,
): Promise<void> {
this.logger.log(`updateGroup ${uuid}`);

await this.prismaService.group
.update({
where: {
uuid,
UserRole: {
some: {
userUuid,
Role: {
authorities: {
has: Authority.GROUP_UPDATE,
},
},
},
},
},
data: {
name,
description,
notionPageId,
},
})
.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 deleteGroup(uuid: string): Promise<void> {
this.logger.log(`deleteGroup: ${uuid}`);
await this.prismaService.group.update({
Expand Down
24 changes: 21 additions & 3 deletions src/group/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ForbiddenException,
Injectable,
Logger,
NotFoundException,
} from '@nestjs/common';
import { GroupRepository } from './group.repository';
import { CreateGroupDto } from './dto/req/createGroup.dto';
Expand All @@ -13,6 +14,7 @@ import * as crypto from 'crypto';
import { Authority, Group } from '@prisma/client';
import { GroupWithRole } from './types/groupWithRole';
import { ExpandedGroup } from './types/ExpandedGroup.type';
import { UpdateGroupDto } from './dto/req/updateGroup.dto';

@Injectable()
export class GroupService {
Expand Down Expand Up @@ -43,13 +45,29 @@ export class GroupService {
createGroupDto.name,
);

if (!checkGroupExistence) {
await this.groupRepository.createGroup(createGroupDto, userUuid);
} else {
if (checkGroupExistence) {
throw new ConflictException(
`Group with name ${createGroupDto.name} already exists`,
);
}

await this.groupRepository.createGroup(createGroupDto, userUuid);
}

async updateGroup(
updateGroupDto: UpdateGroupDto,
userUuid: string,
): Promise<void> {
this.logger.log(`updateGroup: ${updateGroupDto.uuid}`);

const checkGroupExistence =
await this.groupRepository.checkGroupExistenceByUuid(updateGroupDto.uuid);

if (!checkGroupExistence) {
throw new NotFoundException('Group not found');
}

await this.groupRepository.updateGroup(updateGroupDto, userUuid);
}

async deleteGroup(uuid: string, userUuid: string): Promise<void> {
Expand Down

0 comments on commit 28014a7

Please sign in to comment.