diff --git a/app/property/[id]/PropertyDetail.js b/app/property/[id]/PropertyDetail.js index 4b7bc64..fad636c 100644 --- a/app/property/[id]/PropertyDetail.js +++ b/app/property/[id]/PropertyDetail.js @@ -15,7 +15,7 @@ import { TreePine, Building, GraduationCap, ExternalLink, Smile, Ban, Wine, Dog, CassetteTape, Info } from 'lucide-react'; -import { getRentProperty, getSaleProperty, getSalePropertyById, bookReservation, getAvailableDateRanges, getOwnerContactInformation, getMyRentListings, getMySaleListings, getOwnerByUserId } from '../../utils/api'; +import { getRentProperty, getSaleProperty, getSalePropertyById, bookReservation, getAvailableDateRanges, getOwnerContactInformation, getMyRentListings } from '../../utils/api'; import AuthService from '../../services/AuthService'; import { useFavorites } from '@/app/contexts/FavoritesContext'; import { BuildingTypeKeys, PropertyStatusKeys, extractCity } from '../../enums'; @@ -221,53 +221,20 @@ export default function PropertyDetailsPage() { console.warn('Failed to fetch date ranges', e); } } - // Check if current user owns this property + // Check if current user owns this property via their own listings if (AuthService.isAuthenticated() && AuthService.isOwner()) { try { - const authUser = AuthService.getUser(); - const rawInfo = (mapped._raw || {}).propertyInformation || {}; - let owned = false; - - // Check 1: Fetch owner profile to get DB id, then compare with propertyInformation.ownerId - if (authUser?.id) { - try { - const ownerProfile = await getOwnerByUserId(authUser.id); - if (ownerProfile?.id && mapped.ownerId && Number(ownerProfile.id) === Number(mapped.ownerId)) { - owned = true; - } - } catch (e) { - console.warn('[OwnerCheck] getOwnerByUserId failed:', e); - } + const myRent = await getMyRentListings(); + const list = Array.isArray(myRent) ? myRent : (myRent ? [myRent] : []); + const myPropIds = new Set(list.map(p => { + const info = p.propertyInformation || {}; + return Number(info.id || p.id); + })); + if (myPropIds.has(Number(mapped._raw?.propertyInformation?.id || mapped.id))) { + setIsOwnProperty(true); } - - // Check 2: Fallback via managed property IDs (like Flutter ManagedPropertiesService) - if (!owned) { - const [myRent, mySale] = await Promise.allSettled([ - getMyRentListings(), - getMySaleListings(), - ]); - const myPropIds = new Set(); - const collectIds = (result) => { - if (result.status !== 'fulfilled' || !result.value) return; - const list = Array.isArray(result.value) ? result.value : [result.value]; - list.forEach(p => { - const info = p.propertyInformation || {}; - if (info.id) myPropIds.add(Number(info.id)); - if (p.id) myPropIds.add(Number(p.id)); - }); - }; - collectIds(myRent); - collectIds(mySale); - const checkId = mapped.id ? Number(mapped.id) : null; - const checkInfoId = rawInfo.id ? Number(rawInfo.id) : null; - if ((checkId && myPropIds.has(checkId)) || (checkInfoId && myPropIds.has(checkInfoId))) { - owned = true; - } - } - - setIsOwnProperty(owned); } catch (e) { - console.error('[OwnerCheck] error:', e); + console.warn('[OwnerCheck] failed:', e); } } } @@ -831,9 +798,6 @@ export default function PropertyDetailsPage() { 🔍 Owner Debug

isOwnProperty: {String(isOwnProperty)}

-

prop ownerId: {String(property.ownerId ?? 'null')}

-

user id (JWT): {String(AuthService.getUser()?.id ?? 'null')}

-

user name: {String(AuthService.getUser()?.name ?? 'null')}

authenticated: {String(AuthService.isAuthenticated())} | isOwner: {String(AuthService.isOwner())}