dl-commons 0.0.1-testing.57 → 0.0.2-testing.2

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.57",
4
+ "version": "0.0.2-testing.2",
5
5
  "description": "distance learning commons",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -5,7 +5,7 @@ import { validationExceptionFactory } from './factories/exception.factory';
5
5
  import { BaseExceptionsFilter } from './filters/base-exception.filter';
6
6
  import { AccessTokenGuard } from './guards/access-token.guard';
7
7
  import { AllowPermissions, PermissionGuard } from './guards/permission.guard';
8
- import { APP__PORT, APP__DEBUG, APP__ACCESS_TOKEN, MONGO__CS, REDIS__CS, KAFKA__BROKERS } from './models/consts/env-vars.const';
8
+ import { APP__PORT, APP__DEBUG, APP__ACCESS_TOKEN, MONGO__CS, REDIS__CS, KAFKA__BROKERS, APP__MONGO__CS, DL_SESSIONS__MONGO__CS } from './models/consts/env-vars.const';
9
9
  import { I18nDTO } from './models/dtos/commons/i18n.dto';
10
10
  import {
11
11
  IMongoPage,
@@ -60,6 +60,8 @@ import { IGroupSession } from './models/interfaces/group-session.interface';
60
60
  import { GroupState } from './models/enums/group-state.enum';
61
61
  import { GroupSessionSchema, GroupSessionDocument, GroupSession } from './models/schemas/embedded/group-session.schema';
62
62
  import { TeacherSchema, TeacherDocument, Teacher } from './models/schemas/embedded/teacher.schema';
63
+ import { ISMTPAPIConfig } from './models/interfaces/smtp-api-config.interface';
64
+ import { SMTPService } from './services/smtp.service';
63
65
 
64
66
  export {
65
67
  /**
@@ -67,6 +69,7 @@ export {
67
69
  */
68
70
  SessionsService,
69
71
  DictionariesService,
72
+ SMTPService,
70
73
 
71
74
  /**
72
75
  * Rest Wrapper
@@ -95,6 +98,8 @@ export {
95
98
  APP__PORT,
96
99
  APP__DEBUG,
97
100
  APP__ACCESS_TOKEN,
101
+ APP__MONGO__CS,
102
+ DL_SESSIONS__MONGO__CS,
98
103
  MONGO__CS,
99
104
  REDIS__CS,
100
105
  KAFKA__BROKERS,
@@ -134,6 +139,7 @@ export {
134
139
  IStudentEmbedded,
135
140
  ITeacher,
136
141
  IGroupSession,
142
+ ISMTPAPIConfig,
137
143
 
138
144
  /**
139
145
  * Schemas & Documents
@@ -1,6 +1,7 @@
1
1
  export const APP__PORT = 'APP__PORT';
2
2
  export const APP__DEBUG = 'APP__DEBUG';
3
3
  export const APP__ACCESS_TOKEN = 'APP__ACCESS_TOKEN';
4
+ export const APP__MONGO__CS = 'APP__MONGO__CS';
4
5
 
5
6
  export const MONGO__CS = 'MONGO__CS';
6
7
  export const REDIS__CS = 'REDIS__CS';
@@ -0,0 +1,5 @@
1
+ export interface ISMTPAPIConfig {
2
+ endpoint: string;
3
+ headers?: Map<string, string> | null | undefined;
4
+ profile: string;
5
+ }
@@ -0,0 +1,5 @@
1
+ export interface ISMTP {
2
+ _id: string;
3
+ status: string;
4
+ error: any;
5
+ }
@@ -12,4 +12,5 @@ export interface IUser extends IBase {
12
12
  contacts: IContact[];
13
13
  roles: IRole[];
14
14
  verificationLevel: UserVerificationLevel;
15
+ invited?: boolean;
15
16
  }
@@ -7,6 +7,7 @@ import { firstValueFrom } from 'rxjs';
7
7
  import { Model } from 'mongoose';
8
8
  import { Session, SessionDocument } from '../models/schemas/session.schema';
9
9
  import { InjectModel } from '@nestjs/mongoose';
10
+ import { Types } from 'mongoose';
10
11
 
11
12
  @Injectable()
12
13
  export class SessionsService {
@@ -26,7 +27,7 @@ export class SessionsService {
26
27
  //#endregion
27
28
 
28
29
  //#region Public API
29
- public async create(userId: string, userAgent: string, ip: string) {
30
+ public async create(userId: string | Types.ObjectId, userAgent: string, ip: string) {
30
31
  try {
31
32
  const endpoint = `${this._baseUrl}/v1/sessions/${userId}`;
32
33
  const headers = { 'user-agent': userAgent, 'x-ip': ip };
@@ -0,0 +1,71 @@
1
+ import { HttpService } from '@nestjs/axios';
2
+ import { ConfigService } from '@nestjs/config';
3
+ import { catchError, firstValueFrom, map } from 'rxjs';
4
+ import { Injectable } from '@nestjs/common';
5
+ import { RESTResponseBody, RESTError } from '../models/dtos/wrappers/rest-wrapper.class';
6
+ import { ISMTP } from '../models/interfaces/smtp.interface';
7
+
8
+ @Injectable()
9
+ export class SMTPService {
10
+ //#region Private Members
11
+ private _baseUrl: string;
12
+ //#endregion
13
+
14
+ //#region Constructor
15
+ constructor(private readonly _httpService: HttpService, private readonly _configService: ConfigService) {
16
+ this._baseUrl = this._configService.getOrThrow('DL_SMTP__REST__BASE_URL');
17
+ }
18
+ //#endregion
19
+
20
+ //#region Public API
21
+ public async send(subject: string, from: string, to: string, html: string, profileId: string) {
22
+ const requestBody = {
23
+ data: {
24
+ from,
25
+ to: [to],
26
+ subject,
27
+ html,
28
+ },
29
+ };
30
+
31
+ const response$ = this._httpService.post<RESTResponseBody<ISMTP>>(`${this._baseUrl}/v1/emails/send`, requestBody, { params: { profileId } });
32
+ const response = await firstValueFrom(response$);
33
+
34
+ if (response.data.errors) {
35
+ throw new RESTError('smtp_api_error', response.data.errors);
36
+ }
37
+ if (!response.data || !response.data.result) {
38
+ throw new RESTError('smtp_api_error', 'empty response');
39
+ }
40
+
41
+ return response.data.result.data;
42
+ }
43
+
44
+ public send$(subject: string, from: string, to: string, html: string, profileId: string) {
45
+ const requestBody = {
46
+ data: {
47
+ from,
48
+ to: [to],
49
+ subject,
50
+ html,
51
+ },
52
+ };
53
+
54
+ const response$ = this._httpService.post<RESTResponseBody<ISMTP>>(`${this._baseUrl}/v1/emails/send`, requestBody, { params: { profileId } });
55
+ return response$.pipe(
56
+ map((response) => {
57
+ if (response.data.errors) {
58
+ throw new RESTError('smtp_api_error', response.data.errors);
59
+ }
60
+ if (!response.data || !response.data.result) {
61
+ throw new RESTError('smtp_api_error', 'empty response');
62
+ }
63
+ return response.data.result.data;
64
+ }),
65
+ catchError((e) => {
66
+ throw new RESTError('smtp_api_error', e);
67
+ }),
68
+ );
69
+ }
70
+ //#endregion
71
+ }