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

103 lines
2.9 KiB
TypeScript

// routes/api/retrieve/lease_documents.ts
import { define } from "../../../utils.ts";
import {
getDocumentById,
getDocumentsByLease,
getDocumentsByLeaseAndType,
getLeaseSigningStatus,
getMainLeaseDocument,
} from "../../../services/leaseDocumentService.ts";
import { SessionCookie } from "../../auth/callback.ts";
export const handler = define.handlers({
async GET(ctx) {
try {
const stateData: SessionCookie = ctx.state.data;
const url = new URL(ctx.req.url);
const lease_id = url.searchParams.get("lease_id");
const document_id = url.searchParams.get("id");
const document_type = url.searchParams.get("document_type");
const main_only = url.searchParams.get("main_only") === "true";
const signing_status = url.searchParams.get("signing_status") === "true";
if (!lease_id && !document_id) {
return new Response(
JSON.stringify({
error: "Either lease ID or document ID is required",
}),
{
status: 400,
headers: { "Content-Type": "application/json" },
},
);
}
let result;
if (document_id) {
// Get specific document
result = await getDocumentById(document_id);
return new Response(
JSON.stringify({
success: true,
document: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} else if (signing_status) {
// Get signing status for lease
result = await getLeaseSigningStatus(lease_id!);
return new Response(
JSON.stringify({
success: true,
signing_status: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} else if (main_only) {
// Get main lease document only
result = await getMainLeaseDocument(lease_id!);
return new Response(
JSON.stringify({
success: true,
main_document: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} else if (document_type) {
// Get documents by type
result = await getDocumentsByLeaseAndType(lease_id!, document_type);
} else {
// Get all documents for lease
result = await getDocumentsByLease(lease_id!);
}
return new Response(
JSON.stringify({
success: true,
documents: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} catch (e) {
return new Response(JSON.stringify({ error: e }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
},
});