Webhook fixes
This commit is contained in:
parent
7aed9f9e5b
commit
3354201207
3 changed files with 42 additions and 31 deletions
4
src/pages/api/external/ping.ts
vendored
4
src/pages/api/external/ping.ts
vendored
|
@ -6,9 +6,9 @@ export default async function handler(
|
|||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
const success = await validateRequest(req, res);
|
||||
const isValid = await validateRequest(req, res);
|
||||
|
||||
if (success) {
|
||||
if (isValid) {
|
||||
await db.query.votes.findFirst();
|
||||
res.status(200).json({ result: "Pong!" });
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import {
|
|||
onUserCreatedHandler,
|
||||
onUserDeletedHandler,
|
||||
} from "~/server/webhookHelpers";
|
||||
import { type WebhookEvent, WebhookEvents } from "~/utils/types";
|
||||
import { WebhookEventBodySchema, WebhookEvents } from "~/utils/types";
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
|
@ -16,36 +16,24 @@ export default async function handler(
|
|||
return;
|
||||
}
|
||||
|
||||
const requestBody = req.body as {
|
||||
data: {
|
||||
id: string;
|
||||
email_addresses:
|
||||
| [
|
||||
{
|
||||
email_address: string;
|
||||
id: string;
|
||||
verification: {
|
||||
status: string;
|
||||
strategy: string;
|
||||
};
|
||||
}
|
||||
]
|
||||
| null;
|
||||
};
|
||||
type: WebhookEvent;
|
||||
};
|
||||
try {
|
||||
const requestBody = WebhookEventBodySchema.parse(req.body);
|
||||
|
||||
switch (requestBody.type) {
|
||||
case WebhookEvents.USER_CREATED:
|
||||
await onUserCreatedHandler(requestBody.data.id, res);
|
||||
return;
|
||||
switch (requestBody.type) {
|
||||
case WebhookEvents.USER_CREATED:
|
||||
await onUserCreatedHandler(requestBody.data.id, res);
|
||||
return;
|
||||
|
||||
case WebhookEvents.USER_DELETED:
|
||||
await onUserDeletedHandler(requestBody.data.id, res);
|
||||
return;
|
||||
case WebhookEvents.USER_DELETED:
|
||||
await onUserDeletedHandler(requestBody.data.id, res);
|
||||
return;
|
||||
|
||||
default:
|
||||
res.status(400).json({ error: "INVALID WEBHOOK EVENT" });
|
||||
return;
|
||||
default:
|
||||
res.status(400).json({ error: "INVALID WEBHOOK EVENT TYPE" });
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
res.status(400).json({ error: "INVALID WEBHOOK EVENT BODY" });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { z } from "zod";
|
||||
|
||||
type BetterEnum<T> = T[keyof T];
|
||||
|
||||
export const EventTypes = {
|
||||
|
@ -14,6 +16,27 @@ export const WebhookEvents = {
|
|||
} as const;
|
||||
export type WebhookEvent = BetterEnum<typeof WebhookEvents>;
|
||||
|
||||
export const WebhookEventBodySchema = z.object({
|
||||
data: z.object({
|
||||
id: z.string(),
|
||||
email_addresses: z
|
||||
.array(
|
||||
z.object({
|
||||
email_address: z.string().email(),
|
||||
id: z.string(),
|
||||
verification: z.object({
|
||||
status: z.string(),
|
||||
strategy: z.string(),
|
||||
}),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
}),
|
||||
type: z.string(),
|
||||
});
|
||||
|
||||
export type WebhookEventBody = z.infer<typeof WebhookEventBodySchema>;
|
||||
|
||||
export interface PresenceItem {
|
||||
name: string;
|
||||
image: string;
|
||||
|
|
Loading…
Add table
Reference in a new issue