Skip to content

Commit

Permalink
Refactor webinar schema to make appoitment more effective
Browse files Browse the repository at this point in the history
  • Loading branch information
teetangh committed Dec 27, 2024
1 parent 548f000 commit 933a3e8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 30 deletions.
35 changes: 22 additions & 13 deletions app/api/events/webinars/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import prisma from "@/lib/prisma";
import { NextResponse } from "next/server";
import { WebinarStatus } from "@prisma/client";

export async function GET(request: Request) {
try {
Expand All @@ -16,15 +17,11 @@ export async function GET(request: Request) {
// Get webinars where consultee is registered through appointments
{
appointment: {
some: {
slotsOfAppointment: {
some: {
user: {
some: {
consulteeProfile: {
id: consulteeProfileId,
},
},
slotsOfAppointment: {
some: {
user: {
some: {
consulteeProfileId: consulteeProfileId,
},
},
},
Expand Down Expand Up @@ -81,6 +78,7 @@ export async function GET(request: Request) {
},
},
},
meetingRoom: true,
waitlist: {
where: {
userId: consulteeProfileId,
Expand Down Expand Up @@ -115,11 +113,12 @@ export async function GET(request: Request) {
include: {
slotsOfAppointment: {
include: {
user: true, // Changed from consulteeProfile
user: true,
},
},
},
},
meetingRoom: true,
waitlist: true,
},
});
Expand All @@ -136,11 +135,12 @@ export async function GET(request: Request) {
include: {
slotsOfAppointment: {
include: {
user: true, // Changed from consulteeProfile
user: true,
},
},
},
},
meetingRoom: true,
waitlist: true,
},
});
Expand All @@ -160,11 +160,19 @@ export async function POST(request: Request) {
try {
const body = await request.json();

// Validate required fields
if (!body.scheduledAt || !body.endAt || !body.webinarPlanId) {
return NextResponse.json(
{ error: "Missing required fields: scheduledAt, endAt, and webinarPlanId are required" },
{ status: 400 }
);
}

const webinar = await prisma.webinar.create({
data: {
scheduledAt: new Date(body.scheduledAt),
endAt: new Date(body.endAt),
status: body.status,
status: body.status || WebinarStatus.SCHEDULED,
webinarPlan: {
connect: { id: body.webinarPlanId },
},
Expand All @@ -180,11 +188,12 @@ export async function POST(request: Request) {
include: {
slotsOfAppointment: {
include: {
user: true, // Changed from consulteeProfile
user: true,
},
},
},
},
meetingRoom: true,
waitlist: true,
},
});
Expand Down
33 changes: 18 additions & 15 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ model User {
staffProfileId String? @unique
slotsOfAppointment SlotOfAppointment[]
Waitlist Waitlist[]
accounts Account[] // NextAuth Accounts
sessions Session[] // NextAuth Sessions
Expand Down Expand Up @@ -280,7 +281,7 @@ model SlotOfAvailabilityCustom {
model SlotOfAppointment {
id String @id @default(uuid())
user User[]
user User[]
slotStartTimeInUTC DateTime @db.Timestamptz()
slotEndTimeInUTC DateTime @db.Timestamptz()
Expand Down Expand Up @@ -429,7 +430,7 @@ model Webinar {
webinarPlan WebinarPlan @relation(fields: [webinarPlanId], references: [id], onUpdate: Cascade, onDelete: Cascade)
webinarPlanId String
appointment Appointment[]
appointment Appointment?
meetingRoom MeetingRoom?
createdAt DateTime @default(now())
Expand Down Expand Up @@ -472,15 +473,15 @@ model ClassPlan {
}

model Class {
id String @id @default(cuid())
startDate DateTime?
endDate DateTime?
status ClassStatus @default(SCHEDULED)
tentativeStartDate DateTime?
tentativeSchedule String?
waitlist Waitlist[]
recordingUrls String[]
feedbackSummary String? @db.Text
id String @id @default(cuid())
startDate DateTime?
endDate DateTime?
status ClassStatus @default(SCHEDULED)
tentativeStartDate DateTime?
tentativeSchedule String?
waitlist Waitlist[]
recordingUrls String[]
feedbackSummary String? @db.Text
classPlan ClassPlan @relation(fields: [classPlanId], references: [id], onUpdate: Cascade, onDelete: Cascade)
classPlanId String
Expand Down Expand Up @@ -517,8 +518,8 @@ enum ClassStatus {

// Generic Appointment Model
model Appointment {
id String @id @default(uuid())
appointmentType AppointmentsType
id String @id @default(uuid())
appointmentType AppointmentsType
slotsOfAppointment SlotOfAppointment[]
consultation Consultation? @relation(fields: [consultationId], references: [id], onUpdate: Cascade, onDelete: Cascade)
Expand All @@ -528,7 +529,7 @@ model Appointment {
subscriptionId String?
webinar Webinar? @relation(fields: [webinarId], references: [id], onUpdate: Cascade, onDelete: Cascade)
webinarId String?
webinarId String? @unique
class Class? @relation(fields: [classId], references: [id], onUpdate: Cascade, onDelete: Cascade)
classId String?
Expand Down Expand Up @@ -567,9 +568,11 @@ model Newsletter {

model Waitlist {
id String @id @default(uuid())
userId String
joinedAt DateTime @default(now())
user User @relation(fields: [userId], references: [id], onUpdate: Cascade, onDelete: Cascade)
userId String
webinar Webinar? @relation(fields: [webinarId], references: [id], onUpdate: Cascade, onDelete: Cascade)
webinarId String?
Expand Down
32 changes: 30 additions & 2 deletions prisma/seedFiles/createAppointments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ export async function createAppointments(consultees: UserWithProfiles[]) {
slotsOfAppointment: {
create: {
user: {
connect: {
connect: [{
id: consultee.id,
},
}],
},
slotStartTimeInUTC,
slotEndTimeInUTC,
Expand Down Expand Up @@ -206,6 +206,20 @@ export async function createAppointments(consultees: UserWithProfiles[]) {
feedbackSummary: isPastAppointment
? faker.lorem.paragraph()
: null,
waitlist: isPastAppointment
? undefined
: {
create: Array.from(
{ length: faker.number.int({ min: 0, max: 5 }) },
() => ({
user: {
connect: {
id: faker.helpers.arrayElement(consultees).id,
},
},
}),
),
},
meetingRoom: {
create: {
platform: faker.helpers.arrayElement(Object.values(Platform)),
Expand Down Expand Up @@ -257,6 +271,20 @@ export async function createAppointments(consultees: UserWithProfiles[]) {
feedbackSummary: isPastAppointment
? faker.lorem.paragraph()
: null,
waitlist: isPastAppointment
? undefined
: {
create: Array.from(
{ length: faker.number.int({ min: 0, max: 5 }) },
() => ({
user: {
connect: {
id: faker.helpers.arrayElement(consultees).id,
},
},
}),
),
},
meetingRoom: {
create: {
platform: faker.helpers.arrayElement(Object.values(Platform)),
Expand Down

0 comments on commit 933a3e8

Please sign in to comment.