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]>
160 lines
4.0 KiB
TypeScript
160 lines
4.0 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { authOptions } from "@/pages/api/auth/[...nextauth]";
|
|
import { getServerSession } from "next-auth/next";
|
|
|
|
import prisma from "@/lib/prisma";
|
|
import { jobStore } from "@/lib/kv-job-store";
|
|
import { exportVisitsTask } from "@/lib/trigger/export-visits";
|
|
import { CustomUser } from "@/lib/types";
|
|
|
|
export default async function handler(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
) {
|
|
if (req.method === "GET") {
|
|
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;
|
|
|
|
try {
|
|
// Verify team access
|
|
const team = await prisma.team.findUnique({
|
|
where: {
|
|
id: teamId,
|
|
users: {
|
|
some: {
|
|
userId: userId,
|
|
},
|
|
},
|
|
},
|
|
select: { plan: true },
|
|
});
|
|
|
|
if (!team) {
|
|
return res.status(404).end("Team not found");
|
|
}
|
|
|
|
// Get existing exports for this dataroom
|
|
const existingExports = await jobStore.getResourceJobs(
|
|
dataroomId,
|
|
teamId,
|
|
"dataroom",
|
|
undefined,
|
|
10,
|
|
);
|
|
|
|
return res.status(200).json(existingExports);
|
|
} catch (error) {
|
|
console.error("Error fetching existing exports:", error);
|
|
return res
|
|
.status(500)
|
|
.json({ message: "Failed to fetch existing exports" });
|
|
}
|
|
}
|
|
|
|
if (req.method !== "POST") {
|
|
// Changed to POST to trigger background job
|
|
res.setHeader("Allow", ["POST"]);
|
|
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
|
}
|
|
|
|
const session = await getServerSession(req, res, authOptions);
|
|
if (!session) {
|
|
return res.status(401).end("Unauthorized");
|
|
}
|
|
|
|
// get dataroom id and teamId from query params
|
|
const { teamId, id: dataroomId } = req.query as {
|
|
teamId: string;
|
|
id: string;
|
|
};
|
|
|
|
const userId = (session.user as CustomUser).id;
|
|
|
|
try {
|
|
// Fetching Team based on team.id
|
|
const team = await prisma.team.findUnique({
|
|
where: {
|
|
id: teamId,
|
|
users: {
|
|
some: {
|
|
userId: userId,
|
|
},
|
|
},
|
|
},
|
|
select: { plan: true },
|
|
});
|
|
|
|
if (!team) {
|
|
return res.status(404).end("Team not found");
|
|
}
|
|
|
|
if (team.plan === "free") {
|
|
return res
|
|
.status(403)
|
|
.json({ message: "This feature is not available for your plan" });
|
|
}
|
|
|
|
// Fetching Dataroom based on dataroom.id
|
|
const dataroom = await prisma.dataroom.findUnique({
|
|
where: { id: dataroomId, teamId: teamId },
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
},
|
|
});
|
|
|
|
if (!dataroom) {
|
|
return res.status(404).end("Dataroom not found");
|
|
}
|
|
|
|
// Create export job record
|
|
const exportJob = await jobStore.createJob({
|
|
type: "dataroom",
|
|
resourceId: dataroomId,
|
|
resourceName: dataroom.name,
|
|
userId,
|
|
teamId,
|
|
status: "PENDING",
|
|
});
|
|
|
|
// Trigger the background task
|
|
const handle = await exportVisitsTask.trigger(
|
|
{
|
|
type: "dataroom",
|
|
teamId,
|
|
resourceId: dataroomId,
|
|
userId,
|
|
exportId: exportJob.id,
|
|
},
|
|
{
|
|
idempotencyKey: exportJob.id,
|
|
tags: [`team_${teamId}`, `user_${userId}`, `export_${exportJob.id}`],
|
|
},
|
|
);
|
|
|
|
// Update the job with the trigger run ID for cancellation
|
|
const updatedJob = await jobStore.updateJob(exportJob.id, {
|
|
triggerRunId: handle.id,
|
|
});
|
|
|
|
return res.status(200).json({
|
|
exportId: updatedJob?.id || exportJob.id,
|
|
status: updatedJob?.status || exportJob.status,
|
|
message:
|
|
"Export job created successfully. You will be notified when it's ready.",
|
|
});
|
|
} catch (error) {
|
|
console.error("Error creating export job:", error);
|
|
return res.status(500).json({ message: "Something went wrong" });
|
|
}
|
|
}
|