CentralBackend/scripts/testRenewalCron.js
Seazn c2bbb1df15 feat: implement auto-renewal cron service for subscriptions
- Added RenewalCronService to handle automatic subscription renewals and reactivations.
- Introduced listPausedAutoRenew method in AbonemmentRepository to fetch paused subscriptions eligible for reactivation.
- Created test script for renewal cron job to simulate subscription renewal scenarios.
- Updated MailService to send renewal confirmation and payment reminder emails.
- Enhanced EmailVerificationService to auto-grant 'can_subscribe' permission upon email verification.
- Modified createAdminUser script to allow different admin email configurations.
- Added node-cron dependency for scheduling tasks.
2026-03-15 14:16:46 +01:00

50 lines
1.8 KiB
JavaScript

/**
* 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 = <ID>;
*
* -- 2. To test "paid" flow: mark last invoice as paid:
* UPDATE invoices SET status = 'paid' WHERE source_type = 'subscription' AND source_id = <ABO_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 = <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);
})();