CI / build-and-test (push) Has been cancelled
- E2E suite (23 tests, `npm run test:e2e -w @lawdesk/api`): boots the real Fastify app against a disposable Dockerized Postgres (never a real DB) and covers signup/login/lockout/rate limits, CSRF (incl. forged-token rejection), logout, password reset, email verification, the superadmin verified-email promotion gate, and cross-firm tenancy isolation - packages/db: DATABASE_SSL=disable opt-out for local/test databases that don't speak TLS; refused in production - retention-sweep.ts cron enforcing Privacy Policy windows (sessions, tokens, login attempts, tool usage, contact messages, audit log) + sweep-orphaned-storage.ts Spaces reconciliation + scripts/README - Expose emailVerified on the session user; in-app verify-email banner with resend, and verified=1|0 toasts on the login page - Silence Fastify logger under NODE_ENV=test; fix footer resource link; document login-attempt/tool-usage retention in the Privacy Policy Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
76 lines
2.7 KiB
TypeScript
76 lines
2.7 KiB
TypeScript
// Reconciles DO Spaces against the documents table and reports/deletes orphaned objects —
|
|
// files whose DB rows were removed before storage cleanup existed (deleted firms/cases/clients).
|
|
//
|
|
// Dry run (default): npx tsx scripts/sweep-orphaned-storage.ts
|
|
// Actually delete: npx tsx scripts/sweep-orphaned-storage.ts --delete
|
|
//
|
|
// Only keys matching the document layout `<uuid>/<uuid>/<uuid>.<ext>` are eligible for
|
|
// deletion; anything else in the bucket is reported but never touched.
|
|
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import dotenv from 'dotenv';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
dotenv.config({ path: path.resolve(__dirname, '../.env') });
|
|
|
|
import { getDb, getPool, documents } from '@lawdesk/db';
|
|
import { listAllKeys, deleteFile } from '../apps/api/src/lib/storage';
|
|
|
|
const DELETE = process.argv.includes('--delete');
|
|
const UUID = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
|
|
const DOC_KEY = new RegExp(`^${UUID}/${UUID}/${UUID}(\\.[A-Za-z0-9]+)?$`, 'i');
|
|
|
|
async function main() {
|
|
const db = getDb();
|
|
|
|
console.log('Listing bucket contents...');
|
|
const bucketKeys = await listAllKeys();
|
|
console.log(` ${bucketKeys.length} object(s) in bucket.`);
|
|
|
|
const rows = await db.select({ storageKey: documents.storageKey }).from(documents);
|
|
const dbKeys = new Set(rows.map((r) => r.storageKey));
|
|
console.log(` ${dbKeys.size} document row(s) in database.`);
|
|
|
|
const orphans: string[] = [];
|
|
const unrecognized: string[] = [];
|
|
for (const key of bucketKeys) {
|
|
if (dbKeys.has(key)) continue;
|
|
if (DOC_KEY.test(key)) orphans.push(key);
|
|
else unrecognized.push(key);
|
|
}
|
|
|
|
// Reverse check: DB rows whose file is missing from the bucket (report only).
|
|
const bucketSet = new Set(bucketKeys);
|
|
const missing = [...dbKeys].filter((k) => !bucketSet.has(k));
|
|
|
|
console.log(`\nOrphaned objects (in bucket, no DB row): ${orphans.length}`);
|
|
for (const k of orphans) console.log(` ${k}`);
|
|
if (unrecognized.length) {
|
|
console.log(`\nUnrecognized keys (not document-shaped — never deleted): ${unrecognized.length}`);
|
|
for (const k of unrecognized) console.log(` ${k}`);
|
|
}
|
|
if (missing.length) {
|
|
console.log(`\nWARNING — DB rows whose file is MISSING from the bucket: ${missing.length}`);
|
|
for (const k of missing) console.log(` ${k}`);
|
|
}
|
|
|
|
if (!DELETE) {
|
|
console.log(`\nDry run — nothing deleted. Re-run with --delete to remove the ${orphans.length} orphan(s).`);
|
|
} else {
|
|
let deleted = 0;
|
|
for (const key of orphans) {
|
|
await deleteFile(key);
|
|
deleted++;
|
|
}
|
|
console.log(`\nDeleted ${deleted} orphaned object(s).`);
|
|
}
|
|
|
|
await getPool().end();
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|