From 4a3a4efe9e8c6ff7099c6f3d694d2a55d3b99f62 Mon Sep 17 00:00:00 2001 From: sumin Date: Wed, 21 May 2025 18:08:03 +0900 Subject: [PATCH] =?UTF-8?q?#5=20:recycle:=20[Refactor]=20:=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/domain/match/match.service.ts | 32 +++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/domain/match/match.service.ts b/src/domain/match/match.service.ts index 821b56f..5702d56 100644 --- a/src/domain/match/match.service.ts +++ b/src/domain/match/match.service.ts @@ -21,15 +21,21 @@ export class MatchService { async createMatchingResultsBulk( results: { feed_id: number; authorId: number; similarity_score: number }[] - ): Promise<{ feed_id: number; authorId: number; saved: boolean; message?: string }[]> { - + ): Promise<{ + feed_id: number; + authorId: number; + reporterId?: number; + similarity?: number; + saved: boolean; + message?: string; + }[]> { if (!Array.isArray(results)) { throw new BadRequestException('results 필드는 배열이어야 합니다.'); } if (results.length === 0) { throw new BadRequestException('results 배열이 비어있습니다.'); } - // 여러 결과를 병렬로 저장 + return Promise.all( results.map(async (item) => { try { @@ -41,22 +47,31 @@ export class MatchService { const feed = await this.feedRepository.findOne({ where: { id: item.feed_id }, relations: ['author'] }); if (!feed) throw new CommonException(ErrorCode.NOT_FOUND_FEED); - const user = await this.userRepository.findOne({ where: { id: item.authorId } }); - if (!user) throw new CommonException(ErrorCode.NOT_FOUND_USER); + const finder = await this.userRepository.findOne({ where: { id: item.authorId } }); + if (!finder) throw new CommonException(ErrorCode.NOT_FOUND_USER); + + // feed.author가 신고자(잃어버린 사람) + const reporter = feed.author; const result = this.matchingResultRepository.create({ feed, - user, + user: finder, similarity: percent, status, }); await this.matchingResultRepository.save(result); + // 결과 객체에 발견자, 신고자, 유사도, feed_id 포함 return { feed_id: item.feed_id, - authorId: item.authorId, + authorId: finder.id, // 발견자 + reporterId: reporter.id, // 신고자 + similarity: percent, saved: true, - message: percent >= 80 ? `잃어버린 동물을 제보해주셨어요! 유사도 ${Math.round(percent)}%!` : undefined, + message: + percent >= 80 + ? `잃어버린 동물을 제보해주셨어요! 유사도 ${Math.round(percent)}%!` + : undefined, }; } catch (e) { return { @@ -70,4 +85,5 @@ export class MatchService { ); } + }