* 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>
105 lines
2.9 KiB
TypeScript
105 lines
2.9 KiB
TypeScript
// routes/api/lease/reject.ts
|
|
import { define } from "../../../utils.ts";
|
|
import { page } from "fresh";
|
|
import { SessionCookie } from "../../auth/callback.ts";
|
|
import { rejectLease, 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);
|
|
const rejection_reason = (data.rejection_reason as string) ||
|
|
(data["rejection_reason"] 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 rejectLease(lease_id, user_id, rejection_reason);
|
|
|
|
if (!result || result.length === 0) {
|
|
return new Response(
|
|
JSON.stringify({ error: "Failed to reject 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 rejecting lease:", error);
|
|
return new Response(
|
|
JSON.stringify({ error: String(error) }),
|
|
{
|
|
status: 500,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
}
|
|
},
|
|
});
|