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,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user