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 18, 2024
1 parent fcba0bb commit d645bcd
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions app/actions/getFavoriteListings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,43 @@
// }


// import prisma from '@/app/libs/prismadb'
// import getCurrentUser from './getCurrentUser'
// import { Listing } from '@prisma/client' // Adjust the import based on your actual model

// export default async function getFavoriteListings() {
// try {
// const currentUser = await getCurrentUser()

// if (!currentUser) {
// return []
// }

// // Typecasting favorites to the Listing type
// const favorites: Listing[] = await prisma.listing.findMany({
// where: {
// id: {
// in: [...(currentUser.favoriteIds || [])]
// }
// }
// })

// const safeFavorites = favorites.map((favorite) => ({
// ...favorite,
// createdAt: favorite.createdAt.toISOString()
// }))

// return safeFavorites
// } catch (error: any) {
// throw new Error(error)
// }
// }



import prisma from '@/app/libs/prismadb'
import getCurrentUser from './getCurrentUser'
import { Listing } from '@prisma/client' // Adjust the import based on your actual model
import { Prisma } from '@/prisma/generated/client'

export default async function getFavoriteListings() {
try {
Expand All @@ -113,18 +147,19 @@ export default async function getFavoriteListings() {
return []
}

// Typecasting favorites to the Listing type
const favorites: Listing[] = await prisma.listing.findMany({
// Fetch listings that match the user's favorite IDs
const favorites: Prisma.ListingGetPayload<true>[] = 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()
createdAt: favorite.createdAt.toISOString() // Ensure safe serialization
}))

return safeFavorites
Expand Down

0 comments on commit d645bcd

Please sign in to comment.