'use strict'; /* Delete expired reports (14 days, real time), then arm the next cleanup for the earliest remaining expiry. createReport * arms it when none is pending, so there is at most one pending cleanup per world and none while no reports exist. */ module.exports = function reportCleanup(ctx, ev) { const { db } = ctx; db.run('DELETE FROM reports WHERE world_id = ? AND expires_at <= ?', ev.worldId, ev.dueAt); const left = db.get('SELECT MIN(expires_at) AS e FROM reports WHERE world_id = ?', ev.worldId); if (left && left.e !== null) ctx.enqueue({ worldId: ev.worldId, dueAt: left.e, type: 'reportCleanup', payload: { worldId: ev.worldId } }); };