open-swe-dev-andrinoid[bot] f88da558b1
[WIP]: Implement Landlord Approval Mechanism for Tenant Leases (#127)
* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

* Apply patch [skip ci]

---------

Co-authored-by: open-swe-dev-andrinoid[bot] <open-swe-dev-andrinoid@users.noreply.github.com>
2026-01-28 16:48:47 -06:00

103 lines
2.7 KiB
TypeScript

// routes/api/lease/approve.ts
import { define } from "../../../utils.ts";
import { page } from "fresh";
import { SessionCookie } from "../../auth/callback.ts";
import { approveLease, getLeaseById } from "../../../services/leaseService.ts";
export const handler = define.handlers({
async POST(ctx) {
try {
const stateData: SessionCookie = ctx.state.data;
const { user_id } = stateData;
if (!user_id) {
return new Response(
JSON.stringify({ error: "Unauthorized" }),
{
status: 401,
headers: { "Content-Type": "application/json" },
},
);
}
const contentType = ctx.req.headers.get("content-type") || "";
let data: Record<string, unknown> = {};
if (contentType.includes("application/json")) {
data = await ctx.req.json();
} else {
const formData = await ctx.req.formData();
for (const [key, value] of formData.entries()) {
data[key] = typeof value === "string" ? value : String(value);
}
}
const lease_id = (data.lease_id as string) ||
(data["lease_id"] as string);
if (!lease_id) {
return new Response(
JSON.stringify({ error: "lease_id is required" }),
{
status: 400,
headers: { "Content-Type": "application/json" },
},
);
}
const lease = await getLeaseById(lease_id);
if (!lease) {
return new Response(
JSON.stringify({ error: "Lease not found" }),
{
status: 404,
headers: { "Content-Type": "application/json" },
},
);
}
if (lease.owner_id !== user_id) {
return new Response(
JSON.stringify({ error: "Unauthorized: You do not own this lease" }),
{
status: 403,
headers: { "Content-Type": "application/json" },
},
);
}
const result = await approveLease(lease_id, user_id);
if (!result || result.length === 0) {
return new Response(
JSON.stringify({ error: "Failed to approve lease" }),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
return new Response(
JSON.stringify({
success: true,
lease: result[0],
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} catch (error) {
console.error("Error approving lease:", error);
return new Response(
JSON.stringify({ error: String(error) }),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
},
});