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.

fix issue with user profile pictures keep getting deleted

Kiran Ke571c1c5e241
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index be0a1bb69f..72a0ab5fb6 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -1,3 +1,4 @@
+import { DubApiError } from "@/lib/api/errors";
import { withSession } from "@/lib/auth";
import { unsubscribe } from "@/lib/flodesk";
import { prisma } from "@/lib/prisma";
@@ -8,6 +9,14 @@ import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
import { z } from "zod";
+const updateUserSchema = z.object({
+ name: z.preprocess(trim, z.string().min(1).max(64)).optional(),
+ email: z.preprocess(trim, z.string().email()).optional(),
+ image: z.string().url().optional(),
+ source: z.preprocess(trim, z.string().min(1).max(32)).optional(),
+ defaultWorkspace: z.preprocess(trim, z.string().min(1)).optional(),
+});
+
// GET /api/user – get a specific user
export const GET = withSession(async ({ session }) => {
const [user, account] = await Promise.all([
@@ -56,27 +65,38 @@ export const GET = withSession(async ({ session }) => {
});
});
-const updateUserSchema = z.object({
- name: z.preprocess(trim, z.string().min(1).max(64)).optional(),
- email: z.preprocess(trim, z.string().email()).optional(),
- image: z.string().url().optional(),
- source: z.preprocess(trim, z.string().min(1).max(32)).optional(),
- defaultWorkspace: z.preprocess(trim, z.string().min(1)).optional(),
-});
-
// PATCH /api/user – edit a specific user
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}_${nanoid(7)}`,
- image,
- );
- image = url;
+ if (image) {
+ const { url } = await storage.upload(
+ `avatars/${session.user.id}_${nanoid(7)}`,
+ image,
+ );
+ image = url;
+ }
+
+ if (defaultWorkspace) {
+ const workspaceUser = await prisma.projectUsers.findFirst({
+ where: {
+ userId: session.user.id,
+ project: {
+ slug: defaultWorkspace,
+ },
+ },
+ });
+
+ if (!workspaceUser) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: `You don't have access to the workspace ${defaultWorkspace}.`,
+ });
}
+ }
+
+ try {
const response = await prisma.user.update({
where: {
id: session.user.id,
@@ -92,7 +112,9 @@ export const PATCH = withSession(async ({ req, session }) => {
waitUntil(
(async () => {
+ // Delete only if a new image is uploaded and the old image exists
if (
+ image &&
session.user.image &&
session.user.image.startsWith(`${R2_URL}/avatars/${session.user.id}`)
) {
@@ -103,19 +125,10 @@ export const PATCH = withSession(async ({ req, session }) => {
return NextResponse.json(response);
} catch (error) {
- if (error.code === "P2002") {
- // return res.status(422).end("Email is already in use.");
- return NextResponse.json(
- {
- error: {
- code: "conflict",
- message: "Email is already in use.",
- },
- },
- { status: 422 },
- );
- }
- return NextResponse.json({ error }, { status: 500 });
+ throw new DubApiError({
+ code: "conflict",
+ message: "Email is already in use.",
+ });
}
});
diff --git a/apps/web/ui/account/update-default-workspace.tsx b/apps/web/ui/account/update-default-workspace.tsx
index 60c235dc8b..1fe3534ade 100644
--- a/apps/web/ui/account/update-default-workspace.tsx
+++ b/apps/web/ui/account/update-default-workspace.tsx
@@ -50,14 +50,15 @@ export default function UpdateDefaultWorkspace() {
defaultWorkspace: selectedWorkspace?.id || undefined,
}),
});
+
if (response.ok) {
setSaving(false);
update();
} else {
+ setSaving(false);
const { error } = await response.json();
throw new Error(error.message);
}
- setSaving(false);
}
return (
@@ -69,7 +70,6 @@ export default function UpdateDefaultWorkspace() {
success: "Successfully updated your default workspace!",
error: (error) => error,
});
- setSaving(false);
}}
className="rounded-lg border border-gray-200 bg-white"
>