@forklaunch/implementation-iam-base 0.1.8 → 0.1.10

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 (42) hide show
  1. package/lib/__test__/schemaEquality.test.d.ts +1 -1
  2. package/lib/__test__/schemaEquality.test.js +235 -135
  3. package/lib/jest.config.d.ts +1 -1
  4. package/lib/jest.config.js +16 -16
  5. package/lib/schemas/index.d.ts +1 -1
  6. package/lib/schemas/organization.schema.d.ts +361 -150
  7. package/lib/schemas/organization.schema.js +4 -1
  8. package/lib/schemas/permission.schema.d.ts +88 -34
  9. package/lib/schemas/permission.schema.js +4 -1
  10. package/lib/schemas/role.schema.d.ts +139 -50
  11. package/lib/schemas/role.schema.js +4 -1
  12. package/lib/schemas/typebox/organization.schema.d.ts +405 -103
  13. package/lib/schemas/typebox/organization.schema.js +27 -17
  14. package/lib/schemas/typebox/permission.schema.d.ts +122 -38
  15. package/lib/schemas/typebox/permission.schema.js +24 -17
  16. package/lib/schemas/typebox/role.schema.d.ts +190 -50
  17. package/lib/schemas/typebox/role.schema.js +24 -17
  18. package/lib/schemas/typebox/user.schema.d.ts +330 -94
  19. package/lib/schemas/typebox/user.schema.js +40 -32
  20. package/lib/schemas/user.schema.d.ts +262 -116
  21. package/lib/schemas/user.schema.js +4 -1
  22. package/lib/schemas/zod/organization.schema.d.ts +308 -218
  23. package/lib/schemas/zod/organization.schema.js +27 -17
  24. package/lib/schemas/zod/permission.schema.d.ts +54 -38
  25. package/lib/schemas/zod/permission.schema.js +24 -17
  26. package/lib/schemas/zod/role.schema.d.ts +96 -66
  27. package/lib/schemas/zod/role.schema.js +24 -17
  28. package/lib/schemas/zod/user.schema.d.ts +189 -143
  29. package/lib/schemas/zod/user.schema.js +40 -32
  30. package/lib/services/index.d.ts +1 -1
  31. package/lib/services/organization.service.d.ts +106 -33
  32. package/lib/services/organization.service.js +47 -32
  33. package/lib/services/permission.service.d.ts +117 -41
  34. package/lib/services/permission.service.js +193 -149
  35. package/lib/services/role.service.d.ts +104 -35
  36. package/lib/services/role.service.js +70 -54
  37. package/lib/services/user.service.d.ts +121 -37
  38. package/lib/services/user.service.js +121 -80
  39. package/lib/tsconfig.tsbuildinfo +1 -1
  40. package/lib/vitest.config.d.ts +2 -2
  41. package/lib/vitest.config.js +4 -4
  42. package/package.json +13 -13
@@ -1,85 +1,126 @@
1
1
  import { transformIntoInternalDtoMapper } from '@forklaunch/core/mappers';
2
2
  export class BaseUserService {
3
- em;
4
- passwordEncryptionPublicKeyPath;
5
- roleServiceFactory;
6
- organizationServiceFactory;
7
- openTelemetryCollector;
8
- schemaValidator;
9
- mapperss;
10
- #mapperss;
11
- constructor(em, passwordEncryptionPublicKeyPath, roleServiceFactory, organizationServiceFactory, openTelemetryCollector, schemaValidator, mapperss) {
12
- this.em = em;
13
- this.passwordEncryptionPublicKeyPath = passwordEncryptionPublicKeyPath;
14
- this.roleServiceFactory = roleServiceFactory;
15
- this.organizationServiceFactory = organizationServiceFactory;
16
- this.openTelemetryCollector = openTelemetryCollector;
17
- this.schemaValidator = schemaValidator;
18
- this.mapperss = mapperss;
19
- this.#mapperss = transformIntoInternalDtoMapper(mapperss, schemaValidator);
3
+ em;
4
+ passwordEncryptionPublicKeyPath;
5
+ roleServiceFactory;
6
+ organizationServiceFactory;
7
+ openTelemetryCollector;
8
+ schemaValidator;
9
+ mapperss;
10
+ #mapperss;
11
+ constructor(
12
+ em,
13
+ passwordEncryptionPublicKeyPath,
14
+ roleServiceFactory,
15
+ organizationServiceFactory,
16
+ openTelemetryCollector,
17
+ schemaValidator,
18
+ mapperss
19
+ ) {
20
+ this.em = em;
21
+ this.passwordEncryptionPublicKeyPath = passwordEncryptionPublicKeyPath;
22
+ this.roleServiceFactory = roleServiceFactory;
23
+ this.organizationServiceFactory = organizationServiceFactory;
24
+ this.openTelemetryCollector = openTelemetryCollector;
25
+ this.schemaValidator = schemaValidator;
26
+ this.mapperss = mapperss;
27
+ this.#mapperss = transformIntoInternalDtoMapper(mapperss, schemaValidator);
28
+ }
29
+ async createUser(userDto, em) {
30
+ const user =
31
+ await this.#mapperss.CreateUserDtoMapper.deserializeDtoToEntity(
32
+ userDto,
33
+ this.passwordEncryptionPublicKeyPath
34
+ );
35
+ ((await em) ?? this.em).transactional(async (em) => {
36
+ await em.persist(user);
37
+ });
38
+ return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
39
+ }
40
+ async createBatchUsers(userDtos, em) {
41
+ const users = await Promise.all(
42
+ userDtos.map(async (createUserDto) =>
43
+ this.#mapperss.CreateUserDtoMapper.deserializeDtoToEntity(
44
+ createUserDto,
45
+ this.passwordEncryptionPublicKeyPath
46
+ )
47
+ )
48
+ );
49
+ await (em ?? this.em).transactional(async (em) => {
50
+ await em.persist(users);
51
+ });
52
+ return users.map((user) =>
53
+ this.#mapperss.UserDtoMapper.serializeEntityToDto(user)
54
+ );
55
+ }
56
+ async getUser(idDto, em) {
57
+ const user = await (em ?? this.em).findOneOrFail('User', idDto, {
58
+ populate: ['id', '*']
59
+ });
60
+ return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
61
+ }
62
+ async getBatchUsers(idsDto, em) {
63
+ return (
64
+ await (em ?? this.em).find('User', idsDto, {
65
+ populate: ['id', '*']
66
+ })
67
+ ).map((user) => this.#mapperss.UserDtoMapper.serializeEntityToDto(user));
68
+ }
69
+ async updateUser(userDto, em) {
70
+ let user = this.#mapperss.UpdateUserDtoMapper.deserializeDtoToEntity(
71
+ userDto,
72
+ this.passwordEncryptionPublicKeyPath
73
+ );
74
+ await (em ?? this.em).transactional(async (localEm) => {
75
+ user = await localEm.upsert(user);
76
+ });
77
+ return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
78
+ }
79
+ async updateBatchUsers(userDtos, em) {
80
+ let users = await Promise.all(
81
+ userDtos.map(async (updateUserDto) =>
82
+ this.#mapperss.UpdateUserDtoMapper.deserializeDtoToEntity(
83
+ updateUserDto,
84
+ this.passwordEncryptionPublicKeyPath
85
+ )
86
+ )
87
+ );
88
+ await (em ?? this.em).transactional(async (localEm) => {
89
+ users = await localEm.upsertMany(users);
90
+ });
91
+ return users.map((user) =>
92
+ this.#mapperss.UserDtoMapper.serializeEntityToDto(user)
93
+ );
94
+ }
95
+ async deleteUser(idDto, em) {
96
+ const entityManager = em || this.em;
97
+ await entityManager.nativeDelete('User', idDto);
98
+ }
99
+ async deleteBatchUsers(idsDto, em) {
100
+ const entityManager = em || this.em;
101
+ await entityManager.nativeDelete('User', idsDto);
102
+ }
103
+ async verifyHasRole(idDto, roleId) {
104
+ const user = await this.getUser(idDto);
105
+ if (
106
+ user.roles.filter((role) => {
107
+ return roleId == role.id;
108
+ }).length === 0
109
+ ) {
110
+ throw new Error(`User ${idDto.id} does not have role ${roleId}`);
20
111
  }
21
- async createUser(userDto, em) {
22
- const user = await this.#mapperss.CreateUserDtoMapper.deserializeDtoToEntity(userDto, this.passwordEncryptionPublicKeyPath);
23
- ((await em) ?? this.em).transactional(async (em) => {
24
- await em.persist(user);
25
- });
26
- return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
27
- }
28
- async createBatchUsers(userDtos, em) {
29
- const users = await Promise.all(userDtos.map(async (createUserDto) => this.#mapperss.CreateUserDtoMapper.deserializeDtoToEntity(createUserDto, this.passwordEncryptionPublicKeyPath)));
30
- await (em ?? this.em).transactional(async (em) => {
31
- await em.persist(users);
32
- });
33
- return users.map((user) => this.#mapperss.UserDtoMapper.serializeEntityToDto(user));
34
- }
35
- async getUser(idDto, em) {
36
- const user = await (em ?? this.em).findOneOrFail('User', idDto, {
37
- populate: ['id', '*']
38
- });
39
- return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
40
- }
41
- async getBatchUsers(idsDto, em) {
42
- return (await (em ?? this.em).find('User', idsDto, {
43
- populate: ['id', '*']
44
- })).map((user) => this.#mapperss.UserDtoMapper.serializeEntityToDto(user));
45
- }
46
- async updateUser(userDto, em) {
47
- let user = this.#mapperss.UpdateUserDtoMapper.deserializeDtoToEntity(userDto, this.passwordEncryptionPublicKeyPath);
48
- await (em ?? this.em).transactional(async (localEm) => {
49
- user = await localEm.upsert(user);
50
- });
51
- return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
52
- }
53
- async updateBatchUsers(userDtos, em) {
54
- let users = await Promise.all(userDtos.map(async (updateUserDto) => this.#mapperss.UpdateUserDtoMapper.deserializeDtoToEntity(updateUserDto, this.passwordEncryptionPublicKeyPath)));
55
- await (em ?? this.em).transactional(async (localEm) => {
56
- users = await localEm.upsertMany(users);
57
- });
58
- return users.map((user) => this.#mapperss.UserDtoMapper.serializeEntityToDto(user));
59
- }
60
- async deleteUser(idDto, em) {
61
- const entityManager = em || this.em;
62
- await entityManager.nativeDelete('User', idDto);
63
- }
64
- async deleteBatchUsers(idsDto, em) {
65
- const entityManager = em || this.em;
66
- await entityManager.nativeDelete('User', idsDto);
67
- }
68
- async verifyHasRole(idDto, roleId) {
69
- const user = await this.getUser(idDto);
70
- if (user.roles.filter((role) => {
71
- return roleId == role.id;
72
- }).length === 0) {
73
- throw new Error(`User ${idDto.id} does not have role ${roleId}`);
74
- }
75
- }
76
- async verifyHasPermission(idDto, permissionId) {
77
- const user = await this.getUser(idDto);
78
- if (user.roles
79
- .map((role) => role.permissions.map((permission) => permission.id))
80
- .flat()
81
- .filter((id) => id == permissionId).length === 0) {
82
- throw new Error(`User ${idDto.id} does not have permission ${permissionId}`);
83
- }
112
+ }
113
+ async verifyHasPermission(idDto, permissionId) {
114
+ const user = await this.getUser(idDto);
115
+ if (
116
+ user.roles
117
+ .map((role) => role.permissions.map((permission) => permission.id))
118
+ .flat()
119
+ .filter((id) => id == permissionId).length === 0
120
+ ) {
121
+ throw new Error(
122
+ `User ${idDto.id} does not have permission ${permissionId}`
123
+ );
84
124
  }
125
+ }
85
126
  }