Merge branch 'dev'
This commit is contained in:
commit
0a3de36d11
4 changed files with 42 additions and 32 deletions
|
@ -1,32 +1,53 @@
|
|||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import {
|
||||
onUserCreatedHandler,
|
||||
onUserDeletedHandler,
|
||||
} from "~/server/webhookHelpers";
|
||||
import { WebhookEventBodySchema, WebhookEvents } from "~/utils/types";
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
export const config = {
|
||||
runtime: "edge",
|
||||
regions: ["pdx1"],
|
||||
};
|
||||
|
||||
export default async function handler(req: NextRequest) {
|
||||
try {
|
||||
const requestBody = WebhookEventBodySchema.parse(req.body);
|
||||
const requestBody = WebhookEventBodySchema.parse(await req.json());
|
||||
let success = false;
|
||||
|
||||
switch (requestBody.type) {
|
||||
case WebhookEvents.USER_CREATED:
|
||||
await onUserCreatedHandler(requestBody.data.id, res);
|
||||
return;
|
||||
success = await onUserCreatedHandler(requestBody.data.id);
|
||||
if (success) {
|
||||
return NextResponse.json(
|
||||
{ result: "USER CREATED" },
|
||||
{ status: 200, statusText: "USER CREATED" }
|
||||
);
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ result: "USER WITH THIS ID NOT FOUND" },
|
||||
{ status: 404, statusText: "USER WITH THIS ID NOT FOUND" }
|
||||
);
|
||||
}
|
||||
|
||||
case WebhookEvents.USER_DELETED:
|
||||
await onUserDeletedHandler(requestBody.data.id, res);
|
||||
return;
|
||||
success = await onUserDeletedHandler(requestBody.data.id);
|
||||
|
||||
return NextResponse.json(
|
||||
{ result: "USER DELETED" },
|
||||
{ status: 200, statusText: "USER DELETED" }
|
||||
);
|
||||
|
||||
default:
|
||||
res.status(400).json({ error: "INVALID WEBHOOK EVENT TYPE" });
|
||||
return;
|
||||
return NextResponse.json(
|
||||
{ result: "INVALID WEBHOOK EVENT TYPE" },
|
||||
{ status: 400, statusText: "INVALID WEBHOOK EVENT TYPE" }
|
||||
);
|
||||
}
|
||||
} catch {
|
||||
res.status(400).json({ error: "INVALID WEBHOOK EVENT BODY" });
|
||||
return;
|
||||
return NextResponse.json(
|
||||
{ result: "INVALID WEBHOOK EVENT BODY" },
|
||||
{ status: 400, statusText: "INVALID WEBHOOK EVENT BODY" }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ export const voteRouter = createTRPCRouter({
|
|||
},
|
||||
});
|
||||
|
||||
let success = upsertResult.rowCount > 0;
|
||||
const success = upsertResult.rowCount > 0;
|
||||
|
||||
if (success) {
|
||||
await invalidateCache(`kv_votecount`);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Unkey } from "@unkey/api";
|
||||
import { NextRequest } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { env } from "~/env.mjs";
|
||||
|
||||
export const unkey = new Unkey({ token: env.UNKEY_ROOT_KEY });
|
||||
|
|
|
@ -2,25 +2,18 @@ import { eq } from "drizzle-orm";
|
|||
import { db } from "./db";
|
||||
import { rooms } from "./schema";
|
||||
import { env } from "~/env.mjs";
|
||||
import type { NextApiResponse } from "next";
|
||||
|
||||
export const onUserDeletedHandler = async (
|
||||
userId: string,
|
||||
res: NextApiResponse
|
||||
) => {
|
||||
export const onUserDeletedHandler = async (userId: string) => {
|
||||
try {
|
||||
await db.delete(rooms).where(eq(rooms.userId, userId));
|
||||
|
||||
res.status(200).json({ result: "USER DELETED" });
|
||||
return true;
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error });
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const onUserCreatedHandler = async (
|
||||
userId: string,
|
||||
res: NextApiResponse
|
||||
) => {
|
||||
export const onUserCreatedHandler = async (userId: string) => {
|
||||
const userUpdateResponse = await fetch(
|
||||
`https://api.clerk.com/v1/users/${userId}/metadata`,
|
||||
{
|
||||
|
@ -40,9 +33,5 @@ export const onUserCreatedHandler = async (
|
|||
}
|
||||
);
|
||||
|
||||
if (userUpdateResponse.ok) {
|
||||
res.status(200).json({ result: "USER CREATED" });
|
||||
} else {
|
||||
res.status(404).json({ error: "USER WITH THIS ID NOT FOUND" });
|
||||
}
|
||||
return userUpdateResponse.ok;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue