// routes/tenant/property/index.tsx import { define } from "../../../utils.ts"; import { page } from "fresh"; import { getLeasesByTenant } from "../../../services/leaseService.ts"; import { getPropertyById } from "../../../services/propertyService.ts"; import { getMediaByProperty } from "../../../services/propertyMediaService.ts"; import { getDocumentsByLease } from "../../../services/leaseDocumentService.ts"; import { Lease, LeaseDocument, Property, PropertyMedia, } from "../../../db/interfaces.ts"; import { AlertMessage } from "../maintenance/new/components/AlertMessage.tsx"; interface PropertyPageData { lease?: Lease; property?: Property; propertyPhotos?: PropertyMedia[]; leaseDocuments?: LeaseDocument[]; error?: string; } export const handler = define.handlers({ async GET(ctx) { const { user_id } = ctx.state.data; try { const leases = await getLeasesByTenant(user_id); const currentLease = leases?.[0]; // Assuming the first lease is the current active one if (!currentLease || !currentLease.property_id) { return page({ error: "No active lease found for a property.", }); } const property = await getPropertyById(currentLease.property_id); const propertyPhotos = await getMediaByProperty(currentLease.property_id); const leaseDocuments = await getDocumentsByLease(currentLease.id); return page({ lease: currentLease, property, propertyPhotos: propertyPhotos.filter((m: PropertyMedia) => m.media_type === "photo" ), leaseDocuments, }); } catch (error) { console.error("Error loading property details:", error); return page({ error: "Failed to load property details.", }); } }, }); function DetailItem( { label, value }: { label: string; value: string | number | null | undefined; }, ) { return (
{label}
{value !== undefined ? value : "N/A"}
); } function formatPropertyAddress(property: Property) { const street = property.address_1?.trim() || ""; const cityState = [property.city, property.state].filter(Boolean).join(", "); const zip = property.zip?.trim() || ""; const region = [cityState, zip].filter(Boolean).join(" ").trim(); return [street, region].filter(Boolean).join(", ").trim(); } function parseList(raw: string | null | undefined): string[] { if (!raw) return []; try { const parsed = JSON.parse(raw); if (Array.isArray(parsed)) { return parsed.map((item) => String(item)).filter(Boolean); } } catch { // Fall through to comma-separated parsing. } return raw.split(",").map((item) => item.trim()).filter(Boolean); } function DocumentList({ documents }: { documents: LeaseDocument[] }) { return (
    {documents.length > 0 ? ( documents.map((doc) => (
  • {doc.file_name || "Document"} ({doc.document_type})
    {doc.file_url ? ( View ) : Unavailable}
  • )) ) : (
  • No documents found.
  • )}
); } export default define.page( function TenantPropertyPage({ data }: { data: PropertyPageData }) { const { lease, property, propertyPhotos, leaseDocuments, error } = data as PropertyPageData; if (error) { return ( ); } if (!property) { return (
Property details could not be loaded.
); } const addressLine = formatPropertyAddress(property); const amenities = parseList(property.amenities); const rules = parseList(lease?.late_fee_rules ?? ""); const photoItems = (propertyPhotos ?? []).filter( (photo): photo is PropertyMedia & { file_url: string } => Boolean(photo.file_url), ); return (
{/* Header */}

Your Current Residence

{addressLine || "Address not available"}

{/* Main Content */}
{/* Property Photos */} {photoItems.length > 0 && (

Property Photos

{photoItems.map((photo) => ( {photo.file_name ))}
)} {/* Property Details */}

Property Details

{/* Amenities */}

Amenities

{amenities.length > 0 ? (
    {amenities.map((amenity: string) => (
  • {amenity}
  • ))}
) :

No amenities listed.

}
{/* Rules */}

Community Rules

{rules.length > 0 ? (
    {rules.map((rule: string) =>
  • {rule}
  • )}
) : (

No specific rules listed.

)}
{/* Lease Documents */}

Lease Documents

Your current lease agreement and related documents.

); }, );