@stamhoofd/models 2.146.2 → 2.148.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 (84) hide show
  1. package/dist/factories/OrganizationFactory.d.ts.map +1 -1
  2. package/dist/factories/OrganizationFactory.js.map +1 -1
  3. package/dist/factories/TenantFactory.d.ts +30 -0
  4. package/dist/factories/TenantFactory.d.ts.map +1 -0
  5. package/dist/factories/TenantFactory.js +50 -0
  6. package/dist/factories/TenantFactory.js.map +1 -0
  7. package/dist/factories/index.d.ts +1 -0
  8. package/dist/factories/index.d.ts.map +1 -1
  9. package/dist/factories/index.js +1 -0
  10. package/dist/factories/index.js.map +1 -1
  11. package/dist/migrations/1785433585-organizations-tenant-id.sql +1 -0
  12. package/dist/migrations/1785433586-users-tenant-id.sql +1 -0
  13. package/dist/migrations/1785433587-registration-periods-tenant-id.sql +1 -0
  14. package/dist/migrations/1785433588-members-tenant-id.sql +1 -0
  15. package/dist/migrations/1785433589-organizations-tenant-id-index.sql +1 -0
  16. package/dist/migrations/1785433590-users-tenant-id-index.sql +1 -0
  17. package/dist/migrations/1785433591-registration-periods-tenant-id-index.sql +1 -0
  18. package/dist/migrations/1785433592-members-tenant-id-index.sql +1 -0
  19. package/dist/migrations/1787140000-users-language.sql +2 -0
  20. package/dist/migrations/1787140001-organizations-language.sql +2 -0
  21. package/dist/migrations/1787140002-platform-language.sql +2 -0
  22. package/dist/migrations/1787763657-organization-languages.sql +1 -0
  23. package/dist/models/BalanceItem.d.ts +1 -2
  24. package/dist/models/BalanceItem.d.ts.map +1 -1
  25. package/dist/models/BalanceItem.js +4 -4
  26. package/dist/models/BalanceItem.js.map +1 -1
  27. package/dist/models/DocumentTemplate.d.ts.map +1 -1
  28. package/dist/models/DocumentTemplate.js +5 -1
  29. package/dist/models/DocumentTemplate.js.map +1 -1
  30. package/dist/models/Email.d.ts +5 -2
  31. package/dist/models/Email.d.ts.map +1 -1
  32. package/dist/models/Email.js +27 -8
  33. package/dist/models/Email.js.map +1 -1
  34. package/dist/models/Image.d.ts.map +1 -1
  35. package/dist/models/Image.js +1 -0
  36. package/dist/models/Image.js.map +1 -1
  37. package/dist/models/Member.d.ts +4 -0
  38. package/dist/models/Member.d.ts.map +1 -1
  39. package/dist/models/Member.js +7 -0
  40. package/dist/models/Member.js.map +1 -1
  41. package/dist/models/Organization.d.ts +6 -0
  42. package/dist/models/Organization.d.ts.map +1 -1
  43. package/dist/models/Organization.js +14 -0
  44. package/dist/models/Organization.js.map +1 -1
  45. package/dist/models/Platform.d.ts +17 -0
  46. package/dist/models/Platform.d.ts.map +1 -1
  47. package/dist/models/Platform.js +46 -0
  48. package/dist/models/Platform.js.map +1 -1
  49. package/dist/models/RegistrationPeriod.d.ts +4 -0
  50. package/dist/models/RegistrationPeriod.d.ts.map +1 -1
  51. package/dist/models/RegistrationPeriod.js +7 -0
  52. package/dist/models/RegistrationPeriod.js.map +1 -1
  53. package/dist/models/User.d.ts +8 -0
  54. package/dist/models/User.d.ts.map +1 -1
  55. package/dist/models/User.js +16 -0
  56. package/dist/models/User.js.map +1 -1
  57. package/package.json +6 -6
  58. package/src/factories/OrganizationFactory.ts +1 -0
  59. package/src/factories/TenantFactory.test.ts +55 -0
  60. package/src/factories/TenantFactory.ts +62 -0
  61. package/src/factories/index.ts +1 -0
  62. package/src/migrations/1785433585-organizations-tenant-id.sql +1 -0
  63. package/src/migrations/1785433586-users-tenant-id.sql +1 -0
  64. package/src/migrations/1785433587-registration-periods-tenant-id.sql +1 -0
  65. package/src/migrations/1785433588-members-tenant-id.sql +1 -0
  66. package/src/migrations/1785433589-organizations-tenant-id-index.sql +1 -0
  67. package/src/migrations/1785433590-users-tenant-id-index.sql +1 -0
  68. package/src/migrations/1785433591-registration-periods-tenant-id-index.sql +1 -0
  69. package/src/migrations/1785433592-members-tenant-id-index.sql +1 -0
  70. package/src/migrations/1787140000-users-language.sql +2 -0
  71. package/src/migrations/1787140001-organizations-language.sql +2 -0
  72. package/src/migrations/1787140002-platform-language.sql +2 -0
  73. package/src/migrations/1787763657-organization-languages.sql +1 -0
  74. package/src/models/BalanceItem.ts +4 -5
  75. package/src/models/DocumentTemplate.ts +6 -1
  76. package/src/models/Email.ts +26 -9
  77. package/src/models/Image.ts +3 -1
  78. package/src/models/Member.ts +6 -0
  79. package/src/models/Organization.ts +12 -0
  80. package/src/models/Platform.test.ts +63 -1
  81. package/src/models/Platform.ts +54 -0
  82. package/src/models/RegistrationPeriod.ts +6 -0
  83. package/src/models/User.ts +16 -1
  84. package/src/models/tenantId.test.ts +42 -0
@@ -31,6 +31,12 @@ export class Organization extends QueryableModel {
31
31
  })
32
32
  id!: string;
33
33
 
34
+ /**
35
+ * The tenant this row belongs to. Nullable until every row is backfilled.
36
+ */
37
+ @column({ type: 'string', nullable: true })
38
+ tenantId: string | null = null;
39
+
34
40
  @column({ type: 'string' })
35
41
  name: string;
36
42
 
@@ -38,6 +44,10 @@ export class Organization extends QueryableModel {
38
44
  @column({ type: 'string', nullable: true })
39
45
  website: string | null = null;
40
46
 
47
+ /** Null falls back to the language of the platform. */
48
+ @column({ type: 'string', nullable: true })
49
+ language: Language | null = null;
50
+
41
51
  /// A custom domain name that is used to host the register application (should be unique)
42
52
  // E.g. inschrijven.scoutswetteren.be
43
53
  @column({ type: 'string', nullable: true })
@@ -331,6 +341,7 @@ export class Organization extends QueryableModel {
331
341
  registerDomain: this.registerDomain,
332
342
  uri: this.uri,
333
343
  website: this.website,
344
+ language: this.language,
334
345
  createdAt: this.createdAt,
335
346
  hasFutureEvents: this.hasFutureEvents,
336
347
  });
@@ -347,6 +358,7 @@ export class Organization extends QueryableModel {
347
358
  registerDomain: this.registerDomain,
348
359
  uri: this.uri,
349
360
  website: this.website,
361
+ language: this.language,
350
362
  createdAt: this.createdAt,
351
363
  hasFutureEvents: this.hasFutureEvents,
352
364
  });
@@ -1,4 +1,4 @@
1
- import { PlatformConfig, PlatformMembershipType } from '@stamhoofd/structures';
1
+ import { PermissionRoleDetailed, PlatformConfig, PlatformMembershipType, PlatformPrivateConfig } from '@stamhoofd/structures';
2
2
  import { Platform } from './Platform.js';
3
3
  import { Database } from '@simonbackx/simple-database';
4
4
  import { STExpect, TestUtils } from '@stamhoofd/test-utils';
@@ -129,6 +129,68 @@ describe('Model.Platform', () => {
129
129
  });
130
130
  });
131
131
 
132
+ describe('Parent tenant', () => {
133
+ afterEach(async () => {
134
+ await Database.delete('DELETE FROM platform WHERE id != ?', ['1']);
135
+ await Platform.clearCache();
136
+ });
137
+
138
+ async function createChildOfRoot() {
139
+ const root = await Platform.getForEditing();
140
+ root.privateConfig = PlatformPrivateConfig.create({
141
+ roles: [PermissionRoleDetailed.create({ name: 'root role' })],
142
+ });
143
+ root.config = PlatformConfig.create({
144
+ membershipTypes: [PlatformMembershipType.create({ id: 'r', name: 'Root type' })],
145
+ });
146
+ await root.save();
147
+
148
+ const child = new Platform();
149
+ child.id = 'child-tenant';
150
+ child.periodId = root.periodId;
151
+ child.parentTenantId = root.id;
152
+ await child.save();
153
+
154
+ return { root, child };
155
+ }
156
+
157
+ test('a child tenant struct carries its parent', async () => {
158
+ const { root } = await createChildOfRoot();
159
+
160
+ const struct = await Platform.getStructForTenant('child-tenant');
161
+
162
+ expect(struct.parentTenant?.id).toBe(root.id);
163
+ expect(struct.parentTenant?.config.membershipTypes.map(m => m.name)).toEqual(['Root type']);
164
+ });
165
+
166
+ test('the parent never carries its private config', async () => {
167
+ await createChildOfRoot();
168
+
169
+ // Both variants: a child tenant administrator has no rights on the parent either way
170
+ expect((await Platform.getStructForTenant('child-tenant')).parentTenant?.privateConfig).toBeNull();
171
+ expect((await Platform.getPrivateStructForTenant('child-tenant')).parentTenant?.privateConfig).toBeNull();
172
+ });
173
+
174
+ test('the parent is only one level deep', async () => {
175
+ const { root } = await createChildOfRoot();
176
+
177
+ const grandchild = new Platform();
178
+ grandchild.id = 'grandchild-tenant';
179
+ grandchild.periodId = root.periodId;
180
+ grandchild.parentTenantId = 'child-tenant';
181
+ await grandchild.save();
182
+
183
+ const struct = await Platform.getStructForTenant('grandchild-tenant');
184
+
185
+ expect(struct.parentTenant?.id).toBe('child-tenant');
186
+ expect(struct.parentTenant?.parentTenant).toBeNull();
187
+ });
188
+
189
+ test('the root tenant has no parent', async () => {
190
+ expect((await Platform.getStructForTenant('1')).parentTenant).toBeNull();
191
+ });
192
+ });
193
+
132
194
  describe('Tenant identity', () => {
133
195
  afterEach(async () => {
134
196
  await Database.delete('DELETE FROM platform WHERE id != ?', ['1']);
@@ -4,6 +4,7 @@ import { QueueHandler } from '@stamhoofd/queues';
4
4
  import { QueryableModel } from '@stamhoofd/sql';
5
5
  import { AuditLogReplacementDependencies, PlatformConfig, PlatformPrivateConfig, PlatformServerConfig, Platform as PlatformStruct } from '@stamhoofd/structures';
6
6
  import { uuidToName } from '@stamhoofd/structures/helpers/uuidToName.js';
7
+ import type { Language } from '@stamhoofd/types/Language';
7
8
  import { deepFreeze } from '@stamhoofd/utility';
8
9
  import { v4 as uuidv4 } from 'uuid';
9
10
  import { RegistrationPeriod } from './RegistrationPeriod.js';
@@ -48,6 +49,10 @@ export class Platform extends QueryableModel {
48
49
  @column({ type: 'string', nullable: true })
49
50
  domain: string | null = null;
50
51
 
52
+ /** Null for multi-language platforms. */
53
+ @column({ type: 'string', nullable: true })
54
+ language: Language | null = null;
55
+
51
56
  @column({ type: 'json', decoder: PlatformConfig })
52
57
  config: PlatformConfig = PlatformConfig.create({});
53
58
 
@@ -125,6 +130,26 @@ export class Platform extends QueryableModel {
125
130
  return await this.getPrivateStructForTenant(ROOT_TENANT_ID);
126
131
  }
127
132
 
133
+ /**
134
+ * Both columns are nullable. Blank input is rejected so a caller cannot ask for "the tenant with
135
+ * no uri" — SQL would not match NULL with '=' anyway, but an explicit reject beats relying on it.
136
+ */
137
+ static async getByURI(uri: string): Promise<Platform | undefined> {
138
+ if (!uri) {
139
+ return undefined;
140
+ }
141
+
142
+ return await this.select().where('uri', uri).first(false) ?? undefined;
143
+ }
144
+
145
+ static async getByDomain(domain: string): Promise<Platform | undefined> {
146
+ if (!domain) {
147
+ return undefined;
148
+ }
149
+
150
+ return await this.select().where('domain', domain).first(false) ?? undefined;
151
+ }
152
+
128
153
  static async getForEditing(tenantId: string = ROOT_TENANT_ID): Promise<Platform> {
129
154
  return QueueHandler.schedule('Platform.getModel-' + tenantId, async () => {
130
155
  // Build a new one
@@ -210,6 +235,7 @@ export class Platform extends QueryableModel {
210
235
  const struct = PlatformStruct.create({
211
236
  ...model,
212
237
  period: period?.getStructure() ?? undefined,
238
+ parentTenant: await this.getParentStruct(model),
213
239
  });
214
240
 
215
241
  // We clone to avoid the chance of updating the platform model
@@ -236,6 +262,34 @@ export class Platform extends QueryableModel {
236
262
  });
237
263
  }
238
264
 
265
+ /**
266
+ * The immediate parent, without its own parent and without its private config.
267
+ *
268
+ * Reads the parent row directly instead of going through its cache: that keeps this one query
269
+ * deep, so a parentTenantId cycle cannot recurse, and it cannot hand a child tenant the parent's
270
+ * privateConfig.
271
+ */
272
+ private static async getParentStruct(model: Platform): Promise<PlatformStruct | null> {
273
+ if (!model.parentTenantId) {
274
+ return null;
275
+ }
276
+
277
+ const parent = await this.getByID(model.parentTenantId);
278
+ if (!parent) {
279
+ console.error('[Platform] Tenant ' + model.id + ' has an unknown parentTenantId', model.parentTenantId);
280
+ return null;
281
+ }
282
+
283
+ const parentPeriod = await RegistrationPeriod.getByID(parent.periodId);
284
+
285
+ return PlatformStruct.create({
286
+ ...parent,
287
+ period: parentPeriod?.getStructure() ?? undefined,
288
+ parentTenant: null,
289
+ privateConfig: null,
290
+ });
291
+ }
292
+
239
293
  static async clearCacheForTenant(tenantId: string) {
240
294
  await this.clearCacheForTenantWithoutRefresh(tenantId);
241
295
  await this.loadCachesForTenant(tenantId);
@@ -15,6 +15,12 @@ export class RegistrationPeriod extends QueryableModel {
15
15
  })
16
16
  id!: string;
17
17
 
18
+ /**
19
+ * The tenant this row belongs to. Nullable until every row is backfilled.
20
+ */
21
+ @column({ type: 'string', nullable: true })
22
+ tenantId: string | null = null;
23
+
18
24
  @column({ type: 'string', nullable: true })
19
25
  customName: string | null = null;
20
26
 
@@ -4,6 +4,7 @@ import { QueryableModel, SQL, SQLJSONNull } from '@stamhoofd/sql';
4
4
  import type { LoginProviderType, NewUser } from '@stamhoofd/structures';
5
5
  import { Permissions, Recipient, Replacement, UserMeta, UserPermissions, User as UserStruct } from '@stamhoofd/structures';
6
6
  import argon2 from 'argon2';
7
+ import { Language } from '@stamhoofd/types/Language';
7
8
  import { v4 as uuidv4 } from 'uuid';
8
9
 
9
10
  import { SimpleError } from '@simonbackx/simple-errors';
@@ -22,6 +23,12 @@ export class User extends QueryableModel {
22
23
  })
23
24
  id!: string;
24
25
 
26
+ /**
27
+ * The tenant this row belongs to. Nullable until every row is backfilled.
28
+ */
29
+ @column({ type: 'string', nullable: true })
30
+ tenantId: string | null = null;
31
+
25
32
  @column({ type: 'string', nullable: true })
26
33
  organizationId: string | null;
27
34
 
@@ -34,6 +41,10 @@ export class User extends QueryableModel {
34
41
  @column({ type: 'string', nullable: true })
35
42
  lastName: string | null = null;
36
43
 
44
+ /** Preferred language. Null falls back to the default language of the platform or tenant. */
45
+ @column({ type: 'string', nullable: true })
46
+ language: Language | null = null;
47
+
37
48
  @column({ type: 'string' })
38
49
  email: string;
39
50
 
@@ -107,6 +118,7 @@ export class User extends QueryableModel {
107
118
  firstName: this.firstName,
108
119
  lastName: this.lastName,
109
120
  email: this.email,
121
+ language: this.language,
110
122
  replacements: [
111
123
  ...replacements,
112
124
  Replacement.create({
@@ -434,7 +446,7 @@ export class User extends QueryableModel {
434
446
 
435
447
  static async createInvited(
436
448
  organization: Organization | null,
437
- data: { firstName: string | null; lastName: string | null; email: string; allowPlatform?: boolean },
449
+ data: { firstName: string | null; lastName: string | null; email: string; language?: Language | null; allowPlatform?: boolean },
438
450
  ): Promise<User | undefined> {
439
451
  const {
440
452
  email,
@@ -453,6 +465,7 @@ export class User extends QueryableModel {
453
465
  user.verified = false;
454
466
  user.firstName = firstName;
455
467
  user.lastName = lastName;
468
+ user.language = data.language ?? null;
456
469
 
457
470
  try {
458
471
  await user.save();
@@ -501,6 +514,7 @@ export class User extends QueryableModel {
501
514
  user.verified = false;
502
515
  user.firstName = firstName;
503
516
  user.lastName = lastName;
517
+ user.language = data.language;
504
518
 
505
519
  try {
506
520
  await user.save();
@@ -624,6 +638,7 @@ export class User extends QueryableModel {
624
638
  memberId: this.memberId,
625
639
  organizationId: this.organizationId,
626
640
  lastActiveAt: this.lastActiveAt,
641
+ language: this.language,
627
642
  });
628
643
  }
629
644
 
@@ -0,0 +1,42 @@
1
+ import { MemberFactory, OrganizationFactory, RegistrationPeriodFactory, UserFactory } from '../factories/index.js';
2
+ import { Member } from './Member.js';
3
+ import { Organization } from './Organization.js';
4
+ import { RegistrationPeriod } from './RegistrationPeriod.js';
5
+ import { User } from './User.js';
6
+
7
+ describe('tenantId', () => {
8
+ test('it is null on rows created before the backfill', async () => {
9
+ const organization = await new OrganizationFactory({}).create();
10
+ const user = await new UserFactory({ organization }).create();
11
+ const member = await new MemberFactory({ organization }).create();
12
+ const period = await new RegistrationPeriodFactory({}).create();
13
+
14
+ expect(organization.tenantId).toBeNull();
15
+ expect(user.tenantId).toBeNull();
16
+ expect(member.tenantId).toBeNull();
17
+ expect(period.tenantId).toBeNull();
18
+ });
19
+
20
+ test('it round-trips through the database', async () => {
21
+ const organization = await new OrganizationFactory({}).create();
22
+ organization.tenantId = 'some-tenant';
23
+ await organization.save();
24
+
25
+ expect((await Organization.getByID(organization.id))!.tenantId).toBe('some-tenant');
26
+
27
+ const user = await new UserFactory({ organization }).create();
28
+ user.tenantId = 'some-tenant';
29
+ await user.save();
30
+ expect((await User.getByID(user.id))!.tenantId).toBe('some-tenant');
31
+
32
+ const member = await new MemberFactory({ organization }).create();
33
+ member.tenantId = 'some-tenant';
34
+ await member.save();
35
+ expect((await Member.getByID(member.id))!.tenantId).toBe('some-tenant');
36
+
37
+ const period = await new RegistrationPeriodFactory({}).create();
38
+ period.tenantId = 'some-tenant';
39
+ await period.save();
40
+ expect((await RegistrationPeriod.getByID(period.id))!.tenantId).toBe('some-tenant');
41
+ });
42
+ });