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.

Harden staging sandbox creation, invites, and workspace deletion

Kiran Kaaab1bcab3d2
diff --git a/apps/web/app/(ee)/api/cron/workspaces/delete/delete-workspace.ts b/apps/web/app/(ee)/api/cron/workspaces/delete/delete-workspace.ts
index 8bb79c0b6f..89cbe590c2 100644
--- a/apps/web/app/(ee)/api/cron/workspaces/delete/delete-workspace.ts
+++ b/apps/web/app/(ee)/api/cron/workspaces/delete/delete-workspace.ts
@@ -42,11 +42,18 @@ export async function deleteWorkspace(payload: DeleteWorkspacePayload) {
}
// Delete workspace logo if it's a custom logo stored in R2
- if (
- workspace.logo &&
- workspace.logo.startsWith(`${R2_URL}/logos/${workspace.id}`)
- ) {
- await storage.delete({ key: workspace.logo.replace(`${R2_URL}/`, "") });
+ try {
+ if (
+ workspace.logo &&
+ workspace.logo.startsWith(`${R2_URL}/logos/${workspace.id}`)
+ ) {
+ await storage.delete({ key: workspace.logo.replace(`${R2_URL}/`, "") });
+ }
+ } catch (error) {
+ console.error(
+ `Failed to delete logo for workspace ${workspace.id}. Continuing deletion.`,
+ error,
+ );
}
await prisma.project.delete({
diff --git a/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx b/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx
index 5f7a5ecc82..3f1abe7edd 100644
--- a/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx
+++ b/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx
@@ -118,15 +118,15 @@ async function VerifyInvite({ code }: { code: string }) {
});
}
- after(
- addMemberToStaging({
+ after(async () => {
+ await addMemberToStaging({
workspace,
user: {
id: workspaceUser.userId,
role: workspaceUser.role,
},
- }),
- );
+ });
+ });
// Complete onboarding just in case
await onboardingStepCache.set({
diff --git a/apps/web/lib/sandbox/create-staging-workspace.ts b/apps/web/lib/sandbox/create-staging-workspace.ts
index 8d99664ff4..acf45eb127 100644
--- a/apps/web/lib/sandbox/create-staging-workspace.ts
+++ b/apps/web/lib/sandbox/create-staging-workspace.ts
@@ -53,8 +53,8 @@ export async function createStagingWorkspace(workspaceId: string) {
const stagingWorkspaceId = createWorkspaceId();
- await prisma.$transaction([
- prisma.project.create({
+ await prisma.$transaction(async (tx) => {
+ await tx.project.create({
data: {
id: stagingWorkspaceId,
name: `${workspace.name} (Staging)`,
@@ -80,17 +80,24 @@ export async function createStagingWorkspace(workspaceId: string) {
create: {},
},
},
- }),
+ });
- prisma.project.update({
+ const { count } = await tx.project.updateMany({
where: {
id: workspace.id,
+ stagingWorkspaceId: null,
},
data: {
stagingWorkspaceId,
},
- }),
- ]);
+ });
+
+ if (count === 0) {
+ throw new Error(
+ `Staging workspace already exist for the workspace ${workspace.id}`,
+ );
+ }
+ });
// Copy the users to the staging workspace
if (workspace.users.length > 0) {
diff --git a/apps/web/lib/sandbox/schemas.ts b/apps/web/lib/sandbox/schemas.ts
index a5c4d867a2..82869baf0c 100644
--- a/apps/web/lib/sandbox/schemas.ts
+++ b/apps/web/lib/sandbox/schemas.ts
@@ -1,13 +1,13 @@
import * as z from "zod/v4";
export const copyRewardToLiveSchema = z.object({
- workspaceId: z.string(),
- rewardId: z.string(),
- targetGroupId: z.string(),
+ workspaceId: z.string().min(1),
+ rewardId: z.string().min(1),
+ targetGroupId: z.string().min(1),
});
export const copyDiscountToLiveSchema = z.object({
- workspaceId: z.string(),
- discountId: z.string(),
- targetGroupId: z.string(),
+ workspaceId: z.string().min(1),
+ discountId: z.string().min(1),
+ targetGroupId: z.string().min(1),
});