39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import type { APIContext } from "astro";
|
|
import { getSessionUser, deleteDocument } from "../../db";
|
|
import { Prisma } from "@prisma/client";
|
|
|
|
export async function post({ request, cookies }: APIContext) {
|
|
const response: { ok: boolean; reason?: string } = {
|
|
ok: true,
|
|
};
|
|
try {
|
|
const sessId = cookies.get("session").value!;
|
|
const user = (await getSessionUser(sessId))!;
|
|
|
|
const json = await request.json();
|
|
const docId = json.docId as string;
|
|
if (!docId) {
|
|
throw new Error("Предоставлены некорректные данные");
|
|
}
|
|
|
|
const ok = await deleteDocument(docId);
|
|
if (!ok) {
|
|
throw new Error("Неизвестная ошибка");
|
|
}
|
|
|
|
response.ok = true;
|
|
} catch (e: any) {
|
|
response.ok = false;
|
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
|
response.reason = `Неизвестная ошибка базы данных. Код ${e.code}`;
|
|
} else if (e instanceof Error) {
|
|
response.reason = e.message.trim();
|
|
} else {
|
|
response.reason = e.toString().trim();
|
|
}
|
|
}
|
|
|
|
return {
|
|
body: JSON.stringify(response),
|
|
};
|
|
}
|