From d81b3a6516827e08f2f83c3c3a8144392c072911 Mon Sep 17 00:00:00 2001 From: R0m1k3 Date: Thu, 16 Apr 2026 13:28:23 +0200 Subject: [PATCH] feat: implement podcast subscription management, library page, and associated service methods --- backend/src/controllers/podcastController.ts | 32 +++++++++++++++----- frontend/src/pages/Library.tsx | 14 +++++---- frontend/src/services/podcastService.ts | 1 + 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/backend/src/controllers/podcastController.ts b/backend/src/controllers/podcastController.ts index ea94766..1b10c78 100644 --- a/backend/src/controllers/podcastController.ts +++ b/backend/src/controllers/podcastController.ts @@ -29,7 +29,13 @@ export const getUserSubscriptions = async (req: Request, res: Response) => { .populate('podcastId') .sort({ subscribedAt: -1 }); - const podcasts = subscriptions.map((sub) => sub.podcastId); + const podcasts = subscriptions.map((sub) => { + const podcastObj = (sub.podcastId as any).toJSON(); + return { + ...podcastObj, + subscriptionId: sub._id.toString(), + }; + }); res.json({ podcasts, count: podcasts.length }); } catch (error) { @@ -107,21 +113,31 @@ export const unsubscribe = async (req: Request, res: Response) => { const { podcastId } = req.params; - // Log for debugging on the server - console.log(`[Unsubscribe] User ${req.user.id} attempting to unsubscribe from podcast ${podcastId}`); + // Log the request + console.log(`[Unsubscribe] User ${req.user.id} attempting to remove: ${podcastId}`); - const subscription = await UserSubscription.findOneAndDelete({ + // Method 1: Try deletion by Subscription ID (New robust method) + let subscription = await UserSubscription.findOneAndDelete({ + _id: new mongoose.Types.ObjectId(podcastId), userId: new mongoose.Types.ObjectId(req.user.id), - podcastId: new mongoose.Types.ObjectId(podcastId), }); + // Method 2: Fallback to Podcast ID if Method 1 found nothing (Old compatibility method) if (!subscription) { - console.warn(`[Unsubscribe] No subscription found for user ${req.user.id} and podcast ${podcastId}`); + console.log(`[Unsubscribe] Not found by Subscription ID, trying by Podcast ID: ${podcastId}`); + subscription = await UserSubscription.findOneAndDelete({ + userId: new mongoose.Types.ObjectId(req.user.id), + podcastId: new mongoose.Types.ObjectId(podcastId), + }); + } + + if (!subscription) { + console.warn(`[Unsubscribe] No subscription record found for deletion.`); return res.status(404).json({ message: 'Subscription not found' }); } - console.log(`[Unsubscribe] Successfully removed subscription for user ${req.user.id}`); - res.json({ message: 'Unsubscribed successfully' }); + console.log(`[Unsubscribe] Successfully removed subscription ${subscription._id}`); + res.json({ message: 'Unsubscribed successfully', deletedId: subscription._id }); } catch (error) { console.error('Unsubscribe error:', error); res.status(500).json({ message: 'Failed to unsubscribe' }); diff --git a/frontend/src/pages/Library.tsx b/frontend/src/pages/Library.tsx index 5c595d8..1f8c876 100644 --- a/frontend/src/pages/Library.tsx +++ b/frontend/src/pages/Library.tsx @@ -40,8 +40,10 @@ export default function Library() { ['podcasts', 'subscriptions'], (old: any) => { if (!old || !old.podcasts) return old; - // Ensure we compare strings to avoid type issues - const filtered = old.podcasts.filter((p: any) => String(p._id) !== String(podcastId)); + // Optimistically filter by subscriptionId or _id as fallback + const filtered = old.podcasts.filter((p: any) => + String(p.subscriptionId || p._id) !== String(id) + ); return { ...old, podcasts: filtered, count: filtered.length }; } ); @@ -73,9 +75,9 @@ export default function Library() { } }; - const handleUnsubscribeRequest = (podcastId: string) => { - console.log("[UI] Demande de désabonnement reçue pour l'ID:", podcastId); - setConfirmDeleteId(podcastId); + const handleUnsubscribeRequest = (id: string) => { + console.log("[UI] Demande de désabonnement reçue pour l'ID (Sub/Pod):", id); + setConfirmDeleteId(id); }; const handleConfirmUnsubscribe = () => { @@ -230,7 +232,7 @@ export default function Library() { ) : (