@futdevpro/nts-dynamo 1.6.53 → 1.6.55

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.
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- import { NextFunction, Request, Response } from 'express';
3
+ import { Request, Response } from 'express';
4
4
 
5
5
  import { Dynamo_Array, Dynamo_Error, Dynamo_Log } from '@futdevpro/fsm-dynamo';
6
6
  import { DynamoNTS_RouteSecurity } from '../../_enums/route-security.enum';
@@ -134,7 +134,7 @@ export class DynamoNTS_EndpointParams{
134
134
  }
135
135
 
136
136
  } catch (error) {
137
- this.error(req, res, error);
137
+ this.error(req, res, error, issuer);
138
138
  }
139
139
  }
140
140
 
@@ -143,9 +143,11 @@ export class DynamoNTS_EndpointParams{
143
143
  * @returns
144
144
  */
145
145
  getFullExecution(): (req: Request, res: Response) => Promise<void> {
146
+ let issuer: string;
147
+
146
148
  return async (req: Request, res: Response) => {
147
149
  try {
148
- const issuer: string = DynamoNTS_GlobalService?.getAuthService()?.getIssuerFromRequest(req);
150
+ issuer = DynamoNTS_GlobalService?.getAuthService()?.getIssuerFromRequest(req);
149
151
 
150
152
  if (this.logRequest) {
151
153
  await this.preLog(req, res, issuer);
@@ -172,7 +174,7 @@ export class DynamoNTS_EndpointParams{
172
174
  }
173
175
  }
174
176
  } catch (error) {
175
- this.error(req, res, error);
177
+ this.error(req, res, error, issuer);
176
178
  }
177
179
  };
178
180
  }
@@ -182,7 +184,7 @@ export class DynamoNTS_EndpointParams{
182
184
  * @param res
183
185
  * @param error
184
186
  */
185
- private error(req: Request, res: Response, error: Error | Dynamo_Error): void {
187
+ private async error(req: Request, res: Response, error: Error | Dynamo_Error, issuer: string): Promise<void> {
186
188
  try {
187
189
  let msg: string = `Endpoint catched an error. ${this.name} (${this.endpoint})`;
188
190
  msg += this.getPathParamsLogContent(req);
@@ -190,12 +192,17 @@ export class DynamoNTS_EndpointParams{
190
192
 
191
193
  Dynamo_Log.error(msg);
192
194
  console.log(
193
- (error as Dynamo_Error)?.flag.includes('DYNAMO') ?
195
+ (error as Dynamo_Error)?.flag?.includes('DYNAMO') ?
194
196
  (error as Dynamo_Error).getErrorSimplified() :
195
197
  error,
196
198
  '\n'
197
199
  );
198
200
 
201
+ await DynamoNTS_GlobalService.globalErrorHandler?.(error, req, res, issuer).catch(err => {
202
+ Dynamo_Log.warn('DynamoNTS_GlobalService.globalErrorHandler failed to handle error: ', err);
203
+ Dynamo_Log.warn('It will proceed as normal.');
204
+ });
205
+
199
206
  res.status((error as Dynamo_Error)?.___status ?? 501);
200
207
  res.send(error);
201
208
 
@@ -215,7 +222,8 @@ export class DynamoNTS_EndpointParams{
215
222
  }
216
223
  } catch (error) {
217
224
  console.error(
218
- `DYNAMO MULTILEVEL ERROR:DynamoNTS_EndpointParams:error: (${this.name}, ${this.endpoint})` +
225
+ `\n\nDYNAMO MULTILEVEL ERROR:DynamoNTS_EndpointParams:error: (${this.name}, ${this.endpoint})` +
226
+ `\n(DYNAMO MULTILEVEL ERROR means, that the ERRORHANDLING is ALSO FAILED, and the error message was not sent.)` +
219
227
  `\nERROR:`, error, '\n'
220
228
  );
221
229
  }
@@ -698,7 +698,7 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
698
698
  }
699
699
  }
700
700
 
701
- private _getDefaultErrorSettings(fnName: string, error?: Error | Dynamo_Error) {
701
+ private _getDefaultErrorSettings(fnName: string, error: Error | Dynamo_Error) {
702
702
  return {
703
703
  status: (error as Dynamo_Error)?.___status ?? 500,
704
704
  message: (error as Error)?.message ?? `${fnName} was UNSUCCESFUL (NTS; ${this.dataParams.dataName})`,
@@ -100,14 +100,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
100
100
  }
101
101
  });
102
102
 
103
- if (typeof newData._id !== 'string' || typeof newData._id === 'object') {
104
- newData._id = `${newData._id}`;
105
- if (typeof data._id !== 'string' || typeof data._id === 'object') {
106
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.createData)')
107
- }
108
- }
109
- data._id = newData._id;
110
- data.__v = newData.__v;
103
+ data = this.stringifyDataId(newData, 'createData');
111
104
 
112
105
  return data;
113
106
  }
@@ -148,14 +141,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
148
141
  });
149
142
  });
150
143
 
151
- if (typeof newData._id !== 'string' || typeof newData._id === 'object') {
152
- newData._id = `${newData._id}`;
153
- if (typeof data._id !== 'string' || typeof data._id === 'object') {
154
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.modifyData)')
155
- }
156
- }
157
- data._id = newData._id;
158
- data.__v = newData.__v;
144
+ data = this.stringifyDataId(newData, 'modifyData');
159
145
 
160
146
  return data;
161
147
  }
@@ -166,6 +152,15 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
166
152
  * @returns data
167
153
  */
168
154
  async getDataById(id: string): Promise<T> {
155
+ if (!id) {
156
+ throw new Dynamo_Error({
157
+ ...this._getDefaultErrorSettings('getDataById', new Error(`No ID provided! (NTS DB)`)),
158
+
159
+ errorCode: 'NTS-DBS-GI1',
160
+ message: `get ${this.dataParams.dbName} by ID was unsuccessful (NTS DB)`,
161
+ });
162
+ }
163
+
169
164
  let data: T = await this.dataModel.findById(id).then(res => {
170
165
  return res?.toObject() as T ?? null;
171
166
  }).catch(error => {
@@ -177,12 +172,8 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
177
172
  });
178
173
  });
179
174
 
180
- if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
181
- data._id = `${data._id}`;
182
- if (typeof data._id !== 'string' || typeof data._id === 'object') {
183
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.getDataById)')
184
- }
185
- }
175
+ data = this.stringifyDataId(data, 'getDataById');
176
+
186
177
  return data;
187
178
  }
188
179
 
@@ -217,12 +208,8 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
217
208
  });
218
209
  });
219
210
 
220
- if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
221
- data._id = `${data._id}`;
222
- if (typeof data._id !== 'string' || typeof data._id === 'object') {
223
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.getDataByDependencyId)')
224
- }
225
- }
211
+ data = this.stringifyDataId(data, 'getDataByDependencyId');
212
+
226
213
  return data;
227
214
  }
228
215
 
@@ -259,12 +246,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
259
246
 
260
247
  if (0 < dataList.length) {
261
248
  dataList.forEach((data: T) => {
262
- if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
263
- data._id = `${data._id}`;
264
- if (typeof data._id !== 'string' || typeof data._id === 'object') {
265
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.getDataListByDependencyId)')
266
- }
267
- }
249
+ data = this.stringifyDataId(data, 'getDataListByDependencyId');
268
250
  });
269
251
  }
270
252
 
@@ -304,12 +286,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
304
286
 
305
287
  if (0 < dataList.length) {
306
288
  dataList.forEach((data: T) => {
307
- if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
308
- data._id = `${data._id}`;
309
- if (typeof data._id !== 'string' || typeof data._id === 'object') {
310
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.getDataListByDependencyIds)')
311
- }
312
- }
289
+ data = this.stringifyDataId(data, 'getDataListByDependencyIds');
313
290
  });
314
291
  }
315
292
 
@@ -460,25 +437,21 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
460
437
  }
461
438
  });
462
439
 
463
- let dataList: T[] = await this.dataModel.find(filter)
464
- .then(res => res ?? [])
465
- .catch(error => {
466
- throw new Dynamo_Error({
467
- ...this._getDefaultErrorSettings('searchData', error),
468
-
469
- errorCode: 'NTS-DBS-SD1',
470
- message: `search ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
440
+ let dataList: T[] = await this.dataModel
441
+ .find(filter)
442
+ .then(res => res ?? [])
443
+ .catch(error => {
444
+ throw new Dynamo_Error({
445
+ ...this._getDefaultErrorSettings('searchData', error),
446
+
447
+ errorCode: 'NTS-DBS-SD1',
448
+ message: `search ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
449
+ });
471
450
  });
472
- });
473
451
 
474
452
  if (0 < dataList.length) {
475
453
  dataList.forEach((data: T) => {
476
- if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
477
- data._id = `${data._id}`;
478
- if (typeof data._id !== 'string' || typeof data._id === 'object') {
479
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.searchData)')
480
- }
481
- }
454
+ data = this.stringifyDataId(data, 'searchData');
482
455
  });
483
456
  }
484
457
 
@@ -575,12 +548,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
575
548
 
576
549
  if (0 < dataList.length) {
577
550
  dataList.forEach((data: T) => {
578
- if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
579
- data._id = `${data._id}`;
580
- if (typeof data._id !== 'string' || typeof data._id === 'object') {
581
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.find)')
582
- }
583
- }
551
+ data = this.stringifyDataId(data, 'find');
584
552
  });
585
553
  }
586
554
 
@@ -637,12 +605,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
637
605
 
638
606
  if (0 < dataList.length) {
639
607
  dataList.forEach((data: T) => {
640
- if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
641
- data._id = `${data._id}`;
642
- if (typeof data._id !== 'string' || typeof data._id === 'object') {
643
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findWithPaging)')
644
- }
645
- }
608
+ data = this.stringifyDataId(data, 'findWithPaging');
646
609
  });
647
610
  }
648
611
 
@@ -674,12 +637,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
674
637
  });
675
638
  });
676
639
 
677
- if (typeof newData._id !== 'string' || typeof newData._id === 'object') {
678
- newData._id = `${newData._id}`;
679
- if (typeof newData._id !== 'string' || typeof newData._id === 'object') {
680
- Dynamo_Log.error('newData._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findByIdAndUpdate)')
681
- }
682
- }
640
+ newData = this.stringifyDataId(newData, 'findByIdAndUpdate');
683
641
 
684
642
  return newData;
685
643
  }
@@ -823,14 +781,14 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
823
781
  // PRIVATE FUNCTIONS
824
782
 
825
783
  private stringifyDataId(data: T, fnName: string): T {
826
- if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
784
+ if (data?._id && (typeof data._id !== 'string' || typeof data._id === 'object')) {
827
785
  data._id = `${data._id}`;
828
786
 
829
787
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
830
788
  data = JSON.parse(JSON.stringify(data));
831
789
 
832
790
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
833
- Dynamo_Log.error(`data._id stringifying failed! Please notfiy the developers! (${fnName})`, new Error());
791
+ Dynamo_Log.error(`data._id stringifying failed! Please notfiy the DynamoNTS developers! (${fnName})`, new Error());
834
792
  }
835
793
  }
836
794
  }
@@ -41,7 +41,7 @@ export class DynamoNTS_EmailService {
41
41
  }
42
42
  ) {
43
43
  try {
44
- /* this.serviceName = this.constructor?.name; */
44
+ this.serviceName = this.constructor?.name;
45
45
  // console.log('\n\n\n\n\nNEW CONSTRUCT STARTED', set.email);
46
46
  this.senderName = set.senderName;
47
47
  this.senderNEmail = `${set.senderName} <${set.email}>`;
@@ -77,16 +77,19 @@ export class DynamoNTS_EmailService {
77
77
  *
78
78
  * @param set
79
79
  */
80
- public async sendEmail(set: {
81
- to: string,
82
- subject: string,
83
- content?: string,
84
- useTemplate?: string,
85
- templateProperties?: {
86
- [propertyKey: string]: string
87
- },
88
- attachments?: Attachment[]
89
- }): Promise<void> {
80
+ public async sendEmail(
81
+ set: {
82
+ to: string,
83
+ subject: string,
84
+ content?: string,
85
+ useTemplate?: string,
86
+ templateProperties?: {
87
+ [propertyKey: string]: string
88
+ },
89
+ attachments?: Attachment[]
90
+ },
91
+ issuer?: string,
92
+ ): Promise<void> {
90
93
  try {
91
94
  let content: string;
92
95
 
@@ -95,21 +98,25 @@ export class DynamoNTS_EmailService {
95
98
  } else {
96
99
  if (!set.useTemplate) {
97
100
  throw new Dynamo_Error({
101
+ ...this._getDefaultErrorSettings(
102
+ 'sendEmail',
103
+ new Error(`No content setting passsed to emailSending!`),
104
+ issuer
105
+ ),
106
+
98
107
  errorCode: 'NTS-ES4-101',
99
- addECToUserMsg: true,
100
- message: 'No content setting passsed to emailSending!',
101
- userMessage: this.defaultErrorUserMsg,
102
- issuerService: this.serviceName,
103
108
  });
104
109
  }
105
110
 
106
111
  if (!this.templates[set.useTemplate]) {
107
112
  throw new Dynamo_Error({
113
+ ...this._getDefaultErrorSettings(
114
+ 'sendEmail',
115
+ new Error(`No email template found with this parameter! (${set.useTemplate})`),
116
+ issuer
117
+ ),
118
+
108
119
  errorCode: 'NTS-ES4-102',
109
- addECToUserMsg: true,
110
- message: `No email template found with this parameter! (${set.useTemplate})`,
111
- userMessage: this.defaultErrorUserMsg,
112
- issuerService: this.serviceName,
113
120
  });
114
121
  }
115
122
 
@@ -124,11 +131,13 @@ export class DynamoNTS_EmailService {
124
131
  Dynamo_Log.error('\nDynamoBEEmailService ERROR: INVALID sendEmail settings', set);
125
132
 
126
133
  throw new Dynamo_Error({
134
+ ...this._getDefaultErrorSettings(
135
+ 'sendEmail',
136
+ new Error(`TemplateProperties missing! properties needed: ${props}`),
137
+ issuer
138
+ ),
139
+
127
140
  errorCode: 'NTS-ES4-103',
128
- addECToUserMsg: true,
129
- message: `TemplateProperties missing! properties needed: ${props}`,
130
- userMessage: this.defaultErrorUserMsg,
131
- issuerService: this.serviceName,
132
141
  });
133
142
  }
134
143
  this.templatePropertyKeys[set.useTemplate].forEach((propertyKey: string) => {
@@ -159,14 +168,21 @@ export class DynamoNTS_EmailService {
159
168
  });
160
169
  });
161
170
  } catch (error) {
162
- throw new Dynamo_Error({
163
- errorCode: 'NTS-ES4-100',
164
- addECToUserMsg: true,
165
- message: `SendEmail failed!`,
166
- userMessage: this.defaultErrorUserMsg,
167
- issuerService: this.serviceName,
168
- error
169
- });
171
+ if (error.includes('all recipients were rejected')) {
172
+ throw new Dynamo_Error({
173
+ ...this._getDefaultErrorSettings('sendEmail', error, issuer),
174
+
175
+ errorCode: 'NTS-ES4-104',
176
+ userMessage: `Can't send mail to ${set.to}`,
177
+ })
178
+ } else {
179
+ throw new Dynamo_Error({
180
+ ...this._getDefaultErrorSettings('sendEmail', error, issuer),
181
+
182
+ errorCode: 'NTS-ES4-100',
183
+ message: `SendEmail failed!`,
184
+ });
185
+ }
170
186
  }
171
187
  }
172
188
 
@@ -240,4 +256,16 @@ export class DynamoNTS_EmailService {
240
256
  Dynamo_Log.error(`\nDynamoBEEmailService ERROR, getTemplatePropertyKeys ERROR`, new Error(), '\ntemplate:\n', template);
241
257
  }
242
258
  }
259
+
260
+ private _getDefaultErrorSettings(fnName: string, error: Error | Dynamo_Error, issuer: string) {
261
+ return {
262
+ status: (error as Dynamo_Error)?.___status ?? 500,
263
+ message: (error as Error)?.message ?? `${fnName} was UNSUCCESFUL (NTS; ${this.serviceName})`,
264
+ addECToUserMsg: true,
265
+ userMessage: this.defaultErrorUserMsg,
266
+ issuer: issuer,
267
+ issuerService: this.serviceName,
268
+ error: error,
269
+ }
270
+ }
243
271
  }
@@ -1,5 +1,7 @@
1
1
 
2
2
 
3
+ import { Request, Response } from 'express';
4
+
3
5
  import { Dynamo_Metadata, Dynamo_DataParams, Dynamo_Log, Dynamo_Error } from '@futdevpro/fsm-dynamo';
4
6
  import { DynamoNTS_AppParams } from '../../_models/control-models/app-params.control-model';
5
7
 
@@ -34,6 +36,9 @@ export class DynamoNTS_GlobalService extends DynamoNTS_SingletonService {
34
36
  authService: DynamoNTS_AuthService;
35
37
  dbServiceCollection: DynamoNTS_ServiceCollection<DynamoNTS_DBService<any>>; // DynamoNTS_DBServiceCollection;
36
38
  emailServiceCollection: DynamoNTS_ServiceCollection<DynamoNTS_EmailService>; // DynamoNTS_EmailServiceCollection;
39
+
40
+ private static _globalErrorHandler?: (err: any, req: Request, res: Response, issuer: string) => Promise<void>;
41
+ static get globalErrorHandler(): (err: any, req: Request, res: Response, issuer: string) => Promise<void> { return this._globalErrorHandler; }
37
42
 
38
43
  /**
39
44
  * You need to setup global Services through this function
@@ -73,6 +78,10 @@ export class DynamoNTS_GlobalService extends DynamoNTS_SingletonService {
73
78
  this._params = params;
74
79
  }
75
80
 
81
+ static setGlobalErrorHandler(handler?: (err: any, req: Request, res: Response, issuer: string) => Promise<void>): void {
82
+ this.instance.globalErrorHandler = handler;
83
+ }
84
+
76
85
  /**
77
86
  *
78
87
  * @returns
@@ -1,11 +1,14 @@
1
1
 
2
2
  import Mongoose = require("mongoose");
3
3
  import Express = require("express");
4
+
4
5
  import * as Http from 'http';
5
6
  import * as Https from 'https';
6
7
  import * as FileSystem from 'fs';
7
8
  import * as BodyParser from 'body-parser';
8
9
 
10
+ import { Request, Response } from 'express';
11
+
9
12
  import { Dynamo_Array, Dynamo_Error, dynamo_error_default, Dynamo_Log, second, wait } from '@futdevpro/fsm-dynamo';
10
13
 
11
14
  import { DynamoNTS_SingletonService } from '../base/singleton.service';
@@ -241,6 +244,7 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
241
244
  this.globalService = DynamoNTS_GlobalService.getInstance();
242
245
  DynamoNTS_GlobalService.setServices(this.getGlobalServiceCollection());
243
246
  DynamoNTS_GlobalService.setParams(this.params);
247
+ DynamoNTS_GlobalService.setGlobalErrorHandler(this.getGlobalErrorHandler?.());
244
248
 
245
249
  if (this.getPortSettings) {
246
250
  this._ports = this.getPortSettings();
@@ -878,4 +882,9 @@ export abstract class DynamoNTS_App extends DynamoNTS_SingletonService {
878
882
  * MISSING Description (TODO)
879
883
  */
880
884
  postProcess?(): Promise<void>;
885
+
886
+ /**
887
+ * MISSING Description (TODO)
888
+ */
889
+ getGlobalErrorHandler?(): (err: any, req: Request, res: Response, issuer: string) => Promise<void>;
881
890
  }