Files
dataroom/lib/emails/process-dataroom-digest.ts
zeekayandHanzo Dev 6b68c9299e kv: one name for the store, and it is Hanzo KV
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]>
2026-08-09 10:09:34 -07:00

133 lines
3.5 KiB
TypeScript

import prisma from "@/lib/prisma";
import {
DigestBatch,
popDigestQueue,
} from "@/lib/kv-queue/dataroom-notification-queue";
import { log } from "@/lib/utils";
import { generateUnsubscribeUrl } from "@/lib/utils/unsubscribe";
import { sendDataroomDigestNotification } from "./send-dataroom-digest-notification";
export async function processDataroomDigest(frequency: "daily" | "weekly") {
const batches = await popDigestQueue(frequency);
if (batches.length === 0) {
return { processed: 0 };
}
let processed = 0;
for (const batch of batches) {
try {
await processBatch(batch, frequency);
processed++;
} catch (error) {
await log({
message: `Failed to process ${frequency} digest for viewer ${batch.viewerId} in dataroom ${batch.dataroomId}. Error: ${(error as Error).message}`,
type: "error",
mention: true,
});
}
}
return { processed };
}
async function processBatch(batch: DigestBatch, frequency: "daily" | "weekly") {
if (batch.items.length === 0) return;
const [viewer, dataroom, senderUser] = await Promise.all([
prisma.viewer.findUnique({
where: { id: batch.viewerId, teamId: batch.teamId },
select: {
email: true,
views: {
where: {
dataroomId: batch.dataroomId,
viewType: "DATAROOM_VIEW",
verified: true,
},
orderBy: { viewedAt: "desc" },
take: 1,
include: {
link: {
select: {
id: true,
slug: true,
domainSlug: true,
domainId: true,
},
},
},
},
},
}),
prisma.dataroom.findUnique({
where: { id: batch.dataroomId, teamId: batch.teamId },
select: { name: true },
}),
batch.items[0]?.senderUserId
? prisma.user.findUnique({
where: { id: batch.items[0].senderUserId },
select: { email: true },
})
: null,
]);
if (!viewer?.email) return;
const uniqueDocIds = [
...new Set(batch.items.map((item) => item.dataroomDocumentId)),
];
const dataroomDocuments = await prisma.dataroomDocument.findMany({
where: { id: { in: uniqueDocIds } },
select: {
id: true,
document: { select: { name: true } },
},
});
const docNameMap = new Map(
dataroomDocuments.map((dd) => [dd.id, dd.document?.name ?? "Untitled"]),
);
const documents = uniqueDocIds.map((id) => ({
documentName: docNameMap.get(id) ?? "Untitled",
}));
const link = viewer.views[0]?.link;
let linkUrl: string | undefined;
if (link?.domainId && link.domainSlug && link.slug) {
linkUrl = `https://${link.domainSlug}/${link.slug}`;
} else if (link) {
linkUrl = `${process.env.NEXT_PUBLIC_MARKETING_URL}/view/${link.id}`;
}
if (!linkUrl) return;
const preferencesUrl = generateUnsubscribeUrl({
viewerId: batch.viewerId,
dataroomId: batch.dataroomId,
teamId: batch.teamId,
});
try {
await sendDataroomDigestNotification({
dataroomName: dataroom?.name ?? "Unknown Dataroom",
documents,
senderEmail: senderUser?.email ?? "[email protected]",
to: viewer.email,
url: linkUrl,
preferencesUrl,
frequency,
});
} catch (error) {
throw new Error(
`Failed to send ${frequency} digest for dataroom "${dataroom?.name}" ` +
`(viewerId: ${batch.viewerId}, dataroomId: ${batch.dataroomId}): ` +
`${(error as Error).message}`,
);
}
}