Form implementation, api routes

This commit is contained in:
2024-09-26 16:04:57 -04:00
parent 66137ff7b4
commit 6584a48f27
23 changed files with 663 additions and 214 deletions

View File

@@ -0,0 +1,58 @@
import { NextResponse } from "next/server";
import { db } from "~/server/db";
import { informedConsentForms, contents } from "~/server/db/schema";
import { auth } from "@clerk/nextjs/server";
import { eq } from "drizzle-orm";
import fs from 'fs/promises';
import path from 'path';
export async function DELETE(
request: Request,
{ params }: { params: { id: string } }
) {
const { userId } = auth();
if (!userId) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const id = parseInt(params.id);
if (isNaN(id)) {
return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
}
try {
// First, get the content associated with this form
const [form] = await db
.select({
contentId: informedConsentForms.contentId,
location: contents.location,
})
.from(informedConsentForms)
.innerJoin(contents, eq(informedConsentForms.contentId, contents.id))
.where(eq(informedConsentForms.id, id));
if (!form) {
return NextResponse.json({ error: 'Form not found' }, { status: 404 });
}
// Delete the file from the file system
const fullPath = path.join(process.cwd(), form.location);
try {
await fs.access(fullPath);
await fs.unlink(fullPath);
} catch (error) {
console.warn(`File not found or couldn't be deleted: ${fullPath}`);
}
// Delete the form and content from the database
await db.transaction(async (tx) => {
await tx.delete(informedConsentForms).where(eq(informedConsentForms.id, id));
await tx.delete(contents).where(eq(contents.id, form.contentId));
});
return NextResponse.json({ message: "Form deleted successfully" });
} catch (error) {
console.error('Error deleting form:', error);
return NextResponse.json({ error: 'Failed to delete form' }, { status: 500 });
}
}