Finished webhook handler for user.deleted

This commit is contained in:
Atridad Lahiji 2023-08-13 23:20:05 -06:00
parent 37fc8732dd
commit 9b07973c01
No known key found for this signature in database
GPG key ID: 7CB8245F56BC3880

View file

@ -1,4 +1,7 @@
import { eq } from "drizzle-orm";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { db } from "~/server/db";
import { logs, rooms, votes } from "~/server/schema";
import { validateApiKey } from "~/server/unkey"; import { validateApiKey } from "~/server/unkey";
export default async function handler( export default async function handler(
@ -20,7 +23,24 @@ export default async function handler(
res.status(403).json({ error: "UNAUTHORIZED" }); res.status(403).json({ error: "UNAUTHORIZED" });
} }
console.log(req.body); const requestBody = req.body as {
data: {
deleted: string;
id: string;
object: string;
};
object: string;
type: string;
};
res.status(200).json({ result: "AUTHORIZED" }); const deletedRoom = await db
.delete(rooms)
.where(eq(rooms.userId, requestBody.data.id));
if (deletedRoom.rowsAffected > 0) {
await db.delete(logs).where(eq(logs.userId, requestBody.data.id));
await db.delete(votes).where(eq(votes.userId, requestBody.data.id));
}
res.status(200).json({ result: "USER DELETED" });
} }