dl-commons 0.0.2-testing.20 → 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.20",
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
@@ -112,6 +112,9 @@ import { IAuthor } from './models/interfaces/author.interface';
112
112
  import { Author, AuthorDocument, AuthorSchema } from './models/schemas/embedded/author.schema';
113
113
  import { CropAreaDTO } from './models/dtos/commons/crop-area.dto';
114
114
  import { StorageService } from './services/storage.service';
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';
115
118
 
116
119
  export {
117
120
  /**
@@ -121,6 +124,7 @@ export {
121
124
  DictionariesService,
122
125
  SMTPService,
123
126
  StorageService,
127
+ IAMService,
124
128
 
125
129
  /**
126
130
  * Rest Wrapper
@@ -161,6 +165,7 @@ export {
161
165
  RecordState,
162
166
  ContactType,
163
167
  UserVerificationLevel,
168
+ UserType,
164
169
  WeekDay,
165
170
  GroupState,
166
171
  S3FileState,
@@ -172,6 +177,7 @@ export {
172
177
  */
173
178
  I18nDTO,
174
179
  CropAreaDTO,
180
+ IDParams,
175
181
 
176
182
  /**
177
183
  * Interfaces
@@ -0,0 +1,7 @@
1
+ import { IsMongoId, IsNotEmpty } from 'class-validator';
2
+
3
+ export class IDParams {
4
+ @IsNotEmpty()
5
+ @IsMongoId()
6
+ id: string;
7
+ }
@@ -0,0 +1,7 @@
1
+ export enum UserType {
2
+ Regular = 'regular',
3
+ Teacher = 'teacher',
4
+ Bank = 'bank',
5
+ Admin = 'admin',
6
+ System = 'system',
7
+ }
@@ -50,6 +50,5 @@ export interface IFileEmbedded {
50
50
  size: number;
51
51
  mimeType: string;
52
52
  s3: IS3;
53
- state: S3FileState;
54
53
  createdAt: Date;
55
54
  }
@@ -107,9 +107,6 @@ export class FileEmbedded implements IFileEmbedded {
107
107
  @Prop({ type: S3, required: true, default: null })
108
108
  s3: S3;
109
109
 
110
- @Prop({ type: String, enum: S3FileState, required: true })
111
- state: S3FileState;
112
-
113
110
  @Prop({ type: Date, required: true })
114
111
  createdAt: Date;
115
112
  }
@@ -0,0 +1,82 @@
1
+ import { HttpService } from '@nestjs/axios';
2
+ import { Injectable } from '@nestjs/common';
3
+ import { ConfigService } from '@nestjs/config';
4
+ import { RESTResponseBody, RESTError, RESTResult } from '../models/dtos/wrappers/rest-wrapper.class';
5
+ import { firstValueFrom } from 'rxjs';
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';
9
+
10
+ @Injectable()
11
+ export class IAMService {
12
+ //#region Private Members
13
+ private _baseUrl: string;
14
+ //#endregion
15
+
16
+ //#region Constructor
17
+ constructor(private readonly _httpService: HttpService, private readonly _configService: ConfigService) {
18
+ this._baseUrl = this._configService.getOrThrow('DL_IAM__REST__BASE_URL');
19
+ }
20
+ //#endregion
21
+
22
+ //#region Public API
23
+ public async list(firstName: string, lastName: string, types: UserType[], verificationLevels: UserVerificationLevel[]): Promise<RESTResult<IUser[]>> {
24
+ try {
25
+ const queryParams = new URLSearchParams();
26
+ if (firstName) {
27
+ queryParams.append('firstName', firstName);
28
+ }
29
+
30
+ if (lastName) {
31
+ queryParams.append('lastName', lastName);
32
+ }
33
+
34
+ if (types && types.length) {
35
+ types.forEach((type) => queryParams.append('types', type));
36
+ }
37
+
38
+ if (verificationLevels && verificationLevels.length) {
39
+ verificationLevels.forEach((level) => queryParams.append('verificationLevels', level));
40
+ }
41
+
42
+ const qs = queryParams.toString();
43
+
44
+ const endpoint = `${this._baseUrl}/v1/users?${qs}`;
45
+ const response$ = this._httpService.get<RESTResponseBody<IUser[]>>(endpoint);
46
+ const response = await firstValueFrom(response$);
47
+
48
+ if (response.data.errors) {
49
+ throw new RESTError('iam_api_error', response.data.errors);
50
+ }
51
+
52
+ if (!response.data || !response.data.result) {
53
+ throw new RESTError('iam_api_error', 'empty response');
54
+ }
55
+
56
+ return response.data.result;
57
+ } catch (e) {
58
+ throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
59
+ }
60
+ }
61
+
62
+ public async read(id: string) : Promise<RESTResult<IUser>> {
63
+ try {
64
+ const endpoint = `${this._baseUrl}/v1/users/${id}`;
65
+ const response$ = this._httpService.get<RESTResponseBody<IUser>>(endpoint);
66
+ const response = await firstValueFrom(response$);
67
+
68
+ if (response.data.errors) {
69
+ throw new RESTError('iam_api_error', response.data.errors);
70
+ }
71
+
72
+ if (!response.data || !response.data.result) {
73
+ throw new RESTError('iam_api_error', 'empty response');
74
+ }
75
+
76
+ return response.data.result;
77
+ } catch (e) {
78
+ throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
79
+ }
80
+ }
81
+ //#endregion
82
+ }