From 93848c0758576b17578b4a2062fa915016c674bb Mon Sep 17 00:00:00 2001
From: JIN <o3ouuu@inu.ac.kr>
Date: Fri, 29 Nov 2024 10:30:01 +0900
Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix:=20=EC=A4=91=EB=B3=B5?=
 =?UTF-8?q?=EB=90=9C=20=EB=A7=81=ED=81=AC=EB=A5=BC=20=EB=84=A3=EC=A7=80=20?=
 =?UTF-8?q?=EC=95=8A=EA=B2=8C=20=EC=A4=91=EB=B3=B5=EC=9D=84=20=ED=99=95?=
 =?UTF-8?q?=EC=9D=B8=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?=
 =?UTF-8?q?=EA=B0=80#181?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 BE/src/news/news.service.ts | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/BE/src/news/news.service.ts b/BE/src/news/news.service.ts
index 4d05db80..6fff8b84 100644
--- a/BE/src/news/news.service.ts
+++ b/BE/src/news/news.service.ts
@@ -49,9 +49,17 @@ export class NewsService {
   @Cron('*/1 8-16 * * 1-5')
   async cronNewsData() {
     await this.newsRepository.delete({ query: In(['증권', '주식']) });
-    await this.getNewsDataByQuery('주식');
-    await this.getNewsDataByQuery('증권');
+    const stockNews = await this.getNewsDataByQuery('주식');
+    const securityNews = await this.getNewsDataByQuery('증권');
 
+    const allNews = [...stockNews, ...securityNews];
+    const uniqueNews = allNews.filter(
+      (news, index) =>
+        allNews.findIndex((i) => i.originallink === news.originallink) ===
+        index,
+    );
+
+    await this.newsRepository.save(uniqueNews);
     await this.newsRepository.update(
       {},
       {
@@ -67,13 +75,16 @@ export class NewsService {
 
     const response =
       await this.naverApiDomainService.requestApi<NewsApiResponse>(queryParams);
-    const formattedData = this.formatNewsData(value, response.items);
-
-    return this.newsRepository.save(formattedData);
+    return this.newsRepository.save(this.formatNewsData(value, response.items));
   }
 
   private formatNewsData(query: string, items: NewsDataOutputDto[]) {
-    return items.slice(0, 10).map((item) => {
+    const uniqueItems = items.filter(
+      (item, index) =>
+        items.findIndex((i) => i.originallink === item.originallink) === index,
+    );
+
+    return uniqueItems.slice(0, 10).map((item) => {
       const result = new NewsItemDataDto();
 
       result.title = this.htmlEncode(item.title);