Files
zeekayandHanzo Dev 2c4319f61e storage: one object store, ours — and the gate that shut it off is gone
`getS3ClientAndConfig` threw "Invalid upload transport" unless
NEXT_PUBLIC_UPLOAD_TRANSPORT was "s3". The deployment does not set that variable
— it configures storage through NEXT_PRIVATE_UPLOAD_* (transport s3, bucket
dataroom, endpoint http://s3:9000, credentials) — so every call through that
helper threw in production while a correctly configured bucket sat beside it.
The two put paths keyed on the same unset variable and fell to `.otherwise()`,
returning { type: null, data: null }: a server-side upload reported success and
stored nothing.

There is one object store now, so nothing chooses a transport. The VERCEL_BLOB
arms are gone from get/put/copy/delete, CSV exports write through putFileServer,
and branding + expired-export deletion go through deleteFile.

Safe to remove rather than migrate: the database has no rows to orphan —
Document, DocumentVersion, Team and User are all 0, so nothing is stored under a
VERCEL_BLOB path.

The cleanup queue now carries teamId, which deleteFile needs to resolve the
storage region, and its item is a `path` rather than a `blobUrl` — the scheduler
was testing `startsWith("https://")` to decide whether to enqueue, which a
storage path never matches.

Verified with npx tsc --noEmit: 100 -> 99. The one fixed is get-file.ts, whose
ts-pattern match is no longer there to be uncallable. Nothing introduced: the
two duplicate.ts files that looked new carry identical error counts before and
after and were never touched.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-09 11:23:32 -07:00

186 lines
4.7 KiB
TypeScript

import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
import { kv } from "@/lib/kv";
import { CustomUser } from "@/lib/types";
import { authOptions } from "../../auth/[...nextauth]";
import { deleteFile } from "@/lib/files/delete-file-server";
import { DocumentStorageType } from "@prisma/client";
export default async function handle(
req: NextApiRequest,
res: NextApiResponse,
) {
const session = await getServerSession(req, res, authOptions);
if (!session) {
return res.status(401).end("Unauthorized");
}
const { teamId } = req.query as { teamId: string };
try {
const team = await prisma.team.findUnique({
where: {
id: teamId,
},
select: {
id: true,
users: { select: { userId: true } },
},
});
// check that the user is member of the team, otherwise return 403
const teamUsers = team?.users;
const isUserPartOfTeam = teamUsers?.some(
(user) => user.userId === (session.user as CustomUser).id,
);
if (!isUserPartOfTeam) {
return res.status(403).end("Unauthorized to access this team");
}
} catch (error) {
errorhandler(error, res);
}
if (req.method === "GET") {
// GET /api/teams/:teamId/branding
const brand = await prisma.brand.findUnique({
where: {
teamId: teamId,
},
});
if (!brand) {
return res.status(200).json(null);
}
return res.status(200).json(brand);
} else if (req.method === "POST") {
// POST /api/teams/:teamId/branding
const {
logo,
banner,
brandColor,
accentColor,
applyAccentColorToDataroomView,
welcomeMessage,
} = req.body as {
logo?: string;
banner?: string;
brandColor?: string;
accentColor?: string;
applyAccentColorToDataroomView?: boolean;
welcomeMessage?: string;
};
// update team with new branding
const brand = await prisma.brand.create({
data: {
logo: logo,
banner,
brandColor,
accentColor,
applyAccentColorToDataroomView: !!applyAccentColorToDataroomView,
welcomeMessage,
teamId: teamId,
},
});
// Cache the logo URL in KV if logo exists
if (logo) {
await kv.set(`brand:logo:${teamId}`, logo);
}
return res.status(200).json(brand);
} else if (req.method === "PUT") {
// PUT /api/teams/:teamId/branding
const {
logo,
banner,
brandColor,
accentColor,
applyAccentColorToDataroomView,
welcomeMessage,
} = req.body as {
logo?: string;
banner?: string;
brandColor?: string;
accentColor?: string;
applyAccentColorToDataroomView?: boolean;
welcomeMessage?: string;
};
// Use upsert to handle both create and update cases
const brand = await prisma.brand.upsert({
where: {
teamId: teamId,
},
create: {
logo,
banner,
brandColor,
accentColor,
applyAccentColorToDataroomView: !!applyAccentColorToDataroomView,
welcomeMessage,
teamId: teamId,
},
update: {
logo,
banner,
brandColor,
accentColor,
applyAccentColorToDataroomView: !!applyAccentColorToDataroomView,
welcomeMessage,
},
});
// Update logo in KV cache
if (logo) {
await kv.set(`brand:logo:${teamId}`, logo);
} else {
// If logo is null or undefined, delete the cache
await kv.del(`brand:logo:${teamId}`);
}
return res.status(200).json(brand);
} else if (req.method === "DELETE") {
// DELETE /api/teams/:teamId/branding
const brand = await prisma.brand.findFirst({
where: {
teamId: teamId,
},
select: { id: true, logo: true, banner: true },
});
if (brand) {
// remove the logo from our object storage
if (brand.logo) {
await deleteFile({ type: DocumentStorageType.S3_PATH, data: brand.logo, teamId });
}
// remove the banner from our object storage
if (brand.banner) {
await deleteFile({ type: DocumentStorageType.S3_PATH, data: brand.banner, teamId });
}
}
// delete the branding from database
await prisma.brand.delete({
where: {
id: brand?.id,
},
});
// Remove logo from KV cache
await kv.del(`brand:logo:${teamId}`);
return res.status(204).end();
} else {
// We only allow GET and DELETE requests
res.setHeader("Allow", ["GET", "POST", "PUT", "DELETE"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
}