Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemenet application specific #464

Open
github-actions bot opened this issue Apr 20, 2023 · 0 comments
Open

Implemenet application specific #464

github-actions bot opened this issue Apr 20, 2023 · 0 comments
Labels

Comments

@github-actions
Copy link

Implemenet application specific

// TODO: Implemenet application specific

import { Injectable } from '@nestjs/common';
import { I18nService } from 'nestjs-i18n';
import { ConfigService } from '@nestjs/config';
import { render } from 'squirrelly';

import resetTemplate from '../../../templates/email/PasswordResetEmail';
import inviteTemplate from '../../../templates/email/Invitation';

import { Credentials, sendEmail } from './helpers/email-helpers';
import { moduleConfig } from './config';

@Injectable()
export default class EmailService {
  // SES credentials
  private readonly credentials: Credentials = {
    region: this.configService.getOrThrow<string>('AWS_MAIN_REGION'),
    accessKeyId: this.configService.getOrThrow<string>(
      'AWS_ADMIN_ACCESS_KEY_ID',
    ),
    secretAccessKey: this.configService.getOrThrow<string>(
      'AWS_ADMIN_SECRET_ACCESS_KEY',
    ),
  };

  constructor(
    private readonly i18nService: I18nService,
    private readonly configService: ConfigService,
  ) {}

  /**
   * Send a custom e-mail invitation to a newly created user.
   *
   * @param recipient - e-mail of the new user
   * @param lang - user's language
   * @param [tempPassword] - user's temporary password
   */
  async sendCustomInviteEmail(
    recipient: string,
    lang: string,
    tempPassword?: string,
  ): Promise<void> {
    // TODO: Implemenet application specific

    // Get sender from module configuration
    const sender = moduleConfig().emailAddress;
    const template: string | undefined = inviteTemplate[lang];
    if (template === undefined) {
      throw new Error(`No invite email template for language ${lang}`);
    }
    // Send actual e-mail
    await sendEmail(
      this.credentials,
      sender,
      recipient,
      this.i18nService.t('authentication.invitation', {
        lang,
      }),
      // E-mail contents from HTML template
      render(template, {
        password: tempPassword,
      }) as string,
    );
  }

  /**
   * Sends an e-mail notifying the user that their password was reset, containing their new temporary password
   *
   * @param recipient - e-mail recipient
   * @param lang - language
   * @param tempPassword - temporary password
   */
  async sendPasswordResetEmail(
    recipient: string,
    lang: string,
    tempPassword: string,
  ): Promise<void> {
    // Get sender from module configuration
    const sender = moduleConfig().emailAddress;
    const template: string | undefined = resetTemplate[lang];

    if (template === undefined) {
      throw new Error(`No password reset template for language ${lang}`);
    }
    // Send actual e-mail
    await sendEmail(
      this.credentials,
      sender,
      recipient,
      this.i18nService.t('authentication.password_reset', {
        lang,
      }),
      // E-mail contents from HTML template
      render(template, {
        password: tempPassword,
      }) as string,
    );
  }
}

ecf8a94aeaaf71079224d05a604533f2e73e44e7

@github-actions github-actions bot added the todo label Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants