Fix scheduled WhatsApp messages sending as email; add 24h cleanup and recipient display
The scheduled-job store never persisted the channel field, so the send worker always fell through to its email branch regardless of what was requested. Also purges sent jobs 24h after sending instead of keeping them forever, and surfaces who each scheduled job will go to in the admin "manage scheduled" lists (now correctly filtered per channel too). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -57,6 +57,23 @@ function parseFreeformEmails(lines) {
|
||||
return recipients;
|
||||
}
|
||||
|
||||
// Human-readable summary of who a broadcast will go to, for the scheduled-jobs admin UI
|
||||
async function describeBroadcastRecipients({ userIds, emails }) {
|
||||
const parts = [];
|
||||
try {
|
||||
const ids = Array.isArray(userIds) ? userIds.filter(x => typeof x === 'string' && x) : [];
|
||||
if (ids.length) {
|
||||
const users = await prisma.user.findMany({ where: { id: { in: ids } }, select: { name: true } });
|
||||
const names = users.map(u => u.name).filter(Boolean);
|
||||
parts.push(names.slice(0, 3).join(', ') + (names.length > 3 ? ` +${names.length - 3} more` : ''));
|
||||
}
|
||||
if (Array.isArray(emails) && emails.length) {
|
||||
parts.push(`${emails.length} email address${emails.length === 1 ? '' : 'es'}`);
|
||||
}
|
||||
} catch {}
|
||||
return parts.length ? parts.join('; ') : 'No recipients';
|
||||
}
|
||||
|
||||
// @desc Preview broadcast recipients and sample
|
||||
// @route POST /api/broadcasts/preview
|
||||
// @access Private/Supervisor or Admin
|
||||
@@ -191,11 +208,14 @@ const scheduleBroadcast = async (req, res) => {
|
||||
|
||||
const payload = { subject, html, text, userIds, emails, eventId };
|
||||
|
||||
const recipientSummary = await describeBroadcastRecipients({ userIds, emails: parseFreeformEmails(emails) });
|
||||
|
||||
const { addJob } = require('../utils/scheduledEmails');
|
||||
const created = addJob({
|
||||
broadcast: true,
|
||||
scheduledAt: when.toISOString(),
|
||||
createdById: req.user?.id || null,
|
||||
recipientSummary,
|
||||
payload,
|
||||
});
|
||||
|
||||
|
||||
@@ -1569,6 +1569,15 @@ const whatsappEventAttendees = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Human-readable summary of who an attendees-scoped send will go to, for the scheduled-jobs admin UI
|
||||
function describeAttendeeFilter(filter) {
|
||||
if (Array.isArray(filter?.attendeeIds) && filter.attendeeIds.length > 0) {
|
||||
return `${filter.attendeeIds.length} selected attendee${filter.attendeeIds.length === 1 ? '' : 's'}`;
|
||||
}
|
||||
const labels = { paid: 'Paid attendees', unpaid: 'Unpaid attendees', partial_paid: 'Partially paid attendees', cancelled: 'Cancelled registrations' };
|
||||
return labels[filter?.status] || 'All attendees';
|
||||
}
|
||||
|
||||
// @desc Schedule email to attendees at a specific date/time
|
||||
// @route POST /api/events/:id/email-attendees/schedule
|
||||
// @access Private/Supervisor or Admin
|
||||
@@ -1600,6 +1609,7 @@ const scheduleEmailEventAttendees = async (req, res) => {
|
||||
eventId,
|
||||
createdById: req.user?.id || null,
|
||||
scheduledAt: when.toISOString(),
|
||||
recipientSummary: `${event.title} — ${describeAttendeeFilter(filter)}`,
|
||||
payload,
|
||||
});
|
||||
|
||||
@@ -1637,6 +1647,7 @@ const scheduleWhatsappEventAttendees = async (req, res) => {
|
||||
channel: 'whatsapp',
|
||||
createdById: req.user?.id || null,
|
||||
scheduledAt: when.toISOString(),
|
||||
recipientSummary: `${event.title} — ${describeAttendeeFilter(filter)}`,
|
||||
payload,
|
||||
});
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ function toClient(job) {
|
||||
kind,
|
||||
eventId: job.eventId || null,
|
||||
broadcast: !!job.broadcast,
|
||||
channel: job.channel || 'email',
|
||||
recipient: job.recipientSummary || null,
|
||||
scheduledAt: job.scheduledAt,
|
||||
createdAt: job.createdAt,
|
||||
status: job.status,
|
||||
@@ -23,21 +25,13 @@ function toClient(job) {
|
||||
};
|
||||
}
|
||||
|
||||
// GET /api/scheduled-emails
|
||||
// Returns jobs excluding emails sent more than a week ago
|
||||
// GET /api/scheduled-emails?channel=email|whatsapp
|
||||
// Sent jobs are purged from storage 24h after sending, so nothing older than that is ever returned here
|
||||
const listScheduledEmails = async (req, res) => {
|
||||
try {
|
||||
const raw = listJobs();
|
||||
const now = new Date();
|
||||
const weekMs = 7 * 24 * 60 * 60 * 1000;
|
||||
const filtered = raw.filter(j => {
|
||||
if (j.status === 'sent' && j.sentAt) {
|
||||
const sentAt = new Date(j.sentAt).getTime();
|
||||
return (now.getTime() - sentAt) <= weekMs;
|
||||
}
|
||||
// Include queued, sending, error by default
|
||||
return true;
|
||||
})
|
||||
const { channel } = req.query || {};
|
||||
const raw = listJobs(channel ? { channel: String(channel) } : {});
|
||||
const filtered = raw
|
||||
// Provide most-relevant first: queued -> sending -> error -> recent sent
|
||||
.sort((a, b) => {
|
||||
const order = { queued: 0, sending: 1, error: 2, sent: 3 };
|
||||
|
||||
@@ -36,6 +36,22 @@ function parseFreeformPhones(lines) {
|
||||
return recipients;
|
||||
}
|
||||
|
||||
// Human-readable summary of who a WhatsApp broadcast will go to, for the scheduled-jobs admin UI
|
||||
async function describeBroadcastRecipients({ userIds, phones }) {
|
||||
const parts = [];
|
||||
try {
|
||||
const ids = Array.isArray(userIds) ? userIds.filter(x => typeof x === 'string' && x) : [];
|
||||
if (ids.length) {
|
||||
const users = await prisma.user.findMany({ where: { id: { in: ids } }, select: { name: true } });
|
||||
const names = users.map(u => u.name).filter(Boolean);
|
||||
parts.push(names.slice(0, 3).join(', ') + (names.length > 3 ? ` +${names.length - 3} more` : ''));
|
||||
}
|
||||
const extra = parseFreeformPhones(phones);
|
||||
if (extra.length) parts.push(`${extra.length} phone number${extra.length === 1 ? '' : 's'}`);
|
||||
} catch {}
|
||||
return parts.length ? parts.join('; ') : 'No recipients';
|
||||
}
|
||||
|
||||
// @desc Preview WhatsApp broadcast recipients
|
||||
// @route POST /api/whatsapp-broadcasts/preview
|
||||
// @access Private/Supervisor or Admin
|
||||
@@ -152,12 +168,15 @@ const scheduleWhatsAppBroadcast = async (req, res) => {
|
||||
|
||||
const payload = { message, userIds, phones, eventId };
|
||||
|
||||
const recipientSummary = await describeBroadcastRecipients({ userIds, phones });
|
||||
|
||||
const { addJob } = require('../utils/scheduledEmails');
|
||||
const created = addJob({
|
||||
broadcast: true,
|
||||
channel: 'whatsapp',
|
||||
scheduledAt: when.toISOString(),
|
||||
createdById: req.user?.id || null,
|
||||
recipientSummary,
|
||||
payload,
|
||||
});
|
||||
|
||||
|
||||
@@ -1192,13 +1192,14 @@ app.listen(PORT, () => {
|
||||
try {
|
||||
const enabled = String(process.env.SCHEDULED_EMAILS_ENABLED || 'true').toLowerCase() !== 'false';
|
||||
if (enabled) {
|
||||
const { getDueJobs, updateJob } = require('./utils/scheduledEmails');
|
||||
const { getDueJobs, updateJob, purgeSentJobs } = require('./utils/scheduledEmails');
|
||||
const { emailEventAttendees, whatsappEventAttendees } = require('./controllers/eventController');
|
||||
const { sendBroadcast } = require('./controllers/broadcastController');
|
||||
const { sendWhatsAppBroadcast } = require('./controllers/whatsappBroadcastController');
|
||||
const intervalMs = parseInt(process.env.SCHEDULED_EMAILS_INTERVAL_MS || '30000', 10);
|
||||
setInterval(async () => {
|
||||
try {
|
||||
try { purgeSentJobs(24 * 60 * 60 * 1000); } catch (e) { console.warn('[scheduled emails] purge failed:', e?.message || e); }
|
||||
const due = getDueJobs(new Date());
|
||||
if (!due || due.length === 0) return;
|
||||
for (const job of due) {
|
||||
|
||||
@@ -46,6 +46,8 @@ function addJob(job) {
|
||||
id,
|
||||
eventId: job.eventId || null,
|
||||
broadcast: !!job.broadcast,
|
||||
channel: job.channel || 'email',
|
||||
recipientSummary: job.recipientSummary || null,
|
||||
createdById: job.createdById || null,
|
||||
scheduledAt: job.scheduledAt,
|
||||
createdAt: now.toISOString(),
|
||||
@@ -66,10 +68,26 @@ function listJobs(filter = {}) {
|
||||
return jobs.filter(j => {
|
||||
if (filter.status && j.status !== filter.status) return false;
|
||||
if (filter.eventId && j.eventId !== filter.eventId) return false;
|
||||
if (filter.channel && (j.channel || 'email') !== filter.channel) return false;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Permanently remove jobs that finished sending more than maxAgeMs ago.
|
||||
* Only touches 'sent' jobs - queued/sending/error jobs are left for admins to review.
|
||||
*/
|
||||
function purgeSentJobs(maxAgeMs = 24 * 60 * 60 * 1000) {
|
||||
const jobs = loadAll();
|
||||
const now = Date.now();
|
||||
const kept = jobs.filter(j => {
|
||||
if (j.status !== 'sent' || !j.sentAt) return true;
|
||||
return (now - new Date(j.sentAt).getTime()) <= maxAgeMs;
|
||||
});
|
||||
if (kept.length !== jobs.length) saveAll(kept);
|
||||
return jobs.length - kept.length;
|
||||
}
|
||||
|
||||
function getDueJobs(now = new Date()) {
|
||||
const jobs = loadAll();
|
||||
const t = now instanceof Date ? now : new Date(now);
|
||||
@@ -105,4 +123,5 @@ module.exports = {
|
||||
updateJob,
|
||||
getJob,
|
||||
deleteJob,
|
||||
purgeSentJobs,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user