← back to AbramsOS

scripts/claims-autopilot-run.js

38 lines

#!/usr/bin/env node
// One autopilot pass: auto-mark + auto-stage every open claim, then send the
// deadline alert (throttled). Run by the in-process scheduler; also runnable by hand:
//   node scripts/claims-autopilot-run.js            # stage + alert (throttled)
//   node scripts/claims-autopilot-run.js --alert    # stage + force-send alert now
//   node scripts/claims-autopilot-run.js --dry       # report open claims, no writes
// NEVER submits.

const autopilot = require('../lib/claims-autopilot');
const alert = require('../lib/claims-alert');
const db = require('../lib/db');

async function main() {
  const args = process.argv.slice(2);
  const force = args.includes('--alert');

  if (args.includes('--dry')) {
    const items = await alert.collect();
    const { rows } = await db.query(
      `SELECT count(*) FILTER (WHERE deadline>=current_date) open,
              count(*) FILTER (WHERE deadline>=current_date AND eligibility_state='unreviewed') unreviewed
         FROM settlement_claim WHERE user_id=$1`, [autopilot.USER]);
    console.log('DRY:', JSON.stringify(rows[0]), '| staged-ready:', items.length);
    process.exit(0);
  }

  const r = await autopilot.autoProcess();
  console.log(`autopilot: staged ${r.staged.length}, automatic ${r.automatic.length}, already ${r.already.length}` +
    (r.profile_missing.length ? ` · ⚠ profile missing: ${r.profile_missing.join(',')}` : ''));
  for (const s of r.staged) console.log(`  + ${s.name}  [${s.deadline || 'no deadline'}]  ${s.payout || ''}${s.error ? '  ERROR: ' + s.error : ''}`);

  const a = await alert.sendClaimsAlert(autopilot.USER, { force });
  console.log('alert:', a.sent ? `SENT (${a.total} ready, ${a.urgent} urgent) msg=${a.messageId}` : `skipped — ${a.skipped}`);
  process.exit(0);
}

main().catch((e) => { console.error('FATAL', e.message); process.exit(1); });