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 810febf commit fcba0bb
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions app/actions/getFavoriteListings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -79,19 +113,18 @@ 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 || [])]
}
}
})

// 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
Expand Down

0 comments on commit fcba0bb

Please sign in to comment.