Adds missing webhook user relationship (#1070)

pull/1077/head
Omar López 2021-10-29 08:13:51 -06:00 committed by GitHub
parent 5291dade42
commit cc25a772a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 58 deletions

View File

@ -147,6 +147,9 @@
"./{*,{ee,pages,components,lib}/**/*}.{js,ts,jsx,tsx}": [
"prettier --write",
"eslint"
],
"./prisma/schema.prisma": [
"prisma format"
]
}
}

View File

@ -0,0 +1,2 @@
-- AddForeignKey
ALTER TABLE "Webhook" ADD FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -45,6 +45,7 @@ model EventType {
Schedule Schedule[]
price Int @default(0)
currency String @default("usd")
@@unique([userId, slug])
}
@ -89,10 +90,9 @@ model User {
locale String?
twoFactorSecret String?
twoFactorEnabled Boolean @default(false)
plan UserPlan @default(PRO)
Schedule Schedule[]
webhooks Webhook[]
@@map(name: "users")
}
@ -293,10 +293,11 @@ enum WebhookTriggerEvents {
}
model Webhook {
id String @unique @id
id String @id @unique
userId Int
subscriberUrl String
createdAt DateTime @default(now())
active Boolean @default(true)
eventTriggers WebhookTriggerEvents[]
user User @relation(fields: [userId], references: [id])
}

View File

@ -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: ctx.user.id,
},
data: {
webhooks: {
delete: {
id,
},
});
if (!webhook) {
// user does not own this webhook
return null;
}
await ctx.prisma.webhook.delete({
where: {
id,
},
},
});
return {
id,
};