/** * Test script for the auto-renewal cron job. * * Usage: * node scripts/testRenewalCron.js * * This will immediately run the full cron logic: * Phase 1: Active subscriptions due for renewal * - Renew if last invoice is 'paid' (or no invoice exists) * - Pause if user_id is NULL (gift recipient not registered) * - Skip + send reminder if last invoice is unpaid (7+ days: overdue, 30+ days: pause) * Phase 2: Paused subscriptions with paid invoices * - Reactivate for current cycle if paid before the 11th * - Reactivate for next month if paid on/after the 11th * * Test setup (run in MySQL/phpMyAdmin): * * -- 1. Make a subscription due for billing: * UPDATE coffee_abonements SET next_billing_at = NOW() - INTERVAL 1 DAY WHERE id = ; * * -- 2. To test "paid" flow: mark last invoice as paid: * UPDATE invoices SET status = 'paid' WHERE source_type = 'subscription' AND source_id = ORDER BY id DESC LIMIT 1; * * -- 3. To test "unpaid" flow: leave invoice status as 'issued' * * -- 4. To test gift-pause: set user_id to NULL: * UPDATE coffee_abonements SET user_id = NULL WHERE id = ; */ require('dotenv').config(); const RenewalCronService = require('../services/abonemments/RenewalCronService'); (async () => { console.log('=== Renewal Cron Test ==='); console.log('Time:', new Date().toISOString()); console.log('Day of month:', new Date().getDate(), '(reactivation before 11th?', new Date().getDate() <= 10, ')'); console.log(''); const cron = new RenewalCronService(); try { await cron.processDueRenewals(); console.log(''); console.log('=== Test complete ==='); } catch (err) { console.error('Test failed:', err); } setTimeout(() => process.exit(0), 3000); })();