Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 1x 1x 11x 11x 10x 270x 7x 8x 8x 8x 270x 270x 8x 6x 1x | /**
* Cron: Archive resolved reports older than 6 months.
*
* Copies resolved reports to reportsArchive collection,
* then deletes the originals. Uses batch writes (max 500 per commit).
*/
const { db } = require('../utils/firebase');
const log = require('../utils/log');
async function archiveReports() {
const sixMonthsAgo = Date.now() - 6 * 30 * 24 * 60 * 60 * 1000;
const snapshot = await db
.collection('reports')
.where('status', '==', 'resolved')
.where('resolvedAt', '<', sixMonthsAgo)
.limit(500)
.get();
if (snapshot.empty) return;
const docs = snapshot.docs.map((d) => ({ id: d.id, ...d.data() }));
// Process in batches of 250 (each doc = 2 ops: set archive + delete original)
for (let i = 0; i < docs.length; i += 250) {
const batch = db.batch();
const chunk = docs.slice(i, i + 250);
for (const report of chunk) {
// Copy to archive collection
batch.set(db.doc(`reportsArchive/${report.id}`), report);
// Delete from active reports
batch.delete(db.doc(`reports/${report.id}`));
}
await batch.commit();
}
log.info('cron', 'archiveReports: archived old reports', { count: docs.length });
}
module.exports = archiveReports;
|