dl-commons 0.0.2-testing.1 → 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.2-testing.1",
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
@@ -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
@@ -136,6 +139,7 @@ export {
136
139
  IStudentEmbedded,
137
140
  ITeacher,
138
141
  IGroupSession,
142
+ ISMTPAPIConfig,
139
143
 
140
144
  /**
141
145
  * Schemas & Documents
@@ -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
+ }