diff --git a/app/property/[id]/PropertyDetail.js b/app/property/[id]/PropertyDetail.js index 3736955..4b7bc64 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 } from '../../utils/api'; +import { getRentProperty, getSaleProperty, getSalePropertyById, bookReservation, getAvailableDateRanges, getOwnerContactInformation, getMyRentListings, getMySaleListings, getOwnerByUserId } from '../../utils/api'; import AuthService from '../../services/AuthService'; import { useFavorites } from '@/app/contexts/FavoritesContext'; import { BuildingTypeKeys, PropertyStatusKeys, extractCity } from '../../enums'; @@ -221,19 +221,27 @@ export default function PropertyDetailsPage() { console.warn('Failed to fetch date ranges', e); } } - // Check if current user owns this property (Flutter approach) - if (AuthService.isAuthenticated()) { - const currentUserId = AuthService.getUserId(); - const rawInfo = (mapped._raw || {}).propertyInformation || {}; - console.log('[OwnerCheck] currentUserId:', currentUserId); - console.log('[OwnerCheck] mapped.ownerId:', mapped.ownerId, 'rawInfo.ownerId:', rawInfo.ownerId); - // Check 1: Direct ownerId match (like Flutter: property.ownerId == currentUserId) - if (mapped.ownerId != null && currentUserId != null && Number(mapped.ownerId) === Number(currentUserId)) { - setIsOwnProperty(true); - } - // Check 2: ManagedPropertiesService fallback (owner only) - else if (AuthService.isOwner()) { - try { + // Check if current user owns this property + 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); + } + } + + // Check 2: Fallback via managed property IDs (like Flutter ManagedPropertiesService) + if (!owned) { const [myRent, mySale] = await Promise.allSettled([ getMyRentListings(), getMySaleListings(), @@ -253,9 +261,13 @@ export default function PropertyDetailsPage() { 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))) { - setIsOwnProperty(true); + owned = true; } - } catch (e) { console.error('[OwnerCheck] fallback error:', e); } + } + + setIsOwnProperty(owned); + } catch (e) { + console.error('[OwnerCheck] error:', e); } } } @@ -819,8 +831,10 @@ export default function PropertyDetailsPage() { 🔍 Owner Debug

isOwnProperty: {String(isOwnProperty)}

-

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

-

rawInfo keys: {Object.keys((property._raw||{}).propertyInformation||{}).join(', ') || 'none'}

+

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())}

)}