// routes/api/retrieve/maintenance_requests.ts import { define } from "../../../utils.ts"; import { getMaintenanceRequestsByLandlord, getMaintenanceRequestsByUser, getOpenRequests, getRequestById, getRequestsByPriority, getRequestsByProperty, getRequestsByStatus, getRequestsByTenant, getRequestStatsByProperty, getRequestStatsByTenant, getUrgentRequests, } from "../../../services/maintenanceRequestService.ts"; import { getUserById } from "../../../services/userService.ts"; import { SessionCookie } from "../../auth/callback.ts"; export const handler = define.handlers({ async GET(ctx) { try { const stateData: SessionCookie = ctx.state.data; const { user_id } = stateData; const url = new URL(ctx.req.url); const property_id = url.searchParams.get("property_id"); const tenant_id = url.searchParams.get("tenant_id"); const request_id = url.searchParams.get("id"); const status = url.searchParams.get("status"); const priority = url.searchParams.get("priority"); const open_only = url.searchParams.get("open_only") === "true"; const urgent_only = url.searchParams.get("urgent_only") === "true"; const stats_only = url.searchParams.get("stats_only") === "true"; let result; if (request_id) { // Get specific maintenance request result = await getRequestById(request_id); return new Response( JSON.stringify({ success: true, maintenance_request: result, }), { status: 200, headers: { "Content-Type": "application/json" }, }, ); } else if (property_id && stats_only) { // Get statistics for property result = await getRequestStatsByProperty(property_id); return new Response( JSON.stringify({ success: true, stats: result, }), { status: 200, headers: { "Content-Type": "application/json" }, }, ); } else if (tenant_id && stats_only) { // Get statistics for tenant result = await getRequestStatsByTenant(tenant_id); return new Response( JSON.stringify({ success: true, stats: result, }), { status: 200, headers: { "Content-Type": "application/json" }, }, ); } else if (urgent_only) { // Get urgent requests result = await getUrgentRequests(); } else if (open_only) { // Get open requests result = await getOpenRequests(); } else if (status) { // Get requests by status result = await getRequestsByStatus(status); } else if (priority) { // Get requests by priority result = await getRequestsByPriority(priority); } else if (property_id) { // Get requests for a property result = await getRequestsByProperty(property_id); } else if (tenant_id) { // Get requests by tenant result = await getRequestsByTenant(tenant_id); } else { // Get requests based on user role // Landlords see requests for their properties // Tenants see their own requests const user = await getUserById(user_id); if (user && user.user_type === "landlord") { result = await getMaintenanceRequestsByLandlord(user_id); } else { result = await getMaintenanceRequestsByUser(user_id); } } return new Response( JSON.stringify({ success: true, maintenance_requests: result, }), { status: 200, headers: { "Content-Type": "application/json" }, }, ); } catch (e) { return new Response(JSON.stringify({ error: e }), { status: 500, headers: { "Content-Type": "application/json" }, }); } }, });