Skip to content

Commit

Permalink
feat: Add apis for participating meets
Browse files Browse the repository at this point in the history
  • Loading branch information
lsjbh45 committed Nov 26, 2023
1 parent 6229d96 commit f6cdf41
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/apis/fcm/fcm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ export class FcmService {
});
}

async sendMessageNewParticipant(
token: string,
meetId: number,
): Promise<FcmResultDto> {
return this.sendFcm({
token,
title: '참여한 모임에 새로운 참가자가 있습니다.',
body: '지금 LinKU에서 새로운 참가자를 확인해 보세요!',
type: 'newParticipant',
data: {
meetId,
},
});
}

async sendMessageNewReply(
token: string,
meetId: number,
Expand Down
50 changes: 50 additions & 0 deletions src/apis/meet/meet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,56 @@ export class MeetController {
);
}

@Post('/:id')
@UseGuards(JwtAuthGuard, RoleGuard(Role.USER))
@ApiOperation({ summary: '개설된 모임에 참여' })
@ApiBearerAuth()
async joinMeet(
@AuthUser() user: AuthUserDto,
@Param() params: MeetDetailRequestDto,
): Promise<void> {
const { id: meetId } = params;
const { id: userId } = user;

const { gender: userGender, age: userAge } =
await this.userService.getUserById(userId);

const meet = await this.meetService.getMeetById(meetId);
if (!meet) {
throw new BadRequestException('존재하지 않는 모임입니다.');
}

const { participations, maxParticipants, gender, minAge, maxAge } = meet;

const targets = [meet.host, ...participations.map((part) => part.user)];
if (targets.map((target) => target.id).includes(userId)) {
throw new BadRequestException('이미 모임에 참여한 사용자입니다.');
}

if (participations.length + 1 >= maxParticipants) {
throw new BadRequestException('모임의 최대 인원을 초과했습니다.');
}

if (gender !== null && gender !== userGender) {
throw new BadRequestException('모임의 성별 조건을 만족하지 않습니다.');
}

if (
(minAge !== null && minAge > userAge) ||
(maxAge !== null && maxAge < userAge)
) {
throw new BadRequestException('모임의 나이 조건을 만족하지 않습니다.');
}

await this.meetService.addParticipation({ meetId, userId });

await Promise.all(
targets.map((target) =>
this.fcmService.sendMessageNewParticipant(target.fcmToken, meetId),
),
);
}

@Post('/:id/comment')
@UseGuards(JwtAuthGuard, RoleGuard(Role.USER))
@ApiOperation({ summary: '모임에 대한 댓글을 작성' })
Expand Down
7 changes: 5 additions & 2 deletions src/apis/meet/meet.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { FcmModule } from '../fcm/fcm.module';
import { MeetController } from './meet.controller';
import { MeetService } from './meet.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Meet, Comment, User } from 'src/entities';
import { Meet, Comment, User, Participation } from 'src/entities';
import { UserService } from '../user/user.service';

@Module({
imports: [FcmModule, TypeOrmModule.forFeature([Meet, Comment, User])],
imports: [
FcmModule,
TypeOrmModule.forFeature([Meet, Comment, User, Participation]),
],
controllers: [MeetController],
providers: [MeetService, UserService],
})
Expand Down
8 changes: 7 additions & 1 deletion src/apis/meet/meet.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Meet, Comment } from 'src/entities';
import { Meet, Comment, Participation } from 'src/entities';
import { FindOptionsWhere, Repository } from 'typeorm';
import { FindOptionsPage } from 'src/lib/utils/pagination.util';

Expand All @@ -9,6 +9,8 @@ export class MeetService {
constructor(
@InjectRepository(Meet)
private readonly meetRepository: Repository<Meet>,
@InjectRepository(Participation)
private readonly participationRepository: Repository<Participation>,
@InjectRepository(Comment)
private readonly commentRepository: Repository<Comment>,
) {}
Expand Down Expand Up @@ -46,4 +48,8 @@ export class MeetService {
async addComment(comment: Partial<Comment>): Promise<void> {
await this.commentRepository.create(comment).save();
}

async addParticipation(participation: Partial<Participation>): Promise<void> {
await this.participationRepository.create(participation).save();
}
}

0 comments on commit f6cdf41

Please sign in to comment.