@drax/identity-back 0.1.0 → 0.1.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.
- package/dist/services/TenantService.js +42 -11
- package/dist/services/UserApiKeyService.js +36 -11
- package/dist/services/UserService.js +34 -8
- package/package.json +2 -2
- package/src/services/TenantService.ts +49 -17
- package/src/services/UserApiKeyService.ts +42 -17
- package/src/services/UserService.ts +74 -48
- package/tsconfig.tsbuildinfo +1 -1
- package/types/config/IdentityConfig.d.ts +12 -0
- package/types/config/IdentityConfig.d.ts.map +1 -0
- package/types/errors/BadCredentialsError.d.ts +6 -0
- package/types/errors/BadCredentialsError.d.ts.map +1 -0
- package/types/errors/UnauthorizedError.d.ts +6 -0
- package/types/errors/UnauthorizedError.d.ts.map +1 -0
- package/types/factory/RoleServiceFactory.d.ts +4 -0
- package/types/factory/TenantServiceFactory.d.ts +4 -0
- package/types/factory/UserApiKeyServiceFactory.d.ts +4 -0
- package/types/factory/UserServiceFactory.d.ts +4 -0
- package/types/graphql/index.d.ts +6 -0
- package/types/graphql/resolvers/role.resolvers.d.ts +52 -0
- package/types/graphql/resolvers/tenant.resolvers.d.ts +49 -0
- package/types/graphql/resolvers/user-api-key.resolvers.d.ts +37 -0
- package/types/graphql/resolvers/user.resolvers.d.ts +67 -0
- package/types/index.d.ts +35 -0
- package/types/interfaces/IRoleRepository.d.ts +9 -0
- package/types/interfaces/ITenantRepository.d.ts +9 -0
- package/types/interfaces/IUserApiKeyRepository.d.ts +7 -0
- package/types/interfaces/IUserRepository.d.ts +10 -0
- package/types/middleware/apiKeyMiddleware.d.ts +4 -0
- package/types/middleware/jwtMiddleware.d.ts +4 -0
- package/types/middleware/rbacMiddleware.d.ts +4 -0
- package/types/models/RoleModel.d.ts +16 -0
- package/types/models/TenantModel.d.ts +16 -0
- package/types/models/UserApiKeyModel.d.ts +16 -0
- package/types/models/UserGroupModel.d.ts +16 -0
- package/types/models/UserModel.d.ts +16 -0
- package/types/permissions/IdentityPermissions.d.ts +27 -0
- package/types/permissions/IdentityPermissions.d.ts.map +1 -0
- package/types/repository/mongo/RoleMongoRepository.d.ts +14 -0
- package/types/repository/mongo/TenantMongoRepository.d.ts +14 -0
- package/types/repository/mongo/UserApiKeyMongoRepository.d.ts +14 -0
- package/types/repository/mongo/UserMongoRepository.d.ts +17 -0
- package/types/repository/sqlite/RoleSqliteRepository.d.ts +22 -0
- package/types/repository/sqlite/TenantSqliteRepository.d.ts +19 -0
- package/types/repository/sqlite/UserSqliteRepository.d.ts +25 -0
- package/types/routes/RoleRoutes.d.ts +4 -0
- package/types/routes/TenantRoutes.d.ts +4 -0
- package/types/routes/UserApiKeyRoutes.d.ts +4 -0
- package/types/routes/UserAvatarRoutes.d.ts +4 -0
- package/types/routes/UserRoutes.d.ts +4 -0
- package/types/services/PermissionService.d.ts +9 -0
- package/types/services/PermissionService.d.ts.map +1 -0
- package/types/services/RoleService.d.ts +16 -0
- package/types/services/TenantService.d.ts +16 -0
- package/types/services/TenantService.d.ts.map +1 -1
- package/types/services/UserApiKeyService.d.ts +15 -0
- package/types/services/UserApiKeyService.d.ts.map +1 -1
- package/types/services/UserService.d.ts +21 -0
- package/types/services/UserService.d.ts.map +1 -1
- package/types/setup/CreateOrUpdateRole.d.ts +5 -0
- package/types/setup/CreateUserIfNotExist.d.ts +5 -0
- package/types/setup/LoadIdentityConfigFromEnv.d.ts +4 -0
- package/types/setup/LoadPermissions.d.ts +4 -0
- package/types/setup/LoadPermissions.d.ts.map +1 -0
- package/types/setup/RecoveryUserPassword.d.ts +4 -0
- package/types/utils/AuthUtils.d.ts +18 -0
- package/types/zod/RoleZod.d.ts +10 -0
- package/types/zod/RoleZod.d.ts.map +1 -0
- package/types/zod/TenantZod.d.ts +10 -0
- package/types/zod/TenantZod.d.ts.map +1 -0
- package/types/zod/UserZod.d.ts +53 -0
- package/types/zod/UserZod.d.ts.map +1 -0
|
@@ -14,6 +14,7 @@ class TenantService {
|
|
|
14
14
|
return tenant;
|
|
15
15
|
}
|
|
16
16
|
catch (e) {
|
|
17
|
+
console.error("Error creating tenant", e);
|
|
17
18
|
if (e instanceof ZodError) {
|
|
18
19
|
throw ZodErrorToValidationError(e, tenantData);
|
|
19
20
|
}
|
|
@@ -28,6 +29,7 @@ class TenantService {
|
|
|
28
29
|
return tenant;
|
|
29
30
|
}
|
|
30
31
|
catch (e) {
|
|
32
|
+
console.error("Error updating tenant", e);
|
|
31
33
|
if (e instanceof ZodError) {
|
|
32
34
|
throw ZodErrorToValidationError(e, tenantData);
|
|
33
35
|
}
|
|
@@ -35,25 +37,54 @@ class TenantService {
|
|
|
35
37
|
}
|
|
36
38
|
}
|
|
37
39
|
async delete(id) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
try {
|
|
41
|
+
const deletedTenant = await this._repository.delete(id);
|
|
42
|
+
return deletedTenant;
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
console.error("Error deleting tenant", e);
|
|
46
|
+
throw e;
|
|
47
|
+
}
|
|
41
48
|
}
|
|
42
49
|
async findById(id) {
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
try {
|
|
51
|
+
const tenant = await this._repository.findById(id);
|
|
52
|
+
return tenant;
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
console.error("Error finding tenant by id", e);
|
|
56
|
+
throw e;
|
|
57
|
+
}
|
|
45
58
|
}
|
|
46
59
|
async findByName(name) {
|
|
47
|
-
|
|
48
|
-
|
|
60
|
+
try {
|
|
61
|
+
const tenant = await this._repository.findByName(name);
|
|
62
|
+
return tenant;
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
console.error("Error finding tenant by name", e);
|
|
66
|
+
throw e;
|
|
67
|
+
}
|
|
49
68
|
}
|
|
50
69
|
async fetchAll() {
|
|
51
|
-
|
|
52
|
-
|
|
70
|
+
try {
|
|
71
|
+
const tenants = await this._repository.fetchAll();
|
|
72
|
+
return tenants;
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
console.error("Error fetching all tenants", e);
|
|
76
|
+
throw e;
|
|
77
|
+
}
|
|
53
78
|
}
|
|
54
79
|
async paginate({ page = 1, limit = 5, orderBy = '', orderDesc = false, search = '', filters = [] }) {
|
|
55
|
-
|
|
56
|
-
|
|
80
|
+
try {
|
|
81
|
+
const pagination = await this._repository.paginate({ page, limit, orderBy, orderDesc, search, filters });
|
|
82
|
+
return pagination;
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
console.error("Error paginating tenants", e);
|
|
86
|
+
throw e;
|
|
87
|
+
}
|
|
57
88
|
}
|
|
58
89
|
}
|
|
59
90
|
export default TenantService;
|
|
@@ -21,6 +21,7 @@ class UserApiKeyService {
|
|
|
21
21
|
return userApiKey;
|
|
22
22
|
}
|
|
23
23
|
catch (e) {
|
|
24
|
+
console.error("Error creating userApiKey", e);
|
|
24
25
|
if (e instanceof ZodError) {
|
|
25
26
|
throw ZodErrorToValidationError(e, userApiKeyData);
|
|
26
27
|
}
|
|
@@ -36,6 +37,7 @@ class UserApiKeyService {
|
|
|
36
37
|
return userApiKey;
|
|
37
38
|
}
|
|
38
39
|
catch (e) {
|
|
40
|
+
console.error("Error updating userApiKey", e);
|
|
39
41
|
if (e instanceof ZodError) {
|
|
40
42
|
throw ZodErrorToValidationError(e, userApiKeyData);
|
|
41
43
|
}
|
|
@@ -43,23 +45,46 @@ class UserApiKeyService {
|
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
47
|
async delete(id) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
try {
|
|
49
|
+
const deletedUserApiKey = await this._repository.delete(id);
|
|
50
|
+
return deletedUserApiKey;
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
console.error("Error deleting userApiKey", e);
|
|
54
|
+
throw e;
|
|
55
|
+
}
|
|
49
56
|
}
|
|
50
57
|
async findById(id) {
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
try {
|
|
59
|
+
const userApiKey = await this._repository.findById(id);
|
|
60
|
+
return userApiKey;
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
console.error("Error finding userApiKey by id", e);
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
53
66
|
}
|
|
54
67
|
async findBySecret(secret) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
68
|
+
try {
|
|
69
|
+
const APIKEY_SECRET = DraxConfig.getOrLoad(IdentityConfig.ApiKeySecret);
|
|
70
|
+
const hashedSecret = AuthUtils.generateHMAC(APIKEY_SECRET, secret);
|
|
71
|
+
const userApiKey = await this._repository.findBySecret(hashedSecret);
|
|
72
|
+
return userApiKey;
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
console.error("Error finding userApiKey by secret", e);
|
|
76
|
+
throw e;
|
|
77
|
+
}
|
|
59
78
|
}
|
|
60
79
|
async paginate({ page = 1, limit = 5, orderBy = '', orderDesc = false, search = '', filters = [] }) {
|
|
61
|
-
|
|
62
|
-
|
|
80
|
+
try {
|
|
81
|
+
const pagination = await this._repository.paginate({ page, limit, orderBy, orderDesc, search, filters });
|
|
82
|
+
return pagination;
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
console.error("Error paginating userApiKeys", e);
|
|
86
|
+
throw e;
|
|
87
|
+
}
|
|
63
88
|
}
|
|
64
89
|
}
|
|
65
90
|
export default UserApiKeyService;
|
|
@@ -74,6 +74,7 @@ class UserService {
|
|
|
74
74
|
return user;
|
|
75
75
|
}
|
|
76
76
|
catch (e) {
|
|
77
|
+
console.error("Error creating user", e);
|
|
77
78
|
if (e instanceof ZodError) {
|
|
78
79
|
throw ZodErrorToValidationError(e, userData);
|
|
79
80
|
}
|
|
@@ -91,6 +92,7 @@ class UserService {
|
|
|
91
92
|
return user;
|
|
92
93
|
}
|
|
93
94
|
catch (e) {
|
|
95
|
+
console.error("Error updating user", e);
|
|
94
96
|
if (e instanceof ZodError) {
|
|
95
97
|
throw ZodErrorToValidationError(e, userData);
|
|
96
98
|
}
|
|
@@ -98,20 +100,44 @@ class UserService {
|
|
|
98
100
|
}
|
|
99
101
|
}
|
|
100
102
|
async delete(id) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
+
try {
|
|
104
|
+
const deletedRole = await this._repository.delete(id);
|
|
105
|
+
return deletedRole;
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
console.error("Error deleting user", e);
|
|
109
|
+
throw e;
|
|
110
|
+
}
|
|
103
111
|
}
|
|
104
112
|
async findById(id) {
|
|
105
|
-
|
|
106
|
-
|
|
113
|
+
try {
|
|
114
|
+
const user = await this._repository.findById(id);
|
|
115
|
+
return user;
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
console.error("Error finding user by id", e);
|
|
119
|
+
throw e;
|
|
120
|
+
}
|
|
107
121
|
}
|
|
108
122
|
async findByUsername(username) {
|
|
109
|
-
|
|
110
|
-
|
|
123
|
+
try {
|
|
124
|
+
const user = await this._repository.findByUsername(username);
|
|
125
|
+
return user;
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
console.error("Error finding user by username", e);
|
|
129
|
+
throw e;
|
|
130
|
+
}
|
|
111
131
|
}
|
|
112
132
|
async paginate({ page = 1, limit = 5, orderBy = '', orderDesc = false, search = '', filters = [] }) {
|
|
113
|
-
|
|
114
|
-
|
|
133
|
+
try {
|
|
134
|
+
const pagination = await this._repository.paginate({ page, limit, orderBy, orderDesc, search, filters });
|
|
135
|
+
return pagination;
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
console.error("Error paginating users", e);
|
|
139
|
+
throw e;
|
|
140
|
+
}
|
|
115
141
|
}
|
|
116
142
|
}
|
|
117
143
|
export default UserService;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.1.
|
|
6
|
+
"version": "0.1.2",
|
|
7
7
|
"description": "Identity module for user management, authentication and authorization.",
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"types": "types/index.d.ts",
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"debug": "0"
|
|
61
61
|
}
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "79fef47b0aa97b4c46f057396dcafeb132ed1258"
|
|
64
64
|
}
|
|
@@ -21,6 +21,7 @@ class TenantService {
|
|
|
21
21
|
const tenant = await this._repository.create(tenantData)
|
|
22
22
|
return tenant
|
|
23
23
|
} catch (e) {
|
|
24
|
+
console.error("Error creating tenant", e)
|
|
24
25
|
if (e instanceof ZodError) {
|
|
25
26
|
throw ZodErrorToValidationError(e, tenantData)
|
|
26
27
|
}
|
|
@@ -35,6 +36,7 @@ class TenantService {
|
|
|
35
36
|
const tenant = await this._repository.update(id, tenantData)
|
|
36
37
|
return tenant
|
|
37
38
|
} catch (e) {
|
|
39
|
+
console.error("Error updating tenant", e)
|
|
38
40
|
if (e instanceof ZodError) {
|
|
39
41
|
throw ZodErrorToValidationError(e, tenantData)
|
|
40
42
|
}
|
|
@@ -43,35 +45,65 @@ class TenantService {
|
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
async delete(id: string): Promise<boolean> {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
try {
|
|
49
|
+
const deletedTenant = await this._repository.delete(id);
|
|
50
|
+
return deletedTenant;
|
|
51
|
+
} catch (e) {
|
|
52
|
+
console.error("Error deleting tenant", e)
|
|
53
|
+
throw e;
|
|
54
|
+
}
|
|
55
|
+
|
|
49
56
|
}
|
|
50
57
|
|
|
51
58
|
async findById(id: string): Promise<ITenant | null> {
|
|
52
|
-
|
|
53
|
-
|
|
59
|
+
try {
|
|
60
|
+
const tenant: ITenant = await this._repository.findById(id);
|
|
61
|
+
return tenant
|
|
62
|
+
} catch (e) {
|
|
63
|
+
console.error("Error finding tenant by id", e)
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
66
|
+
|
|
54
67
|
}
|
|
55
68
|
|
|
56
69
|
async findByName(name: string): Promise<ITenant | null> {
|
|
57
|
-
|
|
58
|
-
|
|
70
|
+
try {
|
|
71
|
+
const tenant: ITenant = await this._repository.findByName(name);
|
|
72
|
+
return tenant
|
|
73
|
+
} catch (e) {
|
|
74
|
+
console.error("Error finding tenant by name", e)
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
77
|
+
|
|
59
78
|
}
|
|
60
79
|
|
|
61
80
|
async fetchAll(): Promise<ITenant[]> {
|
|
62
|
-
|
|
63
|
-
|
|
81
|
+
try {
|
|
82
|
+
const tenants: ITenant[] = await this._repository.fetchAll();
|
|
83
|
+
return tenants
|
|
84
|
+
} catch (e) {
|
|
85
|
+
console.error("Error fetching all tenants", e)
|
|
86
|
+
throw e;
|
|
87
|
+
}
|
|
88
|
+
|
|
64
89
|
}
|
|
65
90
|
|
|
66
91
|
async paginate({
|
|
67
|
-
page= 1,
|
|
68
|
-
limit= 5,
|
|
69
|
-
orderBy= '',
|
|
70
|
-
orderDesc= false,
|
|
71
|
-
search= '',
|
|
72
|
-
filters= []
|
|
73
|
-
|
|
74
|
-
|
|
92
|
+
page = 1,
|
|
93
|
+
limit = 5,
|
|
94
|
+
orderBy = '',
|
|
95
|
+
orderDesc = false,
|
|
96
|
+
search = '',
|
|
97
|
+
filters = []
|
|
98
|
+
}: IDraxPaginateOptions): Promise<IDraxPaginateResult<ITenant>> {
|
|
99
|
+
try {
|
|
100
|
+
const pagination = await this._repository.paginate({page, limit, orderBy, orderDesc, search, filters});
|
|
101
|
+
return pagination;
|
|
102
|
+
} catch (e) {
|
|
103
|
+
console.error("Error paginating tenants", e)
|
|
104
|
+
throw e;
|
|
105
|
+
}
|
|
106
|
+
|
|
75
107
|
}
|
|
76
108
|
|
|
77
109
|
|
|
@@ -28,6 +28,7 @@ class UserApiKeyService {
|
|
|
28
28
|
userApiKey.secret = secret
|
|
29
29
|
return userApiKey
|
|
30
30
|
} catch (e) {
|
|
31
|
+
console.error("Error creating userApiKey", e)
|
|
31
32
|
if (e instanceof ZodError) {
|
|
32
33
|
throw ZodErrorToValidationError(e, userApiKeyData)
|
|
33
34
|
}
|
|
@@ -43,6 +44,7 @@ class UserApiKeyService {
|
|
|
43
44
|
const userApiKey = await this._repository.update(id, userApiKeyData)
|
|
44
45
|
return userApiKey
|
|
45
46
|
} catch (e) {
|
|
47
|
+
console.error("Error updating userApiKey", e)
|
|
46
48
|
if (e instanceof ZodError) {
|
|
47
49
|
throw ZodErrorToValidationError(e, userApiKeyData)
|
|
48
50
|
}
|
|
@@ -51,33 +53,56 @@ class UserApiKeyService {
|
|
|
51
53
|
}
|
|
52
54
|
|
|
53
55
|
async delete(id: string): Promise<boolean> {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
try {
|
|
57
|
+
const deletedUserApiKey = await this._repository.delete(id);
|
|
58
|
+
return deletedUserApiKey;
|
|
59
|
+
} catch (e) {
|
|
60
|
+
console.error("Error deleting userApiKey", e)
|
|
61
|
+
throw e
|
|
62
|
+
}
|
|
63
|
+
|
|
57
64
|
}
|
|
58
65
|
|
|
59
66
|
async findById(id: string): Promise<IUserApiKey | null> {
|
|
60
|
-
|
|
61
|
-
|
|
67
|
+
try{
|
|
68
|
+
const userApiKey: IUserApiKey = await this._repository.findById(id);
|
|
69
|
+
return userApiKey
|
|
70
|
+
}catch (e){
|
|
71
|
+
console.error("Error finding userApiKey by id", e)
|
|
72
|
+
throw e
|
|
73
|
+
}
|
|
74
|
+
|
|
62
75
|
}
|
|
63
76
|
|
|
64
77
|
async findBySecret(secret: string): Promise<IUserApiKey | null> {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
78
|
+
try{
|
|
79
|
+
const APIKEY_SECRET = DraxConfig.getOrLoad(IdentityConfig.ApiKeySecret)
|
|
80
|
+
const hashedSecret = AuthUtils.generateHMAC(APIKEY_SECRET, secret)
|
|
81
|
+
const userApiKey: IUserApiKey = await this._repository.findBySecret(hashedSecret);
|
|
82
|
+
return userApiKey
|
|
83
|
+
}catch (e){
|
|
84
|
+
console.error("Error finding userApiKey by secret", e)
|
|
85
|
+
throw e
|
|
86
|
+
}
|
|
87
|
+
|
|
69
88
|
}
|
|
70
89
|
|
|
71
90
|
|
|
72
91
|
async paginate({
|
|
73
|
-
page= 1,
|
|
74
|
-
limit= 5,
|
|
75
|
-
orderBy= '',
|
|
76
|
-
orderDesc= false,
|
|
77
|
-
search= '',
|
|
78
|
-
filters= []
|
|
79
|
-
|
|
80
|
-
|
|
92
|
+
page = 1,
|
|
93
|
+
limit = 5,
|
|
94
|
+
orderBy = '',
|
|
95
|
+
orderDesc = false,
|
|
96
|
+
search = '',
|
|
97
|
+
filters = []
|
|
98
|
+
}: IDraxPaginateOptions): Promise<IDraxPaginateResult<IUserApiKey>> {
|
|
99
|
+
try {
|
|
100
|
+
const pagination = await this._repository.paginate({page, limit, orderBy, orderDesc, search, filters});
|
|
101
|
+
return pagination;
|
|
102
|
+
} catch (e) {
|
|
103
|
+
console.error("Error paginating userApiKeys", e)
|
|
104
|
+
throw e
|
|
105
|
+
}
|
|
81
106
|
}
|
|
82
107
|
|
|
83
108
|
|
|
@@ -16,68 +16,68 @@ class UserService {
|
|
|
16
16
|
console.log("UserService constructor")
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
async auth(username
|
|
19
|
+
async auth(username: string, password: string) {
|
|
20
20
|
let user = null
|
|
21
|
-
console.log("auth username",username)
|
|
21
|
+
console.log("auth username", username)
|
|
22
22
|
user = await this.findByUsername(username)
|
|
23
23
|
if (user && user.active && AuthUtils.checkPassword(password, user.password)) {
|
|
24
24
|
//TODO: Generar Sesion
|
|
25
25
|
const session = '123'
|
|
26
26
|
const accessToken = AuthUtils.generateToken(user.id.toString(), user.username, user.role.id, user.tenant?.id, session)
|
|
27
27
|
return {accessToken: accessToken}
|
|
28
|
-
}else{
|
|
28
|
+
} else {
|
|
29
29
|
throw new BadCredentialsError()
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
async changeUserPassword(userId
|
|
33
|
+
async changeUserPassword(userId: string, newPassword: string) {
|
|
34
34
|
const user = await this.findById(userId)
|
|
35
|
-
if(user){
|
|
36
|
-
newPassword =
|
|
35
|
+
if (user) {
|
|
36
|
+
newPassword = AuthUtils.hashPassword(newPassword)
|
|
37
37
|
await this._repository.changePassword(userId, newPassword)
|
|
38
38
|
return true
|
|
39
|
-
}else{
|
|
39
|
+
} else {
|
|
40
40
|
throw new ValidationError([{field: 'userId', reason: 'validation.notFound'}])
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
|
|
45
|
-
async changeOwnPassword(userId
|
|
45
|
+
async changeOwnPassword(userId: string, currentPassword: string, newPassword: string) {
|
|
46
46
|
const user = await this.findById(userId)
|
|
47
47
|
|
|
48
48
|
|
|
49
|
-
if(user && user.active){
|
|
49
|
+
if (user && user.active) {
|
|
50
50
|
|
|
51
|
-
if(currentPassword === newPassword){
|
|
51
|
+
if (currentPassword === newPassword) {
|
|
52
52
|
throw new ValidationError([{field: 'newPassword', reason: 'validation.password.currentDifferent'}])
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
if (AuthUtils.checkPassword(currentPassword, user.password)) {
|
|
56
|
-
newPassword =
|
|
56
|
+
newPassword = AuthUtils.hashPassword(newPassword)
|
|
57
57
|
await this._repository.changePassword(userId, newPassword)
|
|
58
58
|
return true
|
|
59
|
-
}else{
|
|
59
|
+
} else {
|
|
60
60
|
throw new ValidationError([{field: 'currentPassword', reason: 'validation.notMatch'}])
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
}else{
|
|
63
|
+
} else {
|
|
64
64
|
throw new BadCredentialsError()
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
async changeAvatar(userId
|
|
68
|
+
async changeAvatar(userId: string, avatar: string) {
|
|
69
69
|
const user = await this.findById(userId)
|
|
70
|
-
if(user && user.active){
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}else{
|
|
70
|
+
if (user && user.active) {
|
|
71
|
+
await this._repository.changeAvatar(userId, avatar)
|
|
72
|
+
return true
|
|
73
|
+
} else {
|
|
74
74
|
throw new BadCredentialsError()
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
|
|
79
79
|
async create(userData: IUserCreate): Promise<IUser> {
|
|
80
|
-
try{
|
|
80
|
+
try {
|
|
81
81
|
userData.name = userData?.name?.trim()
|
|
82
82
|
userData.username = userData.username.trim()
|
|
83
83
|
userData.password = userData.password.trim()
|
|
@@ -89,9 +89,10 @@ class UserService {
|
|
|
89
89
|
|
|
90
90
|
const user: IUser = await this._repository.create(userData)
|
|
91
91
|
return user
|
|
92
|
-
}catch (e){
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
} catch (e) {
|
|
93
|
+
console.error("Error creating user", e)
|
|
94
|
+
if (e instanceof ZodError) {
|
|
95
|
+
throw ZodErrorToValidationError(e, userData)
|
|
95
96
|
}
|
|
96
97
|
throw e
|
|
97
98
|
}
|
|
@@ -100,50 +101,75 @@ class UserService {
|
|
|
100
101
|
}
|
|
101
102
|
|
|
102
103
|
async update(id: string, userData: IUserUpdate) {
|
|
103
|
-
try{
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
104
|
+
try {
|
|
105
|
+
userData.name = userData.name.trim()
|
|
106
|
+
userData.username = userData.username.trim()
|
|
107
|
+
delete userData.password
|
|
108
|
+
userData.tenant = userData.tenant === "" ? null : userData.tenant
|
|
108
109
|
|
|
109
|
-
|
|
110
|
+
await editUserSchema.parseAsync(userData)
|
|
110
111
|
|
|
111
112
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}catch (e){
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
const user: IUser = await this._repository.update(id, userData)
|
|
114
|
+
return user
|
|
115
|
+
} catch (e) {
|
|
116
|
+
console.error("Error updating user", e)
|
|
117
|
+
if (e instanceof ZodError) {
|
|
118
|
+
throw ZodErrorToValidationError(e, userData)
|
|
117
119
|
}
|
|
118
120
|
throw e
|
|
119
121
|
}
|
|
120
122
|
}
|
|
121
123
|
|
|
122
124
|
async delete(id: string): Promise<boolean> {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
+
try {
|
|
126
|
+
const deletedRole: boolean = await this._repository.delete(id);
|
|
127
|
+
return deletedRole;
|
|
128
|
+
} catch (e) {
|
|
129
|
+
console.error("Error deleting user", e)
|
|
130
|
+
throw e
|
|
131
|
+
}
|
|
132
|
+
|
|
125
133
|
}
|
|
126
134
|
|
|
127
135
|
async findById(id: string): Promise<IUser> {
|
|
128
|
-
|
|
129
|
-
|
|
136
|
+
try {
|
|
137
|
+
const user: IUser = await this._repository.findById(id);
|
|
138
|
+
return user
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.error("Error finding user by id", e)
|
|
141
|
+
throw e
|
|
142
|
+
}
|
|
143
|
+
|
|
130
144
|
}
|
|
131
145
|
|
|
132
146
|
async findByUsername(username: string): Promise<IUser | null> {
|
|
133
|
-
|
|
134
|
-
|
|
147
|
+
try {
|
|
148
|
+
const user: IUser = await this._repository.findByUsername(username);
|
|
149
|
+
return user
|
|
150
|
+
} catch (e) {
|
|
151
|
+
console.error("Error finding user by username", e)
|
|
152
|
+
throw e
|
|
153
|
+
}
|
|
154
|
+
|
|
135
155
|
}
|
|
136
156
|
|
|
137
157
|
async paginate({
|
|
138
|
-
page= 1,
|
|
139
|
-
limit= 5,
|
|
140
|
-
orderBy= '',
|
|
141
|
-
orderDesc= false,
|
|
142
|
-
search= '',
|
|
143
|
-
filters= []
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
158
|
+
page = 1,
|
|
159
|
+
limit = 5,
|
|
160
|
+
orderBy = '',
|
|
161
|
+
orderDesc = false,
|
|
162
|
+
search = '',
|
|
163
|
+
filters = []
|
|
164
|
+
}: IDraxPaginateOptions): Promise<IDraxPaginateResult<IUser>> {
|
|
165
|
+
try {
|
|
166
|
+
const pagination = await this._repository.paginate({page, limit, orderBy, orderDesc, search, filters});
|
|
167
|
+
return pagination;
|
|
168
|
+
} catch (e) {
|
|
169
|
+
console.error("Error paginating users", e)
|
|
170
|
+
throw e;
|
|
171
|
+
}
|
|
172
|
+
|
|
147
173
|
}
|
|
148
174
|
}
|
|
149
175
|
|