email - reorg (not compiling)
MODIFY
src/assets/i18n/en.json
src/assets/i18n/en.json
"userID": "User ID"
},
"email": {
- "user": {
+ "common": {
"username" : "Hello ! {{user.firstName}}"
},
+ "new-registered-user": {
+ "title":"Reset Password",
"buttonText":"E-Mobility",
+ "text":"You have just requested to reset your password.\nClick on the link below to generate and receive a new one."
+ },
+ "request-password": {
"title":"Reset Password",
+ "buttonText":"E-Mobility",
"text":"You have just requested to reset your password.\nClick on the link below to generate and receive a new one."
}
}
+}

MODIFY
src/notification/email/EMailNotificationTask.ts
src/notification/email/EMailNotificationTask.ts
-import { AccountVerificationNotification, AdminAccountVerificationNotification, BillingAccountActivationNotification, BillingAccountCreationLinkNotification, BillingInvoiceSynchronizationFailedNotification, BillingNewInvoiceNotification, BillingUserSynchronizationFailedNotification, CarCatalogSynchronizationFailedNotification, ChargingStationRegisteredNotification, ChargingStationStatusErrorNotification, ComputeAndApplyChargingProfilesFailedNotification, EmailNotificationMessage, EndOfChargeNotification, EndOfSessionNotification, EndOfSignedSessionNotification, EndUserErrorNotification, NewRegisteredUserNotification, NotificationSeverity, OCPIPatchChargingStationsStatusesErrorNotification, OICPPatchChargingStationsErrorNotification, OICPPatchChargingStationsStatusesErrorNotification, OfflineChargingStationNotification, OptimalChargeReachedNotification, PreparingSessionNotStartedNotification, RequestPasswordNotification, SessionNotStartedNotification, TransactionStartedNotification, UnknownUserBadgedNotification, UserAccountInactivityNotification, UserAccountStatusChangedNotification, UserCreatePassword, VerificationEmailNotification } from '../../types/UserNotifications';
+/* eslint-disable max-len */
+import { AccountVerificationNotification, AdminAccountVerificationNotification, BaseNotification, BillingAccountActivationNotification, BillingAccountCreationLinkNotification, BillingInvoiceSynchronizationFailedNotification, BillingNewInvoiceNotification, BillingUserSynchronizationFailedNotification, CarCatalogSynchronizationFailedNotification, ChargingStationRegisteredNotification, ChargingStationStatusErrorNotification, ComputeAndApplyChargingProfilesFailedNotification, EmailNotificationMessage, EndOfChargeNotification, EndOfSessionNotification, EndOfSignedSessionNotification, EndUserErrorNotification, NewRegisteredUserNotification, NotificationSeverity, OCPIPatchChargingStationsStatusesErrorNotification, OICPPatchChargingStationsErrorNotification, OICPPatchChargingStationsStatusesErrorNotification, OfflineChargingStationNotification, OptimalChargeReachedNotification, PreparingSessionNotStartedNotification, RequestPasswordNotification, SessionNotStartedNotification, TransactionStartedNotification, UnknownUserBadgedNotification, UserAccountInactivityNotification, UserAccountStatusChangedNotification, UserCreatePassword, VerificationEmailNotification } from '../../types/UserNotifications';
import { BUTTON, CONFIG, FOOTER, HEADER, TEXT1, TEXT2, TITLE } from './mjmlComponents';
import FeatureToggles, { Feature } from '../../utils/FeatureToggles';
import { Message, SMTPClient, SMTPError } from 'emailjs';
@@ -54,10 +55,16 @@ export default class EMailNotificationTask implements NotificationTask {
}
public async sendNewRegisteredUser(data: NewRegisteredUserNotification, user: User, tenant: Tenant, severity: NotificationSeverity): Promise<void> {
+ if (FeatureToggles.isFeatureActive(Feature.NEW_EMAIL_TEMPLATES)) {
+ return this.sendSmartEmail('new-registered-user', data, user, tenant, severity);
+ }
return this.prepareAndSendEmail('new-registered-user', data, user, tenant, severity);
}
public async sendRequestPassword(data: RequestPasswordNotification, user: User, tenant: Tenant, severity: NotificationSeverity): Promise<void> {
+ if (FeatureToggles.isFeatureActive(Feature.NEW_EMAIL_TEMPLATES)) {
+ return this.sendSmartEmail('request-password', data, user, tenant, severity);
+ }
return this.prepareAndSendEmail('request-password', data, user, tenant, severity);
}
@@ -299,31 +306,19 @@ export default class EMailNotificationTask implements NotificationTask {
}
}
- private async prepareAndSendEmail(templateName: string, data: any, user: User, tenant: Tenant, severity: NotificationSeverity, useSmtpClientBackup = false): Promise<void> {
+ private async sendSmartEmail(prefix: string, context: BaseNotification, user: User, tenant: Tenant, severity: NotificationSeverity, useSmtpClientBackup = false): Promise<void> {
let startTime: number;
let emailContent = {} as EmailNotificationMessage;
try {
startTime = Logging.traceNotificationStart();
- if (!user) {
- throw new BackendError({
- action: ServerAction.EMAIL_NOTIFICATION,
- module: MODULE_NAME, method: 'prepareAndSendEmail',
- message: `No User is provided for '${templateName}'`
- });
- }
- if (!user.email) {
+ if (!user?.email) {
throw new BackendError({
- actionOnUser: user,
action: ServerAction.EMAIL_NOTIFICATION,
module: MODULE_NAME, method: 'prepareAndSendEmail',
- message: `No email is provided for User for '${templateName}'`
+ message: 'User is mandatory'
});
}
-
- // Nader's Code
- if (FeatureToggles.isFeatureActive(Feature.NEW_EMAIL_TEMPLATES)) {
- // create the template
-
+ // Create the template
const template = new mjmlBuilder()
.addConfig(await ComponentsManager.getComponent(CONFIG))
.addHeader(await ComponentsManager.getComponent(HEADER))
@@ -333,24 +328,55 @@ export default class EMailNotificationTask implements NotificationTask {
.addToBody(await ComponentsManager.getComponent(TEXT2))
.addFooter(await ComponentsManager.getComponent(FOOTER))
.buildTemplate();
-
- // resolve
+ // Resolve
const i18nInstance = I18nManager.getInstanceForLocale(user.locale);
-
- template.resolve(i18nInstance,data as Record<string,unknown>);
+ template.resolve(i18nInstance, context);
const html = template.getHtml();
-
emailContent = {
to: user.email,
- subject: 'Create Account',
+ subject: i18nInstance.translate(`email.${prefix}.title`, context),
text: html,
html: html
};
-
- await this.sendEmail(emailContent, data, tenant, user, severity, useSmtpClientBackup);
- return;
+ await this.sendEmail(emailContent, context, tenant, user, severity, useSmtpClientBackup);
+ } catch (error) {
+ await Logging.logError({
+ tenantID: tenant.id,
+ siteID: context?.siteID,
+ siteAreaID: context?.siteAreaID,
+ companyID: context?.companyID,
+ chargingStationID: context?.chargeBoxID,
+ action: ServerAction.EMAIL_NOTIFICATION,
+ module: MODULE_NAME, method: 'prepareAndSendEmail',
+ message: 'Error in preparing email for user',
+ actionOnUser: user,
+ detailedMessages: { error: error.stack }
+ });
+ } finally {
+ await Logging.traceNotificationEnd(tenant, MODULE_NAME, 'prepareAndSendEmail', startTime, templateName, emailContent, user.id);
+ }
}
+ private async prepareAndSendEmail(templateName: string, data: any, user: User, tenant: Tenant, severity: NotificationSeverity, useSmtpClientBackup = false): Promise<void> {
+ let startTime: number;
+ let emailContent = {} as EmailNotificationMessage;
+ try {
+ startTime = Logging.traceNotificationStart();
+ if (!user) {
+ throw new BackendError({
+ action: ServerAction.EMAIL_NOTIFICATION,
+ module: MODULE_NAME, method: 'prepareAndSendEmail',
+ message: `No User is provided for '${templateName}'`
+ });
+ }
+ if (!user.email) {
+ throw new BackendError({
+ actionOnUser: user,
+ action: ServerAction.EMAIL_NOTIFICATION,
+ module: MODULE_NAME, method: 'prepareAndSendEmail',
+ message: `No email is provided for User for '${templateName}'`
+ });
+ }
// Fetch the template
const emailTemplate = await TemplateManager.getInstanceForLocale(user.locale).getTemplate(templateName);
if (!emailTemplate) {

MODIFY
src/types/UserNotifications.ts
src/types/UserNotifications.ts
@@ -96,7 +96,7 @@ export interface EmailNotificationMessage {
html: string;
}
-interface BaseNotification {
+export interface BaseNotification {
tenantLogoURL?: string;
}

email - reorg (not compiling)