mirror of
https://github.com/hanzo-apps/dataroom.git
synced 2026-10-30 14:47:36 +00:00
`skipDuplicates` is not merely ignored on sqlite — Prisma rejects the whole call with "Unknown argument `skipDuplicates`". Verified against a database built from this schema, so all seventeen call sites were throwing at runtime: applying dataroom and folder permissions, adding group members, saving link visitor groups and tags, bulk and folder downloads, and the year-in-review queue. `insert()` keeps the behaviour those sites asked for. The batch insert is still one statement; only a uniqueness collision falls back to row-at-a-time, skipping what is already there. That fallback is idempotent, so it is correct even if the batch wrote some rows before colliding, and a caught P2002 leaves an open transaction usable — checked, since four of these run inside one. Anything that is not a duplicate still throws. The nested customFields write just drops the argument: CustomField has no unique constraint, so nothing could ever have been skipped there. 99 errors -> 21, no file worse than it was. Co-authored-by: Hanzo Dev <[email protected]>
55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
import prisma from "@/lib/prisma";
|
|
|
|
import { getYearInReviewStats } from "./get-stats";
|
|
import { insert } from "@/lib/insert";
|
|
|
|
export async function initializeEmailQueue() {
|
|
const batchSize = 100; // Process teams in batches during initialization
|
|
let skip = 0;
|
|
let totalProcessed = 0;
|
|
|
|
while (true) {
|
|
// Get batch of teams
|
|
const teams = await prisma.team.findMany({
|
|
skip,
|
|
take: batchSize,
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
},
|
|
});
|
|
|
|
if (teams.length === 0) break;
|
|
|
|
// Process each team in the batch and filter out teams with no views
|
|
const jobData = (
|
|
await Promise.all(
|
|
teams.map(async (team) => {
|
|
const stats = await getYearInReviewStats(team.id);
|
|
|
|
// Skip teams with no views
|
|
if (stats.totalViews === 0) return null;
|
|
|
|
return {
|
|
teamId: team.id,
|
|
status: "pending",
|
|
stats: stats,
|
|
};
|
|
}),
|
|
)
|
|
).filter((job): job is NonNullable<typeof job> => job !== null);
|
|
|
|
// Bulk create jobs with precomputed stats
|
|
await insert(prisma.yearInReview, jobData);
|
|
|
|
totalProcessed += teams.length;
|
|
skip += batchSize;
|
|
|
|
console.log(`Processed ${totalProcessed} teams...`);
|
|
}
|
|
|
|
console.log(
|
|
`Completed creating ${totalProcessed} email jobs with precomputed stats`,
|
|
);
|
|
}
|