2023-08-14 21:16:11 -06:00
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
import { db } from "./db";
|
|
|
|
import { logs, rooms, votes } from "./schema";
|
|
|
|
import { env } from "~/env.mjs";
|
2023-08-14 21:19:47 -06:00
|
|
|
import type { NextApiResponse } from "next";
|
2023-08-14 21:16:11 -06:00
|
|
|
|
|
|
|
export const onUserDeletedHandler = async (
|
|
|
|
userId: string,
|
|
|
|
res: NextApiResponse
|
|
|
|
) => {
|
2023-08-14 21:29:18 -06:00
|
|
|
try {
|
|
|
|
await db.delete(rooms).where(eq(rooms.userId, userId));
|
2023-08-14 21:16:11 -06:00
|
|
|
|
|
|
|
res.status(200).json({ result: "USER DELETED" });
|
2023-08-14 21:29:18 -06:00
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({ error: error });
|
2023-08-14 21:16:11 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const onUserCreatedHandler = async (
|
|
|
|
userId: string,
|
|
|
|
res: NextApiResponse
|
|
|
|
) => {
|
|
|
|
const userUpdateResponse = await fetch(
|
|
|
|
`https://api.clerk.com/v1/users/${userId}/metadata`,
|
|
|
|
{
|
|
|
|
method: "PATCH",
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${env.CLERK_SECRET_KEY}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
public_metadata: {
|
|
|
|
isVIP: false,
|
|
|
|
isAdmin: false,
|
|
|
|
},
|
|
|
|
private_metadata: {},
|
|
|
|
unsafe_metadata: {},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (userUpdateResponse.ok) {
|
|
|
|
res.status(200).json({ result: "USER CREATED" });
|
|
|
|
} else {
|
|
|
|
res.status(404).json({ error: "USER WITH THIS ID NOT FOUND" });
|
|
|
|
}
|
|
|
|
};
|