* Apply patch [skip ci] * Empty commit to trigger 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>
126 lines
3.5 KiB
TypeScript
126 lines
3.5 KiB
TypeScript
// routes/api/delete/lease_document.ts
|
|
import { define } from "../../../utils.ts";
|
|
import {
|
|
deleteLeaseDocument,
|
|
getDocumentById,
|
|
} from "../../../services/leaseDocumentService.ts";
|
|
import { SessionCookie } from "../../auth/callback.ts";
|
|
import {
|
|
getLeasesByOwner,
|
|
getLeasesByTenant,
|
|
} from "../../../services/leaseService.ts";
|
|
|
|
export const handler = define.handlers({
|
|
async POST(ctx) {
|
|
try {
|
|
const stateData: SessionCookie = ctx.state.data;
|
|
const requestingUserId = stateData.user_id;
|
|
|
|
if (!requestingUserId) {
|
|
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, string> = {};
|
|
let sender: string | null = null;
|
|
let isJsonRequest = false;
|
|
|
|
// Handle both JSON and form data
|
|
if (contentType.includes("application/json")) {
|
|
isJsonRequest = true;
|
|
const jsonData = await ctx.req.json();
|
|
data = jsonData;
|
|
} else {
|
|
const formData = await ctx.req.formData();
|
|
sender = formData.get("sender")?.toString() || null;
|
|
|
|
data = {
|
|
id: formData.get("id")?.toString() || "",
|
|
};
|
|
}
|
|
|
|
const id = data.id;
|
|
|
|
if (!id) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: "Document ID is required",
|
|
}),
|
|
{
|
|
status: 400,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
}
|
|
|
|
const document = await getDocumentById(id);
|
|
if (!document) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: "Document not found",
|
|
}),
|
|
{
|
|
status: 404,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
}
|
|
|
|
const lease = await getLeasesByTenant(requestingUserId).then((leases) =>
|
|
leases.find((l) => l.id === document.lease_id)
|
|
);
|
|
const ownerLeases = await getLeasesByOwner(requestingUserId);
|
|
const isOwner = ownerLeases.some((l) => l.id === document.lease_id);
|
|
|
|
if (!lease && !isOwner) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: "Access denied - you must be the tenant or property owner",
|
|
}),
|
|
{
|
|
status: 403,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
}
|
|
|
|
const result = await deleteLeaseDocument(id);
|
|
|
|
// Return appropriate response based on request type
|
|
if (isJsonRequest) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: true,
|
|
deleted: result[0],
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
} else {
|
|
// Redirect back to the sender URL
|
|
const senderPath = sender?.toString() || "/";
|
|
let redirectUrl = senderPath;
|
|
try {
|
|
if (senderPath.startsWith("/")) {
|
|
const url = new URL(ctx.req.url);
|
|
redirectUrl = url.origin + senderPath;
|
|
}
|
|
} catch (_) {
|
|
// fallback: just use senderPath
|
|
}
|
|
return Response.redirect(redirectUrl, 303);
|
|
}
|
|
} catch (e) {
|
|
return new Response(JSON.stringify({ error: e }), {
|
|
status: 500,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
},
|
|
});
|