dl-commons 0.0.2-testing.21 → 0.0.2-testing.22

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dl-commons",
3
3
  "type": "commonjs",
4
- "version": "0.0.2-testing.21",
4
+ "version": "0.0.2-testing.22",
5
5
  "description": "distance learning commons",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -113,6 +113,8 @@ import { Author, AuthorDocument, AuthorSchema } from './models/schemas/embedded/
113
113
  import { CropAreaDTO } from './models/dtos/commons/crop-area.dto';
114
114
  import { StorageService } from './services/storage.service';
115
115
  import { IDParams } from './models/dtos/commons/id.param';
116
+ import { UserType } from './models/enums/user-type.enum';
117
+ import { IAMService } from './services/iam.service';
116
118
 
117
119
  export {
118
120
  /**
@@ -122,6 +124,7 @@ export {
122
124
  DictionariesService,
123
125
  SMTPService,
124
126
  StorageService,
127
+ IAMService,
125
128
 
126
129
  /**
127
130
  * Rest Wrapper
@@ -162,6 +165,7 @@ export {
162
165
  RecordState,
163
166
  ContactType,
164
167
  UserVerificationLevel,
168
+ UserType,
165
169
  WeekDay,
166
170
  GroupState,
167
171
  S3FileState,
@@ -0,0 +1,7 @@
1
+ export enum UserType {
2
+ Regular = 'regular',
3
+ Teacher = 'teacher',
4
+ Bank = 'bank',
5
+ Admin = 'admin',
6
+ System = 'system',
7
+ }
@@ -1,10 +1,11 @@
1
1
  import { HttpService } from '@nestjs/axios';
2
2
  import { Injectable } from '@nestjs/common';
3
3
  import { ConfigService } from '@nestjs/config';
4
- import { RESTResponseBody, RESTError } from '../models/dtos/wrappers/rest-wrapper.class';
4
+ import { RESTResponseBody, RESTError, RESTResult } from '../models/dtos/wrappers/rest-wrapper.class';
5
5
  import { firstValueFrom } from 'rxjs';
6
- import { IDictionary } from '../models/interfaces/dictionary.interface';
7
- import { Types } from 'mongoose';
6
+ import { UserVerificationLevel } from '../models/enums/user-verifiation-level.enum';
7
+ import { UserType } from '../models/enums/user-type.enum';
8
+ import { IUser } from '../models/interfaces/user.interface';
8
9
 
9
10
  @Injectable()
10
11
  export class IAMService {
@@ -19,87 +20,60 @@ export class IAMService {
19
20
  //#endregion
20
21
 
21
22
  //#region Public API
22
- public async fetchById(id: string) {
23
+ public async list(firstName: string, lastName: string, types: UserType[], verificationLevels: UserVerificationLevel[]): Promise<RESTResult<IUser[]>> {
23
24
  try {
24
- const endpoint = `${this._baseUrl}/v1/dictionaries/by-id/${id}`;
25
- const response$ = this._httpService.get<RESTResponseBody<IDictionary>>(endpoint);
26
- const response = await firstValueFrom(response$);
27
-
28
- if (response.data.errors) {
29
- throw new RESTError('dictionaries_api_error', response.data.errors);
25
+ const queryParams = new URLSearchParams();
26
+ if (firstName) {
27
+ queryParams.append('firstName', firstName);
30
28
  }
31
29
 
32
- if (!response.data || !response.data.result) {
33
- throw new RESTError('dictionaries_api_error', 'empty response');
30
+ if (lastName) {
31
+ queryParams.append('lastName', lastName);
34
32
  }
35
33
 
36
- if (response.data.result.data) {
37
- const result: IDictionary = {
38
- ...response.data.result.data,
39
- _id: new Types.ObjectId(response.data.result.data._id),
40
- };
34
+ if (types && types.length) {
35
+ types.forEach((type) => queryParams.append('types', type));
36
+ }
41
37
 
42
- return result;
43
- } else {
44
- return null;
38
+ if (verificationLevels && verificationLevels.length) {
39
+ verificationLevels.forEach((level) => queryParams.append('verificationLevels', level));
45
40
  }
46
- } catch (e) {
47
- throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
48
- }
49
- }
50
41
 
51
- public async fetchByKeyword(keyword: string) {
52
- try {
53
- const endpoint = `${this._baseUrl}/v1/dictionaries/by-keyword/${keyword}`;
54
- const response$ = this._httpService.get<RESTResponseBody<IDictionary>>(endpoint);
42
+ const qs = queryParams.toString();
43
+
44
+ const endpoint = `${this._baseUrl}/v1/users?${qs}`;
45
+ const response$ = this._httpService.get<RESTResponseBody<IUser[]>>(endpoint);
55
46
  const response = await firstValueFrom(response$);
56
47
 
57
48
  if (response.data.errors) {
58
- throw new RESTError('dictionaries_api_error', response.data.errors);
49
+ throw new RESTError('iam_api_error', response.data.errors);
59
50
  }
51
+
60
52
  if (!response.data || !response.data.result) {
61
- throw new RESTError('dictionaries_api_error', 'empty response');
53
+ throw new RESTError('iam_api_error', 'empty response');
62
54
  }
63
55
 
64
- if (response.data.result.data) {
65
- const result: IDictionary = {
66
- ...response.data.result.data,
67
- _id: new Types.ObjectId(response.data.result.data._id),
68
- };
69
-
70
- return result;
71
- } else {
72
- return null;
73
- }
56
+ return response.data.result;
74
57
  } catch (e) {
75
58
  throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
76
59
  }
77
60
  }
78
61
 
79
- public async validateIds(ids: string[]) {
62
+ public async read(id: string) : Promise<RESTResult<IUser>> {
80
63
  try {
81
- const endpoint = `${this._baseUrl}/v1/dictionaries/validate/by-ids`;
82
- const requestBody = { data: { ids } };
83
- const response$ = this._httpService.post<RESTResponseBody<IDictionary[]>>(endpoint, requestBody);
64
+ const endpoint = `${this._baseUrl}/v1/users/${id}`;
65
+ const response$ = this._httpService.get<RESTResponseBody<IUser>>(endpoint);
84
66
  const response = await firstValueFrom(response$);
85
67
 
86
68
  if (response.data.errors) {
87
- throw new RESTError('dictionaries_api_error', response.data.errors);
69
+ throw new RESTError('iam_api_error', response.data.errors);
88
70
  }
89
71
 
90
72
  if (!response.data || !response.data.result) {
91
- throw new RESTError('dictionaries_api_error', 'empty response');
92
- }
93
-
94
- const result: IDictionary[] = [];
95
- for (let item of response.data.result.data) {
96
- result.push({
97
- ...item,
98
- _id: new Types.ObjectId(item._id),
99
- });
73
+ throw new RESTError('iam_api_error', 'empty response');
100
74
  }
101
75
 
102
- return result;
76
+ return response.data.result;
103
77
  } catch (e) {
104
78
  throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
105
79
  }