// routes/landlord/applications/index.tsx import { define } from "../../../utils.ts"; import { getTenantApplicationsByLandlord, TenantApplication, updateTenantApplicationStatus, } from "../../../services/tenantApplicationService.ts"; import { page } from "fresh"; import { SessionCookie } from "../../auth/callback.ts"; import { createPublicId } from "../../../helpers.ts"; // Extended interface for tenant applications with additional UI properties interface ExtendedTenantApplication extends TenantApplication { status?: string; // UI status field (maps to application_status) applicant_name?: string; applicant_email?: string; created_at?: number; // UI field (maps to submitted_on) property_address?: string; monthly_income?: number; application_notes?: string; [key: string]: unknown; // Allow additional dynamic properties } // Flexible interface for component data interface LandlordApplicationsData { stateData?: SessionCookie; applications?: ExtendedTenantApplication[]; error?: string; publicId?: string; [key: string]: unknown; // Allow additional properties with unknown type } interface LandlordApplicationsProps { data: LandlordApplicationsData; } export const handler = define.handlers({ async GET(ctx) { const stateData: SessionCookie = ctx.state.data; const { user_id } = stateData; try { const applications = await getTenantApplicationsByLandlord(user_id); const publicId = await createPublicId(user_id); return page({ stateData, applications, error: "", publicId, }); } catch (error) { console.error("Error loading tenant applications:", error); const publicId = await createPublicId(user_id); return page({ stateData, error: "Failed to load applications", publicId, }); } }, async POST(ctx) { const stateData: SessionCookie = ctx.state.data; try { const formData = await ctx.req.formData(); const application_id = formData.get("application_id") as string; const action = formData.get("action") as string; if (action === "approve" || action === "reject") { await updateTenantApplicationStatus( application_id, action === "approve" ? "approved" : "rejected", ); } return new Response("", { status: 302, headers: { Location: "/landlord/applications" }, }); } catch (error) { console.error("Error updating application:", error); return page({ stateData, error: "Failed to update application", }); } }, }); export default define.page( function LandlordApplications({ data }: LandlordApplicationsProps) { const { stateData: _stateData, applications, error } = data; return (
Review and manage tenant applications for your properties
{application.applicant_name}
{application.status}{application.applicant_email}
Applied {application.created_at ? new Date(application.created_at) .toLocaleDateString() : "Unknown date"}
Property: {application.property_address || "Property Address"}
Monthly Income: ${application.monthly_income ?.toLocaleString() || "N/A"}
Notes:{" "} {application.application_notes}