gemcap-be-common 1.2.134 → 1.2.136

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.
@@ -4,17 +4,15 @@ import express from 'express';
4
4
  import axios from 'axios';
5
5
  import dayjs from 'dayjs';
6
6
  import mongoose from 'mongoose';
7
+ import qs from 'qs';
7
8
 
8
9
  import { IUser, IUserDocument, UserModel } from '../models/User.model';
10
+ import { BorrowerCompliance } from '../models/BorrowerCompliance.model';
11
+ import { UserMobileAccess } from '../models/UserMobileAccess.model';
12
+ import { ELogActionType, ELogType, UserLog } from '../models/UserLog.model';
9
13
  import { IGroupedKeycloakRoles, IKeycloakRole } from '../interfaces/keycloak-role.interface';
10
14
  import { IKeycloakUser } from '../interfaces/keycloak-user.interface';
11
15
  import { createLog, ICreateLogParams } from '../db/user-logs.db';
12
- import { ELogActionType, ELogType, UserLog } from '../models/UserLog.model';
13
-
14
- import { BorrowerCompliance } from '../models/BorrowerCompliance.model';
15
- import { UserMobileAccess } from '../models/UserMobileAccess.model';
16
-
17
- const request = require('request');
18
16
 
19
17
  interface IKeycloakConfig {
20
18
  keycloakHost: string;
@@ -61,8 +59,7 @@ export class UsersService {
61
59
  },
62
60
  };
63
61
  const result = await axios.request(options);
64
- const user: IKeycloakUser = result.data;
65
- return user;
62
+ return result.data;
66
63
  }
67
64
 
68
65
  async getUserRepresentationByUsername(authorization: string, username: string): Promise<IKeycloakUser> {
@@ -79,53 +76,35 @@ export class UsersService {
79
76
  return users.find((user) => user.username === username);
80
77
  }
81
78
 
82
- getKeyCloakAdminBearer(): Promise<any> {
83
- return new Promise(function(resolve, reject) {
84
-
85
- const options = {
86
- method: 'POST',
87
- url: `${this.config.keycloakHost}/realms/master/protocol/openid-connect/token`,
88
- headers: {
89
- 'Content-Type': 'application/x-www-form-urlencoded',
90
- },
91
- form: {
92
- username: this.config.adminUser,
93
- password: this.config.adminPass,
94
- client_id: 'admin-cli',
95
- grant_type: 'password',
96
- },
97
- };
79
+ async getKeyCloakAdminBearer() {
80
+ const url = `${this.config.keycloakHost}/realms/master/protocol/openid-connect/token`;
98
81
 
99
- request(options, (error, res, body) => {
100
- if (!error && res.statusCode === 200) {
101
- resolve(JSON.parse(body));
102
- } else {
103
- reject(error);
104
- }
105
- });
82
+ const data = qs.stringify({
83
+ username: this.config.adminUser,
84
+ password: this.config.adminPass,
85
+ client_id: 'admin-cli',
86
+ grant_type: 'password',
106
87
  });
107
- }
108
88
 
109
- getUserList(authorization): Promise<IKeycloakUser[]> {
110
- return new Promise((resolve, reject) => {
89
+ const headers = {
90
+ 'Content-Type': 'application/x-www-form-urlencoded',
91
+ };
111
92
 
112
- const options = {
113
- method: 'GET',
114
- url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users`,
115
- headers: {
116
- 'Content-Type': 'application/json',
117
- Authorization: `Bearer ${authorization}`,
118
- },
119
- };
93
+ const response = await axios.post(url, data, { headers });
94
+ return response.data;
95
+ }
120
96
 
121
- request(options, (error, _res, body) => {
122
- if (!error) {
123
- resolve(JSON.parse(body));
124
- } else {
125
- reject(error);
126
- }
127
- });
128
- });
97
+ async getUserList(authorization: string): Promise<IKeycloakUser[]> {
98
+ const options = {
99
+ method: 'GET',
100
+ url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users`,
101
+ headers: {
102
+ 'Content-Type': 'application/json',
103
+ Authorization: `Bearer ${authorization}`,
104
+ },
105
+ };
106
+ const result = await axios.request(options);
107
+ return result.data;
129
108
  }
130
109
 
131
110
  async getUsersWithRoles(authorization: string) {
@@ -144,77 +123,47 @@ export class UsersService {
144
123
  return usersAndAccess;
145
124
  }
146
125
 
147
- getUserRoles(authorization: string, userId: string): Promise<{ realmMappings: IKeycloakRole[] }> {
148
- return new Promise((resolve, reject) => {
149
-
150
- const options = {
151
- method: 'GET',
152
- url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/role-mappings`,
153
- headers: {
154
- 'Content-Type': 'application/json',
155
- Authorization: `Bearer ${authorization}`,
156
- },
157
- };
158
-
159
- request(options, (error, _res, body) => {
160
- if (!error) {
161
- resolve(JSON.parse(body));
162
- } else {
163
- reject(error);
164
- }
165
- });
166
- });
126
+ async getUserRoles(authorization: string, userId: string): Promise<{ realmMappings: IKeycloakRole[] }> {
127
+ const options = {
128
+ method: 'GET',
129
+ url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/role-mappings`,
130
+ headers: {
131
+ 'Content-Type': 'application/json',
132
+ Authorization: `Bearer ${authorization}`,
133
+ },
134
+ };
135
+ const result = await axios.request(options);
136
+ return result.data;
167
137
  }
168
138
 
169
- createUser(authorization, user): Promise<any> {
170
- return new Promise((resolve, reject) => {
171
- const options = {
172
- method: 'POST',
173
- url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users`,
174
- headers: {
175
- 'Content-Type': 'application/json',
176
- Authorization: `Bearer ${authorization}`,
177
- },
178
- body: {
179
- ...user,
180
- },
181
- json: true,
182
- };
183
-
184
- request(options, (error, res, body) => {
185
- if (!error) {
186
- resolve(body);
187
- } else {
188
- console.error({ error });
189
- reject(error);
190
- }
191
- });
192
- });
139
+ async createUser(authorization: string, user): Promise<any> {
140
+ const options = {
141
+ method: 'POST',
142
+ url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users`,
143
+ headers: {
144
+ 'Content-Type': 'application/json',
145
+ Authorization: `Bearer ${authorization}`,
146
+ },
147
+ data: {
148
+ ...user,
149
+ },
150
+ };
151
+ const result = await axios.request(options);
152
+ return result.data;
193
153
  }
194
154
 
195
- updateUser(authorization: string, userId: string, keyValue): Promise<any> {
196
- return new Promise((resolve, reject) => {
197
-
198
- const options = {
199
- method: 'PUT',
200
- url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}`,
201
- headers: {
202
- 'Content-Type': 'application/json',
203
- Authorization: `Bearer ${authorization}`,
204
- },
205
- body: keyValue,
206
- json: true,
207
- };
208
-
209
- request(options, (error, res, body) => {
210
- if (!error) {
211
- resolve(body);
212
- } else {
213
- console.error({ error });
214
- reject(error);
215
- }
216
- });
217
- });
155
+ async updateUser(authorization: string, userId: string, keyValue): Promise<any> {
156
+ const options = {
157
+ method: 'PUT',
158
+ url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}`,
159
+ headers: {
160
+ 'Content-Type': 'application/json',
161
+ Authorization: `Bearer ${authorization}`,
162
+ },
163
+ data: keyValue,
164
+ };
165
+ const result = await axios.request(options);
166
+ return result.data;
218
167
  }
219
168
 
220
169
  async updateMobileUser(userId: string, keyValue: { [key: string]: string | number | boolean }) {
@@ -226,78 +175,46 @@ export class UsersService {
226
175
  .findOneAndUpdate({ keycloakUserId }, userAccess);
227
176
  }
228
177
 
229
- removeUserRoles(authorization: string, userId: string, roles: IKeycloakRole[]): Promise<any> {
230
- return new Promise((resolve, reject) => {
231
-
232
- const options = {
233
- method: 'DELETE',
234
- url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/role-mappings/realm`,
235
- headers: {
236
- 'Content-Type': 'application/json',
237
- Authorization: `Bearer ${authorization}`,
238
- },
239
- body: [...roles],
240
- json: true,
241
- };
242
-
243
- request(options, (error, res, body) => {
244
- if (!error) {
245
- resolve(body);
246
- } else {
247
- console.error({ error });
248
- reject(error);
249
- }
250
- });
251
- });
178
+ async removeUserRoles(authorization: string, userId: string, roles: IKeycloakRole[]): Promise<any> {
179
+ const options = {
180
+ method: 'DELETE',
181
+ url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/role-mappings/realm`,
182
+ headers: {
183
+ 'Content-Type': 'application/json',
184
+ Authorization: `Bearer ${authorization}`,
185
+ },
186
+ data: [...roles],
187
+ };
188
+ const result = await axios.request(options);
189
+ return result.data;
252
190
  }
253
191
 
254
- addUserRoles(authorization: string, userId: string, roles: IKeycloakRole[]): Promise<any> {
255
- return new Promise((resolve, reject) => {
256
-
257
- const options = {
258
- method: 'POST',
259
- url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/role-mappings/realm`,
260
- headers: {
261
- 'Content-Type': 'application/json',
262
- Authorization: `Bearer ${authorization}`,
263
- },
264
- body: [...roles],
265
- json: true,
266
- };
267
-
268
- request(options, (error, res, body) => {
269
- if (!error) {
270
- resolve(body);
271
- } else {
272
- console.error({ error });
273
- reject(error);
274
- }
275
- });
276
- });
192
+ async addUserRoles(authorization: string, userId: string, roles: IKeycloakRole[]): Promise<any> {
193
+ const options = {
194
+ method: 'POST',
195
+ url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/role-mappings/realm`,
196
+ headers: {
197
+ 'Content-Type': 'application/json',
198
+ Authorization: `Bearer ${authorization}`,
199
+ },
200
+ data: [...roles],
201
+ };
202
+ const result = await axios.request(options);
203
+ return result.data;
277
204
  }
278
205
 
279
- resetPassword(authorization: string, userId: string, temporaryPassword: string): Promise<any> {
280
- return new Promise((resolve, reject) => {
281
-
282
- const options = {
283
- method: 'PUT',
284
- url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/reset-password`,
285
- headers: {
286
- 'Content-Type': 'application/json',
287
- Authorization: `Bearer ${authorization}`,
288
- },
289
- body: { value: temporaryPassword, temporary: true },
290
- json: true,
291
- };
292
- request(options, (error, res, body) => {
293
- if (!error) {
294
- resolve(body);
295
- } else {
296
- console.error({ error });
297
- reject(error);
298
- }
299
- });
300
- });
206
+ async resetPassword(authorization: string, userId: string, temporaryPassword: string): Promise<any> {
207
+ const options = {
208
+ method: 'PUT',
209
+ url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/reset-password`,
210
+ headers: {
211
+ 'Content-Type': 'application/json',
212
+ Authorization: `Bearer ${authorization}`,
213
+ },
214
+ data: { value: temporaryPassword, temporary: true },
215
+ };
216
+ const result = await axios.request(options);
217
+ return result.data;
301
218
  }
302
219
 
303
220
  async createNewPasswordWithEmail(authorization: string, userId: string, newPassword: string) {
@@ -339,26 +256,17 @@ export class UsersService {
339
256
  }
340
257
  }
341
258
 
342
- getRoleList(authorization): Promise<IKeycloakRole[]> {
343
- return new Promise((resolve, reject) => {
344
-
345
- const options = {
346
- method: 'GET',
347
- url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/roles`,
348
- headers: {
349
- 'Content-Type': 'application/json',
350
- Authorization: `Bearer ${authorization}`,
351
- },
352
- };
353
- request(options, (error, _res, body) => {
354
- if (!error) {
355
- resolve(JSON.parse(body));
356
- } else {
357
- console.error({ error });
358
- reject(error);
359
- }
360
- });
361
- });
259
+ async getRoleList(authorization: string): Promise<IKeycloakRole[]> {
260
+ const options = {
261
+ method: 'GET',
262
+ url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/roles`,
263
+ headers: {
264
+ 'Content-Type': 'application/json',
265
+ Authorization: `Bearer ${authorization}`,
266
+ },
267
+ };
268
+ const result = await axios.request(options);
269
+ return result.data;
362
270
  }
363
271
 
364
272
  groupRoles(roles: IKeycloakRole[]): IGroupedKeycloakRoles {
@@ -524,6 +432,6 @@ export class UsersService {
524
432
  lastName: keycloakUser.lastName,
525
433
  });
526
434
  }
527
- }))
435
+ }));
528
436
  }
529
437
  }