Merge pull request #674 from Luca-Timo/feat/workflow-engine

fix: event-reminder, email-language & gallery-publish bugs surfaced during workflow testing
This commit is contained in:
Paul Nothaft
2026-06-27 21:45:07 +02:00
committed by GitHub
9 changed files with 158 additions and 42 deletions
@@ -415,6 +415,16 @@ describe('workflow engine', () => {
expect(await _internal.resolveTemplateKey('zzznotype', 'promo_')).toBe('promo_default');
});
test('pre-event payload passes the RAW event_date (processor formats it — no "Invalid Date")', async () => {
const { _internal } = require('../../src/services/eventReminderService');
const p = _internal.composePayload({
event: { id: 1, event_name: 'X', event_date: '2026-06-25', customer_name: 'A' },
recipientEmail: '[email protected]', daysBefore: 2, businessName: 'Biz',
});
expect(p.event_date).toBe('2026-06-25'); // raw, not pre-formatted DD.MM.YYYY
expect(p.event_date).not.toMatch(/invalid/i);
});
test('webhook action enqueues a delivery for a configured subscription (full pipeline)', async () => {
const webhook = engine.registry.getAction('webhook');
expect(typeof webhook).toBe('function'); // registered — no longer a silent no-op
@@ -450,6 +460,29 @@ describe('workflow engine', () => {
expect((await webhook(ctx({ webhookId: whId }))).skipped).toBe(true);
});
test('pre-event falls back to the assigned customer account when the event has no inline email', async () => {
const eventReminderService = require('../../src/services/eventReminderService');
const farFuture = new Date(Date.now() + 365 * 86400000).toISOString();
const [custId] = await db('customer_accounts').insert({
email: '[email protected]', preferred_language: 'en', is_active: true, created_at: new Date(),
});
// Event with NO inline customer_email / host_email.
await db('events').insert({
event_type: 'wedding', password_hash: 'x', expires_at: farFuture, is_active: true, is_archived: false,
slug: 'rem-assigned', share_link: 'rem-assigned', event_name: 'Assigned',
event_date: new Date(Date.now() + 2 * 86400000).toISOString().slice(0, 10),
});
const ev = await db('events').where({ slug: 'rem-assigned' }).first();
await db('event_customer_assignments').insert({ event_id: ev.id, customer_account_id: custId, assigned_at: new Date() });
const res = await eventReminderService.sendReminderForEvent(ev.id);
expect(res.sent).toBe(1);
const mail = await db('email_queue').where({ recipient_email: '[email protected]' }).first();
expect(mail).toBeTruthy();
// Queued WITHOUT event_id so the resolver uses the customer's preferred_language.
expect(mail.event_id == null).toBe(true);
});
test('isBuiltinFlowActive reflects the built-in ENABLED state (enabled-based mutex)', async () => {
const { seedBuiltinWorkflowsAtBoot } = require('../../src/services/_workflowSeedBoot');
await seedBuiltinWorkflowsAtBoot(db, { info() {}, warn() {} });
+16
View File
@@ -1189,6 +1189,22 @@ router.post('/:id/publish', adminAuth, requirePermission('events.edit'), require
status: 'pending',
created_at: new Date()
});
} else {
// No inline email, but the gallery may be assigned to registered customer
// account(s). Notify them via the account "your galleries" email
// (customer_gallery_assigned, in the customer's own language) instead of
// the gallery_created mail, which needs an inline recipient. Best-effort.
try {
const customerAccountsService = require('../services/customerAccountsService');
const assigned = await customerAccountsService.getAssignmentsForEvent(parseInt(id, 10));
for (const c of assigned.filter((a) => a.is_active !== false && a.is_active !== 0 && a.email)) {
await customerAccountsService
.notifyCustomerOfNewAssignments(c.id, [parseInt(id, 10)])
.catch((err) => logger.warn('Publish: customer gallery notice failed', { customerId: c.id, error: err.message }));
}
} catch (err) {
logger.warn('Publish: assigned-customer notification skipped', { eventId: id, error: err.message });
}
}
// WhatsApp gallery_ready on publish-from-draft (#640D). The PublishGallery
@@ -1456,6 +1456,7 @@ module.exports = {
setAssignmentsForEvent,
setAssignmentsForCustomer,
getAssignmentsForEvent,
notifyCustomerOfNewAssignments,
listEventsForCustomer,
customerHasAccessToEvent,
getPendingInvitations,
+11 -2
View File
@@ -860,10 +860,19 @@ async function processEmailQueue({ ignoreSchedule = false, limit = 10, onlyId =
for (const email of pendingEmails) {
try {
const emailData = typeof email.email_data === 'string'
const emailData = typeof email.email_data === 'string'
? JSON.parse(email.email_data || '{}')
: email.email_data || {};
// Language is resolved from emailData.eventId (event.language is the top
// priority). queueEmail injects it, but direct email_queue inserts (e.g.
// the gallery-publish notification) only set the event_id COLUMN — so
// backfill from the authoritative column so every send path resolves the
// recipient language from the event consistently.
if (emailData.eventId == null && email.event_id != null) {
emailData.eventId = email.event_id;
}
const sendResult = await sendTemplateEmail(
email.recipient_email,
email.email_type,
+64 -30
View File
@@ -103,18 +103,14 @@ function composePayload({ event, recipientEmail, daysBefore, businessName }) {
|| event.host_name
|| recipientEmail
|| '';
// Event date formatted DD.MM.YYYY here for simplicity; the rendered
// email may further re-locale via the template engine when locale-
// aware formatters are introduced.
const ed = event.event_date instanceof Date ? event.event_date : new Date(event.event_date);
const day = String(ed.getUTCDate()).padStart(2, '0');
const month = String(ed.getUTCMonth() + 1).padStart(2, '0');
const year = ed.getUTCFullYear();
const eventDateFormatted = `${day}.${month}.${year}`;
// Pass the RAW event_date — emailProcessor.processTemplate runs it through
// formatDate(value, recipientLanguage). Pre-formatting it (e.g. DD.MM.YYYY)
// makes the processor's new Date(...) reparse fail → "Invalid Date". Same
// contract the expiry mailer uses.
return {
customer_name: customerName,
event_name: event.event_name || `Event #${event.id}`,
event_date: eventDateFormatted,
event_date: event.event_date || '',
event_type: event.event_type || '',
days_before: daysBefore,
business_name: businessName || '',
@@ -191,8 +187,9 @@ async function runEventReminderPass() {
let skipped = 0;
for (const row of rows) {
try {
const recipientEmail = row.customer_email || row.host_email;
if (!recipientEmail) { skipped += 1; continue; }
// Inline event email, else the assigned customer account(s).
const recipients = await resolveReminderRecipients(row);
if (!recipients.length) { skipped += 1; continue; }
const rawOffset = row.event_reminder_offset_days;
const offsetDays = (rawOffset != null && rawOffset !== '' && Number.isFinite(Number(rawOffset)))
? Number(rawOffset)
@@ -203,21 +200,18 @@ async function runEventReminderPass() {
if (now < triggerAt) { skipped += 1; continue; }
const templateKey = await resolveTemplateKey(row.event_type);
const payload = composePayload({
event: row, recipientEmail, daysBefore: offsetDays, businessName,
});
// Per-event body override: when present, append as a synthetic
// `body_override` field. The template engine should branch on it
// (e.g. Handlebars `{{#if body_override}}{{body_override}}{{else}}…default body…{{/if}}`).
// For installs where the templates don't yet handle the branch,
// the override still rides through as a variable the admin can
// reference manually.
if (row.event_reminder_body_override) {
payload.body_override = row.event_reminder_body_override;
for (const r of recipients) {
const payload = composePayload({
event: row, recipientEmail: r.email, daysBefore: offsetDays, businessName,
});
// Per-event body override rides through as a variable the template can branch on.
if (row.event_reminder_body_override) {
payload.body_override = row.event_reminder_body_override;
}
// Inline → event language; assigned account → customer's preferred language (no eventId).
await emailProcessor.queueEmail(r.fromEvent ? row.id : null, r.email, templateKey, payload);
}
await emailProcessor.queueEmail(row.id, recipientEmail, templateKey, payload);
// Stamp sent_at immediately so a same-pass-re-entrancy (or a
// crash between queueEmail and the update) doesn't double-send
// on the next tick. The queueEmail call is itself idempotent at
@@ -283,8 +277,17 @@ async function sendReminderForEvent(eventId, { templateGroup = null } = {}) {
if (row.is_active === false || row.is_active === 0 || row.is_archived === true || row.is_archived === 1) {
return { sent: 0, skipped: 1, reason: 'inactive' };
}
const recipientEmail = row.customer_email || row.host_email;
if (!recipientEmail) return { sent: 0, skipped: 1, reason: 'no_recipient' };
// Recipient resolution:
// - inline event email (customer_email / host_email) → send there, language
// follows the event (eventId passed);
// - else fall back to the assigned customer account(s) (event_customer_assignments)
// → send to each registered customer, honouring THEIR preferred_language
// (queued without eventId so the resolver uses the customer, not the event).
// The gallery-ready mail deliberately doesn't fall back to accounts, but a
// pre-event reminder should still reach an assigned customer.
const recipients = await resolveReminderRecipients(row);
if (!recipients.length) return { sent: 0, skipped: 1, reason: 'no_recipient' };
const globalDaysBefore = Number(await getAppSetting('crm_event_reminders_days_before'));
const daysBeforeDefault = Number.isFinite(globalDaysBefore) && globalDaysBefore >= 0
@@ -299,12 +302,43 @@ async function sendReminderForEvent(eventId, { templateGroup = null } = {}) {
// The flow block chooses the template GROUP (blank → the default group); the
// exact template is still auto-picked by event type within that group.
const templateKey = await resolveTemplateKey(row.event_type, templateGroup || DEFAULT_TEMPLATE_GROUP);
const payload = composePayload({ event: row, recipientEmail, daysBefore: offsetDays, businessName });
if (row.event_reminder_body_override) payload.body_override = row.event_reminder_body_override;
await emailProcessor.queueEmail(row.id, recipientEmail, templateKey, payload);
let sent = 0;
for (const r of recipients) {
const payload = composePayload({ event: row, recipientEmail: r.email, daysBefore: offsetDays, businessName });
if (row.event_reminder_body_override) payload.body_override = row.event_reminder_body_override;
// Inline → pass eventId (event language). Assigned account → no eventId so
// the resolver picks the customer's preferred_language.
await emailProcessor.queueEmail(r.fromEvent ? row.id : null, r.email, templateKey, payload);
sent += 1;
}
await db('events').where({ id: row.id }).update({ event_reminder_sent_at: new Date() });
return { sent: 1, skipped: 0, offsetDays };
return { sent, skipped: 0, offsetDays };
}
/**
* Who receives the pre-event reminder for an event: the inline event email if
* present, otherwise the active assigned customer account(s). `fromEvent` flags
* which language path to use (event vs customer).
*/
async function resolveReminderRecipients(eventRow) {
const inline = eventRow.customer_email || eventRow.host_email;
if (inline) return [{ email: inline, fromEvent: true }];
const assigned = await db('event_customer_assignments as a')
.join('customer_accounts as c', 'c.id', 'a.customer_account_id')
.where('a.event_id', eventRow.id)
.where('c.is_active', true)
.whereNotNull('c.email')
.select('c.email');
// De-dup emails defensively (a customer assigned twice, etc.).
const seen = new Set();
const out = [];
for (const a of assigned) {
const e = String(a.email).toLowerCase();
if (!seen.has(e)) { seen.add(e); out.push({ email: a.email, fromEvent: false }); }
}
return out;
}
module.exports = {