The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add assertStagingWorkspace guard and use it in copy-to-live actions
Kiran K436bdbf1324f
diff --git a/apps/web/lib/sandbox/copy-discount-to-live.ts b/apps/web/lib/sandbox/copy-discount-to-live.tsindex f8c26ef5ae..aebb304566 100644--- a/apps/web/lib/sandbox/copy-discount-to-live.ts+++ b/apps/web/lib/sandbox/copy-discount-to-live.ts@@ -1,9 +1,11 @@"use server";import { prisma } from "@/lib/prisma";-import { assertProductionWorkspace } from "@/lib/sandbox/workspace-guards";+import {+ assertProductionWorkspace,+ assertStagingWorkspace,+} from "@/lib/sandbox/workspace-guards";import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";-import { WorkspaceEnvironment } from "@prisma/client";import { waitUntil } from "@vercel/functions";import { authActionClient } from "../actions/safe-action";import { throwIfNoPermission } from "../actions/throw-if-no-permission";@@ -29,9 +31,9 @@ export const copyDiscountToLiveAction = authActionClientrequiredRoles: ["owner", "member"],});- if (workspace.environment !== WorkspaceEnvironment.staging) {- throw new Error("This action is only available in staging workspaces.");- }+ assertStagingWorkspace(workspace, {+ message: "Discount can only be copied from a staging workspace.",+ });const { program: targetProgram, ...targetGroup } =await prisma.partnerGroup.findUniqueOrThrow({diff --git a/apps/web/lib/sandbox/copy-reward-to-live.ts b/apps/web/lib/sandbox/copy-reward-to-live.tsindex e1288a5a59..1204d5a560 100644--- a/apps/web/lib/sandbox/copy-reward-to-live.ts+++ b/apps/web/lib/sandbox/copy-reward-to-live.ts@@ -1,8 +1,10 @@"use server";import { prisma } from "@/lib/prisma";-import { assertProductionWorkspace } from "@/lib/sandbox/workspace-guards";-import { WorkspaceEnvironment } from "@prisma/client";+import {+ assertProductionWorkspace,+ assertStagingWorkspace,+} from "@/lib/sandbox/workspace-guards";import { waitUntil } from "@vercel/functions";import { authActionClient } from "../actions/safe-action";import { throwIfNoPermission } from "../actions/throw-if-no-permission";@@ -26,9 +28,9 @@ export const copyRewardToLiveAction = authActionClientrequiredRoles: ["owner", "member"],});- if (workspace.environment !== WorkspaceEnvironment.staging) {- throw new Error("This action is only available in staging workspaces.");- }+ assertStagingWorkspace(workspace, {+ message: "Reward can only be copied from a staging workspace.",+ });const { program: targetProgram, ...targetGroup } =await prisma.partnerGroup.findUniqueOrThrow({diff --git a/apps/web/lib/sandbox/workspace-guards.ts b/apps/web/lib/sandbox/workspace-guards.tsindex 57ad23cbc2..4d3027d12c 100644--- a/apps/web/lib/sandbox/workspace-guards.ts+++ b/apps/web/lib/sandbox/workspace-guards.ts@@ -1,6 +1,25 @@import { DubApiError } from "@/lib/api/errors";import { Project, WorkspaceEnvironment } from "@prisma/client";+export function assertStagingWorkspace(+ workspace: Pick<Project, "environment">,+ options?: {+ when?: boolean;+ message?: string;+ },+) {+ const shouldCheck = options?.when ?? true;++ if (shouldCheck && !isStagingEnvironment(workspace.environment)) {+ throw new DubApiError({+ code: "bad_request",+ message:+ options?.message ??+ "This action is only available in staging workspaces.",+ });+ }+}+export function assertNotStagingWorkspace(workspace: Pick<Project, "environment">,options?: {@@ -12,7 +31,7 @@ export function assertNotStagingWorkspace(if (shouldCheck && isStagingEnvironment(workspace.environment)) {throw new DubApiError({- code: "forbidden",+ code: "bad_request",message:options?.message ??"This action is not available in a staging workspace. Use the production workspace instead.",diff --git a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx b/apps/web/ui/layout/sidebar/workspace-dropdown.tsxindex aa95704022..b0f3d74b12 100644--- a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx+++ b/apps/web/ui/layout/sidebar/workspace-dropdown.tsx@@ -10,7 +10,6 @@ import { getUserAvatarUrl } from "@/ui/users/user-avatar";import { BlurImage, Popover, useScrollProgress } from "@dub/ui";import { Check2, Gear, Plus, UserPlus } from "@dub/ui/icons";import { cn, isLegacyBusinessPlan, pluralize } from "@dub/utils";-import { WorkspaceEnvironment } from "@prisma/client";import { useSession } from "next-auth/react";import Link from "next/link";import { useParams, usePathname } from "next/navigation";@@ -247,8 +246,7 @@ function WorkspaceList({<div className="flex flex-col gap-0.5">{workspaces.filter(- (workspace) =>- workspace.environment !== WorkspaceEnvironment.staging,+ (workspace) => !isStagingEnvironment(workspace.environment),).map(({ id, name, slug, logo }) => {const isActive = activeSlug === slug;