mirror of
https://github.com/hanzo-apps/dataroom.git
synced 2026-11-02 22:04:20 +00:00
The client was already @hanzo/kv; only the vocabulary was still Redis. Every module, symbol and comment now says what it connects to: lib/redis.ts -> lib/kv.ts (exports kv, lockKv) lib/redis-job-store.ts -> lib/kv-job-store.ts (KvJobStore) lib/redis-download-job-store.ts -> lib/kv-download-job-store.ts lib/files/tus-redis-locker.ts -> lib/files/tus-kv-locker.ts (KvLocker, KvLock) lib/api/domains/redis.ts -> lib/api/domains/kv.ts (getKey) lib/redis/ -> lib/kv-queue/ KV_URL is now the only endpoint variable — the REDIS_URL fallback and the redis://localhost default are gone, and the default scheme is kv://, which @hanzo/kv parses natively (utils/index.js). .env.example gained the KV_URL it never documented, and lost UPSTASH_REDIS_REST_LOCKER_URL/TOKEN: nothing has read those since the client changed — the locker opens a second connection to the same KV, so there is no second endpoint to configure. The second connection is `lockKv`, not `locker`: both tus routes already have a local `const locker = new KvLocker(...)`, so naming the client `locker` made the lock take itself as its own client. Verified against the repo's own gate (npx tsc --noEmit): 100 errors before and after, 0 introduced, compared set-wise by file:line:code. Behaviour is untouched — this is naming, one connection string, and dead config. Co-authored-by: Hanzo Dev <[email protected]>
114 lines
3.2 KiB
TypeScript
114 lines
3.2 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { authOptions } from "@/pages/api/auth/[...nextauth]";
|
|
import { getServerSession } from "next-auth";
|
|
|
|
import { generateFreshPresignedUrl } from "@/lib/files/bulk-download-presign";
|
|
import prisma from "@/lib/prisma";
|
|
import {
|
|
type DownloadJob,
|
|
downloadJobStore,
|
|
} from "@/lib/kv-download-job-store";
|
|
import { CustomUser } from "@/lib/types";
|
|
|
|
export default async function handler(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
) {
|
|
const session = await getServerSession(req, res, authOptions);
|
|
if (!session) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
const { teamId, id: dataroomId } = req.query as {
|
|
teamId: string;
|
|
id: string;
|
|
};
|
|
|
|
const userId = (session.user as CustomUser).id;
|
|
|
|
if (req.method === "GET") {
|
|
try {
|
|
// Verify team access
|
|
const teamAccess = await prisma.userTeam.findUnique({
|
|
where: {
|
|
userId_teamId: {
|
|
userId: userId,
|
|
teamId: teamId,
|
|
},
|
|
},
|
|
select: { teamId: true },
|
|
});
|
|
|
|
if (!teamAccess) {
|
|
return res.status(403).end("Unauthorized to access this team");
|
|
}
|
|
|
|
// Get download jobs for this dataroom
|
|
const jobs = await downloadJobStore.getDataroomJobs(
|
|
dataroomId,
|
|
teamId,
|
|
10,
|
|
);
|
|
|
|
// Filter to only show relevant jobs for this user
|
|
const userJobs = jobs.filter((job) => {
|
|
// Show user's own jobs
|
|
if (job.userId === userId) {
|
|
// Always show pending/processing jobs
|
|
if (job.status === "PENDING" || job.status === "PROCESSING") {
|
|
return true;
|
|
}
|
|
|
|
// Show completed jobs that haven't expired
|
|
if (job.status === "COMPLETED" && job.expiresAt) {
|
|
return new Date(job.expiresAt) > new Date();
|
|
}
|
|
|
|
// Show failed jobs from the last hour
|
|
if (job.status === "FAILED") {
|
|
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000);
|
|
return new Date(job.createdAt) > oneHourAgo;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
});
|
|
|
|
// Generate fresh presigned URLs for completed jobs
|
|
const jobsWithFreshUrls = await Promise.all(
|
|
userJobs.map(async (job: DownloadJob) => {
|
|
if (
|
|
job.status === "COMPLETED" &&
|
|
job.downloadS3Keys?.length &&
|
|
job.downloadUrls?.length
|
|
) {
|
|
try {
|
|
const freshUrls = await Promise.all(
|
|
job.downloadS3Keys.map((s3Key) =>
|
|
generateFreshPresignedUrl(teamId, s3Key),
|
|
),
|
|
);
|
|
return { ...job, downloadUrls: freshUrls };
|
|
} catch {
|
|
return job;
|
|
}
|
|
}
|
|
return job;
|
|
}),
|
|
);
|
|
|
|
return res.status(200).json(jobsWithFreshUrls);
|
|
} catch (error) {
|
|
console.error("Error fetching download jobs:", error);
|
|
return res.status(500).json({
|
|
error: "Failed to fetch download jobs",
|
|
details: (error as Error).message,
|
|
});
|
|
}
|
|
} else {
|
|
res.setHeader("Allow", ["GET"]);
|
|
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
|
}
|
|
}
|