Files
filezzy-staging/backend/scripts/verify-account-deletion.ts
2026-02-04 14:16:04 +01:00

82 lines
2.8 KiB
TypeScript

/**
* Verify that an account was fully deleted and that DeletedEmail has an insertion.
*
* Run from backend:
* npx ts-node scripts/verify-account-deletion.ts <email>
* npm run db:verify-deletion -- abdelaziz.azouhri@gmail.com
*
* Checks:
* 1. User table: no row with this email (case-insensitive match)
* 2. DeletedEmail table: at least one row for this email
*/
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
const emailArg = process.argv[2];
if (!emailArg) {
console.error("Usage: npx ts-node scripts/verify-account-deletion.ts <email>");
process.exit(1);
}
const email = emailArg.trim().toLowerCase();
async function main() {
console.log("\n=== Account deletion verification ===\n");
console.log("Email:", email);
// 1. User table: should NOT exist
const user = await prisma.user.findFirst({
where: { email: { equals: email, mode: "insensitive" } },
select: { id: true, email: true, name: true, keycloakId: true },
});
if (user) {
console.log("\n❌ User still exists in DB:");
console.log(" id:", user.id);
console.log(" email:", user.email);
console.log(" name:", user.name);
console.log(" keycloakId:", user.keycloakId);
} else {
console.log("\n✅ User: no row found (account removed from User table)");
}
// 2. DeletedEmail: should have at least one row
const deletedRows = await prisma.deletedEmail.findMany({
where: { email },
orderBy: { deletedAt: "desc" },
select: { id: true, email: true, deletedAt: true },
});
if (deletedRows.length === 0) {
console.log("\n❌ DeletedEmail: no row found for this email (expected at least one insertion)");
} else {
console.log("\n✅ DeletedEmail: found", deletedRows.length, "row(s)");
deletedRows.forEach((row, i) => {
console.log(" [" + (i + 1) + "] id:", row.id, "| deletedAt:", row.deletedAt.toISOString());
});
}
// 3. Optional: count any Subscription/Job/Session/UsageLog/Batch that might reference this email
// (We can't query by email for those; they reference userId. So if User is gone, those are gone by cascade.)
if (user) {
const [subs, jobs, sessions, usageLogs, batches] = await Promise.all([
prisma.subscription.count({ where: { userId: user.id } }),
prisma.job.count({ where: { userId: user.id } }),
prisma.session.count({ where: { userId: user.id } }),
prisma.usageLog.count({ where: { userId: user.id } }),
prisma.batch.count({ where: { userId: user.id } }),
]);
console.log("\n Related counts for this user:");
console.log(" Subscription:", subs, "| Jobs:", jobs, "| Sessions:", sessions, "| UsageLogs:", usageLogs, "| Batches:", batches);
}
console.log("\n=== End ===\n");
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(() => prisma.$disconnect());