This commit is contained in:
@ -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()) {
|
||||
// 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() {
|
||||
<summary className="cursor-pointer font-bold">🔍 Owner Debug</summary>
|
||||
<div className="mt-1 space-y-0.5">
|
||||
<p>isOwnProperty: <b>{String(isOwnProperty)}</b></p>
|
||||
<p>ownerId: <b>{String(property.ownerId ?? 'null')}</b></p>
|
||||
<p>rawInfo keys: <b>{Object.keys((property._raw||{}).propertyInformation||{}).join(', ') || 'none'}</b></p>
|
||||
<p>prop ownerId: <b>{String(property.ownerId ?? 'null')}</b></p>
|
||||
<p>user id (JWT): <b>{String(AuthService.getUser()?.id ?? 'null')}</b></p>
|
||||
<p>user name: <b>{String(AuthService.getUser()?.name ?? 'null')}</b></p>
|
||||
<p>authenticated: <b>{String(AuthService.isAuthenticated())}</b> | isOwner: <b>{String(AuthService.isOwner())}</b></p>
|
||||
</div>
|
||||
</details>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user