@stamhoofd/models 2.135.0 → 2.136.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/factories/EmailTemplateFactory.d.ts +4 -1
- package/dist/factories/EmailTemplateFactory.d.ts.map +1 -1
- package/dist/factories/EmailTemplateFactory.js +8 -0
- package/dist/factories/EmailTemplateFactory.js.map +1 -1
- package/dist/helpers/EmailBuilder.d.ts +34 -1
- package/dist/helpers/EmailBuilder.d.ts.map +1 -1
- package/dist/helpers/EmailBuilder.js +191 -133
- package/dist/helpers/EmailBuilder.js.map +1 -1
- package/dist/migrations/1783987708-email-templates-translations.sql +2 -0
- package/dist/migrations/1783987709-emails-translations.sql +2 -0
- package/dist/migrations/1783987710-email-recipients-language.sql +2 -0
- package/dist/migrations/1784037917-email-templates-language.sql +2 -0
- package/dist/migrations/1784037918-emails-language.sql +2 -0
- package/dist/models/CachedBalance.d.ts +11 -0
- package/dist/models/CachedBalance.d.ts.map +1 -1
- package/dist/models/CachedBalance.js +49 -1
- package/dist/models/CachedBalance.js.map +1 -1
- package/dist/models/Email.d.ts +46 -4
- package/dist/models/Email.d.ts.map +1 -1
- package/dist/models/Email.js +165 -24
- package/dist/models/Email.js.map +1 -1
- package/dist/models/EmailRecipient.d.ts +5 -0
- package/dist/models/EmailRecipient.d.ts.map +1 -1
- package/dist/models/EmailRecipient.js +7 -0
- package/dist/models/EmailRecipient.js.map +1 -1
- package/dist/models/EmailTemplate.d.ts +6 -1
- package/dist/models/EmailTemplate.d.ts.map +1 -1
- package/dist/models/EmailTemplate.js +13 -2
- package/dist/models/EmailTemplate.js.map +1 -1
- package/dist/models/Member.d.ts.map +1 -1
- package/dist/models/Member.js +6 -1
- package/dist/models/Member.js.map +1 -1
- package/dist/models/Order.d.ts.map +1 -1
- package/dist/models/Order.js +1 -0
- package/dist/models/Order.js.map +1 -1
- package/dist/models/RegistrationPeriod.d.ts.map +1 -1
- package/dist/models/RegistrationPeriod.js +6 -0
- package/dist/models/RegistrationPeriod.js.map +1 -1
- package/package.json +4 -3
- package/src/factories/EmailTemplateFactory.ts +12 -1
- package/src/helpers/EmailBuilder.test.ts +291 -0
- package/src/helpers/EmailBuilder.ts +215 -144
- package/src/migrations/1783987708-email-templates-translations.sql +2 -0
- package/src/migrations/1783987709-emails-translations.sql +2 -0
- package/src/migrations/1783987710-email-recipients-language.sql +2 -0
- package/src/migrations/1784037917-email-templates-language.sql +2 -0
- package/src/migrations/1784037918-emails-language.sql +2 -0
- package/src/models/CachedBalance.ts +57 -1
- package/src/models/Email.ts +181 -26
- package/src/models/EmailRecipient.ts +7 -0
- package/src/models/EmailTemplate.ts +12 -2
- package/src/models/Member.ts +8 -1
- package/src/models/Order.ts +1 -0
- package/src/models/RegistrationPeriod.ts +8 -2
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Factory } from '@simonbackx/simple-database';
|
|
2
2
|
import { EmailTemplate } from '../models/index.js';
|
|
3
3
|
import type { Organization } from '../models/Organization.js';
|
|
4
|
-
import type { EmailTemplateType } from '@stamhoofd/structures';
|
|
4
|
+
import type { EmailContent, EmailTemplateType } from '@stamhoofd/structures';
|
|
5
|
+
import type { Language } from '@stamhoofd/types/Language';
|
|
5
6
|
declare class Options {
|
|
6
7
|
organization?: Organization;
|
|
7
8
|
groupId?: string;
|
|
@@ -10,6 +11,8 @@ declare class Options {
|
|
|
10
11
|
subject?: string;
|
|
11
12
|
html?: string;
|
|
12
13
|
text?: string;
|
|
14
|
+
translations?: Map<Language, EmailContent>;
|
|
15
|
+
language?: Language;
|
|
13
16
|
}
|
|
14
17
|
export declare class EmailTemplateFactory extends Factory<Options, EmailTemplate> {
|
|
15
18
|
create(): Promise<EmailTemplate>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EmailTemplateFactory.d.ts","sourceRoot":"","sources":["../../src/factories/EmailTemplateFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAEtD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,KAAK,EAAE,iBAAiB,
|
|
1
|
+
{"version":3,"file":"EmailTemplateFactory.d.ts","sourceRoot":"","sources":["../../src/factories/EmailTemplateFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAEtD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAE7E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE1D,cAAM,OAAO;IACT,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,iBAAiB,CAAC;IAExB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,GAAG,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;IAC3C,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACvB;AAED,qBAAa,oBAAqB,SAAQ,OAAO,CAAC,OAAO,EAAE,aAAa,CAAC;IAC/D,MAAM,IAAI,OAAO,CAAC,aAAa,CAAC;CAoCzC"}
|
|
@@ -9,6 +9,8 @@ class Options {
|
|
|
9
9
|
subject;
|
|
10
10
|
html;
|
|
11
11
|
text;
|
|
12
|
+
translations;
|
|
13
|
+
language;
|
|
12
14
|
}
|
|
13
15
|
export class EmailTemplateFactory extends Factory {
|
|
14
16
|
async create() {
|
|
@@ -32,6 +34,12 @@ export class EmailTemplateFactory extends Factory {
|
|
|
32
34
|
template.text += `${replacement.token}: {{${replacement.token}}}\n`;
|
|
33
35
|
}
|
|
34
36
|
}
|
|
37
|
+
if (this.options.translations) {
|
|
38
|
+
template.translations = this.options.translations;
|
|
39
|
+
}
|
|
40
|
+
if (this.options.language) {
|
|
41
|
+
template.language = this.options.language;
|
|
42
|
+
}
|
|
35
43
|
await template.save();
|
|
36
44
|
return template;
|
|
37
45
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EmailTemplateFactory.js","sourceRoot":"","sources":["../../src/factories/EmailTemplateFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAEtD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAGnD,OAAO,EAAE,aAAa,IAAI,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"EmailTemplateFactory.js","sourceRoot":"","sources":["../../src/factories/EmailTemplateFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAEtD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAGnD,OAAO,EAAE,aAAa,IAAI,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAG7E,MAAM,OAAO;IACT,YAAY,CAAgB;IAC5B,OAAO,CAAU;IACjB,SAAS,CAAU;IACnB,IAAI,CAAoB;IAExB,OAAO,CAAU;IACjB,IAAI,CAAU;IACd,IAAI,CAAU;IACd,YAAY,CAA+B;IAC3C,QAAQ,CAAY;CACvB;AAED,MAAM,OAAO,oBAAqB,SAAQ,OAA+B;IACrE,KAAK,CAAC,MAAM;QACR,MAAM,QAAQ,GAAG,IAAI,aAAa,EAAE,CAAC;QAErC,QAAQ,CAAC,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,IAAI,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AC,OAAO,CAAC,QAAQ,EAAE,CAAC;YACxB,QAAQ,CAAC,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;QAC9C,CAAC;QAED,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACtB,OAAO,QAAQ,CAAC;IACpB,CAAC;CACJ"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { EmailBuilder, EmailInterfaceRecipient } from '@stamhoofd/email';
|
|
2
|
-
import type { EmailRecipient as EmailRecipientStruct, EmailTemplateType, Platform as PlatformStruct, Recipient } from '@stamhoofd/structures';
|
|
2
|
+
import type { EmailContent, EmailRecipient as EmailRecipientStruct, EmailTemplateType, Platform as PlatformStruct, Recipient } from '@stamhoofd/structures';
|
|
3
3
|
import { Replacement } from '@stamhoofd/structures';
|
|
4
|
+
import type { Language } from '@stamhoofd/types/Language';
|
|
5
|
+
import { I18n } from '@stamhoofd/backend-i18n/I18n';
|
|
4
6
|
import { EmailRecipient } from '../models/EmailRecipient.js';
|
|
5
7
|
import { EmailTemplate } from '../models/EmailTemplate.js';
|
|
6
8
|
import type { Group } from '../models/Group.js';
|
|
@@ -30,6 +32,11 @@ export type EmailBuilderOptions = {
|
|
|
30
32
|
replyTo?: EmailInterfaceRecipient | null;
|
|
31
33
|
subject: string;
|
|
32
34
|
html: string;
|
|
35
|
+
/**
|
|
36
|
+
* Full content overrides per language. Recipients with a language that has an override
|
|
37
|
+
* receive that content, all others receive the default subject/html.
|
|
38
|
+
*/
|
|
39
|
+
translations?: Map<Language, EmailContent>;
|
|
33
40
|
attachments?: {
|
|
34
41
|
filename: string;
|
|
35
42
|
path?: string;
|
|
@@ -74,6 +81,31 @@ export declare function stripSensitiveRecipientReplacements(recipient: Recipient
|
|
|
74
81
|
export declare function stripRecipientReplacementsForWebDisplay(recipient: Recipient | EmailRecipientStruct | EmailRecipient, options: {
|
|
75
82
|
organization: Organization | null;
|
|
76
83
|
}): void;
|
|
84
|
+
/**
|
|
85
|
+
* Build the I18n an email should be rendered in for a specific recipient.
|
|
86
|
+
*
|
|
87
|
+
* Falls back to the current (ambient) locale when the recipient has no language set — so flows
|
|
88
|
+
* that already established a locale (order emails wrapped in I18n.runWithLocale, or the request
|
|
89
|
+
* locale) keep working unchanged.
|
|
90
|
+
*/
|
|
91
|
+
export declare function getRecipientI18n(recipient: {
|
|
92
|
+
language?: Language | null;
|
|
93
|
+
}, organization: Organization | null, options?: {
|
|
94
|
+
allowedLanguages?: Language[] | null;
|
|
95
|
+
}): I18n;
|
|
96
|
+
/**
|
|
97
|
+
* Render everything inside `handler` in the recipient's language: all $t / $getLanguage /
|
|
98
|
+
* $getCountry calls (and therefore every generated replacement) use the recipient's locale.
|
|
99
|
+
*
|
|
100
|
+
* This is the single place that binds "a recipient" to "its language" when generating email
|
|
101
|
+
* content. Generate recipient replacements inside this wrapper so new replacements are localized
|
|
102
|
+
* automatically, and pass the provided i18n to helpers that take an explicit locale (e.g. getAppHost).
|
|
103
|
+
*/
|
|
104
|
+
export declare function runWithRecipientLocale<T>(recipient: {
|
|
105
|
+
language?: Language | null;
|
|
106
|
+
}, organization: Organization | null, handler: (i18n: I18n) => T, options?: {
|
|
107
|
+
allowedLanguages?: Language[] | null;
|
|
108
|
+
}): T;
|
|
77
109
|
/**
|
|
78
110
|
* @param options.forPreview if true, it will hide sensitive information in the preview that could leak information to admin users
|
|
79
111
|
*/
|
|
@@ -84,5 +116,6 @@ export declare function fillRecipientReplacements(recipient: Recipient | EmailRe
|
|
|
84
116
|
replyTo: EmailInterfaceRecipient | null;
|
|
85
117
|
forPreview?: boolean;
|
|
86
118
|
forceRefresh?: boolean;
|
|
119
|
+
allowedLanguages?: Language[] | null;
|
|
87
120
|
}): Promise<void>;
|
|
88
121
|
//# sourceMappingURL=EmailBuilder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EmailBuilder.d.ts","sourceRoot":"","sources":["../../src/helpers/EmailBuilder.ts"],"names":[],"mappings":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AE;IAAE,QAAQ,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAA;CAAE,EAAE,YAAY,EAAE,YAAY,GAAG,IAAI,EAAE,OAAO,CAAC,EAAE;IAAE,gBAAgB,CAAC,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAA;CAAE,GAAG,IAAI,CAYvK;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,EAAE,SAAS,EAAE;IAAE,QAAQ,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAA;CAAE,EAAE,YAAY,EAAE,YAAY,GAAG,IAAI,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,CAAC,EAAE,OAAO,CAAC,EAAE;IAAE,gBAAgB,CAAC,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAA;CAAE,GAAG,CAAC,CAGzM;AAED;;GAEG;AACH,wBAAsB,yBAAyB,CAAC,SAAS,EAAE,SAAS,GAAG,oBAAoB,GAAG,cAAc,EAAE,OAAO,EAAE;IACnH,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;IAClC,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,IAAI,EAAE,uBAAuB,GAAG,IAAI,CAAC;IACrC,OAAO,EAAE,uBAAuB,GAAG,IAAI,CAAC;IACxC,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,gBAAgB,CAAC,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;CACxC,iBA4LA"}
|
|
@@ -179,6 +179,7 @@ async function getEmailBuilderForTemplate(organization, options) {
|
|
|
179
179
|
...options,
|
|
180
180
|
subject: template.subject,
|
|
181
181
|
html: template.html,
|
|
182
|
+
translations: template.translations,
|
|
182
183
|
});
|
|
183
184
|
}
|
|
184
185
|
/**
|
|
@@ -225,7 +226,9 @@ export async function getEmailBuilder(organization, email) {
|
|
|
225
226
|
}
|
|
226
227
|
continue;
|
|
227
228
|
}
|
|
228
|
-
|
|
229
|
+
// Localize the unsubscribe page to the recipient's language (Organization.i18n is always Dutch)
|
|
230
|
+
const unsubscribeLocale = organization ? getRecipientI18n(recipient, organization).locale : null;
|
|
231
|
+
const unsubscribeUrl = 'https://' + STAMHOOFD.domains.dashboard + '/' + (unsubscribeLocale ? (unsubscribeLocale + '/') : '') + 'unsubscribe?id=' + encodeURIComponent(unsubscribe.id) + '&token=' + encodeURIComponent(unsubscribe.token) + '&type=' + encodeURIComponent(email.unsubscribeType ?? 'all');
|
|
229
232
|
recipient.replacements.push(Replacement.create({
|
|
230
233
|
token: 'unsubscribeUrl',
|
|
231
234
|
value: unsubscribeUrl,
|
|
@@ -258,9 +261,25 @@ export async function getEmailBuilder(organization, email) {
|
|
|
258
261
|
}
|
|
259
262
|
const queue = email.recipients.slice();
|
|
260
263
|
let emailIndex = 0;
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
+
// The subject and html can differ per recipient language
|
|
265
|
+
const contentCache = new Map();
|
|
266
|
+
const resolveContent = (language) => {
|
|
267
|
+
const cached = contentCache.get(language);
|
|
268
|
+
if (cached) {
|
|
269
|
+
return cached;
|
|
270
|
+
}
|
|
271
|
+
// Strict selection: only use a translation of this email, never fall back to
|
|
272
|
+
// a different template for a missing language (content correctness above language)
|
|
273
|
+
const translation = language !== null ? email.translations?.get(language) : undefined;
|
|
274
|
+
const subject = translation ? translation.subject : email.subject;
|
|
275
|
+
let html = translation ? translation.html : email.html;
|
|
276
|
+
for (const s of email.replaceAll ?? []) {
|
|
277
|
+
html = html.replaceAll(s.from, s.to);
|
|
278
|
+
}
|
|
279
|
+
const result = { subject, html };
|
|
280
|
+
contentCache.set(language, result);
|
|
281
|
+
return result;
|
|
282
|
+
};
|
|
264
283
|
if (queue.length === 0) {
|
|
265
284
|
if (email.callback) {
|
|
266
285
|
email.callback(new SimpleError({
|
|
@@ -275,8 +294,9 @@ export async function getEmailBuilder(organization, email) {
|
|
|
275
294
|
if (!recipient) {
|
|
276
295
|
return undefined;
|
|
277
296
|
}
|
|
278
|
-
const
|
|
279
|
-
const
|
|
297
|
+
const content = resolveContent(recipient.language ?? null);
|
|
298
|
+
const replacedHtml = replaceEmailHtml(content.html, recipient.replacements);
|
|
299
|
+
const replacedSubject = replaceEmailText(content.subject, recipient.replacements);
|
|
280
300
|
emailIndex += 1;
|
|
281
301
|
return {
|
|
282
302
|
from: email.from,
|
|
@@ -434,6 +454,39 @@ export function stripRecipientReplacementsForWebDisplay(recipient, options) {
|
|
|
434
454
|
value: $t('%1E9'),
|
|
435
455
|
}));
|
|
436
456
|
}
|
|
457
|
+
/**
|
|
458
|
+
* Build the I18n an email should be rendered in for a specific recipient.
|
|
459
|
+
*
|
|
460
|
+
* Falls back to the current (ambient) locale when the recipient has no language set — so flows
|
|
461
|
+
* that already established a locale (order emails wrapped in I18n.runWithLocale, or the request
|
|
462
|
+
* locale) keep working unchanged.
|
|
463
|
+
*/
|
|
464
|
+
export function getRecipientI18n(recipient, organization, options) {
|
|
465
|
+
let lang = recipient.language ?? $getLanguage();
|
|
466
|
+
if (options?.allowedLanguages && options.allowedLanguages.length) {
|
|
467
|
+
if (!options.allowedLanguages.includes(lang)) {
|
|
468
|
+
if (options.allowedLanguages.includes($getLanguage())) {
|
|
469
|
+
lang = $getLanguage();
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
lang = options.allowedLanguages[0];
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
return new I18n(lang, organization?.address.country ?? $getCountry());
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Render everything inside `handler` in the recipient's language: all $t / $getLanguage /
|
|
480
|
+
* $getCountry calls (and therefore every generated replacement) use the recipient's locale.
|
|
481
|
+
*
|
|
482
|
+
* This is the single place that binds "a recipient" to "its language" when generating email
|
|
483
|
+
* content. Generate recipient replacements inside this wrapper so new replacements are localized
|
|
484
|
+
* automatically, and pass the provided i18n to helpers that take an explicit locale (e.g. getAppHost).
|
|
485
|
+
*/
|
|
486
|
+
export function runWithRecipientLocale(recipient, organization, handler, options) {
|
|
487
|
+
const i18n = getRecipientI18n(recipient, organization, options);
|
|
488
|
+
return I18n.runWithLocale(i18n, () => handler(i18n));
|
|
489
|
+
}
|
|
437
490
|
/**
|
|
438
491
|
* @param options.forPreview if true, it will hide sensitive information in the preview that could leak information to admin users
|
|
439
492
|
*/
|
|
@@ -442,160 +495,165 @@ export async function fillRecipientReplacements(recipient, options) {
|
|
|
442
495
|
options.platform = await Platform.getSharedPrivateStruct();
|
|
443
496
|
}
|
|
444
497
|
const { organization, platform, from, replyTo } = options;
|
|
445
|
-
|
|
446
|
-
recipient.
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
if (!recipient.replacements.find(r => r.token === 'loginDetails')) {
|
|
498
|
+
// Render every recipient replacement (greeting, loginDetails, balance table, URLs...) in the
|
|
499
|
+
// recipient's own language, so translated emails are consistent end-to-end. Any replacement
|
|
500
|
+
// added below is localized automatically; helpers with an explicit locale get the same i18n.
|
|
501
|
+
await runWithRecipientLocale(recipient, organization, async (i18n) => {
|
|
502
|
+
let recipientUser = null;
|
|
503
|
+
recipient.replacements = recipient.replacements.slice();
|
|
504
|
+
if (options.forPreview) {
|
|
505
|
+
stripSensitiveRecipientReplacements(recipient, options);
|
|
506
|
+
}
|
|
507
|
+
if (!recipient.email && !recipient.userId) {
|
|
508
|
+
const signInUrl = 'https://' + (organization && STAMHOOFD.userMode === 'organization' ? getAppHost('registration', organization, false, i18n) : STAMHOOFD.domains.dashboard) + '/login';
|
|
457
509
|
recipient.replacements.push(Replacement.create({
|
|
458
|
-
token: '
|
|
459
|
-
value:
|
|
510
|
+
token: 'signInUrl',
|
|
511
|
+
value: signInUrl,
|
|
460
512
|
}));
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
if (STAMHOOFD.userMode !== 'platform' && recipientUser && recipientUser.organizationId && recipientUser.organizationId !== (organization?.id ?? null)) {
|
|
467
|
-
console.warn('User organization does not match current organization, ignoring userId', recipient.userId, recipientUser.organizationId, organization?.id ?? null);
|
|
468
|
-
recipientUser = null;
|
|
469
|
-
}
|
|
470
|
-
let signInUrl;
|
|
471
|
-
if (!recipientUser || !recipientUser.hasAccount()) {
|
|
472
|
-
// We can create a special token
|
|
473
|
-
if (recipientUser) {
|
|
474
|
-
signInUrl = 'https://' + (organization && STAMHOOFD.userMode === 'organization' ? getAppHost('registration', organization, false) : STAMHOOFD.domains.dashboard) + '/account-aanmaken?email=' + encodeURIComponent(recipientUser?.email);
|
|
475
|
-
}
|
|
476
|
-
else {
|
|
477
|
-
signInUrl = 'https://' + (organization && STAMHOOFD.userMode === 'organization' ? getAppHost('registration', organization, false) : STAMHOOFD.domains.dashboard) + '/account-aanmaken';
|
|
513
|
+
if (!recipient.replacements.find(r => r.token === 'loginDetails')) {
|
|
514
|
+
recipient.replacements.push(Replacement.create({
|
|
515
|
+
token: 'loginDetails',
|
|
516
|
+
value: '',
|
|
517
|
+
}));
|
|
478
518
|
}
|
|
479
519
|
}
|
|
480
520
|
else {
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
const suffixes = [];
|
|
496
|
-
const { Member } = await import('../models/Member.js');
|
|
497
|
-
const memberIds = await Member.getMemberIdsForUser(recipientUser);
|
|
498
|
-
const members = await Member.getByIDs(...memberIds);
|
|
499
|
-
if (members.length > 0) {
|
|
500
|
-
for (const member of members) {
|
|
501
|
-
suffixes.push($t('%1EC', {
|
|
502
|
-
firstName: Formatter.escapeHtml(member.firstName),
|
|
503
|
-
securityCode: `<span class="style-inline-code">${Formatter.escapeHtml(options.forPreview ? '••••' : Formatter.spaceString(member.details.securityCode ?? '', 4, '-'))}</span>`,
|
|
504
|
-
}));
|
|
521
|
+
// Default signInUrl
|
|
522
|
+
recipientUser = recipient.userId ? await User.select().where('id', recipient.userId).first(false) : await User.getForAuthentication(organization?.id ?? null, recipient.email, { allowWithoutAccount: true });
|
|
523
|
+
if (STAMHOOFD.userMode !== 'platform' && recipientUser && recipientUser.organizationId && recipientUser.organizationId !== (organization?.id ?? null)) {
|
|
524
|
+
console.warn('User organization does not match current organization, ignoring userId', recipient.userId, recipientUser.organizationId, organization?.id ?? null);
|
|
525
|
+
recipientUser = null;
|
|
526
|
+
}
|
|
527
|
+
let signInUrl;
|
|
528
|
+
if (!recipientUser || !recipientUser.hasAccount()) {
|
|
529
|
+
// We can create a special token
|
|
530
|
+
if (recipientUser) {
|
|
531
|
+
signInUrl = 'https://' + (organization && STAMHOOFD.userMode === 'organization' ? getAppHost('registration', organization, false, i18n) : STAMHOOFD.domains.dashboard) + '/account-aanmaken?email=' + encodeURIComponent(recipientUser?.email);
|
|
532
|
+
}
|
|
533
|
+
else {
|
|
534
|
+
signInUrl = 'https://' + (organization && STAMHOOFD.userMode === 'organization' ? getAppHost('registration', organization, false, i18n) : STAMHOOFD.domains.dashboard) + '/account-aanmaken';
|
|
505
535
|
}
|
|
506
536
|
}
|
|
507
537
|
else {
|
|
508
|
-
|
|
538
|
+
signInUrl = 'https://' + (organization && STAMHOOFD.userMode === 'organization' ? getAppHost('registration', organization, false, i18n) : STAMHOOFD.domains.dashboard) + '/login?email=' + encodeURIComponent(recipientUser.email);
|
|
509
539
|
}
|
|
510
|
-
const suffix = suffixes.length > 0 ? (' ' + suffixes.join(' ')) : '';
|
|
511
540
|
recipient.replacements.push(Replacement.create({
|
|
512
|
-
token: '
|
|
513
|
-
value:
|
|
514
|
-
html: recipientUser.hasAccount()
|
|
515
|
-
? `<p class="description"><em>${$t('%1EA', { email: emailEscaped })}${suffix}</em></p>`
|
|
516
|
-
: `<p class="description"><em>${$t('%1EB', { email: emailEscaped })}${suffix}</em></p>`,
|
|
541
|
+
token: 'signInUrl',
|
|
542
|
+
value: signInUrl,
|
|
517
543
|
}));
|
|
518
544
|
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
545
|
+
if (options.forceRefresh) {
|
|
546
|
+
// Remove loginDetails to force refresh
|
|
547
|
+
recipient.replacements = recipient.replacements.filter(r => r.token !== 'loginDetails');
|
|
548
|
+
}
|
|
549
|
+
if (!recipient.replacements.find(r => r.token === 'loginDetails')) {
|
|
550
|
+
if (recipientUser) {
|
|
551
|
+
const emailEscaped = `<strong>${Formatter.escapeHtml(recipientUser.email)}</strong>`;
|
|
552
|
+
const suffixes = [];
|
|
553
|
+
const { Member } = await import('../models/Member.js');
|
|
554
|
+
const memberIds = await Member.getMemberIdsForUser(recipientUser);
|
|
555
|
+
const members = await Member.getByIDs(...memberIds);
|
|
556
|
+
if (members.length > 0) {
|
|
557
|
+
for (const member of members) {
|
|
558
|
+
suffixes.push($t('%1EC', {
|
|
559
|
+
firstName: Formatter.escapeHtml(member.firstName),
|
|
560
|
+
securityCode: `<span class="style-inline-code">${Formatter.escapeHtml(options.forPreview ? '••••' : Formatter.spaceString(member.details.securityCode ?? '', 4, '-'))}</span>`,
|
|
561
|
+
}));
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
else {
|
|
565
|
+
console.log('No member found for user', recipientUser.id);
|
|
566
|
+
}
|
|
567
|
+
const suffix = suffixes.length > 0 ? (' ' + suffixes.join(' ')) : '';
|
|
523
568
|
recipient.replacements.push(Replacement.create({
|
|
524
569
|
token: 'loginDetails',
|
|
525
570
|
value: '',
|
|
526
|
-
html:
|
|
571
|
+
html: recipientUser.hasAccount()
|
|
572
|
+
? `<p class="description"><em>${$t('%1EA', { email: emailEscaped })}${suffix}</em></p>`
|
|
573
|
+
: `<p class="description"><em>${$t('%1EB', { email: emailEscaped })}${suffix}</em></p>`,
|
|
527
574
|
}));
|
|
528
575
|
}
|
|
529
576
|
else {
|
|
577
|
+
if (recipient.email) {
|
|
578
|
+
const emailEscaped = `<strong>${Formatter.escapeHtml(recipient.email)}</strong>`;
|
|
579
|
+
console.log('No user found for email', recipient.email);
|
|
580
|
+
recipient.replacements.push(Replacement.create({
|
|
581
|
+
token: 'loginDetails',
|
|
582
|
+
value: '',
|
|
583
|
+
html: `<p class="description"><em>${$t('%1EB', { email: emailEscaped })}</em></p>`,
|
|
584
|
+
}));
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
recipient.replacements.push(Replacement.create({
|
|
588
|
+
token: 'loginDetails',
|
|
589
|
+
value: '',
|
|
590
|
+
html: '',
|
|
591
|
+
}));
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
if (options.forceRefresh) {
|
|
596
|
+
// Remove loginDetails to force refresh
|
|
597
|
+
recipient.replacements = recipient.replacements.filter(r => r.token !== 'balanceTable' && r.token !== 'outstandingBalance');
|
|
598
|
+
}
|
|
599
|
+
// Load balance of this user
|
|
600
|
+
// todo: only if detected it is used
|
|
601
|
+
if (!recipient.replacements.find(r => r.token === 'balanceTable')) {
|
|
602
|
+
if (organization && recipientUser) {
|
|
603
|
+
const balanceItemModels = await CachedBalance.balanceForObjects(organization.id, [recipientUser.id], ReceivableBalanceType.user);
|
|
604
|
+
const balanceItems = balanceItemModels.map(i => i.getStructure());
|
|
605
|
+
// Get members
|
|
530
606
|
recipient.replacements.push(Replacement.create({
|
|
531
|
-
token: '
|
|
607
|
+
token: 'outstandingBalance',
|
|
608
|
+
value: Formatter.price(balanceItems.reduce((sum, i) => sum + i.priceOpen, 0)),
|
|
609
|
+
}), Replacement.create({
|
|
610
|
+
token: 'balanceTable',
|
|
532
611
|
value: '',
|
|
533
|
-
html:
|
|
612
|
+
html: BalanceItemStruct.getDetailsHTMLTable(balanceItems),
|
|
613
|
+
}));
|
|
614
|
+
}
|
|
615
|
+
else {
|
|
616
|
+
recipient.replacements.push(Replacement.create({
|
|
617
|
+
token: 'outstandingBalance',
|
|
618
|
+
value: Formatter.price(0),
|
|
619
|
+
}), Replacement.create({
|
|
620
|
+
token: 'balanceTable',
|
|
621
|
+
value: '',
|
|
622
|
+
html: BalanceItemStruct.getDetailsHTMLTable([]),
|
|
534
623
|
}));
|
|
535
624
|
}
|
|
536
625
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
}), Replacement.create({
|
|
553
|
-
token: 'balanceTable',
|
|
554
|
-
value: '',
|
|
555
|
-
html: BalanceItemStruct.getDetailsHTMLTable(balanceItems),
|
|
556
|
-
}));
|
|
626
|
+
if (from || replyTo) {
|
|
627
|
+
const fromAddress = replyTo?.email ?? from.email;
|
|
628
|
+
if (fromAddress) {
|
|
629
|
+
recipient.replacements.push(Replacement.create({
|
|
630
|
+
token: 'fromAddress',
|
|
631
|
+
value: fromAddress,
|
|
632
|
+
}));
|
|
633
|
+
}
|
|
634
|
+
const name = replyTo?.name ?? from?.name;
|
|
635
|
+
if (name) {
|
|
636
|
+
recipient.replacements.push(Replacement.create({
|
|
637
|
+
token: 'fromName',
|
|
638
|
+
value: name,
|
|
639
|
+
}));
|
|
640
|
+
}
|
|
557
641
|
}
|
|
558
|
-
|
|
559
|
-
recipient.replacements.push(
|
|
560
|
-
token: 'outstandingBalance',
|
|
561
|
-
value: Formatter.price(0),
|
|
562
|
-
}), Replacement.create({
|
|
563
|
-
token: 'balanceTable',
|
|
564
|
-
value: '',
|
|
565
|
-
html: BalanceItemStruct.getDetailsHTMLTable([]),
|
|
566
|
-
}));
|
|
642
|
+
if (recipient instanceof EmailRecipient) {
|
|
643
|
+
recipient.replacements.push(...recipient.getRecipient().getDefaultReplacements());
|
|
567
644
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
const fromAddress = replyTo?.email ?? from.email;
|
|
571
|
-
if (fromAddress) {
|
|
572
|
-
recipient.replacements.push(Replacement.create({
|
|
573
|
-
token: 'fromAddress',
|
|
574
|
-
value: fromAddress,
|
|
575
|
-
}));
|
|
645
|
+
else {
|
|
646
|
+
recipient.replacements.push(...recipient.getDefaultReplacements());
|
|
576
647
|
}
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
recipient.replacements.push(
|
|
580
|
-
token: 'fromName',
|
|
581
|
-
value: name,
|
|
582
|
-
}));
|
|
648
|
+
if (organization) {
|
|
649
|
+
const extra = organization.meta.getEmailReplacements(organization);
|
|
650
|
+
recipient.replacements.push(...extra);
|
|
583
651
|
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
recipient.replacements.push(...recipient.getRecipient().getDefaultReplacements());
|
|
587
|
-
}
|
|
588
|
-
else {
|
|
589
|
-
recipient.replacements.push(...recipient.getDefaultReplacements());
|
|
590
|
-
}
|
|
591
|
-
if (organization) {
|
|
592
|
-
const extra = organization.meta.getEmailReplacements(organization);
|
|
652
|
+
// Defaults
|
|
653
|
+
const extra = platform.config.getEmailReplacements(platform);
|
|
593
654
|
recipient.replacements.push(...extra);
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
recipient.replacements.push(...extra);
|
|
598
|
-
// Remove duplicates
|
|
599
|
-
cleanReplacements(recipient.replacements);
|
|
655
|
+
// Remove duplicates
|
|
656
|
+
cleanReplacements(recipient.replacements);
|
|
657
|
+
}, options);
|
|
600
658
|
}
|
|
601
659
|
//# sourceMappingURL=EmailBuilder.js.map
|