@forklaunch/implementation-iam-base 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
1
  import {
2
+ evaluateTelemetryOptions,
2
3
  MetricsDefinition,
3
- OpenTelemetryCollector
4
+ OpenTelemetryCollector,
5
+ TelemetryOptions
4
6
  } from '@forklaunch/core/http';
5
7
  import { RoleService } from '@forklaunch/interfaces-iam/interfaces';
6
8
  import { EntityManager } from '@mikro-orm/core';
@@ -60,6 +62,11 @@ export class BaseRoleService<
60
62
  Entities,
61
63
  Dto
62
64
  >;
65
+ private evaluatedTelemetryOptions: {
66
+ logging?: boolean;
67
+ metrics?: boolean;
68
+ tracing?: boolean;
69
+ };
63
70
 
64
71
  constructor(
65
72
  public em: EntityManager,
@@ -81,18 +88,39 @@ export class BaseRoleService<
81
88
  Dto['UpdateRoleDtoMapper'],
82
89
  Entities['UpdateRoleDtoMapper']
83
90
  >;
91
+ },
92
+ options?: {
93
+ telemetry?: TelemetryOptions;
84
94
  }
85
95
  ) {
86
96
  this.#mappers = transformIntoInternalDtoMapper(mappers, schemaValidator);
97
+ this.evaluatedTelemetryOptions = options?.telemetry
98
+ ? evaluateTelemetryOptions(options.telemetry).enabled
99
+ : {
100
+ logging: false,
101
+ metrics: false,
102
+ tracing: false
103
+ };
87
104
  }
88
105
 
89
106
  async createRole(
90
107
  roleDto: Dto['CreateRoleDtoMapper'],
91
108
  em?: EntityManager
92
109
  ): Promise<Dto['RoleDtoMapper']> {
93
- const role =
94
- await this.#mappers.CreateRoleDtoMapper.deserializeDtoToEntity(roleDto);
95
- await (em ?? this.em).transactional((em) => em.persist(role));
110
+ if (this.evaluatedTelemetryOptions.logging) {
111
+ this.openTelemetryCollector.info('Creating role', roleDto);
112
+ }
113
+ const role = await this.#mappers.CreateRoleDtoMapper.deserializeDtoToEntity(
114
+ roleDto,
115
+ em ?? this.em
116
+ );
117
+
118
+ if (em) {
119
+ await em.persist(role);
120
+ } else {
121
+ await this.em.persistAndFlush(role);
122
+ }
123
+
96
124
  return this.#mappers.RoleDtoMapper.serializeEntityToDto(role);
97
125
  }
98
126
 
@@ -100,12 +128,25 @@ export class BaseRoleService<
100
128
  roleDtos: Dto['CreateRoleDtoMapper'][],
101
129
  em?: EntityManager
102
130
  ): Promise<Dto['RoleDtoMapper'][]> {
131
+ if (this.evaluatedTelemetryOptions.logging) {
132
+ this.openTelemetryCollector.info('Creating batch roles', roleDtos);
133
+ }
134
+
103
135
  const roles = await Promise.all(
104
136
  roleDtos.map(async (roleDto) =>
105
- this.#mappers.CreateRoleDtoMapper.deserializeDtoToEntity(roleDto)
137
+ this.#mappers.CreateRoleDtoMapper.deserializeDtoToEntity(
138
+ roleDto,
139
+ em ?? this.em
140
+ )
106
141
  )
107
142
  );
108
- await (em ?? this.em).transactional((em) => em.persist(roles));
143
+
144
+ if (em) {
145
+ await em.persist(roles);
146
+ } else {
147
+ await this.em.persistAndFlush(roles);
148
+ }
149
+
109
150
  return Promise.all(
110
151
  roles.map((role) =>
111
152
  this.#mappers.RoleDtoMapper.serializeEntityToDto(role)
@@ -113,21 +154,36 @@ export class BaseRoleService<
113
154
  );
114
155
  }
115
156
 
116
- async getRole(idDto: IdDto, em?: EntityManager): Promise<RoleDto> {
117
- const role = await (em ?? this.em).findOneOrFail('Role', idDto, {
157
+ async getRole({ id }: IdDto, em?: EntityManager): Promise<RoleDto> {
158
+ if (this.evaluatedTelemetryOptions.logging) {
159
+ this.openTelemetryCollector.info('Getting role', { id });
160
+ }
161
+
162
+ const role = await (em ?? this.em).findOneOrFail('Role', id, {
118
163
  populate: ['id', '*']
119
164
  });
165
+
120
166
  return this.#mappers.RoleDtoMapper.serializeEntityToDto(
121
167
  role as Entities['RoleDtoMapper']
122
168
  );
123
169
  }
124
170
 
125
- async getBatchRoles(idsDto: IdsDto, em?: EntityManager): Promise<RoleDto[]> {
171
+ async getBatchRoles({ ids }: IdsDto, em?: EntityManager): Promise<RoleDto[]> {
172
+ if (this.evaluatedTelemetryOptions.logging) {
173
+ this.openTelemetryCollector.info('Getting batch roles', { ids });
174
+ }
175
+
126
176
  return Promise.all(
127
177
  (
128
- await (em ?? this.em).find('Role', idsDto, {
129
- populate: ['id', '*']
130
- })
178
+ await (em ?? this.em).find(
179
+ 'Role',
180
+ {
181
+ id: { $in: ids }
182
+ },
183
+ {
184
+ populate: ['id', '*']
185
+ }
186
+ )
131
187
  ).map((role) =>
132
188
  this.#mappers.RoleDtoMapper.serializeEntityToDto(
133
189
  role as Entities['RoleDtoMapper']
@@ -140,11 +196,21 @@ export class BaseRoleService<
140
196
  roleDto: Dto['UpdateRoleDtoMapper'],
141
197
  em?: EntityManager
142
198
  ): Promise<Dto['RoleDtoMapper']> {
143
- let role =
144
- await this.#mappers.UpdateRoleDtoMapper.deserializeDtoToEntity(roleDto);
145
- await (em ?? this.em).transactional(async (em) => {
146
- role = (await em.upsert('Role', role)) as Entities['RoleDtoMapper'];
147
- });
199
+ if (this.evaluatedTelemetryOptions.logging) {
200
+ this.openTelemetryCollector.info('Updating role', roleDto);
201
+ }
202
+
203
+ const role = await this.#mappers.UpdateRoleDtoMapper.deserializeDtoToEntity(
204
+ roleDto,
205
+ em ?? this.em
206
+ );
207
+
208
+ if (em) {
209
+ await em.persist(role);
210
+ } else {
211
+ await this.em.persistAndFlush(role);
212
+ }
213
+
148
214
  return this.#mappers.RoleDtoMapper.serializeEntityToDto(role);
149
215
  }
150
216
 
@@ -152,14 +218,24 @@ export class BaseRoleService<
152
218
  roleDtos: Dto['UpdateRoleDtoMapper'][],
153
219
  em?: EntityManager
154
220
  ): Promise<Dto['RoleDtoMapper'][]> {
155
- let roles = await Promise.all(
221
+ if (this.evaluatedTelemetryOptions.logging) {
222
+ this.openTelemetryCollector.info('Updating batch roles', roleDtos);
223
+ }
224
+
225
+ const roles = await Promise.all(
156
226
  roleDtos.map(async (roleDto) =>
157
- this.#mappers.UpdateRoleDtoMapper.deserializeDtoToEntity(roleDto)
227
+ this.#mappers.UpdateRoleDtoMapper.deserializeDtoToEntity(
228
+ roleDto,
229
+ em ?? this.em
230
+ )
158
231
  )
159
232
  );
160
- await (em ?? this.em).transactional(async (em) => {
161
- roles = await em.upsertMany('Role', roles);
162
- });
233
+
234
+ if (em) {
235
+ await em.persist(roles);
236
+ } else {
237
+ await this.em.persistAndFlush(roles);
238
+ }
163
239
  return Promise.all(
164
240
  roles.map((role) =>
165
241
  this.#mappers.RoleDtoMapper.serializeEntityToDto(
@@ -170,10 +246,18 @@ export class BaseRoleService<
170
246
  }
171
247
 
172
248
  async deleteRole(idDto: IdDto, em?: EntityManager): Promise<void> {
249
+ if (this.evaluatedTelemetryOptions.logging) {
250
+ this.openTelemetryCollector.info('Deleting role', idDto);
251
+ }
252
+
173
253
  await (em ?? this.em).nativeDelete('Role', idDto);
174
254
  }
175
255
 
176
256
  async deleteBatchRoles(idsDto: IdsDto, em?: EntityManager): Promise<void> {
257
+ if (this.evaluatedTelemetryOptions.logging) {
258
+ this.openTelemetryCollector.info('Deleting batch roles', idsDto);
259
+ }
260
+
177
261
  await (em ?? this.em).nativeDelete('Role', { id: { $in: idsDto.ids } });
178
262
  }
179
263
  }
@@ -6,8 +6,10 @@ import {
6
6
 
7
7
  import { IdDto, IdsDto, InstanceTypeRecord } from '@forklaunch/common';
8
8
  import {
9
+ evaluateTelemetryOptions,
9
10
  MetricsDefinition,
10
- OpenTelemetryCollector
11
+ OpenTelemetryCollector,
12
+ TelemetryOptions
11
13
  } from '@forklaunch/core/http';
12
14
  import {
13
15
  InternalDtoMapper,
@@ -53,6 +55,11 @@ export class BaseUserService<
53
55
  Entities,
54
56
  Dto
55
57
  >;
58
+ private evaluatedTelemetryOptions: {
59
+ logging?: boolean;
60
+ metrics?: boolean;
61
+ tracing?: boolean;
62
+ };
56
63
 
57
64
  constructor(
58
65
  public em: EntityManager,
@@ -70,37 +77,47 @@ export class BaseUserService<
70
77
  CreateUserDtoMapper: RequestDtoMapperConstructor<
71
78
  SchemaValidator,
72
79
  Dto['CreateUserDtoMapper'],
73
- Entities['CreateUserDtoMapper'],
74
- (
75
- dto: never,
76
- passwordEncryptionPublicKeyPath: string
77
- ) => Promise<Entities['UpdateUserDtoMapper']>
80
+ Entities['CreateUserDtoMapper']
78
81
  >;
79
82
  UpdateUserDtoMapper: RequestDtoMapperConstructor<
80
83
  SchemaValidator,
81
84
  Dto['UpdateUserDtoMapper'],
82
- Entities['UpdateUserDtoMapper'],
83
- (
84
- dto: never,
85
- passwordEncryptionPublicKeyPath: string
86
- ) => Promise<Entities['UpdateUserDtoMapper']>
85
+ Entities['UpdateUserDtoMapper']
87
86
  >;
87
+ },
88
+ readonly options?: {
89
+ telemetry?: TelemetryOptions;
88
90
  }
89
91
  ) {
90
92
  this.#mappers = transformIntoInternalDtoMapper(mappers, schemaValidator);
93
+ this.evaluatedTelemetryOptions = options?.telemetry
94
+ ? evaluateTelemetryOptions(options.telemetry).enabled
95
+ : {
96
+ logging: false,
97
+ metrics: false,
98
+ tracing: false
99
+ };
91
100
  }
92
101
 
93
102
  async createUser(
94
103
  userDto: Dto['CreateUserDtoMapper'],
95
104
  em?: EntityManager
96
105
  ): Promise<Dto['UserDtoMapper']> {
106
+ if (this.evaluatedTelemetryOptions.logging) {
107
+ this.openTelemetryCollector.info('Creating user', userDto);
108
+ }
109
+
97
110
  const user = await this.#mappers.CreateUserDtoMapper.deserializeDtoToEntity(
98
111
  userDto,
99
- this.passwordEncryptionPublicKeyPath
112
+ em ?? this.em
100
113
  );
101
- ((await em) ?? this.em).transactional(async (em) => {
114
+
115
+ if (em) {
102
116
  await em.persist(user);
103
- });
117
+ } else {
118
+ await this.em.persistAndFlush(user);
119
+ }
120
+
104
121
  return this.#mappers.UserDtoMapper.serializeEntityToDto(user);
105
122
  }
106
123
 
@@ -108,17 +125,24 @@ export class BaseUserService<
108
125
  userDtos: Dto['CreateUserDtoMapper'][],
109
126
  em?: EntityManager
110
127
  ): Promise<Dto['UserDtoMapper'][]> {
128
+ if (this.evaluatedTelemetryOptions.logging) {
129
+ this.openTelemetryCollector.info('Creating batch users', userDtos);
130
+ }
131
+
111
132
  const users = await Promise.all(
112
133
  userDtos.map(async (createUserDto) =>
113
134
  this.#mappers.CreateUserDtoMapper.deserializeDtoToEntity(
114
135
  createUserDto,
115
- this.passwordEncryptionPublicKeyPath
136
+ em ?? this.em
116
137
  )
117
138
  )
118
139
  );
119
- await (em ?? this.em).transactional(async (em) => {
140
+
141
+ if (em) {
120
142
  await em.persist(users);
121
- });
143
+ } else {
144
+ await this.em.persistAndFlush(users);
145
+ }
122
146
 
123
147
  return Promise.all(
124
148
  users.map((user) =>
@@ -131,9 +155,14 @@ export class BaseUserService<
131
155
  idDto: IdDto,
132
156
  em?: EntityManager
133
157
  ): Promise<Dto['UserDtoMapper']> {
158
+ if (this.evaluatedTelemetryOptions.logging) {
159
+ this.openTelemetryCollector.info('Getting user', idDto);
160
+ }
161
+
134
162
  const user = await (em ?? this.em).findOneOrFail('User', idDto, {
135
163
  populate: ['id', '*']
136
164
  });
165
+
137
166
  return this.#mappers.UserDtoMapper.serializeEntityToDto(
138
167
  user as Entities['UserDtoMapper']
139
168
  );
@@ -143,6 +172,10 @@ export class BaseUserService<
143
172
  idsDto: IdsDto,
144
173
  em?: EntityManager
145
174
  ): Promise<Dto['UserDtoMapper'][]> {
175
+ if (this.evaluatedTelemetryOptions.logging) {
176
+ this.openTelemetryCollector.info('Getting batch users', idsDto);
177
+ }
178
+
146
179
  return Promise.all(
147
180
  (
148
181
  await (em ?? this.em).find('User', idsDto, {
@@ -160,13 +193,21 @@ export class BaseUserService<
160
193
  userDto: Dto['UpdateUserDtoMapper'],
161
194
  em?: EntityManager
162
195
  ): Promise<Dto['UserDtoMapper']> {
163
- let user = await this.#mappers.UpdateUserDtoMapper.deserializeDtoToEntity(
196
+ if (this.evaluatedTelemetryOptions.logging) {
197
+ this.openTelemetryCollector.info('Updating user', userDto);
198
+ }
199
+
200
+ const user = await this.#mappers.UpdateUserDtoMapper.deserializeDtoToEntity(
164
201
  userDto,
165
- this.passwordEncryptionPublicKeyPath
202
+ em ?? this.em
166
203
  );
167
- await (em ?? this.em).transactional(async (localEm) => {
168
- user = await localEm.upsert(user);
169
- });
204
+
205
+ if (em) {
206
+ await em.persist(user);
207
+ } else {
208
+ await this.em.persistAndFlush(user);
209
+ }
210
+
170
211
  return this.#mappers.UserDtoMapper.serializeEntityToDto(user);
171
212
  }
172
213
 
@@ -174,17 +215,25 @@ export class BaseUserService<
174
215
  userDtos: UpdateUserDto[],
175
216
  em?: EntityManager
176
217
  ): Promise<Dto['UserDtoMapper'][]> {
177
- let users = await Promise.all(
218
+ if (this.evaluatedTelemetryOptions.logging) {
219
+ this.openTelemetryCollector.info('Updating batch users', userDtos);
220
+ }
221
+
222
+ const users = await Promise.all(
178
223
  userDtos.map(async (updateUserDto) =>
179
224
  this.#mappers.UpdateUserDtoMapper.deserializeDtoToEntity(
180
225
  updateUserDto,
181
- this.passwordEncryptionPublicKeyPath
226
+ em ?? this.em
182
227
  )
183
228
  )
184
229
  );
185
- await (em ?? this.em).transactional(async (localEm) => {
186
- users = await localEm.upsertMany(users);
187
- });
230
+
231
+ if (em) {
232
+ await em.persist(users);
233
+ } else {
234
+ await this.em.persistAndFlush(users);
235
+ }
236
+
188
237
  return Promise.all(
189
238
  users.map((user) =>
190
239
  this.#mappers.UserDtoMapper.serializeEntityToDto(user)
@@ -193,16 +242,26 @@ export class BaseUserService<
193
242
  }
194
243
 
195
244
  async deleteUser(idDto: IdDto, em?: EntityManager): Promise<void> {
196
- const entityManager = em || this.em;
197
- await entityManager.nativeDelete('User', idDto);
245
+ if (this.evaluatedTelemetryOptions.logging) {
246
+ this.openTelemetryCollector.info('Deleting user', idDto);
247
+ }
248
+ await (em ?? this.em).nativeDelete('User', idDto);
198
249
  }
199
250
 
200
251
  async deleteBatchUsers(idsDto: IdsDto, em?: EntityManager): Promise<void> {
201
- const entityManager = em || this.em;
202
- await entityManager.nativeDelete('User', idsDto);
252
+ if (this.evaluatedTelemetryOptions.logging) {
253
+ this.openTelemetryCollector.info('Deleting batch users', idsDto);
254
+ }
255
+ await (em ?? this.em).nativeDelete('User', idsDto);
203
256
  }
204
257
 
205
258
  async verifyHasRole(idDto: IdDto, roleId: string): Promise<void> {
259
+ if (this.evaluatedTelemetryOptions.logging) {
260
+ this.openTelemetryCollector.info('Verifying user has role', {
261
+ idDto,
262
+ roleId
263
+ });
264
+ }
206
265
  const user = await this.getUser(idDto);
207
266
  if (
208
267
  user.roles.filter((role) => {
@@ -214,6 +273,12 @@ export class BaseUserService<
214
273
  }
215
274
 
216
275
  async verifyHasPermission(idDto: IdDto, permissionId: string): Promise<void> {
276
+ if (this.evaluatedTelemetryOptions.logging) {
277
+ this.openTelemetryCollector.info('Verifying user has permission', {
278
+ idDto,
279
+ permissionId
280
+ });
281
+ }
217
282
  const user = await this.getUser(idDto);
218
283
  if (
219
284
  user.roles
@@ -1,7 +1,8 @@
1
1
  import { IdDto } from '@forklaunch/common';
2
2
  import {
3
3
  MetricsDefinition,
4
- OpenTelemetryCollector
4
+ OpenTelemetryCollector,
5
+ TelemetryOptions
5
6
  } from '@forklaunch/core/http';
6
7
  import {
7
8
  RequestDtoMapperConstructor,
@@ -79,6 +80,7 @@ export declare class BaseOrganizationService<
79
80
  Entities['UpdateOrganizationDtoMapper']
80
81
  >;
81
82
  };
83
+ private evaluatedTelemetryOptions;
82
84
  constructor(
83
85
  em: EntityManager,
84
86
  openTelemetryCollector: OpenTelemetryCollector<Metrics>,
@@ -99,6 +101,9 @@ export declare class BaseOrganizationService<
99
101
  Dto['UpdateOrganizationDtoMapper'],
100
102
  Entities['UpdateOrganizationDtoMapper']
101
103
  >;
104
+ },
105
+ options?: {
106
+ telemetry?: TelemetryOptions;
102
107
  }
103
108
  );
104
109
  createOrganization(
@@ -1 +1 @@
1
- {"version":3,"file":"organization.service.d.ts","sourceRoot":"","sources":["../../services/organization.service.ts"],"names":[],"mappings":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,CAAC,6BAA6B,CAAC,EACnD,EAAE,CAAC,EAAE,aAAa,GACjB,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAelC,eAAe,CACnB,KAAK,EAAE,KAAK,EACZ,EAAE,CAAC,EAAE,aAAa,GACjB,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAUlC,kBAAkB,CACtB,eAAe,EAAE,GAAG,CAAC,6BAA6B,CAAC,EACnD,EAAE,CAAC,EAAE,aAAa,GACjB,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAWlC,kBAAkB,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAG1E"}
1
+ {"version":3,"file":"organization.service.d.ts","sourceRoot":"","sources":["../../services/organization.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAsB,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAEL,iBAAiB,EACjB,sBAAsB,EACtB,gBAAgB,EACjB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAEL,2BAA2B,EAC3B,4BAA4B,EAE7B,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC;AAC5E,OAAO,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAC;AAC5E,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,qBAAqB,EACtB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,qBAAa,uBAAuB,CAClC,eAAe,SAAS,kBAAkB,EAC1C,kBAAkB,EAClB,OAAO,SAAS,iBAAiB,GAAG,iBAAiB,EACrD,GAAG,SAAS;IACV,qBAAqB,EAAE,eAAe,CAAC,kBAAkB,CAAC,CAAC;IAC3D,2BAA2B,E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ClC,QAAQ,CAAC,6BAA6B,CAAC,CACxC,CAAC;KACH;IA1BH,OAAO,CAAC,yBAAyB,CAI/B;gBAGO,EAAE,EAAE,aAAa,EACd,sBAAsB,EAAE,sBAAsB,CAAC,OAAO,CAAC,EACvD,eAAe,EAAE,eAAe,EAChC,OAAO,EAAE;QACjB,qBAAqB,EAAE,4BAA4B,CACjD,eAAe,EACf,GAAG,CAAC,uBAAuB,CAAC,EAC5B,QAAQ,CAAC,uBAAuB,CAAC,CAClC,CAAC;QACF,2BAA2B,EAAE,2BAA2B,CACtD,eAAe,EACf,GAAG,CAAC,6BAA6B,CAAC,EAClC,QAAQ,CAAC,6BAA6B,CAAC,CACxC,CAAC;QACF,2BAA2B,EAAE,2BAA2B,CACtD,eAAe,EACf,GAAG,CAAC,6BAA6B,CAAC,EAClC,QAAQ,CAAC,6BAA6B,CAAC,CACxC,CAAC;KACH,EACD,OAAO,CAAC,EAAE;QACR,SAAS,CAAC,EAAE,gBAAgB,CAAC;KAC9B;IAYG,kBAAkB,CACtB,eAAe,EAAE,GAAG,CAAC,6BAA6B,CAAC,EACnD,EAAE,CAAC,EAAE,aAAa,GACjB,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAyBlC,eAAe,CACnB,KAAK,EAAE,KAAK,EACZ,EAAE,CAAC,EAAE,aAAa,GACjB,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAelC,kBAAkB,CACtB,eAAe,EAAE,GAAG,CAAC,6BAA6B,CAAC,EACnD,EAAE,CAAC,EAAE,aAAa,GACjB,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAyBlC,kBAAkB,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAW1E"}
@@ -1,3 +1,4 @@
1
+ import { evaluateTelemetryOptions } from '@forklaunch/core/http';
1
2
  import { transformIntoInternalDtoMapper } from '@forklaunch/core/mappers';
2
3
  export class BaseOrganizationService {
3
4
  em;
@@ -5,27 +6,46 @@ export class BaseOrganizationService {
5
6
  schemaValidator;
6
7
  mappers;
7
8
  #mappers;
8
- constructor(em, openTelemetryCollector, schemaValidator, mappers) {
9
+ evaluatedTelemetryOptions;
10
+ constructor(em, openTelemetryCollector, schemaValidator, mappers, options) {
9
11
  this.em = em;
10
12
  this.openTelemetryCollector = openTelemetryCollector;
11
13
  this.schemaValidator = schemaValidator;
12
14
  this.mappers = mappers;
13
15
  this.#mappers = transformIntoInternalDtoMapper(mappers, schemaValidator);
16
+ this.evaluatedTelemetryOptions = options?.telemetry
17
+ ? evaluateTelemetryOptions(options.telemetry).enabled
18
+ : {
19
+ logging: false,
20
+ metrics: false,
21
+ tracing: false
22
+ };
14
23
  }
15
24
  async createOrganization(organizationDto, em) {
16
- this.openTelemetryCollector.log('info', 'Creating organization');
25
+ if (this.evaluatedTelemetryOptions.logging) {
26
+ this.openTelemetryCollector.info(
27
+ 'Creating organization',
28
+ organizationDto
29
+ );
30
+ }
17
31
  const organization =
18
32
  await this.#mappers.CreateOrganizationDtoMapper.deserializeDtoToEntity(
19
- organizationDto
33
+ organizationDto,
34
+ em ?? this.em
20
35
  );
21
- await (em ?? this.em).transactional(async (innerEm) => {
22
- await innerEm.persist(organization);
23
- });
36
+ if (em) {
37
+ await em.persist(organization);
38
+ } else {
39
+ await this.em.persistAndFlush(organization);
40
+ }
24
41
  return this.#mappers.OrganizationDtoMapper.serializeEntityToDto(
25
42
  organization
26
43
  );
27
44
  }
28
45
  async getOrganization(idDto, em) {
46
+ if (this.evaluatedTelemetryOptions.logging) {
47
+ this.openTelemetryCollector.info('Getting organization', idDto);
48
+ }
29
49
  const organization = await (em ?? this.em).findOneOrFail(
30
50
  'Organization',
31
51
  idDto
@@ -35,16 +55,34 @@ export class BaseOrganizationService {
35
55
  );
36
56
  }
37
57
  async updateOrganization(organizationDto, em) {
58
+ if (this.evaluatedTelemetryOptions.logging) {
59
+ this.openTelemetryCollector.info(
60
+ 'Updating organization',
61
+ organizationDto
62
+ );
63
+ }
38
64
  const updatedOrganization =
39
65
  await this.#mappers.UpdateOrganizationDtoMapper.deserializeDtoToEntity(
40
- organizationDto
66
+ organizationDto,
67
+ em ?? this.em
41
68
  );
42
- await (em ?? this.em).upsert(updatedOrganization);
69
+ if (em) {
70
+ await em.persist(updatedOrganization);
71
+ } else {
72
+ await this.em.persistAndFlush(updatedOrganization);
73
+ }
43
74
  return this.#mappers.OrganizationDtoMapper.serializeEntityToDto(
44
75
  updatedOrganization
45
76
  );
46
77
  }
47
78
  async deleteOrganization(idDto, em) {
48
- await (em ?? this.em).nativeDelete('Organization', idDto);
79
+ if (this.evaluatedTelemetryOptions.logging) {
80
+ this.openTelemetryCollector.info('Deleting organization', idDto);
81
+ }
82
+ if (em) {
83
+ await em.nativeDelete('Organization', idDto);
84
+ } else {
85
+ await this.em.nativeDelete('Organization', idDto);
86
+ }
49
87
  }
50
88
  }
@@ -5,7 +5,8 @@ import {
5
5
  import { IdDto, IdsDto } from '@forklaunch/common';
6
6
  import {
7
7
  MetricsDefinition,
8
- OpenTelemetryCollector
8
+ OpenTelemetryCollector,
9
+ TelemetryOptions
9
10
  } from '@forklaunch/core/http';
10
11
  import {
11
12
  RequestDtoMapperConstructor,
@@ -15,8 +16,8 @@ import { MapNestedDtoArraysToCollections } from '@forklaunch/core/services';
15
16
  import {
16
17
  CreatePermissionDto,
17
18
  PermissionDto,
18
- RoleDto,
19
- UpdatePermissionDto
19
+ UpdatePermissionDto,
20
+ UpdateRoleDto
20
21
  } from '@forklaunch/interfaces-iam/types';
21
22
  import { AnySchemaValidator } from '@forklaunch/validator';
22
23
  import { EntityManager } from '@mikro-orm/core';
@@ -27,23 +28,29 @@ export declare class BasePermissionService<
27
28
  PermissionDtoMapper: PermissionDto;
28
29
  CreatePermissionDtoMapper: CreatePermissionDto;
29
30
  UpdatePermissionDtoMapper: UpdatePermissionDto;
30
- RoleDtoMapper: RoleDto;
31
+ RoleEntityMapper: UpdateRoleDto;
31
32
  } = {
32
33
  PermissionDtoMapper: PermissionDto;
33
34
  CreatePermissionDtoMapper: CreatePermissionDto;
34
35
  UpdatePermissionDtoMapper: UpdatePermissionDto;
35
- RoleDtoMapper: RoleDto;
36
+ RoleEntityMapper: UpdateRoleDto;
36
37
  },
37
38
  Entities extends {
38
39
  PermissionDtoMapper: PermissionDto;
39
40
  CreatePermissionDtoMapper: PermissionDto;
40
41
  UpdatePermissionDtoMapper: PermissionDto;
41
- RoleDtoMapper: MapNestedDtoArraysToCollections<RoleDto, 'permissions'>;
42
+ RoleEntityMapper: MapNestedDtoArraysToCollections<
43
+ UpdateRoleDto,
44
+ 'permissions'
45
+ >;
42
46
  } = {
43
47
  PermissionDtoMapper: PermissionDto;
44
48
  CreatePermissionDtoMapper: PermissionDto;
45
49
  UpdatePermissionDtoMapper: PermissionDto;
46
- RoleDtoMapper: MapNestedDtoArraysToCollections<RoleDto, 'permissions'>;
50
+ RoleEntityMapper: MapNestedDtoArraysToCollections<
51
+ UpdateRoleDto,
52
+ 'permissions'
53
+ >;
47
54
  }
48
55
  > implements PermissionService
49
56
  {
@@ -68,12 +75,13 @@ export declare class BasePermissionService<
68
75
  Dto['UpdatePermissionDtoMapper'],
69
76
  Entities['UpdatePermissionDtoMapper']
70
77
  >;
71
- RoleDtoMapper: RequestDtoMapperConstructor<
78
+ RoleEntityMapper: RequestDtoMapperConstructor<
72
79
  SchemaValidator,
73
- Dto['RoleDtoMapper'],
74
- Entities['RoleDtoMapper']
80
+ Dto['RoleEntityMapper'],
81
+ Entities['RoleEntityMapper']
75
82
  >;
76
83
  };
84
+ private evaluatedTelemetryOptions;
77
85
  constructor(
78
86
  em: EntityManager,
79
87
  roleServiceFactory: () => RoleService,
@@ -95,11 +103,14 @@ export declare class BasePermissionService<
95
103
  Dto['UpdatePermissionDtoMapper'],
96
104
  Entities['UpdatePermissionDtoMapper']
97
105
  >;
98
- RoleDtoMapper: RequestDtoMapperConstructor<
106
+ RoleEntityMapper: RequestDtoMapperConstructor<
99
107
  SchemaValidator,
100
- Dto['RoleDtoMapper'],
101
- Entities['RoleDtoMapper']
108
+ Dto['RoleEntityMapper'],
109
+ Entities['RoleEntityMapper']
102
110
  >;
111
+ },
112
+ options?: {
113
+ telemetry?: TelemetryOptions;
103
114
  }
104
115
  );
105
116
  private updateRolesWithPermissions;