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.

update defaultWorkspace

Steven Tey9954f29573d1
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index 6e67eef08b..f41df1af4a 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -42,6 +42,7 @@ const updateUserSchema = z.object({
export const PATCH = withSession(async ({ req, session }) => {
let { name, email, image, source, defaultWorkspace } =
await updateUserSchema.parseAsync(await req.json());
+
try {
if (image) {
const { url } = await storage.upload(`avatars/${session.user.id}`, image);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/invites/accept/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/invites/accept/route.ts
index 45afa7d1e1..e36d3081e9 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/invites/accept/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/invites/accept/route.ts
@@ -19,6 +19,7 @@ export const POST = withSession(async ({ session, params }) => {
project: {
select: {
id: true,
+ slug: true,
plan: true,
usersLimit: true,
_count: {
@@ -69,6 +70,15 @@ export const POST = withSession(async ({ session, params }) => {
},
},
}),
+ session.user["defaultWorkspace"] === null &&
+ prisma.user.update({
+ where: {
+ id: session.user.id,
+ },
+ data: {
+ defaultWorkspace: workspace.slug,
+ },
+ }),
]);
return NextResponse.json(response);
});
diff --git a/apps/web/app/api/workspaces/route.ts b/apps/web/app/api/workspaces/route.ts
index d003acb079..ce7f791103 100644
--- a/apps/web/app/api/workspaces/route.ts
+++ b/apps/web/app/api/workspaces/route.ts
@@ -149,9 +149,19 @@ export const POST = withSession(async ({ req, session }) => {
},
});
- if (domain) {
- waitUntil(
- (async () => {
+ waitUntil(
+ (async () => {
+ if (session.user["defaultWorkspace"] === null) {
+ await prisma.user.update({
+ where: {
+ id: session.user.id,
+ },
+ data: {
+ defaultWorkspace: projectResponse.slug,
+ },
+ });
+ }
+ if (domain) {
const domainRepsonse = await addDomainToVercel(domain);
if (domainRepsonse.error) {
await prisma.domain.delete({
@@ -168,9 +178,9 @@ export const POST = withSession(async ({ req, session }) => {
projectId: projectResponse.id,
});
}
- })(),
- );
- }
+ }
+ })(),
+ );
const response = {
...projectResponse,
diff --git a/apps/web/app/app.dub.co/(dashboard)/settings/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/settings/page-client.tsx
index c72747b9b0..872359a769 100644
--- a/apps/web/app/app.dub.co/(dashboard)/settings/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/settings/page-client.tsx
@@ -1,7 +1,7 @@
"use client";
import DeleteAccountSection from "@/ui/account/delete-account";
-import UpdateDefaultWorkspace from "@/ui/account/update-default-workspace-";
+import UpdateDefaultWorkspace from "@/ui/account/update-default-workspace";
import UploadAvatar from "@/ui/account/upload-avatar";
import { Form } from "@dub/ui";
import { APP_NAME } from "@dub/utils";
diff --git a/apps/web/scripts/update-default-workspace.ts b/apps/web/scripts/update-default-workspace.ts
deleted file mode 100644
index 527f6cae02..0000000000
--- a/apps/web/scripts/update-default-workspace.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-import { prisma } from "@/lib/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- // get first project for each user and set it as default workspace
- const res = await prisma.projectUsers.findMany({
- select: {
- userId: true,
- project: {
- select: {
- slug: true,
- },
- },
- },
- distinct: ["userId"],
- orderBy: {
- createdAt: "asc",
- },
- take: 100,
- });
-
- console.table(res);
-}
-
-main();
diff --git a/apps/web/ui/account/update-default-workspace-.tsx b/apps/web/ui/account/update-default-workspace.tsx
similarity index 100%
rename from apps/web/ui/account/update-default-workspace-.tsx
rename to apps/web/ui/account/update-default-workspace.tsx
diff --git a/apps/web/ui/modals/provider.tsx b/apps/web/ui/modals/provider.tsx
index f5892867f8..e2f1bbef0d 100644
--- a/apps/web/ui/modals/provider.tsx
+++ b/apps/web/ui/modals/provider.tsx
@@ -152,7 +152,15 @@ export default function ModalProvider({ children }: { children: ReactNode }) {
session?.user &&
!session.user["defaultWorkspace"]
) {
- update();
+ fetch("/api/user", {
+ method: "PATCH",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ defaultWorkspace: workspaces[0].slug,
+ }),
+ }).then(() => update());
}
}, [session]);