Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,030 changes: 647 additions & 383 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,23 @@ export class Configuration {
liqMail: process.env.LIQ_MAIL || 'liq@dfx.swiss',
noReplyMail: process.env.NOREPLY_MAIL || 'noreply@dfx.swiss',
},
wallet: {
onchainlabs: {
template: 'onChainLabs',
},
...(process.env.REALUNIT_MAIL_USER && {
RealUnit: {
host: 'mail.infomaniak.com',
port: 587,
secure: false,
user: process.env.REALUNIT_MAIL_USER,
pass: process.env.REALUNIT_MAIL_PASS,
fromAddress: process.env.REALUNIT_MAIL_USER,
displayName: 'RealUnit',
template: 'user-v2',
},
}),
},
};

coinGecko = {
Expand Down
28 changes: 17 additions & 11 deletions src/subdomains/supporting/notification/entities/mail/base/mail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetConfig } from 'src/config/config';
import { Config } from 'src/config/config';
import { Notification, NotificationOptions } from '../../notification.entity';

export interface MailParamBase {
Expand All @@ -9,6 +9,7 @@ export interface MailParamBase {
cc?: string;
bcc?: string;
template?: string;
walletName?: string;
options?: NotificationOptions;
correlationId?: string;
}
Expand All @@ -31,29 +32,30 @@ export interface MailParamsNew extends MailParamBase {
}

export class Mail extends Notification {
readonly #from: { name: string; address: string } = {
name: 'DFX.swiss',
address: GetConfig().mail.contact.noReplyMail,
};
readonly #from: { name: string; address: string };
readonly #to: string | string[];
readonly #cc: string;
readonly #bcc: string;
readonly #subject: string;
readonly #template: string = GetConfig().mail.defaultMailTemplate;
readonly #template: string;
readonly #templateParams: { [name: string]: any };
readonly #walletName?: string;

constructor(params: MailParams | MailParamsNew) {
super();

const walletMailConfig = params.walletName ? Config.mail.wallet[params.walletName] : undefined;

this.#walletName = params.walletName;
this.#to = params.to;
this.#subject = params.subject;
this.#from = {
name: params.displayName ?? 'DFX.swiss',
address: params.from ?? GetConfig().mail.contact.noReplyMail,
name: params.displayName ?? walletMailConfig?.displayName ?? 'DFX.swiss',
address: params.from ?? walletMailConfig?.fromAddress ?? Config.mail.contact.noReplyMail,
};
this.#cc = params.cc ?? this.#cc;
this.#bcc = params.bcc ?? this.#bcc;
this.#template = params.template ?? this.#template;
this.#cc = params.cc;
this.#bcc = params.bcc;
this.#template = params.template ?? Config.mail.defaultMailTemplate;
this.#templateParams = params.templateParams;
}

Expand Down Expand Up @@ -85,4 +87,8 @@ export class Mail extends Notification {
get subject(): string {
return this.#subject;
}

get walletName(): string | undefined {
return this.#walletName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ export class UserMailV2 extends Mail {
instagramUrl: Config.social.instagram,
};

const walletMailConfig = wallet?.name ? Config.mail.wallet[wallet.name] : undefined;

super({
...params,
template: wallet?.name === 'onchainlabs' ? 'onChainLabs' : 'user-v2',
walletName: wallet?.name,
template: walletMailConfig?.template ?? 'user-v2',
templateParams: { ...defaultParams, ...params },
});
}
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion src/subdomains/supporting/notification/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export enum NotificationType {
export enum MailType {
GENERIC = 'Generic',
ERROR_MONITORING = 'ErrorMonitoring',
USER_DEPRECATED = 'UserDeprecated',
USER_V2 = 'UserV2',
PERSONAL = 'Personal',
INTERNAL = 'Internal',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Mail, MailParams } from '../entities/mail/base/mail';
import { ErrorMonitoringMail, ErrorMonitoringMailInput } from '../entities/mail/error-monitoring-mail';
import { InternalMail, MailRequestInternalInput } from '../entities/mail/internal-mail';
import { MailRequestPersonalInput, PersonalMail } from '../entities/mail/personal-mail';
import { MailRequestUserInput, UserMail, UserMailTable } from '../entities/mail/user-mail';
import { MailRequestUserInputV2, UserMailV2 } from '../entities/mail/user-mail-v2';
import { MailContext, MailContextType, MailContextTypeMapper, MailType } from '../enums';
import { MailAffix, MailRequest, MailRequestGenericInput, TranslationItem, TranslationParams } from '../interfaces';
Expand Down Expand Up @@ -82,10 +81,6 @@ export class MailFactory {
return this.createErrorMonitoringMail(request);
}

case MailType.USER_DEPRECATED: {
return this.createUserMail(request);
}

case MailType.USER_V2: {
return this.createUserV2Mail(request);
}
Expand Down Expand Up @@ -146,27 +141,6 @@ export class MailFactory {
return new ErrorMonitoringMail({ subject, errors, correlationId, options });
}

private createUserMail(request: MailRequest): UserMail {
const { correlationId, options } = request;
const { userData, wallet, title, salutation, prefix, suffix, table } = request.input as MailRequestUserInput;

const lang = userData.language.symbol;

return new UserMail(
{
to: userData.mail,
subject: this.translate(title, lang),
salutation: salutation && this.translate(salutation.key, lang, salutation.params),
prefix: prefix && this.getMailAffix(prefix, lang),
table: table && this.getTable(table, lang),
suffix: suffix && this.getMailAffix(suffix, lang),
correlationId,
options,
},
wallet,
);
}

private createUserV2Mail(request: MailRequest): UserMailV2 {
const { correlationId, options, context } = request;
const { userData, wallet, title, salutation, texts } = request.input as MailRequestUserInputV2;
Expand Down Expand Up @@ -227,13 +201,6 @@ export class MailFactory {
);
}

private getTable(table: Record<string, string>, lang: string): UserMailTable[] {
return Object.entries(Util.removeNullFields(table)).map(([key, value]) => ({
text: this.translate(key, lang),
value: value,
}));
}

private getMailAffix(affix: TranslationItem[], lang = 'en'): MailAffix[] {
return affix
.filter((i) => i)
Expand Down
2 changes: 0 additions & 2 deletions src/subdomains/supporting/notification/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ErrorMonitoringMailInput } from '../entities/mail/error-monitoring-mail';
import { MailRequestInternalInput } from '../entities/mail/internal-mail';
import { MailRequestPersonalInput } from '../entities/mail/personal-mail';
import { MailRequestUserInput } from '../entities/mail/user-mail';
import { MailRequestUserInputV2 } from '../entities/mail/user-mail-v2';
import { NotificationOptions } from '../entities/notification.entity';
import { MailContext, MailType } from '../enums';
Expand All @@ -12,7 +11,6 @@ export interface MailRequest {
input:
| MailRequestGenericInput
| ErrorMonitoringMailInput
| MailRequestUserInput
| MailRequestUserInputV2
| MailRequestPersonalInput
| MailRequestInternalInput;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { MailerModule } from '@nestjs-modules/mailer';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GetConfig } from 'src/config/config';
import { SharedModule } from 'src/shared/shared.module';
import { Notification } from './entities/notification.entity';
import { MailFactory } from './factories/mail.factory';
Expand All @@ -12,7 +10,7 @@ import { NotificationJobService } from './services/notification-job.service';
import { NotificationService } from './services/notification.service';

@Module({
imports: [TypeOrmModule.forFeature([Notification]), MailerModule.forRoot(GetConfig().mail.options), SharedModule],
imports: [TypeOrmModule.forFeature([Notification]), SharedModule],
providers: [NotificationRepository, MailService, NotificationService, MailFactory, NotificationJobService],
controllers: [NotificationController],
exports: [NotificationService, MailFactory],
Expand Down
66 changes: 58 additions & 8 deletions src/subdomains/supporting/notification/services/mail.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import { MailerOptions, MailerService } from '@nestjs-modules/mailer';
import { MailerOptions } from '@nestjs-modules/mailer';
import { Injectable } from '@nestjs/common';
import * as fs from 'fs';
import * as handlebars from 'handlebars';
import * as nodemailer from 'nodemailer';
import { join } from 'path';
import { Config, Environment } from 'src/config/config';
import { DfxLogger } from 'src/shared/services/dfx-logger';
import { Mail } from '../entities/mail/base/mail';

// custom wallet config for UserMailV2
export interface WalletMailConfig {
host: string;
port: number;
secure: boolean; // true for 465, false for STARTTLS on 587
user: string;
pass: string;
fromAddress: string;
displayName: string;
template: string;
}

export interface MailOptions {
options: MailerOptions;
defaultMailTemplate: string;
Expand All @@ -13,30 +29,31 @@ export interface MailOptions {
liqMail: string;
noReplyMail: string;
};
wallet: Record<string, Partial<WalletMailConfig>>;
}

@Injectable()
export class MailService {
private readonly logger = new DfxLogger(MailService);

constructor(private readonly mailerService: MailerService) {}
private readonly transports = new Map<string, nodemailer.Transporter>();

async send(mail: Mail): Promise<void> {
// Skip mail sending in local environment
if (Config.environment === Environment.LOC) {
this.logger.info(`[LOCAL DEV] Mail skipped - to: ${mail.to}, subject: ${mail.subject}`);
return;
}

try {
await this.mailerService.sendMail({
from: mail.from,
const transport = this.getTransport(mail.walletName);
const html = this.compileTemplate(mail.template, mail.templateParams);

await transport.sendMail({
from: { name: mail.from.name, address: mail.from.address },
to: mail.to,
cc: mail.cc,
bcc: mail.bcc,
subject: mail.subject,
template: mail.template,
context: mail.templateParams,
html,
});
} catch (e) {
this.logger.error(
Expand All @@ -46,4 +63,37 @@ export class MailService {
throw e;
}
}

private getTransport(walletName?: string): nodemailer.Transporter {
const walletConfig = walletName ? Config.mail.wallet[walletName] : undefined;
const key = walletConfig?.host ? walletName : 'default';

let transport = this.transports.get(key);
if (!transport) {
transport = this.createTransport(walletConfig);
this.transports.set(key, transport);
}

return transport;
}

private createTransport(walletConfig?: Partial<WalletMailConfig>): nodemailer.Transporter {
if (walletConfig?.host) {
return nodemailer.createTransport({
host: walletConfig.host,
port: walletConfig.port,
secure: walletConfig.secure,
auth: { user: walletConfig.user, pass: walletConfig.pass },
tls: { rejectUnauthorized: false },
});
}

return nodemailer.createTransport(Config.mail.options.transport as nodemailer.TransportOptions);
}

private compileTemplate(template: string, params: Record<string, unknown>): string {
const templatePath = join(Config.mail.options.template.dir, `${template}.hbs`);
const templateContent = fs.readFileSync(templatePath, 'utf-8');
return handlebars.compile(templateContent)(params);
}
}
Loading
Loading