integratedhub/routes/api/retrieve/tenant_applications.ts
2026-01-20 09:02:26 -06:00

81 lines
2.3 KiB
TypeScript

// routes/api/retrieve/tenant_applications.ts
import { define } from "../../../utils.ts";
import {
getApplicationById,
getApplicationsByApplicant,
getApplicationsByProperty,
getApplicationStats,
} from "../../../services/tenantApplicationService.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 applicant_id = url.searchParams.get("applicant_id");
const application_id = url.searchParams.get("id");
const stats_only = url.searchParams.get("stats_only") === "true";
let result;
if (application_id) {
// Get specific application
result = await getApplicationById(application_id);
return new Response(
JSON.stringify({
success: true,
application: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} else if (property_id) {
// Get applications for a property
if (stats_only) {
result = await getApplicationStats(property_id);
return new Response(
JSON.stringify({
success: true,
stats: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} else {
result = await getApplicationsByProperty(property_id);
}
} else if (applicant_id) {
// Get applications by applicant
result = await getApplicationsByApplicant(applicant_id);
} else {
// Get applications by current user (as applicant)
result = await getApplicationsByApplicant(user_id);
}
return new Response(
JSON.stringify({
success: true,
applications: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} catch (e) {
return new Response(JSON.stringify({ error: e }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
},
});