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.

consolidate workspace logo route with workspace PATCH

Steven Teyb8db4dac6f10
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/logo/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/logo/route.ts
deleted file mode 100644
index 0e16f43efb..0000000000
--- a/apps/web/app/api/workspaces/[idOrSlug]/logo/route.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-import { withWorkspace } from "@/lib/auth";
-import { prisma } from "@/lib/prisma";
-import { storage } from "@/lib/storage";
-import z from "@/lib/zod";
-import { R2_URL, nanoid } from "@dub/utils";
-import { waitUntil } from "@vercel/functions";
-import { NextResponse } from "next/server";
-
-const uploadLogoSchema = z.object({
- image: z.string().url(),
-});
-
-// POST /api/workspaces/[idOrSlug]/logo – upload a new workspace logo
-export const POST = withWorkspace(
- async ({ req, workspace }) => {
- const { image } = uploadLogoSchema.parse(await req.json());
-
- const { url } = await storage.upload(
- `logos/${workspace.id}_${nanoid(7)}`,
- image,
- );
-
- const response = await prisma.project.update({
- where: { id: workspace.id },
- data: { logo: url },
- });
-
- waitUntil(
- (async () => {
- if (
- workspace.logo &&
- workspace.logo.startsWith(`${R2_URL}/logos/${workspace.id}`)
- ) {
- await storage.delete(workspace.logo.replace(`${R2_URL}/`, ""));
- }
- })(),
- );
-
- return NextResponse.json(response);
- },
- {
- requiredPermissions: ["workspaces.write"],
- },
-);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
index ed8f5f9d70..0f367b8a6e 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
@@ -1,13 +1,14 @@
import { DubApiError } from "@/lib/api/errors";
import { deleteWorkspace } from "@/lib/api/workspaces";
import { withWorkspace } from "@/lib/auth";
-import { dub } from "@/lib/dub";
import { getFeatureFlags } from "@/lib/edge-config";
import { prisma } from "@/lib/prisma";
+import { storage } from "@/lib/storage";
import {
- WorkspaceSchema,
updateWorkspaceSchema,
+ WorkspaceSchema,
} from "@/lib/zod/schemas/workspaces";
+import { nanoid, R2_URL } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -44,10 +45,18 @@ export const GET = withWorkspace(
// PATCH /api/workspaces/[idOrSlug] – update a specific workspace by id or slug
export const PATCH = withWorkspace(
async ({ req, workspace }) => {
- const { name, slug } = await updateWorkspaceSchema.parseAsync(
+ const { name, slug, logo } = await updateWorkspaceSchema.parseAsync(
await req.json(),
);
+ const logoUploaded =
+ logo && workspace.plan !== "free"
+ ? await storage.upload(
+ `workspaces/ws_${workspace.id}/logo_${nanoid(7)}`,
+ logo,
+ )
+ : null;
+
try {
const response = await prisma.project.update({
where: {
@@ -56,6 +65,7 @@ export const PATCH = withWorkspace(
data: {
...(name && { name }),
...(slug && { slug }),
+ ...(logoUploaded && { logo: logoUploaded.url }),
},
include: {
domains: true,
@@ -72,17 +82,16 @@ export const PATCH = withWorkspace(
defaultWorkspace: slug,
},
});
- // Update the workspace's referral link to use the new slug
- if (workspace.referralLinkId) {
- waitUntil(
- dub.links.update(workspace.referralLinkId, {
- key: slug,
- identifier: slug,
- }),
- );
- }
}
+ waitUntil(
+ (async () => {
+ if (workspace.logo) {
+ await storage.delete(workspace.logo.replace(`${R2_URL}/`, ""));
+ }
+ })(),
+ );
+
return NextResponse.json(
WorkspaceSchema.parse({
...response,
diff --git a/apps/web/lib/zod/schemas/workspaces.ts b/apps/web/lib/zod/schemas/workspaces.ts
index 103f4b778b..4a9646d2db 100644
--- a/apps/web/lib/zod/schemas/workspaces.ts
+++ b/apps/web/lib/zod/schemas/workspaces.ts
@@ -1,10 +1,6 @@
import { isReservedKey } from "@/lib/edge-config";
import z from "@/lib/zod";
-import {
- DEFAULT_REDIRECTS,
- validDomainRegex,
- validSlugRegex,
-} from "@dub/utils";
+import { DEFAULT_REDIRECTS, validSlugRegex } from "@dub/utils";
import slugify from "@sindresorhus/slugify";
import { DomainSchema } from "./domains";
import { planSchema, roleSchema } from "./misc";
@@ -151,17 +147,7 @@ export const createWorkspaceSchema = z.object({
.refine(async (v) => !((await isReservedKey(v)) || DEFAULT_REDIRECTS[v]), {
message: "Cannot use reserved slugs",
}),
- domain: z
- .string()
- .refine((v) => validDomainRegex.test(v), {
- message: "Invalid domain format",
- })
- .optional(),
+ logo: z.string().optional(),
});
-export const updateWorkspaceSchema = createWorkspaceSchema
- .pick({
- name: true,
- slug: true,
- })
- .partial();
+export const updateWorkspaceSchema = createWorkspaceSchema.partial();
diff --git a/apps/web/ui/workspaces/upload-logo.tsx b/apps/web/ui/workspaces/upload-logo.tsx
index 8015d5ece9..3af9901141 100644
--- a/apps/web/ui/workspaces/upload-logo.tsx
+++ b/apps/web/ui/workspaces/upload-logo.tsx
@@ -22,12 +22,12 @@ export default function UploadLogo() {
onSubmit={async (e) => {
setUploading(true);
e.preventDefault();
- fetch(`/api/workspaces/${id}/logo`, {
- method: "POST",
+ fetch(`/api/workspaces/${id}`, {
+ method: "PATCH",
headers: {
"Content-Type": "application/json",
},
- body: JSON.stringify({ image }),
+ body: JSON.stringify({ logo: image }),
}).then(async (res) => {
if (res.status === 200) {
await Promise.all([