import "dotenv/config"; import { PrismaPg } from "@prisma/adapter-pg"; import { PrismaClient } from "../src/generated/prisma/client"; import { addDays } from "date-fns"; const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! }); const prisma = new PrismaClient({ adapter }); async function main() { console.log("Seeding stage dates for ALL stages (calendar view)..."); // Get all stages grouped by deliverable so we can create realistic timelines const stages = await prisma.deliverableStage.findMany({ include: { template: { select: { order: true, estimatedDays: true } }, }, orderBy: [ { deliverableId: "asc" }, { template: { order: "asc" } }, ], }); if (stages.length === 0) { console.log("No stages found to update."); return; } const today = new Date(); let currentDeliverableId = ""; let deliverableStart = today; let previousEnd = today; let updated = 0; for (const stage of stages) { // When we hit a new deliverable, pick a random project start date if (stage.deliverableId !== currentDeliverableId) { currentDeliverableId = stage.deliverableId; // Spread project starts from 60 days ago to 30 days in the future const startOffset = Math.floor(Math.random() * 90) - 60; deliverableStart = addDays(today, startOffset); previousEnd = deliverableStart; } // Each stage starts when the previous one ends (with 0-2 day gap) const gap = Math.floor(Math.random() * 3); const startDate = addDays(previousEnd, gap); // Duration based on template estimatedDays or a reasonable default const baseDays = stage.template.estimatedDays || (5 + Math.floor(Math.random() * 10)); const variance = Math.floor(Math.random() * 5) - 2; const durationDays = Math.max(2, baseDays + variance); const dueDate = addDays(startDate, durationDays); previousEnd = dueDate; await prisma.deliverableStage.update({ where: { id: stage.id }, data: { startDate, dueDate }, }); updated++; if (updated % 100 === 0) { console.log(`Updated ${updated} stages...`); } } console.log(`Successfully updated ${updated} stages with dates.`); } main() .catch((e) => { console.error(e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });