import { Webhook } from "svix";
import { buffer } from "micro";
import { ClerkResponse } from "../../../../utils/clerk";
import { trpc } from "../../../../utils/trpc";
import { PrismaClient } from "@acme/db";
import { btcRouter } from "@acme/api/src/router/btc";
import { createContext, appRouter } from "@acme/api";
import { createContextInner } from "@acme/api/src/context";
// import { createContext } from "@acme/api";
const prisma = new PrismaClient()
export const config = {
api: {
bodyParser: false,
},
}
const secret = process.env.CLERK_WEBHOOK_SECRET;
export default async function handler(req, res) {
const ctx = await createContext({ req, res });
const caller = appRouter.createCaller(ctx);
const payload = (await buffer(req)).toString();
const headers = req.headers;
const wh = new Webhook(secret as string);
try {
// Verify the webhook signature and parse the payload
const msg: unknown = wh.verify(payload, headers);
console.log('New user created!', (msg as ClerkResponse).data); // The payload of the webhook
console.log('User ID:', (msg as ClerkResponse).data.id); // The user ID
const new_user = await caller.btc.createVA({ userId: (msg as ClerkResponse).data.id, });
etc...