ability to create trials added; form uploader cleaned up

This commit is contained in:
2024-10-03 16:35:19 -04:00
parent 93e2b0323b
commit 7ef9180026
19 changed files with 559 additions and 151 deletions

View File

@@ -1,18 +1,36 @@
import { db } from "~/server/db";
import { participants } from "~/server/db/schema";
import { participants, trialParticipants, trials } from "~/server/db/schema";
import { NextResponse } from "next/server";
import { eq } from "drizzle-orm";
import { eq, sql } from "drizzle-orm";
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const studyId = searchParams.get('studyId');
if (!studyId) {
return NextResponse.json({ error: 'Study ID is required' }, { status: 400 });
}
try {
const { searchParams } = new URL(request.url);
const studyId = searchParams.get('studyId');
if (!studyId) {
return NextResponse.json({ error: 'Study ID is required' }, { status: 400 });
}
const allParticipants = await db.select().from(participants).where(eq(participants.studyId, parseInt(studyId)));
return NextResponse.json(allParticipants);
const participantsWithLatestTrial = await db
.select({
id: participants.id,
name: participants.name,
createdAt: participants.createdAt,
latestTrialTimestamp: sql<Date | null>`MAX(${trials.createdAt})`.as('latestTrialTimestamp')
})
.from(participants)
.leftJoin(trialParticipants, eq(participants.id, trialParticipants.participantId))
.leftJoin(trials, eq(trialParticipants.trialId, trials.id))
.where(eq(participants.studyId, parseInt(studyId)))
.groupBy(participants.id)
.orderBy(sql`COALESCE(MAX(${trials.createdAt}), ${participants.createdAt}) DESC`);
return NextResponse.json(participantsWithLatestTrial);
} catch (error) {
console.error('Error in GET /api/participants:', error);
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}
export async function POST(request: Request) {