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.3 KB
Raw Blame
"use server";

import { authActionClient } from "@/lib/actions/safe-action";
import { encrypt } from "@/lib/encryption";
import { createWebhook } from "@/lib/webhook/create-webhook";
import { SEGMENT_INTEGRATION_ID } from "@dub/utils";
import { WebhookReceiver } from "@prisma/client";
import { revalidatePath } from "next/cache";
import * as z from "zod/v4";
import { installIntegration } from "../install";
import { segmentCredentialsSchema } from "./schema";

const inputSchema = segmentCredentialsSchema.extend({
  workspaceId: z.string(),
});

export const installSegmentAction = authActionClient
  .inputSchema(inputSchema)
  .action(async ({ parsedInput, ctx }) => {
    const { workspace, user } = ctx;
    const { writeKey } = parsedInput;

    const installation = await installIntegration({
      integrationId: SEGMENT_INTEGRATION_ID,
      userId: user.id,
      workspaceId: workspace.id,
      credentials: {
        writeKey: encrypt(writeKey),
      },
    });

    await createWebhook({
      name: "Segment",
      url: "https://api.segment.io/v1/track",
      receiver: WebhookReceiver.segment,
      triggers: [],
      workspace,
      secret: writeKey,
      installationId: installation.id,
    });

    revalidatePath(`/${workspace.slug}/settings/integrations/segment`);
  });