Skip to content

Commit

Permalink
Refactor getFavoriteListings to use Prisma model and add type annotat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
GithmiHashara committed Sep 22, 2024
1 parent b8ae012 commit e7df89e
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions app/actions/getFavoriteListings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -147,20 +148,20 @@ export default async function getFavoriteListings() {
return []
}

// Fetch listings that match the user's favorite IDs
const favorites: Prisma.ListingGetPayload<true>[] = await prisma.listing.findMany({
const favorites = await prisma.listing.findMany({
where: {
id: {
in: [...(currentUser.favoriteIds || [])]
}
}
})

in: [...(currentUser.favoriteIds || [])],
},
},
}) as Prisma.ListingGetPayload<true>[]; // 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) {
Expand Down

0 comments on commit e7df89e

Please sign in to comment.