From fcba0bbf3eb1df551deb83387a846828fd0f993c Mon Sep 17 00:00:00 2001 From: Githmi Hashara Date: Wed, 18 Sep 2024 14:55:58 +0530 Subject: [PATCH] Refactor getFavoriteListings to use Prisma model and add type annotations --- app/actions/getFavoriteListings.ts | 45 ++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/app/actions/getFavoriteListings.ts b/app/actions/getFavoriteListings.ts index 6513eb6..3f55039 100644 --- a/app/actions/getFavoriteListings.ts +++ b/app/actions/getFavoriteListings.ts @@ -66,10 +66,44 @@ // app/actions/getFavoriteListings.ts +// import prisma from '@/app/libs/prismadb' +// import getCurrentUser from './getCurrentUser' +// // import { Prisma } from '@prisma/client' +// import { Prisma } from '@/prisma/generated/client' + +// export default async function getFavoriteListings() { +// try { +// const currentUser = await getCurrentUser() + +// if (!currentUser) { +// return [] +// } + +// // Fetch listings that match the user's favorite IDs +// const favorites: Prisma.ListingGetPayload<{}>[] = await prisma.listing.findMany({ +// where: { +// id: { +// in: [...(currentUser.favoriteIds || [])] +// } +// } +// }) + +// // Ensure the createdAt field is serialized to an ISO string for safe handling +// const safeFavorites = favorites.map((favorite) => ({ +// ...favorite, +// createdAt: favorite.createdAt.toISOString() // Ensure safe serialization +// })) + +// return safeFavorites +// } catch (error: any) { +// throw new Error(error) +// } +// } + + import prisma from '@/app/libs/prismadb' import getCurrentUser from './getCurrentUser' -// import { Prisma } from '@prisma/client' -import { Prisma } from '@/prisma/generated/client' +import { Listing } from '@prisma/client' // Adjust the import based on your actual model export default async function getFavoriteListings() { try { @@ -79,8 +113,8 @@ export default async function getFavoriteListings() { return [] } - // Fetch listings that match the user's favorite IDs - const favorites: Prisma.ListingGetPayload<{}>[] = await prisma.listing.findMany({ + // Typecasting favorites to the Listing type + const favorites: Listing[] = await prisma.listing.findMany({ where: { id: { in: [...(currentUser.favoriteIds || [])] @@ -88,10 +122,9 @@ export default async function getFavoriteListings() { } }) - // Ensure the createdAt field is serialized to an ISO string for safe handling const safeFavorites = favorites.map((favorite) => ({ ...favorite, - createdAt: favorite.createdAt.toISOString() // Ensure safe serialization + createdAt: favorite.createdAt.toISOString() })) return safeFavorites