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