mirror of
https://github.com/soconnor0919/hristudio.git
synced 2025-12-12 07:04:44 -05:00
Begin file upload, add theme change
This commit is contained in:
67
src/app/api/informed-consent/route.ts
Normal file
67
src/app/api/informed-consent/route.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { db } from "~/server/db";
|
||||
import { contents, informedConsentForms, contentTypes } from "~/server/db/schema";
|
||||
import { auth } from "@clerk/nextjs/server";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { saveFile } from '~/lib/fileStorage';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const { userId } = auth();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const formData = await request.formData();
|
||||
const file = formData.get('file') as File;
|
||||
const studyId = formData.get('studyId') as string;
|
||||
const participantId = formData.get('participantId') as string;
|
||||
|
||||
if (!file || !studyId || !participantId) {
|
||||
return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const [contentType] = await db
|
||||
.select()
|
||||
.from(contentTypes)
|
||||
.where(eq(contentTypes.name, "Informed Consent Form"));
|
||||
|
||||
if (!contentType) {
|
||||
return NextResponse.json({ error: 'Content type not found' }, { status: 500 });
|
||||
}
|
||||
|
||||
const [content] = await db
|
||||
.insert(contents)
|
||||
.values({
|
||||
contentTypeId: contentType.id,
|
||||
uploader: userId,
|
||||
location: '', // We'll update this after saving the file
|
||||
})
|
||||
.returning();
|
||||
|
||||
if (!content) {
|
||||
throw new Error("Content not found");
|
||||
}
|
||||
|
||||
const fileLocation = await saveFile(file, content.id);
|
||||
|
||||
await db
|
||||
.update(contents)
|
||||
.set({ location: fileLocation })
|
||||
.where(eq(contents.id, content.id));
|
||||
|
||||
const [form] = await db
|
||||
.insert(informedConsentForms)
|
||||
.values({
|
||||
studyId: parseInt(studyId),
|
||||
participantId: parseInt(participantId),
|
||||
contentId: content.id,
|
||||
})
|
||||
.returning();
|
||||
|
||||
return NextResponse.json(form);
|
||||
} catch (error) {
|
||||
console.error('Error uploading informed consent form:', error);
|
||||
return NextResponse.json({ error: 'Failed to upload form' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ClerkProvider } from '@clerk/nextjs'
|
||||
import { Inter } from "next/font/google"
|
||||
import { StudyProvider } from '~/context/StudyContext'
|
||||
|
||||
import { ThemeProvider } from '~/components/ThemeProvider'
|
||||
import "~/styles/globals.css"
|
||||
|
||||
const inter = Inter({
|
||||
@@ -19,13 +19,15 @@ export const metadata = {
|
||||
export default function RootLayout({ children }: React.PropsWithChildren) {
|
||||
return (
|
||||
<ClerkProvider>
|
||||
<StudyProvider>
|
||||
<html lang="en" className={inter.variable}>
|
||||
<body className="font-sans">
|
||||
{children}
|
||||
</body>
|
||||
</html>
|
||||
</StudyProvider>
|
||||
<html lang="en" className={inter.variable}>
|
||||
<body className="font-sans">
|
||||
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
|
||||
<StudyProvider>
|
||||
{children}
|
||||
</StudyProvider>
|
||||
</ThemeProvider>
|
||||
</body>
|
||||
</html>
|
||||
</ClerkProvider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user