72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import { PrismaClient } from "@prisma/client";
|
|
import { PrismaPg } from "@prisma/adapter-pg";
|
|
import { Pool } from "pg";
|
|
import dotenv from "dotenv";
|
|
import path from "path";
|
|
|
|
// Load env vars
|
|
dotenv.config({ path: path.resolve(process.cwd(), ".env.local") });
|
|
|
|
const connectionString = process.env.DATABASE_URL;
|
|
const pool = new Pool({ connectionString });
|
|
const adapter = new PrismaPg(pool);
|
|
|
|
// Initialize Prisma
|
|
const prisma = new PrismaClient({ adapter });
|
|
|
|
async function main() {
|
|
console.log("Starting migration of RSVP status...");
|
|
|
|
try {
|
|
// 1. Get all guests
|
|
const guests = await prisma.guest.findMany();
|
|
console.log(`Found ${guests.length} guests to check.`);
|
|
|
|
let updatedCount = 0;
|
|
|
|
for (const guest of guests) {
|
|
// Skip if already migrated
|
|
if (guest.rsvpStatus !== 'PENDING') continue;
|
|
|
|
let newStatus: 'ACCEPTED' | 'DECLINED' | 'PENDING' = 'PENDING';
|
|
let completedAt = null;
|
|
|
|
// Legacy `isAttending` was boolean or null
|
|
// If isAttending is true -> ACCEPTED
|
|
// If isAttending is false -> DECLINED
|
|
// If null -> PENDING
|
|
|
|
// Note: Prisma returns boolean true/false or null
|
|
if ((guest as any).isAttending === true) {
|
|
newStatus = 'ACCEPTED';
|
|
completedAt = (guest as any).rsvpTimestamp || new Date();
|
|
} else if ((guest as any).isAttending === false) {
|
|
newStatus = 'DECLINED';
|
|
completedAt = (guest as any).rsvpTimestamp || new Date();
|
|
}
|
|
|
|
if (newStatus !== 'PENDING') {
|
|
await prisma.guest.update({
|
|
where: { id: guest.id },
|
|
data: {
|
|
rsvpStatus: newStatus,
|
|
rsvpCompletedAt: completedAt,
|
|
// also set legacy field for consistency? it's already set
|
|
}
|
|
});
|
|
updatedCount++;
|
|
process.stdout.write(`.`);
|
|
}
|
|
}
|
|
console.log(`\nMigration complete. Updated ${updatedCount} guests.`);
|
|
|
|
} catch (error) {
|
|
console.error("Migration failed:", error);
|
|
} finally {
|
|
await prisma.$disconnect();
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
main();
|