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, 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);
|
||||
}
|
||||
}
|
||||
|
||||
// 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 myRent = await getMyRentListings();
|
||||
const list = Array.isArray(myRent) ? myRent : (myRent ? [myRent] : []);
|
||||
const myPropIds = new Set(list.map(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;
|
||||
return Number(info.id || p.id);
|
||||
}));
|
||||
if (myPropIds.has(Number(mapped._raw?.propertyInformation?.id || mapped.id))) {
|
||||
setIsOwnProperty(true);
|
||||
}
|
||||
}
|
||||
|
||||
setIsOwnProperty(owned);
|
||||
} catch (e) {
|
||||
console.error('[OwnerCheck] error:', e);
|
||||
console.warn('[OwnerCheck] failed:', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -831,9 +798,6 @@ 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>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