From e7df89e40061fafc7dfe4a5b2a00ab230557ef4a Mon Sep 17 00:00:00 2001 From: Githmi Hashara Date: Sun, 22 Sep 2024 18:18:32 +0530 Subject: [PATCH] Refactor getFavoriteListings to use Prisma model and add type annotations --- app/actions/getFavoriteListings.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/app/actions/getFavoriteListings.ts b/app/actions/getFavoriteListings.ts index 8a08ff7..866fa9c 100644 --- a/app/actions/getFavoriteListings.ts +++ b/app/actions/getFavoriteListings.ts @@ -138,6 +138,7 @@ import prisma from '@/app/libs/prismadb' import getCurrentUser from './getCurrentUser' import { Prisma } from '@/prisma/generated/client' +import { SafeListing } from '../types' export default async function getFavoriteListings() { try { @@ -147,20 +148,20 @@ export default async function getFavoriteListings() { return [] } - // Fetch listings that match the user's favorite IDs - const favorites: Prisma.ListingGetPayload[] = await prisma.listing.findMany({ + const favorites = await prisma.listing.findMany({ where: { id: { - in: [...(currentUser.favoriteIds || [])] - } - } - }) - + in: [...(currentUser.favoriteIds || [])], + }, + }, + }) as Prisma.ListingGetPayload[]; // Explicit typecasting here + // Ensure the createdAt field is serialized to an ISO string for safe handling - const safeFavorites = favorites.map((favorite) => ({ + const safeFavorites: SafeListing[] = favorites.map((favorite) => ({ ...favorite, - createdAt: favorite.createdAt.toISOString() // Ensure safe serialization - })) + createdAt: favorite.createdAt.toISOString(), + })); + return safeFavorites } catch (error: any) {