Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] 그룹 생성 시 GroupAccessCode 생성 및 DB 저장 #526

Merged
merged 4 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions app/backend/src/groups/groups.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, UseGuards } from '@nestjs/common';
import { ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBody, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { GroupsService } from './groups.service';
import { GetUser } from 'libs/decorators/get-user.decorator';
import { AtGuard } from 'src/auth/guards/at.guard';
Expand All @@ -26,6 +26,19 @@ export class GroupsController {
return this.groupsService.getAllGroups();
}

@Get('/info')
@ApiOperation({
summary: '승인코드를 사용하여 그룹 정보 추출',
description: '승인 코드를 사용하여 특정 그룹 정보를 추출합니다.',
})
@ApiQuery({ name: 'access-code', description: '참가할 그룹의 승인 코드' })
@ApiResponse({ status: 201, description: 'Successfully retrieved group information' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 404, description: 'Group not found for the provided access code' })
async getGroupByAccessCode(@Query('access_code') accessCode: string): Promise<Group & { membersCount: number }> {
return this.groupsService.getGroupByAccessCode(accessCode);
}

@Get('/:id')
@ApiOperation({
summary: '특정 그룹 정보 조회',
Expand Down Expand Up @@ -58,7 +71,10 @@ export class GroupsController {
@ApiBody({ type: CreateGroupsDto })
@ApiResponse({ status: 201, description: 'Successfully created', type: CreateGroupsDto })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async createGroups(@Body() createGroupsDto: CreateGroupsDto, @GetUser() member: Member): Promise<Group> {
async createGroups(
@Body() createGroupsDto: CreateGroupsDto,
@GetUser() member: Member,
): Promise<{ group: Group; accessCode: string }> {
return this.groupsService.createGroups(createGroupsDto, member);
}

Expand All @@ -84,7 +100,6 @@ export class GroupsController {
@ApiParam({ name: 'id', description: '참가를 취소할 그룹의 Id' })
@ApiResponse({ status: 200, description: 'Successfully leaved join' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 403, description: 'Forbidden' })
@ApiResponse({ status: 404, description: 'Group with id not found' })
async leaveGroup(@Param('id', ParseIntPipe) id: number, @GetUser() member: Member): Promise<void> {
return this.groupsService.leaveGroup(id, member);
Expand Down
60 changes: 44 additions & 16 deletions app/backend/src/groups/groups.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PrismaService } from 'prisma/prisma.service';
import { Group, Member } from '@prisma/client';
import { MemberInformationDto } from 'src/member/dto/member.dto';
import { CreateGroupsDto } from './dto/create-groups.dto';
import { v4 as uuidv4 } from 'uuid';

@Injectable()
export class GroupsRepository {
Expand All @@ -27,6 +28,27 @@ export class GroupsRepository {
return Promise.all(groupPromises);
}

async getGroupByAccessCode(accessCode: string): Promise<Group & { membersCount: number }> {
const groupAccessCode = await this.prisma.groupAccessCode.findUnique({
where: {
accessCode: accessCode,
},
include: {
groupAccessCodes: true,
},
});

if (!groupAccessCode) {
throw new NotFoundException('Group not found for the provided access code');
}

const membersCount = await this.getGroupMembersCount(Number(groupAccessCode.groupId));
return {
...groupAccessCode.groupAccessCodes,
membersCount,
};
}

async getGroups(id: number): Promise<Group & { membersCount: number }> {
const group = await this.prisma.group.findUnique({
where: {
Expand Down Expand Up @@ -60,24 +82,30 @@ export class GroupsRepository {
}));
}

async createGroups(createGroupsDto: CreateGroupsDto, member: Member): Promise<Group> {
try {
const { title, groupTypeId } = createGroupsDto;

const group = await this.prisma.group.create({
data: {
title: title,
groupTypeId: groupTypeId,
member: {
connect: { id: Number(member.id) },
},
async createGroups(createGroupsDto: CreateGroupsDto, member: Member): Promise<{ group: Group; accessCode: string }> {
const { title, groupTypeId } = createGroupsDto;

const group = await this.prisma.group.create({
data: {
title: title,
groupTypeId: groupTypeId,
member: {
connect: { id: Number(member.id) },
},
});
},
});

return group;
} catch (error) {
throw new Error(`Failed to create group: ${error.message}`);
}
const accessCode = uuidv4();
await this.prisma.groupAccessCode.create({
data: {
accessCode,
groupId: group.id,
},
});

await this.joinGroup(Number(group.id), member);

return { group, accessCode };
}

async joinGroup(id: number, member: Member): Promise<void> {
Expand Down
6 changes: 5 additions & 1 deletion app/backend/src/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export class GroupsService {
return this.groupsRepository.getAllGroups();
}

async getGroupByAccessCode(accessCode: string): Promise<Group & { membersCount: number }> {
return this.groupsRepository.getGroupByAccessCode(accessCode);
}

async getGroups(id: number): Promise<Group & { membersCount: number }> {
return this.groupsRepository.getGroups(id);
}
Expand All @@ -20,7 +24,7 @@ export class GroupsService {
return this.groupsRepository.getAllMembersOfGroup(id);
}

async createGroups(createGroupsDto: CreateGroupsDto, member: Member): Promise<Group> {
async createGroups(createGroupsDto: CreateGroupsDto, member: Member): Promise<{ group: Group; accessCode: string }> {
return this.groupsRepository.createGroups(createGroupsDto, member);
}

Expand Down