Update notification.service.ts
This commit is contained in:
@@ -376,7 +376,21 @@ export class NotificationService {
|
|||||||
? translatedPayload.details
|
? translatedPayload.details
|
||||||
: JSON.stringify(translatedPayload.details || {}, null, 2))
|
: JSON.stringify(translatedPayload.details || {}, null, 2))
|
||||||
};
|
};
|
||||||
const requestBody = this._renderTemplate(config.bodyTemplate || defaultBodyTemplate, templateDataWebhook, defaultBody); // Use new signature (3 args)
|
let templateToRender = config.bodyTemplate || defaultBodyTemplate;
|
||||||
|
let isCustomTemplate = !!config.bodyTemplate;
|
||||||
|
|
||||||
|
if (isCustomTemplate) {
|
||||||
|
console.log(`[_sendWebhook] Original custom body template for event ${payload.event}:`, templateToRender);
|
||||||
|
// --- PRE-PROCESS TEMPLATE: Replace {event} with {eventDisplay} ---
|
||||||
|
templateToRender = templateToRender.replace(/\{event\}/g, '{eventDisplay}');
|
||||||
|
console.log(`[_sendWebhook] Pre-processed body template (replaced {event} with {eventDisplay}):`, templateToRender);
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
} else {
|
||||||
|
console.log(`[_sendWebhook] No custom body template found. Using default template for event ${payload.event}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render the potentially modified template or the default one
|
||||||
|
const requestBody = this._renderTemplate(templateToRender, templateDataWebhook, defaultBody);
|
||||||
|
|
||||||
const requestConfig: AxiosRequestConfig = {
|
const requestConfig: AxiosRequestConfig = {
|
||||||
method: config.method || 'POST',
|
method: config.method || 'POST',
|
||||||
@@ -455,20 +469,17 @@ export class NotificationService {
|
|||||||
console.log(`[_sendEmail] Prepared templateDataEmailBody for event ${payload.event}:`, templateDataEmailBody);
|
console.log(`[_sendEmail] Prepared templateDataEmailBody for event ${payload.event}:`, templateDataEmailBody);
|
||||||
|
|
||||||
let body = '';
|
let body = '';
|
||||||
// Correctly construct the default body text using translated/formatted variables
|
|
||||||
const defaultBodyText = `Event: ${eventDisplayName}\nTimestamp: ${formattedTimestampForEmail}\nDetails:\n${detailsString}`;
|
const defaultBodyText = `Event: ${eventDisplayName}\nTimestamp: ${formattedTimestampForEmail}\nDetails:\n${detailsString}`;
|
||||||
|
|
||||||
// Check if the user provided a bodyTemplate in the config
|
|
||||||
if (config.bodyTemplate) {
|
if (config.bodyTemplate) {
|
||||||
// Use custom body template if provided
|
|
||||||
let templateToRender = config.bodyTemplate;
|
let templateToRender = config.bodyTemplate;
|
||||||
console.log(`[_sendEmail] Original custom body template for event ${payload.event}:`, templateToRender);
|
console.log(`[_sendEmail] Original custom body template for event ${payload.event}:`, templateToRender);
|
||||||
|
|
||||||
// --- PRE-PROCESS TEMPLATE: Replace {event} with {eventDisplay} ---
|
|
||||||
// This ensures the translated event name is used even if the user's template uses {event}
|
|
||||||
templateToRender = templateToRender.replace(/\{event\}/g, '{eventDisplay}');
|
templateToRender = templateToRender.replace(/\{event\}/g, '{eventDisplay}');
|
||||||
console.log(`[_sendEmail] Pre-processed body template (replaced {event} with {eventDisplay}):`, templateToRender);
|
console.log(`[_sendEmail] Pre-processed body template (replaced {event} with {eventDisplay}):`, templateToRender);
|
||||||
// -----------------------------------------------------------------
|
|
||||||
|
|
||||||
// Render the potentially modified template.
|
// Render the potentially modified template.
|
||||||
body = this._renderTemplate(templateToRender, templateDataEmailBody, defaultBodyText);
|
body = this._renderTemplate(templateToRender, templateDataEmailBody, defaultBodyText);
|
||||||
@@ -476,20 +487,14 @@ export class NotificationService {
|
|||||||
// No custom template, use the directly constructed default text
|
// No custom template, use the directly constructed default text
|
||||||
console.log(`[_sendEmail] No custom body template found. Using default constructed body text for event ${payload.event}`);
|
console.log(`[_sendEmail] No custom body template found. Using default constructed body text for event ${payload.event}`);
|
||||||
body = defaultBodyText;
|
body = defaultBodyText;
|
||||||
// Removed lines related to unused bodyKey:
|
|
||||||
// const bodyKey = `eventBody.${payload.event}`;
|
|
||||||
// console.log(`[_sendEmail] No custom body template found. Using i18n body key '${bodyKey}' for event ${payload.event}`);
|
|
||||||
// body = i18next.t(bodyKey, { ... });
|
|
||||||
}
|
}
|
||||||
console.log(`[_sendEmail] Final email body for event ${payload.event}:\n${body}`);
|
console.log(`[_sendEmail] Final email body for event ${payload.event}:\n${body}`);
|
||||||
|
|
||||||
const mailOptions: Mail.Options = {
|
const mailOptions: Mail.Options = {
|
||||||
from: config.from,
|
from: config.from,
|
||||||
to: config.to,
|
to: config.to,
|
||||||
subject: subject, // Use the fixed subject
|
subject: subject,
|
||||||
text: body, // Use the rendered/translated body
|
text: body,
|
||||||
// Consider adding an html property if your templates/i18n generate HTML
|
|
||||||
// html: bodyHtml,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -518,47 +523,33 @@ export class NotificationService {
|
|||||||
} else if (typeof payload.details === 'string') {
|
} else if (typeof payload.details === 'string') {
|
||||||
detailsText = payload.details;
|
detailsText = payload.details;
|
||||||
} else {
|
} else {
|
||||||
// Generic fallback for unhandled object details
|
|
||||||
detailsText = JSON.stringify(payload.details);
|
detailsText = JSON.stringify(payload.details);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(`[_sendTelegram] Formatted detailsText:`, detailsText);
|
console.log(`[_sendTelegram] Formatted detailsText:`, detailsText);
|
||||||
|
|
||||||
// 3. Prepare data for template placeholders AND i18n interpolation (NO escaping here)
|
|
||||||
// Get the translated event name using the correct key 'event.<EVENT_NAME>'
|
|
||||||
const translatedEventName = i18next.t(`event.${payload.event}`, { lng: userLang, defaultValue: payload.event });
|
const translatedEventName = i18next.t(`event.${payload.event}`, { lng: userLang, defaultValue: payload.event });
|
||||||
|
|
||||||
const templateData: Record<string, string> = {
|
const templateData: Record<string, string> = {
|
||||||
// Assign the *translated* event name to the 'event' key (NO escaping)
|
|
||||||
event: translatedEventName,
|
event: translatedEventName,
|
||||||
// NEW: Format timestamp using user's timezone for Telegram (adjust format as needed)
|
|
||||||
timestamp: formatInTimeZone(new Date(payload.timestamp), userTimezone, "yyyy-MM-dd HH:mm:ss zzz"),
|
timestamp: formatInTimeZone(new Date(payload.timestamp), userTimezone, "yyyy-MM-dd HH:mm:ss zzz"),
|
||||||
// Formatted details string (NO escaping)
|
|
||||||
details: detailsText
|
details: detailsText
|
||||||
// Note: We no longer create eventDisplay key
|
|
||||||
};
|
};
|
||||||
console.log(`[_sendTelegram] Prepared templateData (NO escaping):`, JSON.stringify(templateData, null, 2));
|
console.log(`[_sendTelegram] Prepared templateData (NO escaping):`, JSON.stringify(templateData, null, 2));
|
||||||
|
|
||||||
// 4. Handle template
|
// 4. Handle template
|
||||||
let messageText = '';
|
let messageText = '';
|
||||||
if (config.messageTemplate) {
|
if (config.messageTemplate) {
|
||||||
// User has a custom template, render it using prepared data
|
|
||||||
console.log(`[_sendTelegram] Using custom template:`, config.messageTemplate);
|
console.log(`[_sendTelegram] Using custom template:`, config.messageTemplate);
|
||||||
// Provide a fallback text in case rendering fails
|
|
||||||
const fallbackForCustom = `Event: ${templateData.event}, Details: ${templateData.details}`; // Use 'event' key now
|
const fallbackForCustom = `Event: ${templateData.event}, Details: ${templateData.details}`; // Use 'event' key now
|
||||||
messageText = this._renderTemplate(config.messageTemplate, templateData, fallbackForCustom);
|
messageText = this._renderTemplate(config.messageTemplate, templateData, fallbackForCustom);
|
||||||
} else {
|
} else {
|
||||||
// No custom template, use i18n to generate the full body
|
|
||||||
const i18nKey = `eventBody.${payload.event}`;
|
const i18nKey = `eventBody.${payload.event}`;
|
||||||
console.log(`[_sendTelegram] Using i18n template key:`, i18nKey);
|
console.log(`[_sendTelegram] Using i18n template key:`, i18nKey);
|
||||||
// Define a fallback structure using the prepared data (NO escaping)
|
const fallbackBody = `*Fallback Notification*\nEvent: ${templateData.event}\nTime: \`${templateData.timestamp}\`\nDetails: ${templateData.details}`;
|
||||||
const fallbackBody = `*Fallback Notification*\nEvent: ${templateData.event}\nTime: \`${templateData.timestamp}\`\nDetails: ${templateData.details}`; // Use 'event' key now
|
|
||||||
// Pass the prepared data for interpolation within the i18n translation
|
|
||||||
messageText = i18next.t(i18nKey, {
|
messageText = i18next.t(i18nKey, {
|
||||||
lng: userLang,
|
lng: userLang,
|
||||||
...templateData, // Pass all prepared data for interpolation (event, timestamp, details)
|
...templateData,
|
||||||
// If specific i18n keys need raw data (like the keys array), add them here:
|
|
||||||
// updatedKeys: (payload.event === 'SETTINGS_UPDATED' && Array.isArray(payload.details?.updatedKeys)) ? payload.details.updatedKeys.join(', ') : '',
|
|
||||||
defaultValue: fallbackBody
|
defaultValue: fallbackBody
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user