Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

1.2 KB
Raw Blame
"use server";

import { prisma } from "@/lib/prisma";
import * as z from "zod/v4";
import { WEBHOOK_TRIGGERS } from "../webhook/constants";
import { sendWebhooks } from "../webhook/qstash";
import { samplePayload } from "../webhook/sample-events/payload";
import { authActionClient } from "./safe-action";
import { throwIfNoPermission } from "./throw-if-no-permission";

const schema = z.object({
  workspaceId: z.string(),
  webhookId: z.string(),
  trigger: z.enum(WEBHOOK_TRIGGERS),
});

// Test send webhook event
export const sendTestWebhookEvent = authActionClient
  .inputSchema(schema)
  .action(async ({ ctx, parsedInput }) => {
    const { workspace } = ctx;
    const { webhookId, trigger } = parsedInput;

    throwIfNoPermission({
      role: workspace.role,
      requiredPermissions: ["webhooks.write"],
    });

    const webhook = await prisma.webhook.findUniqueOrThrow({
      where: {
        id: webhookId,
        projectId: workspace.id,
      },
      select: {
        id: true,
        url: true,
        secret: true,
      },
    });

    await sendWebhooks({
      webhooks: [webhook],
      trigger,
      data: samplePayload[trigger],
    });

    return { ok: true };
  });