@forklaunch/implementation-iam-base 0.2.0 → 0.2.2

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,3 +1,4 @@
1
+ import { evaluateTelemetryOptions } from '@forklaunch/core/http';
1
2
  import { transformIntoInternalDtoMapper } from '@forklaunch/core/mappers';
2
3
  export class BaseUserService {
3
4
  em;
@@ -7,7 +8,9 @@ export class BaseUserService {
7
8
  openTelemetryCollector;
8
9
  schemaValidator;
9
10
  mappers;
11
+ options;
10
12
  #mappers;
13
+ evaluatedTelemetryOptions;
11
14
  constructor(
12
15
  em,
13
16
  passwordEncryptionPublicKeyPath,
@@ -15,7 +18,8 @@ export class BaseUserService {
15
18
  organizationServiceFactory,
16
19
  openTelemetryCollector,
17
20
  schemaValidator,
18
- mappers
21
+ mappers,
22
+ options
19
23
  ) {
20
24
  this.em = em;
21
25
  this.passwordEncryptionPublicKeyPath = passwordEncryptionPublicKeyPath;
@@ -24,30 +28,48 @@ export class BaseUserService {
24
28
  this.openTelemetryCollector = openTelemetryCollector;
25
29
  this.schemaValidator = schemaValidator;
26
30
  this.mappers = mappers;
31
+ this.options = options;
27
32
  this.#mappers = transformIntoInternalDtoMapper(mappers, schemaValidator);
33
+ this.evaluatedTelemetryOptions = options?.telemetry
34
+ ? evaluateTelemetryOptions(options.telemetry).enabled
35
+ : {
36
+ logging: false,
37
+ metrics: false,
38
+ tracing: false
39
+ };
28
40
  }
29
41
  async createUser(userDto, em) {
42
+ if (this.evaluatedTelemetryOptions.logging) {
43
+ this.openTelemetryCollector.info('Creating user', userDto);
44
+ }
30
45
  const user = await this.#mappers.CreateUserDtoMapper.deserializeDtoToEntity(
31
46
  userDto,
32
- this.passwordEncryptionPublicKeyPath
47
+ em ?? this.em
33
48
  );
34
- ((await em) ?? this.em).transactional(async (em) => {
49
+ if (em) {
35
50
  await em.persist(user);
36
- });
51
+ } else {
52
+ await this.em.persistAndFlush(user);
53
+ }
37
54
  return this.#mappers.UserDtoMapper.serializeEntityToDto(user);
38
55
  }
39
56
  async createBatchUsers(userDtos, em) {
57
+ if (this.evaluatedTelemetryOptions.logging) {
58
+ this.openTelemetryCollector.info('Creating batch users', userDtos);
59
+ }
40
60
  const users = await Promise.all(
41
61
  userDtos.map(async (createUserDto) =>
42
62
  this.#mappers.CreateUserDtoMapper.deserializeDtoToEntity(
43
63
  createUserDto,
44
- this.passwordEncryptionPublicKeyPath
64
+ em ?? this.em
45
65
  )
46
66
  )
47
67
  );
48
- await (em ?? this.em).transactional(async (em) => {
68
+ if (em) {
49
69
  await em.persist(users);
50
- });
70
+ } else {
71
+ await this.em.persistAndFlush(users);
72
+ }
51
73
  return Promise.all(
52
74
  users.map((user) =>
53
75
  this.#mappers.UserDtoMapper.serializeEntityToDto(user)
@@ -55,12 +77,18 @@ export class BaseUserService {
55
77
  );
56
78
  }
57
79
  async getUser(idDto, em) {
80
+ if (this.evaluatedTelemetryOptions.logging) {
81
+ this.openTelemetryCollector.info('Getting user', idDto);
82
+ }
58
83
  const user = await (em ?? this.em).findOneOrFail('User', idDto, {
59
84
  populate: ['id', '*']
60
85
  });
61
86
  return this.#mappers.UserDtoMapper.serializeEntityToDto(user);
62
87
  }
63
88
  async getBatchUsers(idsDto, em) {
89
+ if (this.evaluatedTelemetryOptions.logging) {
90
+ this.openTelemetryCollector.info('Getting batch users', idsDto);
91
+ }
64
92
  return Promise.all(
65
93
  (
66
94
  await (em ?? this.em).find('User', idsDto, {
@@ -70,27 +98,37 @@ export class BaseUserService {
70
98
  );
71
99
  }
72
100
  async updateUser(userDto, em) {
73
- let user = await this.#mappers.UpdateUserDtoMapper.deserializeDtoToEntity(
101
+ if (this.evaluatedTelemetryOptions.logging) {
102
+ this.openTelemetryCollector.info('Updating user', userDto);
103
+ }
104
+ const user = await this.#mappers.UpdateUserDtoMapper.deserializeDtoToEntity(
74
105
  userDto,
75
- this.passwordEncryptionPublicKeyPath
106
+ em ?? this.em
76
107
  );
77
- await (em ?? this.em).transactional(async (localEm) => {
78
- user = await localEm.upsert(user);
79
- });
108
+ if (em) {
109
+ await em.persist(user);
110
+ } else {
111
+ await this.em.persistAndFlush(user);
112
+ }
80
113
  return this.#mappers.UserDtoMapper.serializeEntityToDto(user);
81
114
  }
82
115
  async updateBatchUsers(userDtos, em) {
83
- let users = await Promise.all(
116
+ if (this.evaluatedTelemetryOptions.logging) {
117
+ this.openTelemetryCollector.info('Updating batch users', userDtos);
118
+ }
119
+ const users = await Promise.all(
84
120
  userDtos.map(async (updateUserDto) =>
85
121
  this.#mappers.UpdateUserDtoMapper.deserializeDtoToEntity(
86
122
  updateUserDto,
87
- this.passwordEncryptionPublicKeyPath
123
+ em ?? this.em
88
124
  )
89
125
  )
90
126
  );
91
- await (em ?? this.em).transactional(async (localEm) => {
92
- users = await localEm.upsertMany(users);
93
- });
127
+ if (em) {
128
+ await em.persist(users);
129
+ } else {
130
+ await this.em.persistAndFlush(users);
131
+ }
94
132
  return Promise.all(
95
133
  users.map((user) =>
96
134
  this.#mappers.UserDtoMapper.serializeEntityToDto(user)
@@ -98,14 +136,24 @@ export class BaseUserService {
98
136
  );
99
137
  }
100
138
  async deleteUser(idDto, em) {
101
- const entityManager = em || this.em;
102
- await entityManager.nativeDelete('User', idDto);
139
+ if (this.evaluatedTelemetryOptions.logging) {
140
+ this.openTelemetryCollector.info('Deleting user', idDto);
141
+ }
142
+ await (em ?? this.em).nativeDelete('User', idDto);
103
143
  }
104
144
  async deleteBatchUsers(idsDto, em) {
105
- const entityManager = em || this.em;
106
- await entityManager.nativeDelete('User', idsDto);
145
+ if (this.evaluatedTelemetryOptions.logging) {
146
+ this.openTelemetryCollector.info('Deleting batch users', idsDto);
147
+ }
148
+ await (em ?? this.em).nativeDelete('User', idsDto);
107
149
  }
108
150
  async verifyHasRole(idDto, roleId) {
151
+ if (this.evaluatedTelemetryOptions.logging) {
152
+ this.openTelemetryCollector.info('Verifying user has role', {
153
+ idDto,
154
+ roleId
155
+ });
156
+ }
109
157
  const user = await this.getUser(idDto);
110
158
  if (
111
159
  user.roles.filter((role) => {
@@ -116,6 +164,12 @@ export class BaseUserService {
116
164
  }
117
165
  }
118
166
  async verifyHasPermission(idDto, permissionId) {
167
+ if (this.evaluatedTelemetryOptions.logging) {
168
+ this.openTelemetryCollector.info('Verifying user has permission', {
169
+ idDto,
170
+ permissionId
171
+ });
172
+ }
119
173
  const user = await this.getUser(idDto);
120
174
  if (
121
175
  user.roles