import axios from 'axios'; import { config } from './config'; import { MailSendingError } from './errors/MailErrors'; /** * This class is responsible for all things mail sending. * This uses axios to communicate with the mailer api (https://git.odit.services/lfk/mailer). */ export class Mailer { public static base: string = config.mailer_url; public static key: string = config.mailer_key; public static testing: boolean = config.testing; /** * Function for sending a password reset mail. * @param to_address The address the mail will be sent to. Should always get pulled from a user object. * @param token The requested password reset token - will be combined with the app_url to generate a password reset link. */ public static async sendResetMail(to_address: string, token: string, locale: string = "en") { try { await axios.request({ method: 'POST', url: `${Mailer.base}/api/v1/email`, headers: { authorization: `Bearer ${Mailer.key}`, 'content-type': 'application/json' }, data: { to: to_address, templateName: 'password-reset', language: locale, data: { token: token } } }); } catch (error) { if (Mailer.testing) { return true; } throw new MailSendingError(); } } /** * Function for sending a runner selfservice welcome mail. * @param to_address The address the mail will be sent to. Should always get pulled from a runner object. * @param token The requested selfservice token - will be combined with the app_url to generate a selfservice profile link. */ public static async sendSelfserviceWelcomeMail(to_address: string, runner_id: number, firstname: string, middlename: string, lastname: string, token: string, locale: string = "en") { try { await axios.request({ method: 'POST', url: `${Mailer.base}/api/v1/email`, headers: { authorization: `Bearer ${Mailer.key}`, 'content-type': 'application/json' }, data: { to: to_address, templateName: 'welcome', language: locale, data: { name: `${firstname} ${middlename} ${lastname}`, barcode_content: `${runner_id}`, link: `${process.env.SELFSERVICE_URL}/profile/${token}` } } }); } catch (error) { if (Mailer.testing) { return true; } throw new MailSendingError(); } } /** * Function for sending a runner selfservice link forgotten mail. * @param to_address The address the mail will be sent to. Should always get pulled from a runner object. * @param token The requested selfservice token - will be combined with the app_url to generate a selfservice profile link. */ public static async sendSelfserviceForgottenMail(to_address: string, runner_id: number, firstname: string, middlename: string, lastname: string, token: string, locale: string = "en") { try { await axios.request({ method: 'POST', url: `${Mailer.base}/api/v1/email`, headers: { authorization: `Bearer ${Mailer.key}`, 'content-type': 'application/json' }, data: { to: to_address, templateName: 'welcome', language: locale, data: { name: `${firstname} ${middlename} ${lastname}`, barcode_content: `${runner_id}`, link: `${process.env.SELFSERVICE_URL}/profile/${token}` } } }); } catch (error) { if (Mailer.testing) { return true; } throw new MailSendingError(); } } }