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

import { authActionClient } from "@/lib/actions/safe-action";
import { throwIfNoPermission } from "@/lib/actions/throw-if-no-permission";
import { prisma } from "@/lib/prisma";
import { STRIPE_INTEGRATION_ID } from "@dub/utils";
import { revalidatePath } from "next/cache";
import * as z from "zod/v4";
import { stripeIntegrationSettingsSchema } from "./schema";

const schema = z.object({
  workspaceId: z.string(),
  freeTrials: stripeIntegrationSettingsSchema.shape.freeTrials,
  discountCodeRestrictions: z
    .object({
      firstTimeTransaction: z.boolean(),
    })
    .optional(),
});

export const updateStripeSettingsAction = authActionClient
  .inputSchema(schema)
  .action(async ({ parsedInput, ctx }) => {
    const { workspace } = ctx;
    const { freeTrials, discountCodeRestrictions } = parsedInput;

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

    const installedIntegration = await prisma.installedIntegration.findFirst({
      where: {
        integrationId: STRIPE_INTEGRATION_ID,
        projectId: workspace.id,
      },
    });

    if (!installedIntegration) {
      throw new Error("Stripe integration is not installed on your workspace.");
    }

    const current = (installedIntegration.settings as any) ?? {};

    await prisma.installedIntegration.update({
      where: {
        id: installedIntegration.id,
      },
      data: {
        settings: {
          ...current,
          freeTrials,
          ...(discountCodeRestrictions !== undefined
            ? { discountCodeRestrictions }
            : {}),
        },
      },
    });

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