@stamhoofd/models 2.137.4 → 2.138.0

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.
Files changed (157) hide show
  1. package/dist/helpers/Handlebars.d.ts.map +1 -1
  2. package/dist/helpers/Handlebars.js +1 -1
  3. package/dist/helpers/Handlebars.js.map +1 -1
  4. package/dist/helpers/errorToSimpleErrors.d.ts +3 -0
  5. package/dist/helpers/errorToSimpleErrors.d.ts.map +1 -0
  6. package/dist/helpers/errorToSimpleErrors.js +17 -0
  7. package/dist/helpers/errorToSimpleErrors.js.map +1 -0
  8. package/dist/index.d.ts +0 -1
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +0 -1
  11. package/dist/index.js.map +1 -1
  12. package/dist/migrations/1782902314-tokens-authenticated-at.sql +2 -0
  13. package/dist/migrations/1782902315-create-mfa-totp-table.sql +13 -0
  14. package/dist/migrations/1782902316-create-webauthn-credentials-table.sql +17 -0
  15. package/dist/migrations/1782902317-create-mfa-recovery-codes-table.sql +10 -0
  16. package/dist/migrations/1782902318-create-mfa-tokens-table.sql +13 -0
  17. package/dist/migrations/1782902319-create-webauthn-challenges-table.sql +10 -0
  18. package/dist/migrations/1784039947-mfa-totp-last-used-counter.sql +2 -0
  19. package/dist/migrations/1784122354-webauthn-credential-provider.sql +3 -0
  20. package/dist/migrations/1784500000-webauthn-credential-rp-id.sql +9 -0
  21. package/dist/migrations/1785354091-platform-parent-tenant-id.sql +1 -0
  22. package/dist/migrations/1785354092-platform-fees-tenant-id.sql +1 -0
  23. package/dist/migrations/1785354093-platform-uri.sql +1 -0
  24. package/dist/migrations/1785354094-platform-domain.sql +1 -0
  25. package/dist/migrations/1785354095-platform-uri-unique.sql +1 -0
  26. package/dist/migrations/1785354096-platform-domain-unique.sql +1 -0
  27. package/dist/migrations/1785354097-platform-parent-tenant-id-index.sql +1 -0
  28. package/dist/migrations/1785354098-platform-tenant-backfill.d.ts +4 -0
  29. package/dist/migrations/1785354098-platform-tenant-backfill.d.ts.map +1 -0
  30. package/dist/migrations/1785354098-platform-tenant-backfill.js +10 -0
  31. package/dist/migrations/1785354098-platform-tenant-backfill.js.map +1 -0
  32. package/dist/migrations/1785417301-users-last-active-at.sql +2 -0
  33. package/dist/models/Document.d.ts +0 -4
  34. package/dist/models/Document.d.ts.map +1 -1
  35. package/dist/models/Document.js +1 -75
  36. package/dist/models/Document.js.map +1 -1
  37. package/dist/models/DocumentTemplate.d.ts +0 -3
  38. package/dist/models/DocumentTemplate.d.ts.map +1 -1
  39. package/dist/models/DocumentTemplate.js +1 -63
  40. package/dist/models/DocumentTemplate.js.map +1 -1
  41. package/dist/models/Email.d.ts +1 -29
  42. package/dist/models/Email.d.ts.map +1 -1
  43. package/dist/models/Email.js +4 -823
  44. package/dist/models/Email.js.map +1 -1
  45. package/dist/models/EmailVerificationCode.d.ts +0 -5
  46. package/dist/models/EmailVerificationCode.d.ts.map +1 -1
  47. package/dist/models/EmailVerificationCode.js +0 -84
  48. package/dist/models/EmailVerificationCode.js.map +1 -1
  49. package/dist/models/Image.d.ts.map +1 -1
  50. package/dist/models/Image.js +11 -14
  51. package/dist/models/Image.js.map +1 -1
  52. package/dist/models/MFARecoveryCode.d.ts +17 -0
  53. package/dist/models/MFARecoveryCode.d.ts.map +1 -0
  54. package/dist/models/MFARecoveryCode.js +55 -0
  55. package/dist/models/MFARecoveryCode.js.map +1 -0
  56. package/dist/models/MFATOTP.d.ts +44 -0
  57. package/dist/models/MFATOTP.d.ts.map +1 -0
  58. package/dist/models/MFATOTP.js +116 -0
  59. package/dist/models/MFATOTP.js.map +1 -0
  60. package/dist/models/MFAToken.d.ts +49 -0
  61. package/dist/models/MFAToken.d.ts.map +1 -0
  62. package/dist/models/MFAToken.js +137 -0
  63. package/dist/models/MFAToken.js.map +1 -0
  64. package/dist/models/MemberPlatformMembership.d.ts +0 -8
  65. package/dist/models/MemberPlatformMembership.d.ts.map +1 -1
  66. package/dist/models/MemberPlatformMembership.js +1 -242
  67. package/dist/models/MemberPlatformMembership.js.map +1 -1
  68. package/dist/models/Order.d.ts +1 -32
  69. package/dist/models/Order.d.ts.map +1 -1
  70. package/dist/models/Order.js +1 -182
  71. package/dist/models/Order.js.map +1 -1
  72. package/dist/models/Organization.d.ts +1 -51
  73. package/dist/models/Organization.d.ts.map +1 -1
  74. package/dist/models/Organization.js +1 -530
  75. package/dist/models/Organization.js.map +1 -1
  76. package/dist/models/PasswordToken.d.ts +0 -8
  77. package/dist/models/PasswordToken.d.ts.map +1 -1
  78. package/dist/models/PasswordToken.js +0 -23
  79. package/dist/models/PasswordToken.js.map +1 -1
  80. package/dist/models/Platform.d.ts +28 -4
  81. package/dist/models/Platform.d.ts.map +1 -1
  82. package/dist/models/Platform.js +111 -50
  83. package/dist/models/Platform.js.map +1 -1
  84. package/dist/models/STPackage.d.ts +1 -6
  85. package/dist/models/STPackage.d.ts.map +1 -1
  86. package/dist/models/STPackage.js +1 -98
  87. package/dist/models/STPackage.js.map +1 -1
  88. package/dist/models/Token.d.ts +40 -1
  89. package/dist/models/Token.d.ts.map +1 -1
  90. package/dist/models/Token.js +68 -2
  91. package/dist/models/Token.js.map +1 -1
  92. package/dist/models/User.d.ts +25 -0
  93. package/dist/models/User.d.ts.map +1 -1
  94. package/dist/models/User.js +36 -0
  95. package/dist/models/User.js.map +1 -1
  96. package/dist/models/WebauthnChallenge.d.ts +27 -0
  97. package/dist/models/WebauthnChallenge.d.ts.map +1 -0
  98. package/dist/models/WebauthnChallenge.js +81 -0
  99. package/dist/models/WebauthnChallenge.js.map +1 -0
  100. package/dist/models/WebauthnCredential.d.ts +58 -0
  101. package/dist/models/WebauthnCredential.d.ts.map +1 -0
  102. package/dist/models/WebauthnCredential.js +144 -0
  103. package/dist/models/WebauthnCredential.js.map +1 -0
  104. package/dist/models/index.d.ts +5 -0
  105. package/dist/models/index.d.ts.map +1 -1
  106. package/dist/models/index.js +5 -0
  107. package/dist/models/index.js.map +1 -1
  108. package/package.json +3 -3
  109. package/src/helpers/Handlebars.ts +5 -9
  110. package/src/helpers/errorToSimpleErrors.ts +17 -0
  111. package/src/index.ts +0 -1
  112. package/src/migrations/1782902314-tokens-authenticated-at.sql +2 -0
  113. package/src/migrations/1782902315-create-mfa-totp-table.sql +13 -0
  114. package/src/migrations/1782902316-create-webauthn-credentials-table.sql +17 -0
  115. package/src/migrations/1782902317-create-mfa-recovery-codes-table.sql +10 -0
  116. package/src/migrations/1782902318-create-mfa-tokens-table.sql +13 -0
  117. package/src/migrations/1782902319-create-webauthn-challenges-table.sql +10 -0
  118. package/src/migrations/1784039947-mfa-totp-last-used-counter.sql +2 -0
  119. package/src/migrations/1784122354-webauthn-credential-provider.sql +3 -0
  120. package/src/migrations/1784500000-webauthn-credential-rp-id.sql +9 -0
  121. package/src/migrations/1785354091-platform-parent-tenant-id.sql +1 -0
  122. package/src/migrations/1785354092-platform-fees-tenant-id.sql +1 -0
  123. package/src/migrations/1785354093-platform-uri.sql +1 -0
  124. package/src/migrations/1785354094-platform-domain.sql +1 -0
  125. package/src/migrations/1785354095-platform-uri-unique.sql +1 -0
  126. package/src/migrations/1785354096-platform-domain-unique.sql +1 -0
  127. package/src/migrations/1785354097-platform-parent-tenant-id-index.sql +1 -0
  128. package/src/migrations/1785354098-platform-tenant-backfill.ts +14 -0
  129. package/src/migrations/1785417301-users-last-active-at.sql +2 -0
  130. package/src/models/Document.ts +1 -84
  131. package/src/models/DocumentTemplate.ts +1 -71
  132. package/src/models/Email.ts +5 -941
  133. package/src/models/EmailVerificationCode.ts +0 -96
  134. package/src/models/Image.ts +11 -14
  135. package/src/models/MFARecoveryCode.ts +52 -0
  136. package/src/models/MFATOTP.ts +112 -0
  137. package/src/models/MFAToken.ts +138 -0
  138. package/src/models/MemberPlatformMembership.ts +1 -285
  139. package/src/models/Order.ts +1 -204
  140. package/src/models/Organization.ts +2 -637
  141. package/src/models/PasswordToken.ts +0 -29
  142. package/src/models/Platform.test.ts +167 -3
  143. package/src/models/Platform.ts +125 -50
  144. package/src/models/STPackage.ts +1 -110
  145. package/src/models/Token.ts +73 -2
  146. package/src/models/User.ts +37 -0
  147. package/src/models/WebauthnChallenge.ts +80 -0
  148. package/src/models/WebauthnCredential.ts +130 -0
  149. package/src/models/index.ts +6 -0
  150. package/dist/helpers/EmailBuilder.d.ts +0 -121
  151. package/dist/helpers/EmailBuilder.d.ts.map +0 -1
  152. package/dist/helpers/EmailBuilder.js +0 -659
  153. package/dist/helpers/EmailBuilder.js.map +0 -1
  154. package/src/helpers/EmailBuilder.test.ts +0 -291
  155. package/src/helpers/EmailBuilder.ts +0 -818
  156. package/src/models/Email.test.ts +0 -1222
  157. package/src/models/MemberPlatformMembership.test.ts +0 -70
@@ -1,1222 +0,0 @@
1
- import type { LimitedFilteredRequest } from '@stamhoofd/structures';
2
- import { BalanceItemType, EmailRecipientFilter, EmailRecipientsStatus, EmailRecipient as EmailRecipientStruct, EmailRecipientSubfilter, EmailStatus, OrganizationMetaData, PaginatedResponse } from '@stamhoofd/structures';
3
- import { Email } from './Email.js';
4
- import { EmailRecipient } from './EmailRecipient.js';
5
- import { EmailMocker } from '@stamhoofd/email';
6
- import { STExpect, TestUtils } from '@stamhoofd/test-utils';
7
- import { OrganizationFactory } from '../factories/OrganizationFactory.js';
8
- import { Platform } from './Platform.js';
9
- import { BalanceItemFactory, MemberFactory, UserFactory } from '../factories/index.js';
10
- import { CachedBalance } from './CachedBalance.js';
11
- import { Formatter } from '@stamhoofd/utility';
12
- import { EmailRecipientFilterType } from '@stamhoofd/structures/email/EmailRecipientFilterType.js';
13
-
14
- async function buildEmail(data: {
15
- recipients: EmailRecipientStruct[];
16
- } & Partial<Email>) {
17
- Email.recipientLoaders.set(EmailRecipientFilterType.Members, {
18
- fetch: async (query: LimitedFilteredRequest) => {
19
- return Promise.resolve(
20
- new PaginatedResponse<EmailRecipientStruct[], LimitedFilteredRequest>({
21
- results: data.recipients,
22
- next: undefined,
23
- }),
24
- );
25
- },
26
-
27
- count: async (query: LimitedFilteredRequest) => {
28
- return data.recipients.length;
29
- },
30
- });
31
-
32
- const model = new Email();
33
- model.userId = null;
34
- model.organizationId = data.organizationId ?? null;
35
- model.recipientFilter = EmailRecipientFilter.create({
36
- filters: [
37
- EmailRecipientSubfilter.create({
38
- type: EmailRecipientFilterType.Members,
39
- filter: { id: { $in: ['test'] } },
40
- }),
41
- ],
42
- });
43
-
44
- model.subject = data.subject ?? 'This is a test email';
45
- model.html = data.html ?? '<p>This is a test email</p> {{unsubscribeUrl}}';
46
- model.text = data.text ?? 'This is a test email {{unsubscribeUrl}}';
47
- model.json = data.json ?? {};
48
- model.status = data.status ?? EmailStatus.Draft;
49
- model.attachments = [];
50
- model.fromAddress = data.fromAddress ?? 'test@stamhoofd.be';
51
- model.fromName = data.fromName ?? null;
52
- model.emailType = data.emailType ?? null;
53
-
54
- await model.save();
55
-
56
- return model;
57
- }
58
-
59
- /**
60
- * An email that won't be able to fetch recipients
61
- */
62
- async function buildFailEmail(data: {
63
- recipients: EmailRecipientStruct[];
64
- } & Partial<Email>) {
65
- Email.recipientLoaders.set(EmailRecipientFilterType.Members, {
66
- fetch: async (query: LimitedFilteredRequest) => {
67
- throw new Error('This is a simulated error while fetching recipients');
68
- },
69
-
70
- count: async (query: LimitedFilteredRequest) => {
71
- return data.recipients.length;
72
- },
73
- });
74
-
75
- const model = new Email();
76
- model.userId = null;
77
- model.organizationId = data.organizationId ?? null;
78
- model.recipientFilter = EmailRecipientFilter.create({
79
- filters: [
80
- EmailRecipientSubfilter.create({
81
- type: EmailRecipientFilterType.Members,
82
- filter: { id: { $in: ['test'] } },
83
- }),
84
- ],
85
- });
86
-
87
- model.subject = data.subject ?? 'This is a test email';
88
- model.html = data.html ?? '<p>This is a test email</p> {{unsubscribeUrl}}';
89
- model.text = data.text ?? 'This is a test email {{unsubscribeUrl}}';
90
- model.json = data.json ?? {};
91
- model.status = data.status ?? EmailStatus.Draft;
92
- model.attachments = [];
93
- model.fromAddress = data.fromAddress ?? 'test@stamhoofd.be';
94
- model.fromName = data.fromName ?? null;
95
- model.emailType = data.emailType ?? null;
96
-
97
- await model.save();
98
-
99
- return model;
100
- }
101
-
102
- describe('Model.Email', () => {
103
- it('Correctly whitelists email recipients', async () => {
104
- TestUtils.setEnvironment('WHITELISTED_EMAIL_DESTINATIONS', ['*@stamhoofd-allowed-domain-tests.be']);
105
- const model = await buildEmail({
106
- recipients: [
107
- EmailRecipientStruct.create({
108
- firstName: 'Test',
109
- lastName: 'Test',
110
- email: 'example@not-whitelisted-domain.be',
111
- }),
112
- EmailRecipientStruct.create({
113
- firstName: 'Test',
114
- lastName: 'Test',
115
- email: 'example2@stamhoofd-allowed-domain-tests.be',
116
- }),
117
- ],
118
- });
119
- await model.queueForSending(true);
120
- await model.refresh();
121
-
122
- // Check if it was sent correctly
123
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
124
- expect(model.emailRecipientsCount).toBe(2);
125
- expect(model.succeededCount).toBe(1);
126
- expect(model.failedCount).toBe(1);
127
- expect(model.emailErrors).toBe(null);
128
- expect(model.recipientsErrors).toBe(null);
129
- expect(model.status).toBe(EmailStatus.Sent);
130
- expect(await EmailMocker.getSucceededCount()).toBe(1);
131
- expect(await EmailMocker.getFailedCount()).toBe(0); // never tried to send any failed emails (whitelist)
132
-
133
- // Load recipietns
134
- const recipients = await EmailRecipient.select().where('emailId', model.id).fetch();
135
- expect(recipients).toIncludeSameMembers([
136
- expect.objectContaining({
137
- email: 'example2@stamhoofd-allowed-domain-tests.be',
138
- sentAt: expect.any(Date),
139
- failCount: 0,
140
- failErrorMessage: null,
141
- firstFailedAt: null,
142
- lastFailedAt: null,
143
- }),
144
- expect.objectContaining({
145
- email: 'example@not-whitelisted-domain.be',
146
- sentAt: null,
147
- failCount: 1,
148
- failErrorMessage: 'All recipients are filtered due to environment',
149
- firstFailedAt: expect.any(Date),
150
- lastFailedAt: expect.any(Date),
151
- }),
152
- ]);
153
- }, 15_000);
154
-
155
- it('Retries emails on network errors', async () => {
156
- const model = await buildEmail({
157
- recipients: [
158
- EmailRecipientStruct.create({
159
- firstName: 'Test',
160
- lastName: 'Test',
161
- email: 'example@domain.be',
162
- }),
163
- EmailRecipientStruct.create({
164
- firstName: 'Test',
165
- lastName: 'Test',
166
- email: 'example2@domain.be',
167
- }),
168
- ],
169
- });
170
-
171
- // Only one failure (the first email)
172
- // It should automatically retry to send the email
173
- EmailMocker.broadcast.failNext(new Error('This is a simulated network error'));
174
-
175
- await model.queueForSending(true);
176
- await model.refresh();
177
-
178
- // Check if it was sent correctly
179
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
180
- expect(model.emailRecipientsCount).toBe(2);
181
- expect(model.status).toBe(EmailStatus.Sent);
182
- expect(model.succeededCount).toBe(2);
183
- expect(model.failedCount).toBe(0);
184
- expect(model.softFailedCount).toBe(0);
185
-
186
- // Both have succeeded
187
- expect(await EmailMocker.getSucceededCount()).toBe(2);
188
- expect(await EmailMocker.getFailedCount()).toBe(1); // One retry
189
-
190
- // Load recipietns
191
- const recipients = await EmailRecipient.select().where('emailId', model.id).fetch();
192
- expect(recipients).toIncludeSameMembers([
193
- expect.objectContaining({
194
- email: 'example@domain.be',
195
- sentAt: expect.any(Date),
196
- failCount: 0,
197
- }),
198
- expect.objectContaining({
199
- email: 'example2@domain.be',
200
- sentAt: expect.any(Date),
201
- failCount: 0,
202
- }),
203
- ]);
204
- }, 15_000);
205
-
206
- it('Email changes to failed state if email recipients fails to build', async () => {
207
- const model = await buildFailEmail({
208
- recipients: [
209
- EmailRecipientStruct.create({
210
- firstName: 'Test',
211
- lastName: 'Test',
212
- email: 'example@domain.be',
213
- }),
214
- EmailRecipientStruct.create({
215
- firstName: 'Test',
216
- lastName: 'Test',
217
- email: 'example2@domain.be',
218
- }),
219
- ],
220
- });
221
-
222
- await expect(model.queueForSending(true)).rejects.toThrow();
223
- await model.refresh();
224
-
225
- // Check if it was sent correctly
226
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.NotCreated);
227
- expect(model.recipientsErrors).toEqual(STExpect.simpleErrors([{
228
- code: 'unknown_error',
229
- message: 'This is a simulated error while fetching recipients',
230
- }]));
231
-
232
- expect(model.emailRecipientsCount).toBe(null);
233
- expect(model.status).toBe(EmailStatus.Failed);
234
- expect(model.succeededCount).toBe(0);
235
- expect(model.failedCount).toBe(0);
236
- expect(model.softFailedCount).toBe(0);
237
-
238
- // Both have succeeded
239
- expect(await EmailMocker.getSucceededCount()).toBe(0);
240
- expect(await EmailMocker.getFailedCount()).toBe(0); // One retry
241
- }, 15_000);
242
-
243
- it('Marks email recipient as failed if fails three times', async () => {
244
- const model = await buildEmail({
245
- recipients: [
246
- EmailRecipientStruct.create({
247
- firstName: 'Test',
248
- lastName: 'Test',
249
- email: 'example@domain.be',
250
- }),
251
- EmailRecipientStruct.create({
252
- firstName: 'Test',
253
- lastName: 'Test',
254
- email: 'example2@domain.be',
255
- }),
256
- ],
257
- });
258
-
259
- EmailMocker.broadcast.failNext(new Error('This is a simulated network error 1'));
260
- EmailMocker.broadcast.failNext(new Error('This is a simulated network error 2'));
261
- EmailMocker.broadcast.failNext(new Error('This is a simulated network error 3'));
262
- EmailMocker.broadcast.failNext(new Error('This is a simulated network error 4'));
263
- EmailMocker.broadcast.failNext(new Error('This is a simulated network error 5'));
264
- EmailMocker.broadcast.failNext(new Error('This is a simulated network error 6'));
265
-
266
- await model.queueForSending(true);
267
- await model.refresh();
268
-
269
- // Check if it was sent correctly
270
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
271
- expect(model.emailRecipientsCount).toBe(2);
272
- expect(model.status).toBe(EmailStatus.Sent);
273
- expect(model.succeededCount).toBe(0);
274
- expect(model.failedCount).toBe(2);
275
- expect(model.softFailedCount).toBe(0);
276
-
277
- // Both have succeeded
278
- expect(await EmailMocker.getSucceededCount()).toBe(0);
279
- expect(await EmailMocker.getFailedCount()).toBe(6); // Two retries for each recipient
280
-
281
- // Load recipietns
282
- const recipients = await EmailRecipient.select().where('emailId', model.id).fetch();
283
- expect(recipients).toIncludeSameMembers([
284
- expect.objectContaining({
285
- email: 'example@domain.be',
286
- sentAt: null,
287
- failCount: 1,
288
- failError: STExpect.simpleErrors([{
289
- message: /This is a simulated network error ([3-6])/,
290
- }]),
291
- firstFailedAt: expect.any(Date),
292
- lastFailedAt: expect.any(Date),
293
- }),
294
- expect.objectContaining({
295
- email: 'example2@domain.be',
296
- sentAt: null,
297
- failCount: 1,
298
- failError: STExpect.simpleErrors([{
299
- message: /This is a simulated network error ([3-6])/,
300
- }]),
301
- firstFailedAt: expect.any(Date),
302
- lastFailedAt: expect.any(Date),
303
- }),
304
- ]);
305
- }, 15_000);
306
-
307
- it('Can handle recipients without name', async () => {
308
- const model = await buildEmail({
309
- recipients: [
310
- EmailRecipientStruct.create({
311
- email: 'example@domain.be',
312
- }),
313
- ],
314
- });
315
-
316
- await model.queueForSending(true);
317
- await model.refresh();
318
-
319
- // Check if it was sent correctly
320
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
321
- expect(model.emailRecipientsCount).toBe(1);
322
- expect(model.status).toBe(EmailStatus.Sent);
323
- expect(model.succeededCount).toBe(1);
324
- expect(model.failedCount).toBe(0);
325
- expect(model.softFailedCount).toBe(0);
326
-
327
- // Both have succeeded
328
- expect(await EmailMocker.getSucceededCount()).toBe(1);
329
- expect(await EmailMocker.getFailedCount()).toBe(0);
330
-
331
- // Load recipietns
332
- const recipients = await EmailRecipient.select().where('emailId', model.id).fetch();
333
- expect(recipients).toIncludeSameMembers([
334
- expect.objectContaining({
335
- email: 'example@domain.be',
336
- sentAt: expect.any(Date),
337
- failCount: 0,
338
- }),
339
- ]);
340
-
341
- // Check to header
342
- expect(EmailMocker.getSucceededEmail(0).to).toEqual('example@domain.be');
343
- }, 15_000);
344
-
345
- it('Includes recipient names in mail header', async () => {
346
- const model = await buildEmail({
347
- recipients: [
348
- EmailRecipientStruct.create({
349
- firstName: 'John',
350
- lastName: 'Von Doe',
351
- email: 'example@domain.be',
352
- }),
353
- ],
354
- });
355
-
356
- await model.queueForSending(true);
357
- await model.refresh();
358
-
359
- // Check if it was sent correctly
360
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
361
- expect(model.emailRecipientsCount).toBe(1);
362
- expect(model.status).toBe(EmailStatus.Sent);
363
- expect(model.succeededCount).toBe(1);
364
- expect(model.failedCount).toBe(0);
365
- expect(model.softFailedCount).toBe(0);
366
-
367
- // Both have succeeded
368
- expect(await EmailMocker.getSucceededCount()).toBe(1);
369
- expect(await EmailMocker.getFailedCount()).toBe(0);
370
-
371
- // Load recipietns
372
- const recipients = await EmailRecipient.select().where('emailId', model.id).fetch();
373
- expect(recipients).toIncludeSameMembers([
374
- expect.objectContaining({
375
- firstName: 'John',
376
- lastName: 'Von Doe',
377
- email: 'example@domain.be',
378
- sentAt: expect.any(Date),
379
- failCount: 0,
380
- }),
381
- ]);
382
-
383
- // Check to header
384
- expect(EmailMocker.getSucceededEmail(0).to).toEqual('"John Von Doe" <example@domain.be>');
385
- }, 15_000);
386
-
387
- it('Skips invalid email addresses', async () => {
388
- const model = await buildEmail({
389
- recipients: [
390
- EmailRecipientStruct.create({
391
- firstName: 'John',
392
- lastName: 'Von Doe',
393
- email: 'invalid',
394
- }),
395
- ],
396
- });
397
-
398
- await model.queueForSending(true);
399
- await model.refresh();
400
-
401
- // Check if it was sent correctly
402
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
403
- expect(model.emailRecipientsCount).toBe(1);
404
- expect(model.status).toBe(EmailStatus.Sent);
405
- expect(model.succeededCount).toBe(0);
406
- expect(model.failedCount).toBe(1);
407
- expect(model.softFailedCount).toBe(0);
408
-
409
- // Both have succeeded
410
- expect(await EmailMocker.getSucceededCount()).toBe(0);
411
- expect(await EmailMocker.getFailedCount()).toBe(0);
412
-
413
- // Load recipietns
414
- const recipients = await EmailRecipient.select().where('emailId', model.id).fetch();
415
- expect(recipients).toIncludeSameMembers([
416
- expect.objectContaining({
417
- firstName: 'John',
418
- lastName: 'Von Doe',
419
- email: 'invalid',
420
- failCount: 1,
421
- failError: STExpect.simpleErrors([{
422
- code: 'invalid_email_address',
423
- message: 'Invalid email address',
424
- }]),
425
- firstFailedAt: expect.any(Date),
426
- lastFailedAt: expect.any(Date),
427
- }),
428
- ]);
429
- }, 15_000);
430
-
431
- it('Skips empty email addresses while creating recipients', async () => {
432
- const model = await buildEmail({
433
- recipients: [
434
- EmailRecipientStruct.create({
435
- firstName: '',
436
- lastName: '',
437
- email: '',
438
- }),
439
- EmailRecipientStruct.create({
440
- firstName: '',
441
- lastName: '',
442
- email: 'valid@example.com',
443
- }),
444
- ],
445
- });
446
-
447
- await model.queueForSending(true);
448
- await model.refresh();
449
-
450
- // Check if it was sent correctly
451
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
452
- expect(model.emailRecipientsCount).toBe(1);
453
- expect(model.status).toBe(EmailStatus.Sent);
454
- expect(model.succeededCount).toBe(1);
455
- expect(model.failedCount).toBe(0);
456
- expect(model.softFailedCount).toBe(0);
457
-
458
- // Both have succeeded
459
- expect(await EmailMocker.getSucceededCount()).toBe(1);
460
- expect(await EmailMocker.getFailedCount()).toBe(0);
461
-
462
- // Load recipietns
463
- const recipients = await EmailRecipient.select().where('emailId', model.id).fetch();
464
- expect(recipients).toIncludeSameMembers([
465
- expect.objectContaining({
466
- email: 'valid@example.com',
467
- sentAt: expect.any(Date),
468
- failError: null,
469
- }),
470
- ]);
471
- }, 15_000);
472
-
473
- it('Platform can send from default domain', async () => {
474
- TestUtils.setEnvironment('domains', {
475
- ...STAMHOOFD.domains,
476
- defaultTransactionalEmail: {
477
- '': 'my-platform.com',
478
- },
479
- defaultBroadcastEmail: {
480
- '': 'broadcast.my-platform.com',
481
- },
482
- });
483
-
484
- const model = await buildEmail({
485
- recipients: [
486
- EmailRecipientStruct.create({
487
- email: 'example@domain.be',
488
- }),
489
- ],
490
- fromAddress: 'info@my-platform.com',
491
- fromName: 'My Platform',
492
- });
493
-
494
- await model.queueForSending(true);
495
- await model.refresh();
496
-
497
- // Check if it was sent correctly
498
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
499
- expect(model.emailRecipientsCount).toBe(1);
500
- expect(model.status).toBe(EmailStatus.Sent);
501
- expect(model.succeededCount).toBe(1);
502
- expect(model.failedCount).toBe(0);
503
- expect(model.softFailedCount).toBe(0);
504
-
505
- // Both have succeeded
506
- expect(await EmailMocker.getSucceededCount()).toBe(1);
507
- expect(await EmailMocker.getFailedCount()).toBe(0);
508
-
509
- // Check to header
510
- expect(EmailMocker.getSucceededEmail(0)).toMatchObject({
511
- to: 'example@domain.be',
512
- from: '"My Platform" <info@my-platform.com>',
513
- replyTo: undefined,
514
- });
515
- }, 15_000);
516
-
517
- it('Platform cannot send from unsupported domain', async () => {
518
- TestUtils.setEnvironment('domains', {
519
- ...STAMHOOFD.domains,
520
- defaultTransactionalEmail: {
521
- '': 'my-platform.com',
522
- },
523
- defaultBroadcastEmail: {
524
- '': 'broadcast.my-platform.com',
525
- },
526
- });
527
-
528
- const model = await buildEmail({
529
- recipients: [
530
- EmailRecipientStruct.create({
531
- email: 'example@domain.be',
532
- }),
533
- ],
534
- fromAddress: 'info@other-platform.com',
535
- fromName: 'My Platform',
536
- });
537
-
538
- await model.queueForSending(true);
539
- await model.refresh();
540
-
541
- // Check if it was sent correctly
542
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
543
- expect(model.emailRecipientsCount).toBe(1);
544
- expect(model.status).toBe(EmailStatus.Sent);
545
- expect(model.succeededCount).toBe(1);
546
- expect(model.failedCount).toBe(0);
547
- expect(model.softFailedCount).toBe(0);
548
-
549
- // Both have succeeded
550
- expect(await EmailMocker.getSucceededCount()).toBe(1);
551
- expect(await EmailMocker.getFailedCount()).toBe(0);
552
-
553
- // Check to header
554
- expect(EmailMocker.getSucceededEmail(0)).toMatchObject({
555
- to: 'example@domain.be',
556
- from: '"My Platform" <noreply@broadcast.my-platform.com>', // domain has changed here
557
- replyTo: '"My Platform" <info@other-platform.com>', // Reply to should be set
558
- });
559
- }, 15_000);
560
-
561
- it('Organization cannot send from platform domain', async () => {
562
- TestUtils.setEnvironment('domains', {
563
- ...STAMHOOFD.domains,
564
- defaultTransactionalEmail: {
565
- '': 'my-platform.com',
566
- },
567
- defaultBroadcastEmail: {
568
- '': 'broadcast.my-platform.com',
569
- },
570
- });
571
-
572
- const organization = await new OrganizationFactory({
573
- uri: 'uritest',
574
- }).create();
575
-
576
- const model = await buildEmail({
577
- organizationId: organization.id,
578
- recipients: [
579
- EmailRecipientStruct.create({
580
- email: 'example@domain.be',
581
- }),
582
- ],
583
- fromAddress: 'info@my-platform.com',
584
- fromName: 'My Platform',
585
- });
586
-
587
- await model.queueForSending(true);
588
- await model.refresh();
589
-
590
- // Check if it was sent correctly
591
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
592
- expect(model.emailRecipientsCount).toBe(1);
593
- expect(model.status).toBe(EmailStatus.Sent);
594
- expect(model.succeededCount).toBe(1);
595
- expect(model.failedCount).toBe(0);
596
- expect(model.softFailedCount).toBe(0);
597
-
598
- // Both have succeeded
599
- expect(await EmailMocker.getSucceededCount()).toBe(1);
600
- expect(await EmailMocker.getFailedCount()).toBe(0);
601
-
602
- // Check to header
603
- expect(EmailMocker.getSucceededEmail(0)).toMatchObject({
604
- to: 'example@domain.be',
605
- from: '"My Platform" <noreply-uritest@broadcast.my-platform.com>', // domain has changed here
606
- replyTo: '"My Platform" <info@my-platform.com>', // Reply to should be set
607
- });
608
- }, 15_000);
609
-
610
- it('Organization can send from platform domain if default membership organization', async () => {
611
- TestUtils.setEnvironment('domains', {
612
- ...STAMHOOFD.domains,
613
- defaultTransactionalEmail: {
614
- '': 'my-platform.com',
615
- },
616
- defaultBroadcastEmail: {
617
- '': 'broadcast.my-platform.com',
618
- },
619
- });
620
-
621
- const organization = await new OrganizationFactory({}).create();
622
-
623
- const platform = await Platform.getForEditing();
624
- platform.membershipOrganizationId = organization.id;
625
- await platform.save();
626
-
627
- const model = await buildEmail({
628
- organizationId: organization.id,
629
- recipients: [
630
- EmailRecipientStruct.create({
631
- email: 'example@domain.be',
632
- }),
633
- ],
634
- fromAddress: 'info@my-platform.com',
635
- fromName: 'My Platform',
636
- });
637
-
638
- await model.queueForSending(true);
639
- await model.refresh();
640
-
641
- // Check if it was sent correctly
642
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
643
- expect(model.emailRecipientsCount).toBe(1);
644
- expect(model.status).toBe(EmailStatus.Sent);
645
- expect(model.succeededCount).toBe(1);
646
- expect(model.failedCount).toBe(0);
647
- expect(model.softFailedCount).toBe(0);
648
-
649
- // Both have succeeded
650
- expect(await EmailMocker.getSucceededCount()).toBe(1);
651
- expect(await EmailMocker.getFailedCount()).toBe(0);
652
-
653
- // Check to header
654
- expect(EmailMocker.getSucceededEmail(0)).toMatchObject({
655
- to: 'example@domain.be',
656
- from: '"My Platform" <info@my-platform.com>', // domain has changed here
657
- replyTo: undefined,
658
- });
659
- }, 15_000);
660
-
661
- describe('Replacements', () => {
662
- it('[Regression] When sending an email the organization fromAddress is replaced to the reply-to if it is not allowed to be sent from', async () => {
663
- TestUtils.setEnvironment('domains', {
664
- ...STAMHOOFD.domains,
665
- defaultTransactionalEmail: {
666
- '': 'my-platform.com',
667
- },
668
- defaultBroadcastEmail: {
669
- '': 'broadcast.my-platform.com',
670
- },
671
- });
672
-
673
- const organization = await new OrganizationFactory({}).create();
674
-
675
- const model = await buildEmail({
676
- organizationId: organization.id,
677
- recipients: [
678
- EmailRecipientStruct.create({
679
- email: 'example@domain.be',
680
- }),
681
- ],
682
- fromAddress: 'custom@customdomain.com',
683
- fromName: 'Custom Name',
684
- html: '{{fromAddress}}',
685
- text: '{{fromAddress}}',
686
- subject: '{{fromAddress}}',
687
- emailType: 'system-test', // Makes sure we don't need to include unsubscribeUrl
688
- });
689
-
690
- await model.queueForSending(true);
691
- await model.refresh();
692
-
693
- // Check if it was sent correctly
694
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
695
- expect(model.emailRecipientsCount).toBe(1);
696
- expect(model.status).toBe(EmailStatus.Sent);
697
-
698
- // Both have succeeded
699
- expect(await EmailMocker.getSucceededCount()).toBe(1);
700
- expect(await EmailMocker.getFailedCount()).toBe(0);
701
-
702
- // Check to header
703
- expect(EmailMocker.getSucceededEmail(0)).toMatchObject({
704
- to: 'example@domain.be',
705
- from: '"Custom Name" <noreply-' + organization.uri + '@broadcast.my-platform.com>',
706
- replyTo: '"Custom Name" <custom@customdomain.com>', // domain has changed here
707
- subject: 'custom@customdomain.com',
708
- html: 'custom@customdomain.com',
709
- text: 'custom@customdomain.com',
710
- });
711
- }, 15_000);
712
-
713
- it('Default organization replacements work as expected', async () => {
714
- TestUtils.setEnvironment('domains', {
715
- ...STAMHOOFD.domains,
716
- defaultTransactionalEmail: {
717
- '': 'my-platform.com',
718
- },
719
- defaultBroadcastEmail: {
720
- '': 'broadcast.my-platform.com',
721
- },
722
- });
723
-
724
- const brightYellow = '#FFD700';
725
- const expectedContrastColor = '#000'; // black
726
-
727
- const organization = await new OrganizationFactory({
728
- meta: OrganizationMetaData.create({
729
- color: brightYellow,
730
- }),
731
- }).create();
732
-
733
- const model = await buildEmail({
734
- organizationId: organization.id,
735
- recipients: [
736
- EmailRecipientStruct.create({
737
- email: 'example@domain.be',
738
- }),
739
- ],
740
- fromAddress: 'custom@customdomain.com',
741
- html: '{{primaryColor}};{{primaryColorContrast}};{{organizationName}};{{fromName}}',
742
- text: '{{primaryColor}};{{primaryColorContrast}};{{organizationName}};{{fromName}}',
743
- subject: '{{primaryColor}};{{primaryColorContrast}};{{organizationName}};{{fromName}}',
744
- emailType: 'system-test', // Makes sure we don't need to include unsubscribeUrl
745
- });
746
-
747
- await model.queueForSending(true);
748
- await model.refresh();
749
-
750
- // Check if it was sent correctly
751
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
752
- expect(model.emailRecipientsCount).toBe(1);
753
- expect(model.status).toBe(EmailStatus.Sent);
754
-
755
- // Both have succeeded
756
- expect(await EmailMocker.getSucceededCount()).toBe(1);
757
- expect(await EmailMocker.getFailedCount()).toBe(0);
758
-
759
- // Check to header
760
- expect(EmailMocker.getSucceededEmail(0)).toMatchObject({
761
- subject: `${brightYellow};${expectedContrastColor};${organization.name};${organization.name}`,
762
- html: `${brightYellow};${expectedContrastColor};${organization.name};${organization.name}`,
763
- text: `${brightYellow};${expectedContrastColor};${organization.name};${organization.name}`,
764
- });
765
- }, 15_000);
766
-
767
- it('Organization replacements default to platform replacements if not set', async () => {
768
- TestUtils.setEnvironment('domains', {
769
- ...STAMHOOFD.domains,
770
- defaultTransactionalEmail: {
771
- '': 'my-platform.com',
772
- },
773
- defaultBroadcastEmail: {
774
- '': 'broadcast.my-platform.com',
775
- },
776
- });
777
-
778
- const brightBlue = '#0000FF';
779
- const expectedContrastColor = '#fff'; // white
780
-
781
- const platform = await Platform.getForEditing();
782
- platform.config.color = brightBlue;
783
- await platform.save();
784
-
785
- const organization = await new OrganizationFactory({
786
- meta: OrganizationMetaData.create({
787
- color: null,
788
- }),
789
- }).create();
790
-
791
- const model = await buildEmail({
792
- organizationId: organization.id,
793
- recipients: [
794
- EmailRecipientStruct.create({
795
- email: 'example@domain.be',
796
- }),
797
- ],
798
- fromAddress: 'custom@customdomain.com',
799
- fromName: 'Custom Name',
800
- html: '{{primaryColor}};{{primaryColorContrast}};{{organizationName}};{{fromName}}',
801
- text: '{{primaryColor}};{{primaryColorContrast}};{{organizationName}};{{fromName}}',
802
- subject: '{{primaryColor}};{{primaryColorContrast}};{{organizationName}};{{fromName}}',
803
- emailType: 'system-test', // Makes sure we don't need to include unsubscribeUrl
804
- });
805
-
806
- await model.queueForSending(true);
807
- await model.refresh();
808
-
809
- // Check if it was sent correctly
810
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
811
- expect(model.emailRecipientsCount).toBe(1);
812
- expect(model.status).toBe(EmailStatus.Sent);
813
-
814
- // Both have succeeded
815
- expect(await EmailMocker.getSucceededCount()).toBe(1);
816
- expect(await EmailMocker.getFailedCount()).toBe(0);
817
-
818
- // Check to header
819
- expect(EmailMocker.getSucceededEmail(0)).toMatchObject({
820
- subject: `${brightBlue};${expectedContrastColor};${organization.name};Custom Name`,
821
- html: `${brightBlue};${expectedContrastColor};${organization.name};Custom Name`,
822
- text: `${brightBlue};${expectedContrastColor};${organization.name};Custom Name`,
823
- });
824
- }, 15_000);
825
-
826
- it('Platform replacements are used for platform level emails', async () => {
827
- TestUtils.setEnvironment('domains', {
828
- ...STAMHOOFD.domains,
829
- defaultTransactionalEmail: {
830
- '': 'my-platform.com',
831
- },
832
- defaultBroadcastEmail: {
833
- '': 'broadcast.my-platform.com',
834
- },
835
- });
836
-
837
- const darkRed = '#8B0000';
838
- const expectedContrastColor = '#fff'; // white
839
-
840
- const platform = await Platform.getForEditing();
841
- platform.config.color = darkRed;
842
- await platform.save();
843
-
844
- const model = await buildEmail({
845
- recipients: [
846
- EmailRecipientStruct.create({
847
- email: 'example@domain.be',
848
- }),
849
- ],
850
- fromAddress: 'custom@customdomain.com',
851
- html: '{{primaryColor}};{{primaryColorContrast}};{{organizationName}};{{fromName}}',
852
- text: '{{primaryColor}};{{primaryColorContrast}};{{organizationName}};{{fromName}}',
853
- subject: '{{primaryColor}};{{primaryColorContrast}};{{organizationName}};{{fromName}}',
854
- emailType: 'system-test', // Makes sure we don't need to include unsubscribeUrl
855
- });
856
-
857
- await model.queueForSending(true);
858
- await model.refresh();
859
-
860
- // Check if it was sent correctly
861
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
862
- expect(model.emailRecipientsCount).toBe(1);
863
- expect(model.status).toBe(EmailStatus.Sent);
864
-
865
- // Both have succeeded
866
- expect(await EmailMocker.getSucceededCount()).toBe(1);
867
- expect(await EmailMocker.getFailedCount()).toBe(0);
868
-
869
- // Check to header
870
- expect(EmailMocker.getSucceededEmail(0)).toMatchObject({
871
- subject: `${darkRed};${expectedContrastColor};${platform.config.name};${platform.config.name}`,
872
- html: `${darkRed};${expectedContrastColor};${platform.config.name};${platform.config.name}`,
873
- text: `${darkRed};${expectedContrastColor};${platform.config.name};${platform.config.name}`,
874
- });
875
- }, 15_000);
876
-
877
- describe('User based replacements', () => {
878
- it('The balance is added for existing users', async () => {
879
- TestUtils.setEnvironment('domains', {
880
- ...STAMHOOFD.domains,
881
- defaultTransactionalEmail: {
882
- '': 'my-platform.com',
883
- },
884
- defaultBroadcastEmail: {
885
- '': 'broadcast.my-platform.com',
886
- },
887
- });
888
-
889
- const organization = await new OrganizationFactory({}).create();
890
- const existingUser = await new UserFactory({
891
- organization,
892
- }).create();
893
-
894
- // Add outstanding balance items for this user
895
- const balanceItem = await new BalanceItemFactory({
896
- userId: existingUser.id,
897
- organizationId: organization.id,
898
- type: BalanceItemType.Other,
899
- amount: 2,
900
- unitPrice: 12_99_00,
901
- description: 'Test balance item',
902
- }).create();
903
- await CachedBalance.updateForUsers(organization.id, [existingUser.id]);
904
-
905
- const model = await buildEmail({
906
- organizationId: organization.id,
907
- recipients: [
908
- EmailRecipientStruct.create({
909
- email: existingUser.email,
910
- userId: existingUser.id,
911
- }),
912
- ],
913
- fromAddress: 'custom@customdomain.com',
914
- html: '<p>{{outstandingBalance}}</p>{{balanceTable}}',
915
- subject: '{{outstandingBalance}}',
916
- emailType: 'system-test', // Makes sure we don't need to include unsubscribeUrl
917
- });
918
-
919
- await model.queueForSending(true);
920
- await model.refresh();
921
-
922
- // Check if it was sent correctly
923
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
924
- expect(model.emailRecipientsCount).toBe(1);
925
- expect(model.status).toBe(EmailStatus.Sent);
926
-
927
- // Both have succeeded
928
- expect(await EmailMocker.getSucceededCount()).toBe(1);
929
- expect(await EmailMocker.getFailedCount()).toBe(0);
930
-
931
- const expectedAmount = Formatter.price(balanceItem.unitPrice * balanceItem.amount);
932
-
933
- // Check to header
934
- expect(EmailMocker.getSucceededEmail(0)).toMatchObject({
935
- subject: `${expectedAmount}`,
936
- });
937
-
938
- expect(EmailMocker.getSucceededEmail(0).html).toMatch('<p>' + expectedAmount + '</p>');
939
- expect(EmailMocker.getSucceededEmail(0).text).toMatch(expectedAmount);
940
-
941
- // Check if the table is correct
942
- expect(EmailMocker.getSucceededEmail(0).html).toMatch('<table');
943
- expect(EmailMocker.getSucceededEmail(0).html).toMatch('2 x '); // amount
944
- expect(EmailMocker.getSucceededEmail(0).html).toMatch(Formatter.price(12_99_00)); // unit price
945
- expect(EmailMocker.getSucceededEmail(0).html).toMatch('<td>' + expectedAmount); // total price in table
946
- expect(EmailMocker.getSucceededEmail(0).html).toMatch('Test balance item'); // description
947
-
948
- expect(EmailMocker.getSucceededEmail(0).text).toMatch('2 x '); // amount
949
- expect(EmailMocker.getSucceededEmail(0).text).toMatch(Formatter.price(12_99_00)); // unit price
950
- expect(EmailMocker.getSucceededEmail(0).text).toMatch(expectedAmount); // total price in table
951
- expect(EmailMocker.getSucceededEmail(0).text?.toLowerCase()).toMatch('test balance item'); // description
952
- }, 15_000);
953
-
954
- it('The balance is zero for unknown users', async () => {
955
- TestUtils.setEnvironment('domains', {
956
- ...STAMHOOFD.domains,
957
- defaultTransactionalEmail: {
958
- '': 'my-platform.com',
959
- },
960
- defaultBroadcastEmail: {
961
- '': 'broadcast.my-platform.com',
962
- },
963
- });
964
-
965
- const organization = await new OrganizationFactory({}).create();
966
-
967
- const model = await buildEmail({
968
- organizationId: organization.id,
969
- recipients: [
970
- EmailRecipientStruct.create({
971
- email: 'unknown-user@example.com',
972
- }),
973
- ],
974
- fromAddress: 'custom@customdomain.com',
975
- html: '<p>{{outstandingBalance}}</p>{{balanceTable}}',
976
- subject: '{{outstandingBalance}}',
977
- emailType: 'system-test', // Makes sure we don't need to include unsubscribeUrl
978
- });
979
-
980
- await model.queueForSending(true);
981
- await model.refresh();
982
-
983
- // Check if it was sent correctly
984
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
985
- expect(model.emailRecipientsCount).toBe(1);
986
- expect(model.status).toBe(EmailStatus.Sent);
987
-
988
- // Both have succeeded
989
- expect(await EmailMocker.getSucceededCount()).toBe(1);
990
- expect(await EmailMocker.getFailedCount()).toBe(0);
991
-
992
- const expectedAmount = Formatter.price(0);
993
-
994
- // Check to header
995
- expect(EmailMocker.getSucceededEmail(0)).toMatchObject({
996
- subject: `${expectedAmount}`,
997
- });
998
-
999
- expect(EmailMocker.getSucceededEmail(0).html).toMatch('<p>' + expectedAmount + '</p>');
1000
- expect(EmailMocker.getSucceededEmail(0).text).toMatch(expectedAmount);
1001
-
1002
- // Check if the table is correct
1003
- expect(EmailMocker.getSucceededEmail(0).html).not.toMatch('<table');
1004
- expect(EmailMocker.getSucceededEmail(0).html).not.toMatch(' x '); // amount
1005
- expect(EmailMocker.getSucceededEmail(0).html).toMatch('<p class="description">' + $t('%hX') + '</p>');
1006
-
1007
- expect(EmailMocker.getSucceededEmail(0).text).not.toMatch(' x '); // amount
1008
- expect(EmailMocker.getSucceededEmail(0).text?.toLowerCase()).toMatch($t('%hX').toLowerCase());
1009
- }, 15_000);
1010
-
1011
- it('loginDetails are added for existing users without password', async () => {
1012
- TestUtils.setEnvironment('domains', {
1013
- ...STAMHOOFD.domains,
1014
- defaultTransactionalEmail: {
1015
- '': 'my-platform.com',
1016
- },
1017
- defaultBroadcastEmail: {
1018
- '': 'broadcast.my-platform.com',
1019
- },
1020
- });
1021
-
1022
- const organization = await new OrganizationFactory({}).create();
1023
- const existingUser = await new UserFactory({
1024
- organization,
1025
- password: null,
1026
- }).create();
1027
-
1028
- const model = await buildEmail({
1029
- organizationId: organization.id,
1030
- recipients: [
1031
- EmailRecipientStruct.create({
1032
- email: existingUser.email,
1033
- userId: existingUser.id,
1034
- }),
1035
- ],
1036
- fromAddress: 'custom@customdomain.com',
1037
- html: '{{loginDetails}}',
1038
- emailType: 'system-test', // Makes sure we don't need to include unsubscribeUrl
1039
- });
1040
-
1041
- await model.queueForSending(true);
1042
- await model.refresh();
1043
-
1044
- // Check if it was sent correctly
1045
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
1046
- expect(model.emailRecipientsCount).toBe(1);
1047
- expect(model.status).toBe(EmailStatus.Sent);
1048
-
1049
- // Both have succeeded
1050
- expect(await EmailMocker.getSucceededCount()).toBe(1);
1051
- expect(await EmailMocker.getFailedCount()).toBe(0);
1052
-
1053
- expect(EmailMocker.getSucceededEmail(0).html).toMatch(
1054
- $t('%1EB', { email: existingUser.email }),
1055
- );
1056
- expect(EmailMocker.getSucceededEmail(0).text).toMatch(
1057
- $t('%1EB', { email: existingUser.email }),
1058
- );
1059
- }, 15_000);
1060
-
1061
- it('loginDetails are added for inexisting users', async () => {
1062
- TestUtils.setEnvironment('domains', {
1063
- ...STAMHOOFD.domains,
1064
- defaultTransactionalEmail: {
1065
- '': 'my-platform.com',
1066
- },
1067
- defaultBroadcastEmail: {
1068
- '': 'broadcast.my-platform.com',
1069
- },
1070
- });
1071
-
1072
- const organization = await new OrganizationFactory({}).create();
1073
-
1074
- const model = await buildEmail({
1075
- organizationId: organization.id,
1076
- recipients: [
1077
- EmailRecipientStruct.create({
1078
- email: 'unknown@example.com',
1079
- }),
1080
- ],
1081
- fromAddress: 'custom@customdomain.com',
1082
- html: '{{loginDetails}}',
1083
- emailType: 'system-test', // Makes sure we don't need to include unsubscribeUrl
1084
- });
1085
-
1086
- await model.queueForSending(true);
1087
- await model.refresh();
1088
-
1089
- // Check if it was sent correctly
1090
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
1091
- expect(model.emailRecipientsCount).toBe(1);
1092
- expect(model.status).toBe(EmailStatus.Sent);
1093
-
1094
- // Both have succeeded
1095
- expect(await EmailMocker.getSucceededCount()).toBe(1);
1096
- expect(await EmailMocker.getFailedCount()).toBe(0);
1097
-
1098
- expect(EmailMocker.getSucceededEmail(0).html).toMatch(
1099
- $t('%1EB', { email: 'unknown@example.com' }),
1100
- );
1101
- expect(EmailMocker.getSucceededEmail(0).text).toMatch(
1102
- $t('%1EB', { email: 'unknown@example.com' }),
1103
- );
1104
- }, 15_000);
1105
-
1106
- it('loginDetails are added for existing users with password', async () => {
1107
- TestUtils.setEnvironment('domains', {
1108
- ...STAMHOOFD.domains,
1109
- defaultTransactionalEmail: {
1110
- '': 'my-platform.com',
1111
- },
1112
- defaultBroadcastEmail: {
1113
- '': 'broadcast.my-platform.com',
1114
- },
1115
- });
1116
-
1117
- const organization = await new OrganizationFactory({}).create();
1118
- const existingUser = await new UserFactory({
1119
- organization,
1120
- password: 'existing',
1121
- }).create();
1122
-
1123
- const model = await buildEmail({
1124
- organizationId: organization.id,
1125
- recipients: [
1126
- EmailRecipientStruct.create({
1127
- email: existingUser.email,
1128
- userId: existingUser.id,
1129
- }),
1130
- ],
1131
- fromAddress: 'custom@customdomain.com',
1132
- html: '{{loginDetails}}',
1133
- emailType: 'system-test', // Makes sure we don't need to include unsubscribeUrl
1134
- });
1135
-
1136
- await model.queueForSending(true);
1137
- await model.refresh();
1138
-
1139
- // Check if it was sent correctly
1140
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
1141
- expect(model.emailRecipientsCount).toBe(1);
1142
- expect(model.status).toBe(EmailStatus.Sent);
1143
-
1144
- // Both have succeeded
1145
- expect(await EmailMocker.getSucceededCount()).toBe(1);
1146
- expect(await EmailMocker.getFailedCount()).toBe(0);
1147
-
1148
- expect(EmailMocker.getSucceededEmail(0).html).toMatch(
1149
- $t('%1EA', { email: existingUser.email }),
1150
- );
1151
- expect(EmailMocker.getSucceededEmail(0).text).toMatch(
1152
- $t('%1EA', { email: existingUser.email }),
1153
- );
1154
- }, 15_000);
1155
-
1156
- it('loginDetails include member security keys for existing users without password', async () => {
1157
- TestUtils.setEnvironment('userMode', 'platform');
1158
- TestUtils.setEnvironment('domains', {
1159
- ...STAMHOOFD.domains,
1160
- defaultTransactionalEmail: {
1161
- '': 'my-platform.com',
1162
- },
1163
- defaultBroadcastEmail: {
1164
- '': 'broadcast.my-platform.com',
1165
- },
1166
- });
1167
-
1168
- const organization = await new OrganizationFactory({}).create();
1169
- const existingUser = await new UserFactory({
1170
- organization,
1171
- password: null,
1172
- }).create();
1173
- const member = await new MemberFactory({
1174
- organization,
1175
- user: existingUser,
1176
- }).create();
1177
- member.details.securityCode = '123456790123456'; // 16 chars
1178
- await member.save();
1179
-
1180
- // Add a linked member
1181
-
1182
- const model = await buildEmail({
1183
- organizationId: organization.id,
1184
- recipients: [
1185
- EmailRecipientStruct.create({
1186
- email: existingUser.email,
1187
- userId: existingUser.id,
1188
- }),
1189
- ],
1190
- fromAddress: 'custom@customdomain.com',
1191
- html: '{{loginDetails}}',
1192
- emailType: 'system-test', // Makes sure we don't need to include unsubscribeUrl
1193
- });
1194
-
1195
- await model.queueForSending(true);
1196
- await model.refresh();
1197
-
1198
- // Check if it was sent correctly
1199
- expect(model.recipientsStatus).toBe(EmailRecipientsStatus.Created);
1200
- expect(model.emailRecipientsCount).toBe(1);
1201
- expect(model.status).toBe(EmailStatus.Sent);
1202
-
1203
- // Both have succeeded
1204
- expect(await EmailMocker.getSucceededCount()).toBe(1);
1205
- expect(await EmailMocker.getFailedCount()).toBe(0);
1206
-
1207
- expect(EmailMocker.getSucceededEmail(0).html).toMatch(
1208
- $t('%1EC', {
1209
- firstName: Formatter.escapeHtml(member.firstName),
1210
- securityCode: `<span class="style-inline-code">${Formatter.escapeHtml(Formatter.spaceString(member.details.securityCode ?? '', 4, '-'))}</span>`,
1211
- }),
1212
- );
1213
- expect(EmailMocker.getSucceededEmail(0).text).toMatch(
1214
- $t('%1EC', {
1215
- firstName: member.firstName,
1216
- securityCode: Formatter.spaceString(member.details.securityCode ?? '', 4, '-'),
1217
- }),
1218
- );
1219
- }, 15_000);
1220
- });
1221
- });
1222
- });