From 0f4c8b2051cae17fbdd7e02017ad5b41c61e210c Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Wed, 11 Dec 2024 18:26:57 +0100 Subject: [PATCH 1/4] refactor: move to new mailer --- .../RunnerSelfServiceController.ts | 6 +- src/mailer.ts | 64 +++++++++++++++---- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/controllers/RunnerSelfServiceController.ts b/src/controllers/RunnerSelfServiceController.ts index ec98eda..f708576 100644 --- a/src/controllers/RunnerSelfServiceController.ts +++ b/src/controllers/RunnerSelfServiceController.ts @@ -131,7 +131,7 @@ export class RunnerSelfServiceController { const token = JwtCreator.createSelfService(runner); try { - await Mailer.sendSelfserviceForgottenMail(runner.email, token, locale) + await Mailer.sendSelfserviceForgottenMail(runner.email, runner.id, runner.firstname, runner.lastname, token, locale) } catch (error) { throw new MailSendingError(); } @@ -157,7 +157,7 @@ export class RunnerSelfServiceController { response.token = JwtCreator.createSelfService(runner); try { - await Mailer.sendSelfserviceWelcomeMail(runner.email, response.token, locale) + await Mailer.sendSelfserviceWelcomeMail(runner.email, runner.id, runner.firstname, runner.lastname, response.token, locale) } catch (error) { throw new MailSendingError(); } @@ -182,7 +182,7 @@ export class RunnerSelfServiceController { response.token = JwtCreator.createSelfService(runner); try { - await Mailer.sendSelfserviceWelcomeMail(runner.email, response.token, locale) + await Mailer.sendSelfserviceWelcomeMail(runner.email, runner.id, runner.firstname, runner.lastname, response.token, locale) } catch (error) { throw new MailSendingError(); } diff --git a/src/mailer.ts b/src/mailer.ts index fae23d1..5fbf5bd 100644 --- a/src/mailer.ts +++ b/src/mailer.ts @@ -18,9 +18,19 @@ export class Mailer { */ public static async sendResetMail(to_address: string, token: string, locale: string = "en") { try { - await axios.post(`${Mailer.base}/reset?locale=${locale}&key=${Mailer.key}`, { - address: to_address, - resetKey: token + 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; } @@ -32,12 +42,26 @@ export class Mailer { * 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, token: string, locale: string = "en") { + */ + public static async sendSelfserviceWelcomeMail(to_address: string, runner_id: number, firstname: string, lastname: string, token: string, locale: string = "en") { try { - await axios.post(`${Mailer.base}/registration?locale=${locale}&key=${Mailer.key}`, { - address: to_address, - selfserviceToken: token + 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} ${lastname}`, + barcode_content: `${runner_id}`, + link: 'https://portal.lauf-fuer-kaya.de/profile/' + token + } + } }); } catch (error) { if (Mailer.testing) { return true; } @@ -49,12 +73,26 @@ export class Mailer { * 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, token: string, locale: string = "en") { + */ + public static async sendSelfserviceForgottenMail(to_address: string, runner_id: number, firstname: string, lastname: string, token: string, locale: string = "en") { try { - await axios.post(`${Mailer.base}/registration_forgot?locale=${locale}&key=${Mailer.key}`, { - address: to_address, - selfserviceToken: token + 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} ${lastname}`, + barcode_content: `${runner_id}`, + link: 'https://portal.lauf-fuer-kaya.de/profile/' + token + } + } }); } catch (error) { if (Mailer.testing) { return true; } From 6eff2438035b368eb45931fad9402a6cb942b350 Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Wed, 11 Dec 2024 18:34:40 +0100 Subject: [PATCH 2/4] feat: middlename --- src/controllers/RunnerSelfServiceController.ts | 6 +++--- src/mailer.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/controllers/RunnerSelfServiceController.ts b/src/controllers/RunnerSelfServiceController.ts index f708576..5168e0c 100644 --- a/src/controllers/RunnerSelfServiceController.ts +++ b/src/controllers/RunnerSelfServiceController.ts @@ -131,7 +131,7 @@ export class RunnerSelfServiceController { const token = JwtCreator.createSelfService(runner); try { - await Mailer.sendSelfserviceForgottenMail(runner.email, runner.id, runner.firstname, runner.lastname, token, locale) + await Mailer.sendSelfserviceForgottenMail(runner.email, runner.id, runner.firstname, runner.middlename, runner.lastname, token, locale) } catch (error) { throw new MailSendingError(); } @@ -157,7 +157,7 @@ export class RunnerSelfServiceController { response.token = JwtCreator.createSelfService(runner); try { - await Mailer.sendSelfserviceWelcomeMail(runner.email, runner.id, runner.firstname, runner.lastname, response.token, locale) + await Mailer.sendSelfserviceWelcomeMail(runner.email, runner.id, runner.firstname, runner.middlename, runner.lastname, response.token, locale) } catch (error) { throw new MailSendingError(); } @@ -182,7 +182,7 @@ export class RunnerSelfServiceController { response.token = JwtCreator.createSelfService(runner); try { - await Mailer.sendSelfserviceWelcomeMail(runner.email, runner.id, runner.firstname, runner.lastname, response.token, locale) + await Mailer.sendSelfserviceWelcomeMail(runner.email, runner.id, runner.firstname, runner.middlename, runner.lastname, response.token, locale) } catch (error) { throw new MailSendingError(); } diff --git a/src/mailer.ts b/src/mailer.ts index 5fbf5bd..e658af6 100644 --- a/src/mailer.ts +++ b/src/mailer.ts @@ -43,7 +43,7 @@ export class Mailer { * @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, lastname: string, token: string, locale: string = "en") { + 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', @@ -57,7 +57,7 @@ export class Mailer { templateName: 'welcome', language: locale, data: { - name: `${firstname} ${lastname}`, + name: `${firstname} ${middlename} ${lastname}`, barcode_content: `${runner_id}`, link: 'https://portal.lauf-fuer-kaya.de/profile/' + token } @@ -74,7 +74,7 @@ export class Mailer { * @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, lastname: string, token: string, locale: string = "en") { + 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', @@ -88,7 +88,7 @@ export class Mailer { templateName: 'welcome', language: locale, data: { - name: `${firstname} ${lastname}`, + name: `${firstname} ${middlename} ${lastname}`, barcode_content: `${runner_id}`, link: 'https://portal.lauf-fuer-kaya.de/profile/' + token } From 296ba8ddab1dba46f8201829d9a7e5fc1c88c0f8 Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Wed, 11 Dec 2024 18:40:21 +0100 Subject: [PATCH 3/4] FRONTEND_URL env --- .env.example | 3 ++- README.md | 1 + src/mailer.ts | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 565a391..b180f2c 100644 --- a/.env.example +++ b/.env.example @@ -7,4 +7,5 @@ DB_PASSWORD=bla DB_NAME=./test.sqlite NODE_ENV=production POSTALCODE_COUNTRYCODE=DE -SEED_TEST_DATA=false \ No newline at end of file +SEED_TEST_DATA=false +FRONTEND_URL=bla \ No newline at end of file diff --git a/README.md b/README.md index cbbfcd0..038cac9 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ pnpm docs | SEED_TEST_DATA | Boolean | False | If you want the app to seed some example data set this to true | | MAILER_URL | String(Url) | N/A | The mailer's base url (no trailing slash) | | MAILER_KEY | String | N/A | The mailer's api key. | +| FRONTEND_URL | String(Url) | N/A | The link to frontend (no trailing slash) | | IMPRINT_URL | String(Url) | /imprint | The link to a imprint page for the system (Defaults to the frontend's imprint) | | PRIVACY_URL | String(Url) | /privacy | The link to a privacy page for the system (Defaults to the frontend's privacy page) | diff --git a/src/mailer.ts b/src/mailer.ts index e658af6..0a419b2 100644 --- a/src/mailer.ts +++ b/src/mailer.ts @@ -59,7 +59,7 @@ export class Mailer { data: { name: `${firstname} ${middlename} ${lastname}`, barcode_content: `${runner_id}`, - link: 'https://portal.lauf-fuer-kaya.de/profile/' + token + link: `${process.env.FRONTEND_URL}/profile/${token}` } } }); @@ -90,7 +90,7 @@ export class Mailer { data: { name: `${firstname} ${middlename} ${lastname}`, barcode_content: `${runner_id}`, - link: 'https://portal.lauf-fuer-kaya.de/profile/' + token + link: `${process.env.FRONTEND_URL}/profile/${token}` } } }); From 765ef849035ca4f8b2253bb76d15be8e9a3e6763 Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Wed, 11 Dec 2024 18:43:11 +0100 Subject: [PATCH 4/4] SELFSERVICE_URL --- .env.example | 2 +- README.md | 2 +- src/mailer.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index b180f2c..5a0dec3 100644 --- a/.env.example +++ b/.env.example @@ -8,4 +8,4 @@ DB_NAME=./test.sqlite NODE_ENV=production POSTALCODE_COUNTRYCODE=DE SEED_TEST_DATA=false -FRONTEND_URL=bla \ No newline at end of file +SELFSERVICE_URL=bla \ No newline at end of file diff --git a/README.md b/README.md index 038cac9..ea2200f 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ pnpm docs | SEED_TEST_DATA | Boolean | False | If you want the app to seed some example data set this to true | | MAILER_URL | String(Url) | N/A | The mailer's base url (no trailing slash) | | MAILER_KEY | String | N/A | The mailer's api key. | -| FRONTEND_URL | String(Url) | N/A | The link to frontend (no trailing slash) | +| SELFSERVICE_URL | String(Url) | N/A | The link to selfservice (no trailing slash) | | IMPRINT_URL | String(Url) | /imprint | The link to a imprint page for the system (Defaults to the frontend's imprint) | | PRIVACY_URL | String(Url) | /privacy | The link to a privacy page for the system (Defaults to the frontend's privacy page) | diff --git a/src/mailer.ts b/src/mailer.ts index 0a419b2..447df70 100644 --- a/src/mailer.ts +++ b/src/mailer.ts @@ -59,7 +59,7 @@ export class Mailer { data: { name: `${firstname} ${middlename} ${lastname}`, barcode_content: `${runner_id}`, - link: `${process.env.FRONTEND_URL}/profile/${token}` + link: `${process.env.SELFSERVICE_URL}/profile/${token}` } } }); @@ -90,7 +90,7 @@ export class Mailer { data: { name: `${firstname} ${middlename} ${lastname}`, barcode_content: `${runner_id}`, - link: `${process.env.FRONTEND_URL}/profile/${token}` + link: `${process.env.SELFSERVICE_URL}/profile/${token}` } } });