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

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.21",
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,7 @@ 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';
115
116
 
116
117
  export {
117
118
  /**
@@ -172,6 +173,7 @@ export {
172
173
  */
173
174
  I18nDTO,
174
175
  CropAreaDTO,
176
+ IDParams,
175
177
 
176
178
  /**
177
179
  * 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
+ }
@@ -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,108 @@
1
+ import { HttpService } from '@nestjs/axios';
2
+ import { Injectable } from '@nestjs/common';
3
+ import { ConfigService } from '@nestjs/config';
4
+ import { RESTResponseBody, RESTError } from '../models/dtos/wrappers/rest-wrapper.class';
5
+ import { firstValueFrom } from 'rxjs';
6
+ import { IDictionary } from '../models/interfaces/dictionary.interface';
7
+ import { Types } from 'mongoose';
8
+
9
+ @Injectable()
10
+ export class IAMService {
11
+ //#region Private Members
12
+ private _baseUrl: string;
13
+ //#endregion
14
+
15
+ //#region Constructor
16
+ constructor(private readonly _httpService: HttpService, private readonly _configService: ConfigService) {
17
+ this._baseUrl = this._configService.getOrThrow('DL_IAM__REST__BASE_URL');
18
+ }
19
+ //#endregion
20
+
21
+ //#region Public API
22
+ public async fetchById(id: string) {
23
+ 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);
30
+ }
31
+
32
+ if (!response.data || !response.data.result) {
33
+ throw new RESTError('dictionaries_api_error', 'empty response');
34
+ }
35
+
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
+ };
41
+
42
+ return result;
43
+ } else {
44
+ return null;
45
+ }
46
+ } catch (e) {
47
+ throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
48
+ }
49
+ }
50
+
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);
55
+ const response = await firstValueFrom(response$);
56
+
57
+ if (response.data.errors) {
58
+ throw new RESTError('dictionaries_api_error', response.data.errors);
59
+ }
60
+ if (!response.data || !response.data.result) {
61
+ throw new RESTError('dictionaries_api_error', 'empty response');
62
+ }
63
+
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
+ }
74
+ } catch (e) {
75
+ throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
76
+ }
77
+ }
78
+
79
+ public async validateIds(ids: string[]) {
80
+ 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);
84
+ const response = await firstValueFrom(response$);
85
+
86
+ if (response.data.errors) {
87
+ throw new RESTError('dictionaries_api_error', response.data.errors);
88
+ }
89
+
90
+ 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
+ });
100
+ }
101
+
102
+ return result;
103
+ } catch (e) {
104
+ throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
105
+ }
106
+ }
107
+ //#endregion
108
+ }