Files
ScraperControl/scripts/dedup-mass-schedules.ts
Albert 2c51513851 chore: sync with Gitea master and restore local-only files
Reset local main to gitea/master (new source of truth) and restored
local-only files: web scrapers, admin dashboard, ChromaDB integration,
debug scripts, and utility libraries that aren't tracked in Gitea.

Gitea master adds: discovermass, buscarmisas-network, hk-parishes,
bohosluzby, kerknet, gottesdienstzeiten, miserend importers,
ClaimRequest model, forward geocoding, heartbeat healthcheck.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-12 19:11:22 -04:00

64 lines
1.7 KiB
TypeScript

#!/usr/bin/env tsx
import dotenv from 'dotenv';
import path from 'path';
dotenv.config({ path: path.resolve(process.cwd(), '.env.local') });
import { Pool } from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '@prisma/client';
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });
async function main() {
const dryRun = !process.argv.includes('--execute');
if (dryRun) {
console.log('DRY RUN - pass --execute to actually delete duplicates\n');
}
const churches = await prisma.church.findMany({
where: { massSchedules: { some: { isActive: true } } },
include: { massSchedules: { where: { isActive: true }, orderBy: { createdAt: 'asc' } } },
});
let churchesFixed = 0;
let rowsDeleted = 0;
for (const church of churches) {
const seen = new Map<string, string>();
const toDelete: string[] = [];
for (const m of church.massSchedules) {
const key = `${m.dayOfWeek}:${m.time}:${m.language}`;
if (seen.has(key)) {
toDelete.push(m.id);
} else {
seen.set(key, m.id);
}
}
if (toDelete.length > 0) {
churchesFixed++;
rowsDeleted += toDelete.length;
if (!dryRun) {
await prisma.massSchedule.deleteMany({
where: { id: { in: toDelete } },
});
}
}
}
console.log(`Churches with duplicates: ${churchesFixed}`);
console.log(`Duplicate rows ${dryRun ? 'found' : 'deleted'}: ${rowsDeleted}`);
await prisma.$disconnect();
await pool.end();
}
main().catch((err) => {
console.error('Fatal error:', err);
process.exit(1);
});