All files / src/cron subscriptions.js

100% Statements 20/20
100% Branches 4/4
100% Functions 3/3
100% Lines 17/17

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 45 46 47 48 49 50              1x 1x     9x   9x             9x     8x 511x 511x   8x     6x 7x 7x   7x 507x             7x     6x     1x  
/**
 * Cron: Expire SuperShy subscriptions.
 *
 * Queries users with isSuperShy==true and superShyExpiry <= now,
 * filters out lifetime subscribers, then batch-updates to remove SuperShy status.
 */
 
const { db } = require('../utils/firebase');
const log = require('../utils/log');
 
async function subscriptions() {
  const timestamp = Date.now();
 
  const snapshot = await db
    .collection('users')
    .where('isSuperShy', '==', true)
    .where('superShyExpiry', '<=', timestamp)
    .limit(500)
    .get();
 
  if (snapshot.empty) return;
 
  // Filter out lifetime subscribers client-side
  const toExpire = snapshot.docs
    .map((d) => ({ id: d.id, ...d.data() }))
    .filter((u) => u.superShyTier !== 'lifetime');
 
  if (toExpire.length === 0) return;
 
  // Batch update in chunks of 500
  for (let i = 0; i < toExpire.length; i += 500) {
    const batch = db.batch();
    const chunk = toExpire.slice(i, i + 500);
 
    for (const user of chunk) {
      batch.update(db.doc(`users/${user.id}`), {
        isSuperShy: false,
        superShyExpiry: null,
        superShyTier: null,
      });
    }
 
    await batch.commit();
  }
 
  log.info('cron', 'subscriptions: expired Super Shy subscriptions', { count: toExpire.length });
}
 
module.exports = subscriptions;