diff --git a/package.json b/package.json index aaade78e..d691f723 100644 --- a/package.json +++ b/package.json @@ -147,6 +147,9 @@ "./{*,{ee,pages,components,lib}/**/*}.{js,ts,jsx,tsx}": [ "prettier --write", "eslint" + ], + "./prisma/schema.prisma": [ + "prisma format" ] } } diff --git a/prisma/migrations/20211028233838_add_user_webhooks_relation/migration.sql b/prisma/migrations/20211028233838_add_user_webhooks_relation/migration.sql new file mode 100644 index 00000000..21a39a4a --- /dev/null +++ b/prisma/migrations/20211028233838_add_user_webhooks_relation/migration.sql @@ -0,0 +1,2 @@ +-- AddForeignKey +ALTER TABLE "Webhook" ADD FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a6991add..7db629eb 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -7,13 +7,13 @@ datasource db { } generator client { - provider = "prisma-client-js" + provider = "prisma-client-js" previewFeatures = ["selectRelationCount"] } enum SchedulingType { - ROUND_ROBIN @map("roundRobin") - COLLECTIVE @map("collective") + ROUND_ROBIN @map("roundRobin") + COLLECTIVE @map("collective") } model EventType { @@ -33,7 +33,7 @@ model EventType { eventName String? customInputs EventTypeCustomInput[] timeZone String? - periodType String @default("unlimited") // unlimited | rolling | range + periodType String @default("unlimited") // unlimited | rolling | range periodStartDate DateTime? periodEndDate DateTime? periodDays Int? @@ -45,6 +45,7 @@ model EventType { Schedule Schedule[] price Int @default(0) currency String @default("usd") + @@unique([userId, slug]) } @@ -86,26 +87,25 @@ model User { availability Availability[] selectedCalendars SelectedCalendar[] completedOnboarding Boolean @default(false) - locale String? + locale String? twoFactorSecret String? twoFactorEnabled Boolean @default(false) - - - plan UserPlan @default(PRO) - Schedule Schedule[] + plan UserPlan @default(PRO) + Schedule Schedule[] + webhooks Webhook[] @@map(name: "users") } model Team { - id Int @id @default(autoincrement()) - name String? - slug String? @unique - logo String? - bio String? - hideBranding Boolean @default(false) - members Membership[] - eventTypes EventType[] + id Int @id @default(autoincrement()) + name String? + slug String? @unique + logo String? + bio String? + hideBranding Boolean @default(false) + members Membership[] + eventTypes EventType[] } enum MembershipRole { @@ -156,17 +156,17 @@ model Attendee { } enum BookingStatus { - CANCELLED @map("cancelled") - ACCEPTED @map("accepted") - REJECTED @map("rejected") - PENDING @map("pending") + CANCELLED @map("cancelled") + ACCEPTED @map("accepted") + REJECTED @map("rejected") + PENDING @map("pending") } model DailyEventReference { - id Int @id @default(autoincrement()) - dailyurl String @default("dailycallurl") - dailytoken String @default("dailytoken") - booking Booking? @relation(fields: [bookingId], references: [id]) + id Int @id @default(autoincrement()) + dailyurl String @default("dailycallurl") + dailytoken String @default("dailytoken") + booking Booking? @relation(fields: [bookingId], references: [id]) bookingId Int? } @@ -187,14 +187,14 @@ model Booking { attendees Attendee[] location String? - dailyRef DailyEventReference? + dailyRef DailyEventReference? - createdAt DateTime @default(now()) + createdAt DateTime @default(now()) updatedAt DateTime? - confirmed Boolean @default(true) - rejected Boolean @default(false) + confirmed Boolean @default(true) + rejected Boolean @default(false) status BookingStatus @default(ACCEPTED) - paid Boolean @default(false) + paid Boolean @default(false) payment Payment[] } @@ -272,18 +272,18 @@ enum PaymentType { } model Payment { - id Int @id @default(autoincrement()) - uid String @unique - type PaymentType - bookingId Int - booking Booking? @relation(fields: [bookingId], references: [id]) - amount Int - fee Int - currency String - success Boolean - refunded Boolean - data Json - externalId String @unique + id Int @id @default(autoincrement()) + uid String @unique + type PaymentType + bookingId Int + booking Booking? @relation(fields: [bookingId], references: [id]) + amount Int + fee Int + currency String + success Boolean + refunded Boolean + data Json + externalId String @unique } enum WebhookTriggerEvents { @@ -293,10 +293,11 @@ enum WebhookTriggerEvents { } model Webhook { - id String @unique @id - userId Int + id String @id @unique + userId Int subscriberUrl String - createdAt DateTime @default(now()) - active Boolean @default(true) + createdAt DateTime @default(now()) + active Boolean @default(true) eventTriggers WebhookTriggerEvents[] + user User @relation(fields: [userId], references: [id]) } diff --git a/server/routers/viewer/webhook.tsx b/server/routers/viewer/webhook.tsx index f0a3665c..a5015807 100644 --- a/server/routers/viewer/webhook.tsx +++ b/server/routers/viewer/webhook.tsx @@ -65,21 +65,20 @@ export const webhookRouter = createProtectedRouter() }), async resolve({ ctx, input }) { const { id } = input; - const webhook = await ctx.prisma.webhook.findFirst({ + + await ctx.prisma.user.update({ where: { - userId: ctx.user.id, - id, - }, - }); - if (!webhook) { - // user does not own this webhook - return null; - } - await ctx.prisma.webhook.delete({ - where: { - id, + id: ctx.user.id, + }, + data: { + webhooks: { + delete: { + id, + }, + }, }, }); + return { id, };