The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refine staging member sync and block direct member removal in staging
Kiran K3aa8e28c081d
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/users/route.tsindex 7c160a9980..ceda54309f 100644--- a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts+++ b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts@@ -12,7 +12,7 @@ import {workspaceUserSchema,} from "@/lib/zod/schemas/workspaces";import { prisma } from "@dub/prisma";-import { WorkspaceRole } from "@dub/prisma/client";+import { WorkspaceEnvironment, WorkspaceRole } from "@dub/prisma/client";import { waitUntil } from "@vercel/functions";import { NextResponse } from "next/server";import * as z from "zod/v4";@@ -133,6 +133,7 @@ export const DELETE = withWorkspace(role: true,user: {select: {+ id: true,isMachine: true,defaultWorkspace: true,},@@ -155,6 +156,17 @@ export const DELETE = withWorkspace(});}+ if (+ workspace.environment === WorkspaceEnvironment.staging &&+ !projectUser.user.isMachine+ ) {+ throw new DubApiError({+ code: "forbidden",+ message:+ "This action is not available in a staging workspace. Use the live workspace instead.",+ });+ }+// If there is only one owner and the user is an owner and the user is trying to remove themselvesif (totalOwners === 1 &&@@ -168,7 +180,7 @@ export const DELETE = withWorkspace(});}- const [response] = await Promise.allSettled([+ await Promise.allSettled([// Remove the user from the workspaceprisma.projectUsers.delete({where: {@@ -202,9 +214,7 @@ export const DELETE = withWorkspace(waitUntil(removeMemberFromStaging({workspace,- user: {- id: userId,- },+ user: projectUser.user,}),);@@ -217,9 +227,6 @@ export const DELETE = withWorkspace(});}- return NextResponse.json(response);- },- {- rejectStagingWorkspace: true,+ return NextResponse.json({});},);diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.tsindex e327a204de..dd57738c50 100644--- a/apps/web/lib/auth/workspace.ts+++ b/apps/web/lib/auth/workspace.ts@@ -467,7 +467,7 @@ export const withWorkspace = (workspace.environment === WorkspaceEnvironment.staging) {throw new DubApiError({- code: "bad_request",+ code: "forbidden",message:"This action is not available in a staging workspace. Use the live workspace instead.",});diff --git a/apps/web/lib/sandbox/sync-member-to-staging.ts b/apps/web/lib/sandbox/sync-member-to-staging.tsindex 0a5a856f40..ef66af91fd 100644--- a/apps/web/lib/sandbox/sync-member-to-staging.ts+++ b/apps/web/lib/sandbox/sync-member-to-staging.ts@@ -1,97 +1,93 @@import { prisma } from "@dub/prisma";-import { WorkspaceRole } from "@dub/prisma/client";+import { Project, WorkspaceRole } from "@dub/prisma/client";++// We don't sync the machine user from the live workspace to the staging workspaceexport async function addMemberToStaging({- liveWorkspaceId,- userId,- role,+ workspace,+ user,}: {- liveWorkspaceId: string;- userId: string;- role: WorkspaceRole;+ workspace: Pick<Project, "stagingWorkspaceId">;+ user: { id: string; role: WorkspaceRole };}) {- const liveWorkspace = await prisma.project.findUnique({- where: {- id: liveWorkspaceId,- },- select: {- stagingWorkspaceId: true,- },- });-- if (!liveWorkspace?.stagingWorkspaceId) return;+ if (!workspace.stagingWorkspaceId) {+ return;+ }try {await prisma.projectUsers.create({data: {- projectId: liveWorkspace.stagingWorkspaceId,- userId,- role,+ projectId: workspace.stagingWorkspaceId,+ userId: user.id,+ role: user.role,notificationPreference: {create: {},},},});- } catch (e) {- // P2002: unique constraint violation — user already exists in staging- if ((e as any)?.code === "P2002") return;- throw e;+ } catch (error) {+ console.error("[addMemberToStaging]", error);}}-export async function removeMemberFromStaging({- liveWorkspaceId,- userId,+export async function updateMemberRoleInStaging({+ workspace,+ user,}: {- liveWorkspaceId: string;- userId: string;+ workspace: Pick<Project, "stagingWorkspaceId">;+ user: { id: string; role: WorkspaceRole };}) {- const liveWorkspace = await prisma.project.findUnique({- where: {- id: liveWorkspaceId,- },- select: {- stagingWorkspaceId: true,- },- });-- if (!liveWorkspace?.stagingWorkspaceId) return;+ if (!workspace.stagingWorkspaceId) {+ return;+ }- await prisma.projectUsers.deleteMany({- where: {- projectId: liveWorkspace.stagingWorkspaceId,- userId,- },- });+ try {+ await prisma.projectUsers.update({+ where: {+ userId_projectId: {+ userId: user.id,+ projectId: workspace.stagingWorkspaceId,+ },+ },+ data: {+ role: user.role,+ },+ });+ } catch (error) {+ console.error("[updateMemberRoleInStaging]", error);+ }}-export async function updateMemberRoleInStaging({- liveWorkspaceId,- userId,- role,+export async function removeMemberFromStaging({+ workspace,+ user,}: {- liveWorkspaceId: string;- userId: string;- role: WorkspaceRole;+ workspace: Pick<Project, "stagingWorkspaceId">;+ user: { id: string; isMachine: boolean };}) {- const liveWorkspace = await prisma.project.findUnique({- where: {- id: liveWorkspaceId,- },- select: {- stagingWorkspaceId: true,- },- });+ if (!workspace.stagingWorkspaceId || user.isMachine) {+ return;+ }- if (!liveWorkspace?.stagingWorkspaceId) return;+ try {+ await Promise.allSettled([+ prisma.projectUsers.delete({+ where: {+ userId_projectId: {+ userId: user.id,+ projectId: workspace.stagingWorkspaceId,+ },+ },+ }),- await prisma.projectUsers.updateMany({- where: {- projectId: liveWorkspace.stagingWorkspaceId,- userId,- },- data: {- role,- },- });+ prisma.restrictedToken.deleteMany({+ where: {+ projectId: workspace.stagingWorkspaceId,+ userId: user.id,+ },+ }),+ ]);+ } catch (error) {+ console.error("[removeMemberFromStaging]", error);+ }}diff --git a/apps/web/scripts/dev/seed-installation.ts b/apps/web/scripts/dev/seed-installation.tsnew file mode 100644index 0000000000..a40d5f2ca7--- /dev/null+++ b/apps/web/scripts/dev/seed-installation.ts@@ -0,0 +1,30 @@+import { prisma } from "@dub/prisma";+import { STRIPE_INTEGRATION_ID } from "@dub/utils";+import "dotenv-flow/config";++async function main() {+ await prisma.installedIntegration.upsert({+ where: {+ userId_integrationId_projectId: {+ userId: "user_cludszk1h0000wmd2e0ea2b0p",+ integrationId: STRIPE_INTEGRATION_ID,+ projectId: "ws_1KETZ919F83ZJH6A80HWEHW6E",+ },+ },+ update: {+ settings: {+ stripeMode: "test",+ },+ },+ create: {+ userId: "user_cludszk1h0000wmd2e0ea2b0p",+ integrationId: STRIPE_INTEGRATION_ID,+ projectId: "ws_1KETZ919F83ZJH6A80HWEHW6E",+ settings: {+ stripeMode: "test",+ },+ },+ });+}++main();