dl-commons 0.0.1-testing.2 → 0.0.1-testing.4

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.1-testing.2",
4
+ "version": "0.0.1-testing.4",
5
5
  "description": "distance learning commons",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -10,7 +10,7 @@
10
10
  "build": "tsc",
11
11
  "pack": "npm run build && npm pack --pack-destination ./packs"
12
12
  },
13
- "author": "",
13
+ "author": "Giorgi Maisuradze",
14
14
  "license": "ISC",
15
15
  "dependencies": {
16
16
  "@nestjs/axios": "^3.1.0",
@@ -6,6 +6,7 @@ import { DL_SESSIONS__MONGO__CS } from './models/consts/env-vars.const';
6
6
  import { Session, SessionSchema } from './models/schemas/session.schema';
7
7
  import { HttpModule } from '@nestjs/axios';
8
8
  import { ISession } from './models/interfaces/session.interface';
9
+ import { DictionariesService } from './services/dictionaries.service';
9
10
 
10
11
  declare global {
11
12
  namespace Express {
@@ -29,7 +30,7 @@ declare global {
29
30
  MongooseModule.forFeature([{ name: Session.name, schema: SessionSchema }], 'sessions-store'),
30
31
  ],
31
32
  controllers: [],
32
- providers: [SessionsService],
33
- exports: [SessionsService],
33
+ providers: [SessionsService, DictionariesService],
34
+ exports: [SessionsService, DictionariesService],
34
35
  })
35
36
  export class DLCommonsModule {}
package/src/index.ts CHANGED
@@ -49,12 +49,14 @@ import { transformers } from './transformers/common.transformer';
49
49
  import { IUser } from './models/interfaces/user.interface';
50
50
  import { SessionUser, SessionUserDocument, SessionUserSchema } from './models/schemas/embedded/session-user.schema';
51
51
  import { Session, SessionDocument, SessionSchema } from './models/schemas/session.schema';
52
+ import { DictionariesService } from './services/dictionaries.service';
52
53
 
53
54
  export {
54
55
  /**
55
56
  * Services
56
57
  */
57
58
  SessionsService,
59
+ DictionariesService,
58
60
 
59
61
  /**
60
62
  * Rest Wrapper
@@ -1,9 +1,11 @@
1
+ import { IDictionary } from './dictionary.interface';
1
2
  import { IResidence } from './residence.interface';
2
3
 
3
4
  export interface IPerson {
4
5
  pid?: string | null | undefined;
5
6
  firstName: string;
6
7
  lastName: string;
8
+ gender?: IDictionary | null | undefined;
7
9
  birthDate?: Date | null | undefined;
8
10
  residence?: IResidence | null | undefined;
9
11
  }
@@ -3,6 +3,8 @@ import { IPerson } from '../../interfaces/person.interface';
3
3
  import { HydratedDocument } from 'mongoose';
4
4
  import { IResidence } from '../../interfaces/residence.interface';
5
5
  import { Residence, ResidenceSchema } from './residence.schema';
6
+ import { DictionaryEmbeddedSchema } from './dictionary-embedded.schema';
7
+ import { IDictionary } from '../../interfaces/dictionary.interface';
6
8
 
7
9
  @Schema({ _id: false })
8
10
  export class Person implements IPerson {
@@ -18,6 +20,9 @@ export class Person implements IPerson {
18
20
  @Prop({ type: Date, required: false, default: null })
19
21
  birthDate: Date;
20
22
 
23
+ @Prop({ type: DictionaryEmbeddedSchema, required: false })
24
+ gender: IDictionary;
25
+
21
26
  @Prop({ type: ResidenceSchema, required: true, default: () => new Residence() })
22
27
  residence: IResidence;
23
28
  }
@@ -0,0 +1,99 @@
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 DictionariesService {
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_DICTIONARIES__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
+ if (!response.data || !response.data.result) {
32
+ throw new RESTError('dictionaries_api_error', 'empty response');
33
+ }
34
+
35
+ const result: IDictionary = {
36
+ ...response.data.result.data,
37
+ _id: new Types.ObjectId(response.data.result.data._id),
38
+ };
39
+
40
+ return result;
41
+ } catch (e) {
42
+ throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
43
+ }
44
+ }
45
+
46
+ public async fetchByKeyword(keyword: string) {
47
+ try {
48
+ const endpoint = `${this._baseUrl}/v1/dictionaries/by-keyword/${keyword}`;
49
+ const response$ = this._httpService.get<RESTResponseBody<IDictionary>>(endpoint);
50
+ const response = await firstValueFrom(response$);
51
+
52
+ if (response.data.errors) {
53
+ throw new RESTError('dictionaries_api_error', response.data.errors);
54
+ }
55
+ if (!response.data || !response.data.result) {
56
+ throw new RESTError('dictionaries_api_error', 'empty response');
57
+ }
58
+
59
+ const result: IDictionary = {
60
+ ...response.data.result.data,
61
+ _id: new Types.ObjectId(response.data.result.data._id),
62
+ };
63
+
64
+ return result;
65
+ } catch (e) {
66
+ throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
67
+ }
68
+ }
69
+
70
+ public async validateIds(ids: string[]) {
71
+ try {
72
+ const endpoint = `${this._baseUrl}/v1/dictionaries/validate/by-ids`;
73
+ const requestBody = { data: { ids } };
74
+ const response$ = this._httpService.post<RESTResponseBody<IDictionary[]>>(endpoint, requestBody);
75
+ const response = await firstValueFrom(response$);
76
+
77
+ if (response.data.errors) {
78
+ throw new RESTError('dictionaries_api_error', response.data.errors);
79
+ }
80
+
81
+ if (!response.data || !response.data.result) {
82
+ throw new RESTError('dictionaries_api_error', 'empty response');
83
+ }
84
+
85
+ const result: IDictionary[] = [];
86
+ for (let item of response.data.result.data) {
87
+ result.push({
88
+ ...item,
89
+ _id: new Types.ObjectId(item._id),
90
+ });
91
+ }
92
+
93
+ return result;
94
+ } catch (e) {
95
+ throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
96
+ }
97
+ }
98
+ //#endregion
99
+ }