@forklaunch/implementation-iam-base 0.8.24 → 1.0.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.
@@ -11,10 +11,11 @@ import {
11
11
  UpdateOrganizationDto
12
12
  } from '@forklaunch/interfaces-iam/types';
13
13
  import { AnySchemaValidator } from '@forklaunch/validator';
14
- import { EntityManager } from '@mikro-orm/core';
14
+ import { EntityManager, FilterQuery, InferEntity } from '@mikro-orm/core';
15
15
  import { OrganizationDtos } from '../domain/types/iamDto.types';
16
16
  import { OrganizationEntities } from '../domain/types/iamEntities.types';
17
17
  import { OrganizationMappers } from '../domain/types/organization.mapper.types';
18
+ import { Organization } from '../persistence/entities';
18
19
 
19
20
  export class BaseOrganizationService<
20
21
  SchemaValidator extends AnySchemaValidator,
@@ -87,14 +88,15 @@ export class BaseOrganizationService<
87
88
  if (em) {
88
89
  await em.persist(organization);
89
90
  } else {
90
- await this.em.persistAndFlush(organization);
91
+ await this.em.persist(organization).flush();
91
92
  }
92
93
 
93
94
  return this.mappers.OrganizationMapper.toDto(organization);
94
95
  }
95
96
 
96
97
  async getOrganization(
97
- idDto: IdDto,
98
+ idDto: IdDto &
99
+ FilterQuery<InferEntity<MapperEntities['OrganizationMapper']>>,
98
100
  em?: EntityManager
99
101
  ): Promise<MapperDomains['OrganizationMapper']> {
100
102
  if (this.evaluatedTelemetryOptions.logging) {
@@ -102,15 +104,12 @@ export class BaseOrganizationService<
102
104
  }
103
105
 
104
106
  const organization = await (em ?? this.em).findOneOrFail(
105
- 'Organization',
106
- idDto,
107
- {
108
- populate: ['id', '*']
109
- }
107
+ this.mappers.OrganizationMapper.entity as typeof Organization,
108
+ idDto
110
109
  );
111
110
 
112
111
  return this.mappers.OrganizationMapper.toDto(
113
- organization as MapperEntities['OrganizationMapper']
112
+ organization as InferEntity<MapperEntities['OrganizationMapper']>
114
113
  );
115
114
  }
116
115
 
@@ -136,21 +135,31 @@ export class BaseOrganizationService<
136
135
  if (em) {
137
136
  await em.persist(updatedOrganization);
138
137
  } else {
139
- await this.em.persistAndFlush(updatedOrganization);
138
+ await this.em.persist(updatedOrganization).flush();
140
139
  }
141
140
 
142
141
  return this.mappers.OrganizationMapper.toDto(updatedOrganization);
143
142
  }
144
143
 
145
- async deleteOrganization(idDto: IdDto, em?: EntityManager): Promise<void> {
144
+ async deleteOrganization(
145
+ idDto: IdDto &
146
+ FilterQuery<InferEntity<MapperEntities['OrganizationMapper']>>,
147
+ em?: EntityManager
148
+ ): Promise<void> {
146
149
  if (this.evaluatedTelemetryOptions.logging) {
147
150
  this.openTelemetryCollector.info('Deleting organization', idDto);
148
151
  }
149
152
 
150
153
  if (em) {
151
- await em.nativeDelete('Organization', idDto);
154
+ await em.nativeDelete(
155
+ this.mappers.OrganizationMapper.entity as typeof Organization,
156
+ idDto
157
+ );
152
158
  } else {
153
- await this.em.nativeDelete('Organization', idDto);
159
+ await this.em.nativeDelete(
160
+ this.mappers.OrganizationMapper.entity as typeof Organization,
161
+ idDto
162
+ );
154
163
  }
155
164
  }
156
165
  }
@@ -15,7 +15,8 @@ import {
15
15
  UpdatePermissionDto
16
16
  } from '@forklaunch/interfaces-iam/types';
17
17
  import { AnySchemaValidator } from '@forklaunch/validator';
18
- import { EntityManager } from '@mikro-orm/core';
18
+ import { EntityManager, InferEntity } from '@mikro-orm/core';
19
+ import { Permission } from '../persistence/entities';
19
20
  import { PermissionDtos } from '../domain/types/iamDto.types';
20
21
  import { PermissionEntities } from '../domain/types/iamEntities.types';
21
22
  import { PermissionMappers } from '../domain/types/permission.mapper.types';
@@ -63,25 +64,25 @@ export class BasePermissionService<
63
64
 
64
65
  // start: global helper functions
65
66
  private async updateRolesWithPermissions(
66
- roles: MapperEntities['RoleEntityMapper'][],
67
- permissions: MapperEntities['PermissionMapper'][]
68
- ): Promise<MapperEntities['RoleEntityMapper'][]> {
67
+ roles: InferEntity<MapperEntities['RoleEntityMapper']>[],
68
+ permissions: InferEntity<MapperEntities['PermissionMapper']>[]
69
+ ): Promise<InferEntity<MapperEntities['RoleEntityMapper']>[]> {
69
70
  return Promise.all(
70
71
  roles.map(async (role) => {
71
- permissions.forEach((permission) => role.permissions.add(permission));
72
+ permissions.forEach((permission) => role.permissions?.add(permission));
72
73
  return role;
73
74
  })
74
75
  );
75
76
  }
76
77
 
77
78
  private async removePermissionsFromRoles(
78
- roles: MapperEntities['RoleEntityMapper'][],
79
- permissions: MapperEntities['PermissionMapper'][]
80
- ): Promise<MapperEntities['RoleEntityMapper'][]> {
79
+ roles: InferEntity<MapperEntities['RoleEntityMapper']>[],
80
+ permissions: InferEntity<MapperEntities['PermissionMapper']>[]
81
+ ): Promise<InferEntity<MapperEntities['RoleEntityMapper']>[]> {
81
82
  return Promise.all(
82
83
  roles.map(async (role) => {
83
84
  permissions.forEach((permission) =>
84
- role.permissions.remove(permission)
85
+ role.permissions?.remove(permission)
85
86
  );
86
87
  return role;
87
88
  })
@@ -91,16 +92,16 @@ export class BasePermissionService<
91
92
  private async getBatchRoles(
92
93
  roles?: IdsDto,
93
94
  em?: EntityManager
94
- ): Promise<MapperEntities['RoleEntityMapper'][]> {
95
+ ): Promise<InferEntity<MapperEntities['RoleEntityMapper']>[]> {
95
96
  return roles
96
97
  ? await Promise.all(
97
98
  (await this.roleServiceFactory().getBatchRoles(roles, em)).map(
98
99
  async (role) => {
99
100
  return (em ?? this.em).merge(
100
- await this.mappers.RoleEntityMapper.toEntity(
101
+ (await this.mappers.RoleEntityMapper.toEntity(
101
102
  role,
102
103
  em ?? this.em
103
- )
104
+ )) as InferEntity<MapperEntities['RoleEntityMapper']>
104
105
  );
105
106
  }
106
107
  )
@@ -114,18 +115,21 @@ export class BasePermissionService<
114
115
  permission,
115
116
  addToRoles
116
117
  }: {
117
- permission: MapperEntities['PermissionMapper'];
118
- addToRoles: MapperEntities['RoleEntityMapper'][];
118
+ permission: InferEntity<MapperEntities['PermissionMapper']>;
119
+ addToRoles: InferEntity<MapperEntities['RoleEntityMapper']>[];
119
120
  }): Promise<{
120
- permission: MapperEntities['PermissionMapper'];
121
- roles: MapperEntities['RoleEntityMapper'][];
121
+ permission: InferEntity<MapperEntities['PermissionMapper']>;
122
+ roles: InferEntity<MapperEntities['RoleEntityMapper']>[];
122
123
  }> {
123
- let roles: MapperEntities['RoleEntityMapper'][] = [];
124
+ let roles: InferEntity<MapperEntities['RoleEntityMapper']>[] = [];
124
125
  if (addToRoles) {
125
126
  roles = await this.updateRolesWithPermissions(addToRoles, [permission]);
126
127
  }
127
128
 
128
- return { permission, roles };
129
+ return {
130
+ permission,
131
+ roles
132
+ };
129
133
  }
130
134
 
131
135
  private async extractCreatePermissionEntityToEntityData(
@@ -133,8 +137,8 @@ export class BasePermissionService<
133
137
  em?: EntityManager,
134
138
  ...args: unknown[]
135
139
  ): Promise<{
136
- permission: MapperEntities['PermissionMapper'];
137
- addToRoles: MapperEntities['RoleEntityMapper'][];
140
+ permission: InferEntity<MapperEntities['PermissionMapper']>;
141
+ addToRoles: InferEntity<MapperEntities['RoleEntityMapper']>[];
138
142
  }> {
139
143
  return {
140
144
  permission: (em ?? this.em).merge(
@@ -143,7 +147,7 @@ export class BasePermissionService<
143
147
  em ?? this.em,
144
148
  ...args
145
149
  )
146
- ),
150
+ ) as InferEntity<MapperEntities['PermissionMapper']>,
147
151
  addToRoles: permissionDto.addToRolesIds
148
152
  ? await this.getBatchRoles({ ids: permissionDto.addToRolesIds }, em)
149
153
  : []
@@ -173,7 +177,7 @@ export class BasePermissionService<
173
177
  if (em) {
174
178
  await em.persist([permission, ...roles]);
175
179
  } else {
176
- await this.em.persistAndFlush([permission, ...roles]);
180
+ await this.em.persist([permission, ...roles]).flush();
177
181
  }
178
182
 
179
183
  return this.mappers.PermissionMapper.toDto(permission);
@@ -189,8 +193,11 @@ export class BasePermissionService<
189
193
  permissionDtos
190
194
  );
191
195
  }
192
- const rolesCache: Record<string, MapperEntities['RoleEntityMapper']> = {};
193
- const permissions: MapperEntities['PermissionMapper'][] = [];
196
+ const rolesCache: Record<
197
+ string,
198
+ InferEntity<MapperEntities['RoleEntityMapper']>
199
+ > = {};
200
+ const permissions: InferEntity<MapperEntities['PermissionMapper']>[] = [];
194
201
  for (const createPermissionEntity of permissionDtos) {
195
202
  const { permission, roles } = await this.createPermissionEntity(
196
203
  await this.extractCreatePermissionEntityToEntityData(
@@ -200,15 +207,15 @@ export class BasePermissionService<
200
207
  );
201
208
  await Promise.all(
202
209
  roles.map(async (role) => {
203
- if (!role.permissions.isInitialized()) {
204
- return role.permissions.init();
210
+ if (!role.permissions?.isInitialized()) {
211
+ return role.permissions?.init();
205
212
  }
206
213
  })
207
214
  );
208
215
  await Promise.all(
209
216
  roles.map(async (role) => {
210
- if (!role.permissions.isInitialized()) {
211
- return role.permissions.init();
217
+ if (!role.permissions?.isInitialized()) {
218
+ return role.permissions?.init();
212
219
  }
213
220
  })
214
221
  );
@@ -217,9 +224,9 @@ export class BasePermissionService<
217
224
  rolesCache[role.id] &&
218
225
  role.permissions !== rolesCache[role.id].permissions
219
226
  ) {
220
- role.permissions.getItems().forEach((permission) => {
221
- if (!rolesCache[role.id].permissions.contains(permission)) {
222
- rolesCache[role.id].permissions.add(permission);
227
+ role.permissions?.getItems().forEach((permission) => {
228
+ if (!rolesCache[role.id].permissions?.contains(permission)) {
229
+ rolesCache[role.id].permissions?.add(permission);
223
230
  }
224
231
  });
225
232
  } else {
@@ -233,7 +240,7 @@ export class BasePermissionService<
233
240
  if (em) {
234
241
  await em.persist(entities);
235
242
  } else {
236
- await this.em.persistAndFlush(entities);
243
+ await this.em.persist(entities).flush();
237
244
  }
238
245
 
239
246
  return Promise.all(
@@ -250,9 +257,12 @@ export class BasePermissionService<
250
257
  if (this.evaluatedTelemetryOptions.logging) {
251
258
  this.openTelemetryCollector.info('Getting permission', idDto);
252
259
  }
253
- const permission = await (em ?? this.em).findOneOrFail('Permission', idDto);
260
+ const permission = await (em ?? this.em).findOneOrFail(
261
+ this.mappers.PermissionMapper.entity as typeof Permission,
262
+ idDto
263
+ );
254
264
  return this.mappers.PermissionMapper.toDto(
255
- permission as MapperEntities['PermissionMapper']
265
+ permission as InferEntity<MapperEntities['PermissionMapper']>
256
266
  );
257
267
  }
258
268
 
@@ -265,12 +275,15 @@ export class BasePermissionService<
265
275
  }
266
276
  return Promise.all(
267
277
  (
268
- await (em ?? this.em).find('Permission', {
269
- id: { $in: idsDto.ids }
270
- })
278
+ await (em ?? this.em).find(
279
+ this.mappers.PermissionMapper.entity as typeof Permission,
280
+ {
281
+ id: { $in: idsDto.ids }
282
+ }
283
+ )
271
284
  ).map((permission) =>
272
285
  this.mappers.PermissionMapper.toDto(
273
- permission as MapperEntities['PermissionMapper']
286
+ permission as InferEntity<MapperEntities['PermissionMapper']>
274
287
  )
275
288
  )
276
289
  );
@@ -282,8 +295,8 @@ export class BasePermissionService<
282
295
  em?: EntityManager,
283
296
  ...args: unknown[]
284
297
  ): Promise<{
285
- permission: MapperEntities['PermissionMapper'];
286
- roles: MapperEntities['RoleEntityMapper'][];
298
+ permission: InferEntity<MapperEntities['PermissionMapper']>;
299
+ roles: InferEntity<MapperEntities['RoleEntityMapper']>[];
287
300
  }> => {
288
301
  const permission = await this.mappers.UpdatePermissionMapper.toEntity(
289
302
  permissionDto,
@@ -297,17 +310,21 @@ export class BasePermissionService<
297
310
  ? await this.getBatchRoles({ ids: permissionDto.removeFromRolesIds }, em)
298
311
  : [];
299
312
 
300
- let roles: MapperEntities['RoleEntityMapper'][] = [];
313
+ let roles: InferEntity<MapperEntities['RoleEntityMapper']>[] = [];
301
314
 
302
315
  roles = roles.concat(
303
- await this.updateRolesWithPermissions(addToRoles, [permission])
316
+ await this.updateRolesWithPermissions(addToRoles, [
317
+ permission as InferEntity<MapperEntities['PermissionMapper']>
318
+ ])
304
319
  );
305
320
  roles = roles.concat(
306
- await this.removePermissionsFromRoles(removeFromRoles, [permission])
321
+ await this.removePermissionsFromRoles(removeFromRoles, [
322
+ permission as InferEntity<MapperEntities['PermissionMapper']>
323
+ ])
307
324
  );
308
325
 
309
326
  return {
310
- permission,
327
+ permission: permission as InferEntity<MapperEntities['PermissionMapper']>,
311
328
  roles
312
329
  };
313
330
  };
@@ -326,7 +343,7 @@ export class BasePermissionService<
326
343
  if (em) {
327
344
  await em.persist(entities);
328
345
  } else {
329
- await this.em.persistAndFlush(entities);
346
+ await this.em.persist(entities).flush();
330
347
  }
331
348
 
332
349
  return this.mappers.PermissionMapper.toDto(permission);
@@ -342,8 +359,11 @@ export class BasePermissionService<
342
359
  permissionDtos
343
360
  );
344
361
  }
345
- const rolesCache: Record<string, MapperEntities['RoleEntityMapper']> = {};
346
- const permissions: MapperEntities['PermissionMapper'][] = [];
362
+ const rolesCache: Record<
363
+ string,
364
+ InferEntity<MapperEntities['RoleEntityMapper']>
365
+ > = {};
366
+ const permissions: InferEntity<MapperEntities['PermissionMapper']>[] = [];
347
367
  await (em ?? this.em).transactional(async (em) => {
348
368
  permissionDtos.map(async (updatePermissionDto) => {
349
369
  const { permission, roles } =
@@ -353,9 +373,9 @@ export class BasePermissionService<
353
373
  rolesCache[role.id] &&
354
374
  role.permissions !== rolesCache[role.id].permissions
355
375
  ) {
356
- role.permissions.getItems().forEach((permission) => {
357
- if (!rolesCache[role.id].permissions.contains(permission)) {
358
- rolesCache[role.id].permissions.add(permission);
376
+ role.permissions?.getItems().forEach((permission) => {
377
+ if (!rolesCache[role.id].permissions?.contains(permission)) {
378
+ rolesCache[role.id].permissions?.add(permission);
359
379
  }
360
380
  });
361
381
  } else {
@@ -369,7 +389,7 @@ export class BasePermissionService<
369
389
  if (em) {
370
390
  await em.persist(entities);
371
391
  } else {
372
- await this.em.persistAndFlush(entities);
392
+ await this.em.persist(entities).flush();
373
393
  }
374
394
  });
375
395
 
@@ -384,7 +404,10 @@ export class BasePermissionService<
384
404
  if (this.evaluatedTelemetryOptions.logging) {
385
405
  this.openTelemetryCollector.info('Deleting permission', idDto);
386
406
  }
387
- await (em ?? this.em).nativeDelete('Permission', idDto);
407
+ await (em ?? this.em).nativeDelete(
408
+ this.mappers.PermissionMapper.entity as typeof Permission,
409
+ idDto
410
+ );
388
411
  }
389
412
 
390
413
  async deleteBatchPermissions(
@@ -394,8 +417,11 @@ export class BasePermissionService<
394
417
  if (this.evaluatedTelemetryOptions.logging) {
395
418
  this.openTelemetryCollector.info('Deleting batch permissions', idsDto);
396
419
  }
397
- await (em ?? this.em).nativeDelete('Permission', {
398
- id: { $in: idsDto.ids }
399
- });
420
+ await (em ?? this.em).nativeDelete(
421
+ this.mappers.PermissionMapper.entity as typeof Permission,
422
+ {
423
+ id: { $in: idsDto.ids }
424
+ }
425
+ );
400
426
  }
401
427
  }
@@ -5,7 +5,8 @@ import {
5
5
  TelemetryOptions
6
6
  } from '@forklaunch/core/http';
7
7
  import { RoleService } from '@forklaunch/interfaces-iam/interfaces';
8
- import { EntityManager } from '@mikro-orm/core';
8
+ import { EntityManager, FilterQuery, InferEntity } from '@mikro-orm/core';
9
+ import { Role } from '../persistence/entities';
9
10
 
10
11
  import { IdDto, IdsDto } from '@forklaunch/common';
11
12
  import {
@@ -73,7 +74,7 @@ export class BaseRoleService<
73
74
  if (em) {
74
75
  await em.persist(role);
75
76
  } else {
76
- await this.em.persistAndFlush(role);
77
+ await this.em.persist(role).flush();
77
78
  }
78
79
 
79
80
  return this.mappers.RoleMapper.toDto(role);
@@ -97,7 +98,7 @@ export class BaseRoleService<
97
98
  if (em) {
98
99
  await em.persist(roles);
99
100
  } else {
100
- await this.em.persistAndFlush(roles);
101
+ await this.em.persist(roles).flush();
101
102
  }
102
103
 
103
104
  return Promise.all(
@@ -110,11 +111,17 @@ export class BaseRoleService<
110
111
  this.openTelemetryCollector.info('Getting role', { id });
111
112
  }
112
113
 
113
- const role = await (em ?? this.em).findOneOrFail('Role', id, {
114
- populate: ['id', '*']
115
- });
114
+ const role = await (em ?? this.em).findOneOrFail(
115
+ this.mappers.RoleMapper.entity as typeof Role,
116
+ id as FilterQuery<InferEntity<MapperEntities['RoleMapper']>>,
117
+ {
118
+ populate: ['id', '*']
119
+ }
120
+ );
116
121
 
117
- return this.mappers.RoleMapper.toDto(role as MapperEntities['RoleMapper']);
122
+ return this.mappers.RoleMapper.toDto(
123
+ role as InferEntity<MapperEntities['RoleMapper']>
124
+ );
118
125
  }
119
126
 
120
127
  async getBatchRoles({ ids }: IdsDto, em?: EntityManager): Promise<RoleDto[]> {
@@ -125,7 +132,7 @@ export class BaseRoleService<
125
132
  return Promise.all(
126
133
  (
127
134
  await (em ?? this.em).find(
128
- 'Role',
135
+ this.mappers.RoleMapper.entity as typeof Role,
129
136
  {
130
137
  id: { $in: ids }
131
138
  },
@@ -134,7 +141,9 @@ export class BaseRoleService<
134
141
  }
135
142
  )
136
143
  ).map((role) =>
137
- this.mappers.RoleMapper.toDto(role as MapperEntities['RoleMapper'])
144
+ this.mappers.RoleMapper.toDto(
145
+ role as InferEntity<MapperEntities['RoleMapper']>
146
+ )
138
147
  )
139
148
  );
140
149
  }
@@ -157,7 +166,7 @@ export class BaseRoleService<
157
166
  if (em) {
158
167
  await em.persist(role);
159
168
  } else {
160
- await this.em.persistAndFlush(role);
169
+ await this.em.persist(role).flush();
161
170
  }
162
171
 
163
172
  return this.mappers.RoleMapper.toDto(role);
@@ -181,12 +190,10 @@ export class BaseRoleService<
181
190
  if (em) {
182
191
  await em.persist(roles);
183
192
  } else {
184
- await this.em.persistAndFlush(roles);
193
+ await this.em.persist(roles).flush();
185
194
  }
186
195
  return Promise.all(
187
- roles.map((role) =>
188
- this.mappers.RoleMapper.toDto(role as MapperEntities['RoleMapper'])
189
- )
196
+ roles.map((role) => this.mappers.RoleMapper.toDto(role))
190
197
  );
191
198
  }
192
199
 
@@ -195,7 +202,10 @@ export class BaseRoleService<
195
202
  this.openTelemetryCollector.info('Deleting role', idDto);
196
203
  }
197
204
 
198
- await (em ?? this.em).nativeDelete('Role', idDto);
205
+ await (em ?? this.em).nativeDelete(
206
+ this.mappers.RoleMapper.entity as typeof Role,
207
+ idDto
208
+ );
199
209
  }
200
210
 
201
211
  async deleteBatchRoles(idsDto: IdsDto, em?: EntityManager): Promise<void> {
@@ -203,6 +213,11 @@ export class BaseRoleService<
203
213
  this.openTelemetryCollector.info('Deleting batch roles', idsDto);
204
214
  }
205
215
 
206
- await (em ?? this.em).nativeDelete('Role', { id: { $in: idsDto.ids } });
216
+ await (em ?? this.em).nativeDelete(
217
+ this.mappers.RoleMapper.entity as typeof Role,
218
+ {
219
+ id: { $in: idsDto.ids }
220
+ }
221
+ );
207
222
  }
208
223
  }