From 6075a0ed1ca1a7f8665c3d1e31620770919dafe2 Mon Sep 17 00:00:00 2001 From: Alma Date: Mon, 14 Apr 2025 00:36:39 +0200 Subject: [PATCH] correction news widget v2 --- .env | 4 ++- app/api/news/route.ts | 30 +++++++++++++++++++ components/news.tsx | 67 ++++++++++++++++++++++++++++++------------- prisma/schema.prisma | 25 ++++++++++++++++ 4 files changed, 105 insertions(+), 21 deletions(-) create mode 100644 app/api/news/route.ts diff --git a/.env b/.env index c58a6e2b..89dca287 100644 --- a/.env +++ b/.env @@ -45,4 +45,6 @@ NEXT_PUBLIC_IFRAME_AI_ASSISTANT_URL=https://alma.slm-lab.net ROCKET_CHAT_TOKEN=w91TYgkH-Z67Oz72usYdkW5TZLLRwnre7qyAhp7aHJB ROCKET_CHAT_USER_ID=Tpuww59PJKsrGNQJB LEANTIME_TOKEN=lt_lsdShQdoYHaPUWuL07XZR1Rf3GeySsIs_UDlll3VJPk5EwAuILpMC4BwzJ9MZFRrb -LEANTIME_API_URL=https://agilite.slm-lab.net \ No newline at end of file +LEANTIME_API_URL=https://agilite.slm-lab.net + +NEWSDB_URL=postgresql://alma:Sict33711###@cube.governance-labs.com/rivacube \ No newline at end of file diff --git a/app/api/news/route.ts b/app/api/news/route.ts new file mode 100644 index 00000000..f4f4ef5c --- /dev/null +++ b/app/api/news/route.ts @@ -0,0 +1,30 @@ +import { NextResponse } from 'next/server'; +import { prisma } from '@/lib/prisma'; + +export async function GET() { + try { + const news = await prisma.news.findMany({ + orderBy: { + date: 'desc' + }, + take: 5, + select: { + id: true, + title: true, + date: true, + source: true, + description: true, + category: true, + url: true + } + }); + + return NextResponse.json(news); + } catch (error) { + console.error('Error fetching news:', error); + return NextResponse.json( + { error: 'Failed to fetch news' }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/components/news.tsx b/components/news.tsx index e51ef7b5..cbb7d8ea 100644 --- a/components/news.tsx +++ b/components/news.tsx @@ -5,12 +5,17 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { RefreshCw } from "lucide-react"; import { useSession } from "next-auth/react"; +import { formatDistance } from 'date-fns'; +import { fr } from 'date-fns/locale'; interface NewsItem { - id: string; + id: number; title: string; date: string; - category: string; + source: string; + description: string | null; + category: string | null; + url: string; } export function News() { @@ -22,19 +27,16 @@ export function News() { const fetchNews = async (isRefresh = false) => { if (isRefresh) setRefreshing(true); - setLoading(true); - - // Placeholder data - replace with actual API call - const mockNews = [ - { id: '1', title: 'New Project Management Features Released', date: '2024-03-20', category: 'Product Update' }, - { id: '2', title: 'Team Meeting Schedule Changes', date: '2024-03-19', category: 'Announcement' }, - { id: '3', title: 'Upcoming Training Sessions', date: '2024-03-18', category: 'Training' }, - ]; + if (!isRefresh) setLoading(true); try { - // Simulate API call - await new Promise(resolve => setTimeout(resolve, 1000)); - setNews(mockNews); + const response = await fetch('/api/news'); + if (!response.ok) { + throw new Error('Failed to fetch news'); + } + + const data = await response.json(); + setNews(data); setError(null); } catch (err) { setError('Failed to fetch news'); @@ -51,6 +53,19 @@ export function News() { } }, [status]); + const formatDate = (dateString: string) => { + try { + const date = new Date(dateString); + return formatDistance(date, new Date(), { + addSuffix: true, + locale: fr + }); + } catch (err) { + console.error('Error formatting date:', err); + return dateString; + } + }; + if (status === 'loading' || loading) { return ( @@ -58,7 +73,9 @@ export function News() { News -

Loading...

+
+ +
); @@ -89,15 +106,25 @@ export function News() { news.map((item) => (
window.open(item.url, '_blank')} >
- {item.date} - - {item.category} - + {formatDate(item.date)} + {item.category && ( + + {item.category} + + )}
-

{item.title}

+

+ {item.title} +

+ {item.description && ( +

+ {item.description} +

+ )}
)) )} diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7c2e3447..8af7fe4b 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -11,6 +11,11 @@ datasource db { url = env("DATABASE_URL") } +datasource newsdb { + provider = "postgresql" + url = env("NEWSDB_URL") +} + model Calendar { id String @id @default(uuid()) name String @@ -40,4 +45,24 @@ model Event { @@index([calendarId]) @@index([userId]) +} + +model News { + id Int @id @default(autoincrement()) + title String + url String @unique + date DateTime + source String + content String? + sentiment_score Float? + sentiment String? + symbols String[] + symbol String? + processed_at DateTime @default(now()) + description String? + category String? @db.VarChar(50) + + @@index([category]) + @@index([date]) + @@index([symbol]) } \ No newline at end of file