@stamhoofd/models 2.146.1 → 2.147.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 (63) hide show
  1. package/dist/factories/TenantFactory.d.ts +30 -0
  2. package/dist/factories/TenantFactory.d.ts.map +1 -0
  3. package/dist/factories/TenantFactory.js +50 -0
  4. package/dist/factories/TenantFactory.js.map +1 -0
  5. package/dist/factories/index.d.ts +1 -0
  6. package/dist/factories/index.d.ts.map +1 -1
  7. package/dist/factories/index.js +1 -0
  8. package/dist/factories/index.js.map +1 -1
  9. package/dist/helpers/InvoiceCounter.d.ts.map +1 -1
  10. package/dist/helpers/InvoiceCounter.js +4 -3
  11. package/dist/helpers/InvoiceCounter.js.map +1 -1
  12. package/dist/migrations/1785433585-organizations-tenant-id.sql +1 -0
  13. package/dist/migrations/1785433586-users-tenant-id.sql +1 -0
  14. package/dist/migrations/1785433587-registration-periods-tenant-id.sql +1 -0
  15. package/dist/migrations/1785433588-members-tenant-id.sql +1 -0
  16. package/dist/migrations/1785433589-organizations-tenant-id-index.sql +1 -0
  17. package/dist/migrations/1785433590-users-tenant-id-index.sql +1 -0
  18. package/dist/migrations/1785433591-registration-periods-tenant-id-index.sql +1 -0
  19. package/dist/migrations/1785433592-members-tenant-id-index.sql +1 -0
  20. package/dist/models/Image.d.ts.map +1 -1
  21. package/dist/models/Image.js +1 -0
  22. package/dist/models/Image.js.map +1 -1
  23. package/dist/models/Member.d.ts +4 -0
  24. package/dist/models/Member.d.ts.map +1 -1
  25. package/dist/models/Member.js +7 -0
  26. package/dist/models/Member.js.map +1 -1
  27. package/dist/models/Organization.d.ts +4 -0
  28. package/dist/models/Organization.d.ts.map +1 -1
  29. package/dist/models/Organization.js +7 -0
  30. package/dist/models/Organization.js.map +1 -1
  31. package/dist/models/Platform.d.ts +14 -0
  32. package/dist/models/Platform.d.ts.map +1 -1
  33. package/dist/models/Platform.js +41 -0
  34. package/dist/models/Platform.js.map +1 -1
  35. package/dist/models/RegistrationPeriod.d.ts +4 -0
  36. package/dist/models/RegistrationPeriod.d.ts.map +1 -1
  37. package/dist/models/RegistrationPeriod.js +7 -0
  38. package/dist/models/RegistrationPeriod.js.map +1 -1
  39. package/dist/models/User.d.ts +4 -0
  40. package/dist/models/User.d.ts.map +1 -1
  41. package/dist/models/User.js +7 -0
  42. package/dist/models/User.js.map +1 -1
  43. package/package.json +4 -4
  44. package/src/factories/TenantFactory.test.ts +55 -0
  45. package/src/factories/TenantFactory.ts +62 -0
  46. package/src/factories/index.ts +1 -0
  47. package/src/helpers/InvoiceCounter.ts +28 -28
  48. package/src/migrations/1785433585-organizations-tenant-id.sql +1 -0
  49. package/src/migrations/1785433586-users-tenant-id.sql +1 -0
  50. package/src/migrations/1785433587-registration-periods-tenant-id.sql +1 -0
  51. package/src/migrations/1785433588-members-tenant-id.sql +1 -0
  52. package/src/migrations/1785433589-organizations-tenant-id-index.sql +1 -0
  53. package/src/migrations/1785433590-users-tenant-id-index.sql +1 -0
  54. package/src/migrations/1785433591-registration-periods-tenant-id-index.sql +1 -0
  55. package/src/migrations/1785433592-members-tenant-id-index.sql +1 -0
  56. package/src/models/Image.ts +3 -1
  57. package/src/models/Member.ts +6 -0
  58. package/src/models/Organization.ts +6 -0
  59. package/src/models/Platform.test.ts +63 -1
  60. package/src/models/Platform.ts +49 -0
  61. package/src/models/RegistrationPeriod.ts +6 -0
  62. package/src/models/User.ts +6 -0
  63. package/src/models/tenantId.test.ts +42 -0
@@ -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']);
@@ -125,6 +125,26 @@ export class Platform extends QueryableModel {
125
125
  return await this.getPrivateStructForTenant(ROOT_TENANT_ID);
126
126
  }
127
127
 
128
+ /**
129
+ * Both columns are nullable. Blank input is rejected so a caller cannot ask for "the tenant with
130
+ * no uri" — SQL would not match NULL with '=' anyway, but an explicit reject beats relying on it.
131
+ */
132
+ static async getByURI(uri: string): Promise<Platform | undefined> {
133
+ if (!uri) {
134
+ return undefined;
135
+ }
136
+
137
+ return await this.select().where('uri', uri).first(false) ?? undefined;
138
+ }
139
+
140
+ static async getByDomain(domain: string): Promise<Platform | undefined> {
141
+ if (!domain) {
142
+ return undefined;
143
+ }
144
+
145
+ return await this.select().where('domain', domain).first(false) ?? undefined;
146
+ }
147
+
128
148
  static async getForEditing(tenantId: string = ROOT_TENANT_ID): Promise<Platform> {
129
149
  return QueueHandler.schedule('Platform.getModel-' + tenantId, async () => {
130
150
  // Build a new one
@@ -210,6 +230,7 @@ export class Platform extends QueryableModel {
210
230
  const struct = PlatformStruct.create({
211
231
  ...model,
212
232
  period: period?.getStructure() ?? undefined,
233
+ parentTenant: await this.getParentStruct(model),
213
234
  });
214
235
 
215
236
  // We clone to avoid the chance of updating the platform model
@@ -236,6 +257,34 @@ export class Platform extends QueryableModel {
236
257
  });
237
258
  }
238
259
 
260
+ /**
261
+ * The immediate parent, without its own parent and without its private config.
262
+ *
263
+ * Reads the parent row directly instead of going through its cache: that keeps this one query
264
+ * deep, so a parentTenantId cycle cannot recurse, and it cannot hand a child tenant the parent's
265
+ * privateConfig.
266
+ */
267
+ private static async getParentStruct(model: Platform): Promise<PlatformStruct | null> {
268
+ if (!model.parentTenantId) {
269
+ return null;
270
+ }
271
+
272
+ const parent = await this.getByID(model.parentTenantId);
273
+ if (!parent) {
274
+ console.error('[Platform] Tenant ' + model.id + ' has an unknown parentTenantId', model.parentTenantId);
275
+ return null;
276
+ }
277
+
278
+ const parentPeriod = await RegistrationPeriod.getByID(parent.periodId);
279
+
280
+ return PlatformStruct.create({
281
+ ...parent,
282
+ period: parentPeriod?.getStructure() ?? undefined,
283
+ parentTenant: null,
284
+ privateConfig: null,
285
+ });
286
+ }
287
+
239
288
  static async clearCacheForTenant(tenantId: string) {
240
289
  await this.clearCacheForTenantWithoutRefresh(tenantId);
241
290
  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
 
@@ -22,6 +22,12 @@ export class User extends QueryableModel {
22
22
  })
23
23
  id!: string;
24
24
 
25
+ /**
26
+ * The tenant this row belongs to. Nullable until every row is backfilled.
27
+ */
28
+ @column({ type: 'string', nullable: true })
29
+ tenantId: string | null = null;
30
+
25
31
  @column({ type: 'string', nullable: true })
26
32
  organizationId: string | null;
27
33
 
@@ -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
+ });