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 workspace setup and reward copy-to-live

Kiran Kb96e322bbfb2
diff --git a/apps/web/lib/sandbox/copy-reward-to-live.ts b/apps/web/lib/sandbox/copy-reward-to-live.ts
index b77dec1dab..9d9cdfdfc2 100644
--- a/apps/web/lib/sandbox/copy-reward-to-live.ts
+++ b/apps/web/lib/sandbox/copy-reward-to-live.ts
@@ -88,10 +88,6 @@ export const copyRewardToLiveAction = authActionClient
const rewardIdColumn = REWARD_EVENT_COLUMN_MAPPING[reward.event];
- if (targetGroup[rewardIdColumn]) {
- throw new Error(`The target group already has a ${reward.event} reward.`);
- }
-
const newReward = await prisma.$transaction(async (tx) => {
const newReward = await tx.reward.create({
data: {
@@ -109,15 +105,23 @@ export const copyRewardToLiveAction = authActionClient
},
});
- await tx.partnerGroup.update({
+ const { count } = await tx.partnerGroup.updateMany({
where: {
id: targetGroupId,
+ [rewardIdColumn]: null,
},
data: {
[rewardIdColumn]: newReward.id,
},
});
+ // This will revert the transaction if the target group already has a reward
+ if (count === 0) {
+ throw new Error(
+ `The target group already has a ${reward.event} reward.`,
+ );
+ }
+
await tx.programEnrollment.updateMany({
where: {
groupId: targetGroupId,
diff --git a/apps/web/lib/sandbox/create-sandbox-workspace.ts b/apps/web/lib/sandbox/create-sandbox-workspace.ts
index 08ae382e86..e2bfc30315 100644
--- a/apps/web/lib/sandbox/create-sandbox-workspace.ts
+++ b/apps/web/lib/sandbox/create-sandbox-workspace.ts
@@ -1,6 +1,6 @@
import { PlanProps } from "@/lib/types";
import { prisma } from "@dub/prisma";
-import { WorkspaceEnvironment, WorkspaceRole } from "@dub/prisma/client";
+import { User, WorkspaceEnvironment, WorkspaceRole } from "@dub/prisma/client";
import { nanoid, TRIAL_LIMITS } from "@dub/utils";
import { generateRandomString } from "../api/utils/generate-random-string";
import { createWorkspaceId } from "../api/workspaces/create-workspace-id";
@@ -21,6 +21,29 @@ export async function createSandboxWorkspace({
workspace,
users,
}: CreateSandboxWorkspaceProps) {
+ let usersFound: Pick<User, "id" | "email">[] = [];
+
+ if (users.length > 0) {
+ usersFound = await prisma.user.findMany({
+ where: {
+ email: {
+ in: users.map((user) => user.email),
+ },
+ },
+ select: {
+ id: true,
+ email: true,
+ },
+ });
+
+ if (usersFound.length === 0) {
+ console.error(
+ "No users found. Please ensure the users exist in the database.",
+ );
+ return;
+ }
+ }
+
const workspaceId = createWorkspaceId();
await prisma.project.create({
@@ -51,50 +74,35 @@ export async function createSandboxWorkspace({
},
});
- if (users.length > 0) {
- const usersFound = await prisma.user.findMany({
+ const usersWithRoles = usersFound.map((user) => ({
+ id: user.id,
+ role: users.find((u) => u.email === user.email)?.role ?? "member",
+ }));
+
+ await prisma.$transaction(async (tx) => {
+ await tx.projectUsers.createMany({
+ skipDuplicates: true,
+ data: usersWithRoles.map((user) => ({
+ projectId: workspaceId,
+ userId: user.id,
+ role: user.role,
+ })),
+ });
+
+ const workspaceUsers = await tx.projectUsers.findMany({
where: {
- email: {
- in: users.map((user) => user.email),
- },
+ projectId: workspaceId,
+ },
+ select: {
+ id: true,
},
});
- if (usersFound.length === 0) {
- console.error("No users found.");
- return;
- }
-
- const usersWithRoles = usersFound.map((user) => ({
- id: user.id,
- role: users.find((u) => u.email === user.email)?.role ?? "member",
- }));
-
- await prisma.$transaction(async (tx) => {
- await tx.projectUsers.createMany({
- skipDuplicates: true,
- data: usersWithRoles.map((user) => ({
- projectId: workspaceId,
- userId: user.id,
- role: user.role,
- })),
- });
-
- const workspaceUsers = await tx.projectUsers.findMany({
- where: {
- projectId: workspaceId,
- },
- select: {
- id: true,
- },
- });
-
- await tx.notificationPreference.createMany({
- skipDuplicates: true,
- data: workspaceUsers.map((user) => ({
- projectUserId: user.id,
- })),
- });
+ await tx.notificationPreference.createMany({
+ skipDuplicates: true,
+ data: workspaceUsers.map((user) => ({
+ projectUserId: user.id,
+ })),
});
- }
+ });
}
diff --git a/apps/web/lib/sandbox/create-staging-workspace.ts b/apps/web/lib/sandbox/create-staging-workspace.ts
index 64f303e82a..8d99664ff4 100644
--- a/apps/web/lib/sandbox/create-staging-workspace.ts
+++ b/apps/web/lib/sandbox/create-staging-workspace.ts
@@ -22,12 +22,19 @@ export async function createStagingWorkspace(workspaceId: string) {
if (!workspace) {
console.error(
- "Failed to find workspace in createStagingWorkspace",
+ "Failed to find workspace in createStagingWorkspace.",
workspaceId,
);
return;
}
+ if (workspace.environment !== WorkspaceEnvironment.production) {
+ console.error(
+ `Skipping staging creation for non-production workspace ${workspace.id}.`,
+ );
+ return;
+ }
+
if (workspace.stagingWorkspaceId) {
console.log(
`Staging workspace already exists for the workspace ${workspace.id}.`,
diff --git a/apps/web/scripts/migrations/backfill-staging-workspaces.ts b/apps/web/scripts/migrations/backfill-staging-workspaces.ts
index e0e55ddf6a..7ed093ec3b 100644
--- a/apps/web/scripts/migrations/backfill-staging-workspaces.ts
+++ b/apps/web/scripts/migrations/backfill-staging-workspaces.ts
@@ -22,6 +22,11 @@ async function main() {
plan: {
notIn: ["free", "pro"],
},
+ slug: {
+ not: {
+ endsWith: "-staging",
+ },
+ },
},
select: {
id: true,