@forklaunch/implementation-iam-base 0.1.2 → 0.1.4

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 (84) hide show
  1. package/lib/__test__/schemaEquality.test.d.ts +2 -0
  2. package/lib/__test__/schemaEquality.test.d.ts.map +1 -0
  3. package/lib/__test__/schemaEquality.test.js +175 -0
  4. package/{schemas/typebox → lib/eject/schemas/zod}/organization.schema.ts +4 -4
  5. package/{schemas/typebox → lib/eject/schemas/zod}/permission.schema.ts +1 -1
  6. package/{schemas/typebox → lib/eject/schemas/zod}/role.schema.ts +4 -4
  7. package/{schemas/typebox → lib/eject/schemas/zod}/user.schema.ts +4 -4
  8. package/lib/eject/services/services/organization.service.ts +146 -0
  9. package/lib/eject/services/services/permission.service.ts +349 -0
  10. package/lib/eject/services/services/role.service.ts +182 -0
  11. package/lib/eject/services/user.service.ts +225 -0
  12. package/lib/index.d.ts +9 -0
  13. package/lib/index.d.ts.map +1 -0
  14. package/lib/jest.config.d.ts +4 -0
  15. package/lib/jest.config.d.ts.map +1 -0
  16. package/lib/jest.config.js +19 -0
  17. package/lib/schemas/organization.schema.d.ts +58 -0
  18. package/lib/schemas/organization.schema.d.ts.map +1 -0
  19. package/lib/schemas/organization.schema.js +14 -0
  20. package/lib/schemas/permission.schema.d.ts +42 -0
  21. package/lib/schemas/permission.schema.d.ts.map +1 -0
  22. package/lib/schemas/permission.schema.js +14 -0
  23. package/lib/schemas/role.schema.d.ts +42 -0
  24. package/lib/schemas/role.schema.d.ts.map +1 -0
  25. package/lib/schemas/role.schema.js +14 -0
  26. package/lib/schemas/typebox/organization.schema.d.ts +30 -0
  27. package/lib/schemas/typebox/organization.schema.d.ts.map +1 -0
  28. package/lib/schemas/typebox/organization.schema.js +28 -0
  29. package/lib/schemas/typebox/permission.schema.d.ts +20 -0
  30. package/lib/schemas/typebox/permission.schema.d.ts.map +1 -0
  31. package/lib/schemas/typebox/permission.schema.js +20 -0
  32. package/lib/schemas/typebox/role.schema.d.ts +22 -0
  33. package/lib/schemas/typebox/role.schema.d.ts.map +1 -0
  34. package/lib/schemas/typebox/role.schema.js +20 -0
  35. package/lib/schemas/typebox/user.schema.d.ts +37 -0
  36. package/lib/schemas/typebox/user.schema.d.ts.map +1 -0
  37. package/lib/schemas/typebox/user.schema.js +35 -0
  38. package/lib/schemas/user.schema.d.ts +72 -0
  39. package/lib/schemas/user.schema.d.ts.map +1 -0
  40. package/lib/schemas/user.schema.js +14 -0
  41. package/lib/schemas/zod/organization.schema.d.ts +30 -0
  42. package/lib/schemas/zod/organization.schema.d.ts.map +1 -0
  43. package/lib/schemas/zod/organization.schema.js +28 -0
  44. package/lib/schemas/zod/permission.schema.d.ts +20 -0
  45. package/lib/schemas/zod/permission.schema.d.ts.map +1 -0
  46. package/lib/schemas/zod/permission.schema.js +20 -0
  47. package/lib/schemas/zod/role.schema.d.ts +22 -0
  48. package/lib/schemas/zod/role.schema.d.ts.map +1 -0
  49. package/lib/schemas/zod/role.schema.js +20 -0
  50. package/lib/schemas/zod/user.schema.d.ts +37 -0
  51. package/lib/schemas/zod/user.schema.d.ts.map +1 -0
  52. package/lib/schemas/zod/user.schema.js +35 -0
  53. package/lib/services/organization.service.d.ts +44 -0
  54. package/lib/services/organization.service.d.ts.map +1 -0
  55. package/lib/services/organization.service.js +35 -0
  56. package/lib/services/permission.service.d.ts +61 -0
  57. package/lib/services/permission.service.d.ts.map +1 -0
  58. package/lib/services/permission.service.js +159 -0
  59. package/lib/services/role.service.d.ts +48 -0
  60. package/lib/services/role.service.d.ts.map +1 -0
  61. package/lib/services/role.service.js +57 -0
  62. package/lib/services/user.service.d.ts +54 -0
  63. package/lib/services/user.service.d.ts.map +1 -0
  64. package/lib/services/user.service.js +85 -0
  65. package/lib/tsconfig.tsbuildinfo +1 -0
  66. package/lib/vitest.config.d.ts +3 -0
  67. package/lib/vitest.config.d.ts.map +1 -0
  68. package/lib/vitest.config.js +7 -0
  69. package/package.json +6 -4
  70. package/__test__/schemaEquality.test.ts +0 -293
  71. package/schemas/organization.schema.ts +0 -28
  72. package/schemas/permission.schema.ts +0 -28
  73. package/schemas/role.schema.ts +0 -28
  74. package/schemas/user.schema.ts +0 -28
  75. package/tsconfig.json +0 -7
  76. /package/{schemas/zod → lib/eject/schemas}/organization.schema.ts +0 -0
  77. /package/{schemas/zod → lib/eject/schemas}/permission.schema.ts +0 -0
  78. /package/{schemas/zod → lib/eject/schemas}/role.schema.ts +0 -0
  79. /package/{schemas/zod → lib/eject/schemas}/user.schema.ts +0 -0
  80. /package/{services → lib/eject/services}/organization.service.ts +0 -0
  81. /package/{services → lib/eject/services}/permission.service.ts +0 -0
  82. /package/{services → lib/eject/services}/role.service.ts +0 -0
  83. /package/{services → lib/eject/services/services}/user.service.ts +0 -0
  84. /package/{index.ts → lib/index.js} +0 -0
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=schemaEquality.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemaEquality.test.d.ts","sourceRoot":"","sources":["../../__test__/schemaEquality.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,175 @@
1
+ import { isTrue } from '@forklaunch/common';
2
+ import { DummyEnum, testSchemaEquality } from '@forklaunch/core/test';
3
+ import { CreateOrganizationSchema as TypeboxCreateOrganizationSchema, OrganizationSchema as TypeboxOrganizationSchema, UpdateOrganizationSchema as TypeboxUpdateOrganizationSchema } from '../schemas/typebox/organization.schema';
4
+ import { CreatePermissionSchema as TypeboxCreatePermissionSchema, PermissionSchema as TypeboxPermissionSchema, UpdatePermissionSchema as TypeboxUpdatePermissionSchema } from '../schemas/typebox/permission.schema';
5
+ import { CreateRoleSchema as TypeboxCreateRoleSchema, RoleSchema as TypeboxRoleSchema, UpdateRoleSchema as TypeboxUpdateRoleSchema } from '../schemas/typebox/role.schema';
6
+ import { CreateUserSchema as TypeboxCreateUserSchema, UpdateUserSchema as TypeboxUpdateUserSchema, UserSchema as TypeboxUserSchema } from '../schemas/typebox/user.schema';
7
+ import { CreateOrganizationSchema as ZodCreateOrganizationSchema, OrganizationSchema as ZodOrganizationSchema, UpdateOrganizationSchema as ZodUpdateOrganizationSchema } from '../schemas/zod/organization.schema';
8
+ import { CreatePermissionSchema as ZodCreatePermissionSchema, PermissionSchema as ZodPermissionSchema, UpdatePermissionSchema as ZodUpdatePermissionSchema } from '../schemas/zod/permission.schema';
9
+ import { CreateRoleSchema as ZodCreateRoleSchema, RoleSchema as ZodRoleSchema, UpdateRoleSchema as ZodUpdateRoleSchema } from '../schemas/zod/role.schema';
10
+ import { CreateUserSchema as ZodCreateUserSchema, UpdateUserSchema as ZodUpdateUserSchema, UserSchema as ZodUserSchema } from '../schemas/zod/user.schema';
11
+ const zodUpdatePermissionSchema = ZodUpdatePermissionSchema(false);
12
+ const typeboxUpdatePermissionSchema = TypeboxUpdatePermissionSchema(false);
13
+ const zodPermissionSchema = ZodPermissionSchema(false);
14
+ const typeboxPermissionSchema = TypeboxPermissionSchema(false);
15
+ const zodUpdateRoleSchema = ZodUpdateRoleSchema(false);
16
+ const typeboxUpdateRoleSchema = TypeboxUpdateRoleSchema(false);
17
+ const zodRoleSchema = ZodRoleSchema(false)(zodPermissionSchema);
18
+ const typeboxRoleSchema = TypeboxRoleSchema(false)(typeboxPermissionSchema);
19
+ const zodUpdateUserSchema = ZodUpdateUserSchema(false);
20
+ const typeboxUpdateUserSchema = TypeboxUpdateUserSchema(false);
21
+ const zodUserSchema = ZodUserSchema(false)(zodRoleSchema);
22
+ const typeboxUserSchema = TypeboxUserSchema(false)(typeboxRoleSchema);
23
+ const zodUpdateOrganizationSchema = ZodUpdateOrganizationSchema(false);
24
+ const typeboxUpdateOrganizationSchema = TypeboxUpdateOrganizationSchema(false);
25
+ const zodOrganizationSchema = ZodOrganizationSchema(false)(zodUserSchema, DummyEnum);
26
+ const typeboxOrganizationSchema = TypeboxOrganizationSchema(false)(typeboxUserSchema, DummyEnum);
27
+ describe('schema equality', () => {
28
+ it('should be equal for permission', () => {
29
+ expect(isTrue(testSchemaEquality(ZodCreatePermissionSchema, TypeboxCreatePermissionSchema, {
30
+ slug: 'test',
31
+ addToRolesIds: ['test'],
32
+ extraFields: {
33
+ test: 'test'
34
+ }
35
+ }))).toBeTruthy();
36
+ expect(isTrue(testSchemaEquality(zodUpdatePermissionSchema, typeboxUpdatePermissionSchema, {
37
+ id: 'test',
38
+ slug: 'test',
39
+ addToRolesIds: ['test'],
40
+ removeFromRolesIds: ['test'],
41
+ extraFields: {
42
+ test: 'test'
43
+ }
44
+ }))).toBeTruthy();
45
+ expect(isTrue(testSchemaEquality(zodPermissionSchema, typeboxPermissionSchema, {
46
+ id: 'test',
47
+ slug: 'test',
48
+ extraFields: {
49
+ test: 'test'
50
+ }
51
+ }))).toBeTruthy();
52
+ });
53
+ it('should be equal for role', () => {
54
+ expect(isTrue(testSchemaEquality(ZodCreateRoleSchema, TypeboxCreateRoleSchema, {
55
+ name: 'test',
56
+ permissionIds: ['test'],
57
+ extraFields: {
58
+ test: 'test'
59
+ }
60
+ }))).toBeTruthy();
61
+ expect(isTrue(testSchemaEquality(zodUpdateRoleSchema, typeboxUpdateRoleSchema, {
62
+ id: 'test',
63
+ name: 'test',
64
+ permissionIds: ['test'],
65
+ extraFields: {
66
+ test: 'test'
67
+ }
68
+ }))).toBeTruthy();
69
+ expect(isTrue(testSchemaEquality(zodRoleSchema, typeboxRoleSchema, {
70
+ id: 'test',
71
+ name: 'test',
72
+ permissions: [
73
+ {
74
+ id: 'test',
75
+ slug: 'test',
76
+ extraFields: { test: 'test' }
77
+ }
78
+ ],
79
+ extraFields: {
80
+ test: 'test'
81
+ }
82
+ }))).toBeTruthy();
83
+ });
84
+ it('should be equal for user', () => {
85
+ expect(isTrue(testSchemaEquality(ZodCreateUserSchema, TypeboxCreateUserSchema, {
86
+ email: 'test@test.com',
87
+ password: 'test',
88
+ firstName: 'test',
89
+ lastName: 'test',
90
+ organizationId: 'test',
91
+ roleIds: ['test'],
92
+ phoneNumber: 'test',
93
+ subscription: 'test',
94
+ extraFields: {
95
+ test: 'test'
96
+ }
97
+ }))).toBeTruthy();
98
+ expect(isTrue(testSchemaEquality(zodUpdateUserSchema, typeboxUpdateUserSchema, {
99
+ id: 'test',
100
+ email: 'test@test.com',
101
+ password: 'test',
102
+ firstName: 'test',
103
+ lastName: 'test',
104
+ roleIds: ['test'],
105
+ phoneNumber: 'test',
106
+ subscription: 'test',
107
+ extraFields: {
108
+ test: 'test'
109
+ }
110
+ }))).toBeTruthy();
111
+ expect(isTrue(testSchemaEquality(zodUserSchema, typeboxUserSchema, {
112
+ id: 'test',
113
+ email: 'test@test.com',
114
+ firstName: 'test',
115
+ lastName: 'test',
116
+ roles: [
117
+ {
118
+ id: 'test',
119
+ name: 'test',
120
+ permissions: [{ id: 'test', slug: 'test' }],
121
+ extraFields: {
122
+ test: 'test'
123
+ }
124
+ }
125
+ ],
126
+ phoneNumber: 'test',
127
+ subscription: 'test',
128
+ extraFields: {
129
+ test: 'test'
130
+ }
131
+ }))).toBeTruthy();
132
+ });
133
+ it('should be equal for organization', () => {
134
+ expect(isTrue(testSchemaEquality(ZodCreateOrganizationSchema, TypeboxCreateOrganizationSchema, {
135
+ name: 'test',
136
+ domain: 'test',
137
+ subscription: 'test',
138
+ logoUrl: 'test',
139
+ extraFields: { test: 'test' }
140
+ }))).toBeTruthy();
141
+ expect(isTrue(testSchemaEquality(zodUpdateOrganizationSchema, typeboxUpdateOrganizationSchema, {
142
+ id: 'test',
143
+ name: 'test',
144
+ domain: 'test',
145
+ subscription: 'test',
146
+ logoUrl: 'test',
147
+ extraFields: { test: 'test' }
148
+ }))).toBeTruthy();
149
+ expect(isTrue(testSchemaEquality(zodOrganizationSchema, typeboxOrganizationSchema, {
150
+ id: 'test',
151
+ name: 'test',
152
+ domain: 'test',
153
+ subscription: 'test',
154
+ logoUrl: 'test',
155
+ extraFields: { test: 'test' },
156
+ status: DummyEnum.A,
157
+ users: [
158
+ {
159
+ id: 'test',
160
+ email: 'test@test.com',
161
+ firstName: 'test',
162
+ lastName: 'test',
163
+ roles: [
164
+ {
165
+ id: 'test',
166
+ name: 'test',
167
+ permissions: [{ id: 'test', slug: 'test' }],
168
+ extraFields: { test: 'test' }
169
+ }
170
+ ]
171
+ }
172
+ ]
173
+ }))).toBeTruthy();
174
+ });
175
+ });
@@ -5,10 +5,10 @@ import {
5
5
  enum_,
6
6
  optional,
7
7
  string,
8
- TypeboxSchemaValidator,
9
8
  unknown,
10
- uuid
11
- } from '@forklaunch/validator/typebox';
9
+ uuid,
10
+ ZodSchemaValidator
11
+ } from '@forklaunch/validator/zod';
12
12
 
13
13
  export const CreateOrganizationSchema = {
14
14
  name: string,
@@ -30,7 +30,7 @@ export const UpdateOrganizationSchema = (uuidId: boolean) => ({
30
30
  export const OrganizationSchema =
31
31
  (uuidId: boolean) =>
32
32
  <
33
- UserDtoSchema extends IdiomaticSchema<TypeboxSchemaValidator>,
33
+ UserDtoSchema extends IdiomaticSchema<ZodSchemaValidator>,
34
34
  OrganizationStatus extends Record<string, LiteralSchema>
35
35
  >(
36
36
  UserDtoSchema: UserDtoSchema,
@@ -5,7 +5,7 @@ import {
5
5
  string,
6
6
  unknown,
7
7
  uuid
8
- } from '@forklaunch/validator/typebox';
8
+ } from '@forklaunch/validator/zod';
9
9
 
10
10
  export const CreatePermissionSchema = {
11
11
  slug: string,
@@ -4,10 +4,10 @@ import {
4
4
  date,
5
5
  optional,
6
6
  string,
7
- TypeboxSchemaValidator,
8
7
  unknown,
9
- uuid
10
- } from '@forklaunch/validator/typebox';
8
+ uuid,
9
+ ZodSchemaValidator
10
+ } from '@forklaunch/validator/zod';
11
11
 
12
12
  export const CreateRoleSchema = {
13
13
  name: string,
@@ -24,7 +24,7 @@ export const UpdateRoleSchema = (uuidId: boolean) => ({
24
24
 
25
25
  export const RoleSchema =
26
26
  (uuidId: boolean) =>
27
- <PermissionSchema extends IdiomaticSchema<TypeboxSchemaValidator>>(
27
+ <PermissionSchema extends IdiomaticSchema<ZodSchemaValidator>>(
28
28
  PermissionSchema: PermissionSchema
29
29
  ) => ({
30
30
  id: uuidId ? uuid : string,
@@ -5,10 +5,10 @@ import {
5
5
  email,
6
6
  optional,
7
7
  string,
8
- TypeboxSchemaValidator,
9
8
  unknown,
10
- uuid
11
- } from '@forklaunch/validator/typebox';
9
+ uuid,
10
+ ZodSchemaValidator
11
+ } from '@forklaunch/validator/zod';
12
12
 
13
13
  export const CreateUserSchema = {
14
14
  email: email,
@@ -36,7 +36,7 @@ export const UpdateUserSchema = (uuidId: boolean) => ({
36
36
 
37
37
  export const UserSchema =
38
38
  (uuidId: boolean) =>
39
- <RoleDtoSchema extends IdiomaticSchema<TypeboxSchemaValidator>>(
39
+ <RoleDtoSchema extends IdiomaticSchema<ZodSchemaValidator>>(
40
40
  RoleDtoSchema: RoleDtoSchema
41
41
  ) => ({
42
42
  id: uuidId ? uuid : string,
@@ -0,0 +1,146 @@
1
+ import {
2
+ CreateOrganizationDto,
3
+ OrganizationDto,
4
+ OrganizationService,
5
+ UpdateOrganizationDto
6
+ } from '@forklaunch/interfaces-iam';
7
+ import { IdDto, InstanceTypeRecord } from '@forklaunch/common';
8
+ import {
9
+ InternalDtoMapper,
10
+ RequestDtoMapperConstructor,
11
+ ResponseDtoMapperConstructor,
12
+ transformIntoInternalDtoMapper
13
+ } from '@forklaunch/core/dtoMapper';
14
+ import {
15
+ MetricsDefinition,
16
+ OpenTelemetryCollector
17
+ } from '@forklaunch/core/http';
18
+ import { MapNestedDtoArraysToCollections } from '@forklaunch/core/services';
19
+ import { AnySchemaValidator } from '@forklaunch/validator';
20
+ import { EntityManager } from '@mikro-orm/core';
21
+
22
+ export class BaseOrganizationService<
23
+ SchemaValidator extends AnySchemaValidator,
24
+ OrganizationStatus,
25
+ Metrics extends MetricsDefinition = MetricsDefinition,
26
+ Dto extends {
27
+ OrganizationDtoMapper: OrganizationDto<OrganizationStatus>;
28
+ CreateOrganizationDtoMapper: CreateOrganizationDto;
29
+ UpdateOrganizationDtoMapper: UpdateOrganizationDto;
30
+ } = {
31
+ OrganizationDtoMapper: OrganizationDto<OrganizationStatus>;
32
+ CreateOrganizationDtoMapper: CreateOrganizationDto;
33
+ UpdateOrganizationDtoMapper: UpdateOrganizationDto;
34
+ },
35
+ Entities extends {
36
+ OrganizationDtoMapper: MapNestedDtoArraysToCollections<
37
+ OrganizationDto<OrganizationStatus>,
38
+ 'users'
39
+ >;
40
+ CreateOrganizationDtoMapper: MapNestedDtoArraysToCollections<
41
+ OrganizationDto<OrganizationStatus>,
42
+ 'users'
43
+ >;
44
+ UpdateOrganizationDtoMapper: MapNestedDtoArraysToCollections<
45
+ OrganizationDto<OrganizationStatus>,
46
+ 'users'
47
+ >;
48
+ } = {
49
+ OrganizationDtoMapper: MapNestedDtoArraysToCollections<
50
+ OrganizationDto<OrganizationStatus>,
51
+ 'users'
52
+ >;
53
+ CreateOrganizationDtoMapper: MapNestedDtoArraysToCollections<
54
+ OrganizationDto<OrganizationStatus>,
55
+ 'users'
56
+ >;
57
+ UpdateOrganizationDtoMapper: MapNestedDtoArraysToCollections<
58
+ OrganizationDto<OrganizationStatus>,
59
+ 'users'
60
+ >;
61
+ }
62
+ > implements OrganizationService<OrganizationStatus>
63
+ {
64
+ #dtoMappers: InternalDtoMapper<
65
+ InstanceTypeRecord<typeof this.dtoMappers>,
66
+ Entities,
67
+ Dto
68
+ >;
69
+
70
+ constructor(
71
+ public em: EntityManager,
72
+ protected openTelemetryCollector: OpenTelemetryCollector<Metrics>,
73
+ protected schemaValidator: SchemaValidator,
74
+ protected dtoMappers: {
75
+ OrganizationDtoMapper: ResponseDtoMapperConstructor<
76
+ SchemaValidator,
77
+ Dto['OrganizationDtoMapper'],
78
+ Entities['OrganizationDtoMapper']
79
+ >;
80
+ CreateOrganizationDtoMapper: RequestDtoMapperConstructor<
81
+ SchemaValidator,
82
+ Dto['CreateOrganizationDtoMapper'],
83
+ Entities['CreateOrganizationDtoMapper']
84
+ >;
85
+ UpdateOrganizationDtoMapper: RequestDtoMapperConstructor<
86
+ SchemaValidator,
87
+ Dto['UpdateOrganizationDtoMapper'],
88
+ Entities['UpdateOrganizationDtoMapper']
89
+ >;
90
+ }
91
+ ) {
92
+ this.#dtoMappers = transformIntoInternalDtoMapper(
93
+ dtoMappers,
94
+ schemaValidator
95
+ );
96
+ }
97
+
98
+ async createOrganization(
99
+ organizationDto: Dto['CreateOrganizationDtoMapper'],
100
+ em?: EntityManager
101
+ ): Promise<Dto['OrganizationDtoMapper']> {
102
+ this.openTelemetryCollector.log('info', 'Creating organization');
103
+ const organization =
104
+ this.#dtoMappers.CreateOrganizationDtoMapper.deserializeDtoToEntity(
105
+ organizationDto
106
+ );
107
+ await (em ?? this.em).transactional(async (innerEm) => {
108
+ await innerEm.persist(organization);
109
+ });
110
+
111
+ return this.#dtoMappers.OrganizationDtoMapper.serializeEntityToDto(
112
+ organization
113
+ );
114
+ }
115
+
116
+ async getOrganization(
117
+ idDto: IdDto,
118
+ em?: EntityManager
119
+ ): Promise<Dto['OrganizationDtoMapper']> {
120
+ const organization = await (em ?? this.em).findOneOrFail(
121
+ 'Organization',
122
+ idDto
123
+ );
124
+ return this.#dtoMappers.OrganizationDtoMapper.serializeEntityToDto(
125
+ organization as Entities['OrganizationDtoMapper']
126
+ );
127
+ }
128
+
129
+ async updateOrganization(
130
+ organizationDto: Dto['UpdateOrganizationDtoMapper'],
131
+ em?: EntityManager
132
+ ): Promise<Dto['OrganizationDtoMapper']> {
133
+ const updatedOrganization =
134
+ this.#dtoMappers.UpdateOrganizationDtoMapper.deserializeDtoToEntity(
135
+ organizationDto
136
+ );
137
+ await (em ?? this.em).upsert(updatedOrganization);
138
+ return this.#dtoMappers.OrganizationDtoMapper.serializeEntityToDto(
139
+ updatedOrganization
140
+ );
141
+ }
142
+
143
+ async deleteOrganization(idDto: IdDto, em?: EntityManager): Promise<void> {
144
+ await (em ?? this.em).nativeDelete('Organization', idDto);
145
+ }
146
+ }