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