129 lines
4.2 KiB
TypeScript
129 lines
4.2 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import mongoose from 'mongoose';
|
|
import { MatchService } from '../../match/match.service';
|
|
import { Position, PositionPreference } from '../../global/database/schema';
|
|
import { DakaoSegment } from '../dakao.types';
|
|
import { DakaoUserError, DakaoUserService } from '../dakao-user.service';
|
|
|
|
interface JoinHandlerParams {
|
|
communityId: mongoose.Types.ObjectId;
|
|
matchId: string;
|
|
senderUserId: string; // kakaoTalkId
|
|
senderNickname: string; // kakaoTalkNickname
|
|
gameName: string;
|
|
tagLine: string;
|
|
positions: Position[];
|
|
}
|
|
|
|
@Injectable()
|
|
export class MatchJoinHandler {
|
|
constructor(
|
|
private readonly matchService: MatchService,
|
|
private readonly dakaoUserService: DakaoUserService,
|
|
) {}
|
|
|
|
public async handle(params: JoinHandlerParams): Promise<DakaoSegment[]> {
|
|
if (!mongoose.isValidObjectId(params.matchId)) {
|
|
return this.errorReply(params, '유효하지 않은 내전 ID 입니다.');
|
|
}
|
|
const matchObjectId = new mongoose.Types.ObjectId(params.matchId);
|
|
|
|
let resolved;
|
|
try {
|
|
resolved = await this.dakaoUserService.resolveRiotAccount({
|
|
communityId: params.communityId,
|
|
kakaoTalkId: params.senderUserId,
|
|
kakaoTalkNickname: params.senderNickname,
|
|
gameName: params.gameName,
|
|
tagLine: params.tagLine,
|
|
});
|
|
} catch (err) {
|
|
if (err instanceof DakaoUserError) {
|
|
return this.errorReply(params, this.translateUserError(err));
|
|
}
|
|
throw err;
|
|
}
|
|
|
|
const preferredPositions: PositionPreference[] = params.positions.map(
|
|
(position, index) => ({ position, priority: index + 1 }),
|
|
);
|
|
|
|
try {
|
|
const match = await this.matchService.join({
|
|
matchId: matchObjectId,
|
|
userId: resolved.user._id,
|
|
preferredPositions,
|
|
riotPuuid: resolved.riotAccount.puuid,
|
|
});
|
|
|
|
return [
|
|
{
|
|
kind: 'mention',
|
|
userId: params.senderUserId,
|
|
displayText: params.senderNickname,
|
|
},
|
|
{
|
|
kind: 'text',
|
|
text:
|
|
` 신청 완료 (${match.participants.length}/${match.maxParticipants})` +
|
|
`\n내전: ${match.title}` +
|
|
`\n계정: ${resolved.riotAccount.gameName}#${resolved.riotAccount.tagLine}` +
|
|
`\n포지션: ${params.positions.join(', ')}`,
|
|
},
|
|
];
|
|
} catch (err) {
|
|
if (err instanceof NotFoundException) {
|
|
return this.errorReply(params, '내전을 찾을 수 없습니다.');
|
|
}
|
|
if (err instanceof BadRequestException) {
|
|
return this.errorReply(params, this.translateBadRequest(err.message));
|
|
}
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
private errorReply(
|
|
params: JoinHandlerParams,
|
|
message: string,
|
|
): DakaoSegment[] {
|
|
return [
|
|
{
|
|
kind: 'mention',
|
|
userId: params.senderUserId,
|
|
displayText: params.senderNickname,
|
|
},
|
|
{ kind: 'text', text: ` ${message}` },
|
|
];
|
|
}
|
|
|
|
private translateUserError(err: DakaoUserError): string {
|
|
switch (err.code) {
|
|
case 'invalid_project_id':
|
|
return '커뮤니티 식별자가 올바르지 않습니다.';
|
|
case 'community_not_found':
|
|
return '연결된 커뮤니티를 찾을 수 없습니다.';
|
|
case 'riot_account_not_found':
|
|
return `라이엇 계정을 찾을 수 없습니다 (${err.detail ?? ''}).`;
|
|
case 'riot_account_already_registered':
|
|
return `이미 등록된 라이엇 계정이 있습니다 (${err.detail ?? ''}).`;
|
|
case 'riot_account_conflict':
|
|
return `이미 다른 사용자가 등록한 라이엇 계정입니다 (${err.detail ?? ''}).`;
|
|
case 'riot_account_lookup_failed':
|
|
return '라이엇 계정 조회 중 오류가 발생했습니다. 잠시 후 다시 시도해주세요.';
|
|
}
|
|
}
|
|
|
|
private translateBadRequest(raw: string): string {
|
|
if (raw.includes('different communities'))
|
|
return '이 내전은 현재 채팅방의 커뮤니티 소속이 아닙니다.';
|
|
if (raw.includes('not recruiting')) return '모집 중인 내전이 아닙니다.';
|
|
if (raw.includes('full')) return '정원이 가득 찼습니다.';
|
|
if (raw.includes('already joined')) return '이미 신청한 내전입니다.';
|
|
return raw;
|
|
}
|
|
}
|