File size: 673 Bytes
c33bf01
 
 
 
 
5084680
c33bf01
 
e883d0c
5084680
 
 
 
 
 
 
 
 
 
 
 
 
 
c33bf01
 
 
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
const sms = require('../services/sms');

// In-memory cooldown — don't spam the same alert key more than once per 10 min
const lastSent = {};
const COOLDOWN_MS = 10 * 60 * 1000;
const ADMIN_ALERT_PHONE = '0769590766';

async function alertAdmin(key, message) {
  console.error(`[SYSTEM ALERT] ${key}: ${message}`);

  const now = Date.now();
  if (lastSent[key] && now - lastSent[key] < COOLDOWN_MS) {
    return false;
  }

  lastSent[key] = now;
  try {
    await sms.sendSMS(ADMIN_ALERT_PHONE, message);
    return true;
  } catch (err) {
    console.error(`[SYSTEM ALERT SMS FAILED] ${key}: ${err.message}`);
    return false;
  }
}

module.exports = { alertAdmin };