// routes/landlord/tenants/invite.tsx import { define } from "../../../utils.ts"; import { page } from "fresh"; import type { SessionCookie } from "../../auth/callback.ts"; import type { Property, TenantInvite } from "../../../db/interfaces.ts"; import { getPropertiesByOwner } from "../../../services/propertyService.ts"; import LandlordInviteTenant from "../../../islands/LandlordInviteTenant.tsx"; import { getTenantInviteById } from "../../../services/tenantInviteService.ts"; function parseInvitedEmails(value?: string | null) { if (!value) return []; try { const parsed = JSON.parse(value); if (Array.isArray(parsed)) { return parsed.filter((item) => typeof item === "string"); } } catch (_error) { // Fall back to comma-separated parsing. } return value.split(",").map((item) => item.trim()).filter(Boolean); } interface InviteTenantPageData { stateData: SessionCookie; properties: Property[]; invite: TenantInvite | null; error?: string; } export const handler = define.handlers({ async GET(ctx) { const stateData = ctx.state.data as SessionCookie; const { user_id } = stateData; try { const properties = await getPropertiesByOwner(user_id); const url = new URL(ctx.req.url); const inviteId = url.searchParams.get("invite_id"); const invite = inviteId ? await getTenantInviteById(inviteId) : null; if (inviteId && (!invite || invite.landlord_id !== user_id)) { return page({ stateData, properties, invite: null, error: "Invite not found. Please start a new invite.", }); } return page({ stateData, properties, invite, }); } catch (error) { console.error("Error loading invite tenant page:", error); return page({ stateData, properties: [], invite: null, error: "Unable to load your properties. Please try again.", }); } }, }); export default define.page(function InviteTenantPage({ data }) { const { stateData, properties, invite, error } = data as InviteTenantPageData; const landlordName = stateData?.user_name || "Your landlord"; const inviteEmails = parseInvitedEmails(invite?.invited_emails); return (

Invite a Tenant

Send a pre-filled invite email linked to the right rental.

{error && (
{error}
)} {properties.length === 0 ? (

Add a property first

You need at least one property to generate an invite link.

Add Property
) : invite ? ( ) : ( )}
); });