@lenne.tech/nest-server 10.2.5 → 10.2.7

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "10.2.5",
3
+ "version": "10.2.7",
4
4
  "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
5
5
  "keywords": [
6
6
  "node",
@@ -63,6 +63,7 @@
63
63
  },
64
64
  "dependencies": {
65
65
  "@apollo/gateway": "2.5.7",
66
+ "@getbrevo/brevo": "1.0.1",
66
67
  "@lenne.tech/mongoose-gridfs": "1.4.2",
67
68
  "@lenne.tech/multer-gridfs-storage": "5.0.6",
68
69
  "@nestjs/apollo": "12.0.11",
@@ -0,0 +1,35 @@
1
+ export function htmlTable(
2
+ header: string[],
3
+ rows: string[][],
4
+ options?: {
5
+ tableStyle?: string;
6
+ theadStyle?: string;
7
+ trHeadStyle?: string;
8
+ thStyle?: string;
9
+ tbodyStyle?: string;
10
+ trStyle?: string;
11
+ tdStyle?: string;
12
+ },
13
+ ): string {
14
+ const config = {
15
+ tableStyle: 'width: 100%; border: 1px solid #000; border-collapse: collapse;',
16
+ trHeadStyle: 'background-color: #f0f0f0;',
17
+ thStyle: 'border: 1px solid #000; padding: 10px;',
18
+ tcStyle: 'border: 1px solid #000; padding: 10px;',
19
+ ...options,
20
+ };
21
+ let table = `<table style="${config.tableStyle}"><thead style="${config.theadStyle}"><tr style="${config.trHeadStyle}">`;
22
+ for (const head of header) {
23
+ table += `<th style="${config.thStyle}">${head}</th>`;
24
+ }
25
+ table += '</tr></thead><tbody style="${config.tbodyStyle}">';
26
+ for (const row of rows) {
27
+ table += `<tr style="${config.trStyle}">`;
28
+ for (const cell of row) {
29
+ table += `<td style="${config.tdStyle}">${cell}</td>`;
30
+ }
31
+ table += '</tr>';
32
+ }
33
+ table += '</tbody></table>';
34
+ return table;
35
+ }
@@ -12,7 +12,7 @@ import { processDeep } from '../helpers/input.helper';
12
12
  export class CheckSecurityInterceptor implements NestInterceptor {
13
13
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
14
14
  // Get current user
15
- const user = getContextData(context)?.currentUser;
15
+ const user = getContextData(context)?.currentUser || null;
16
16
 
17
17
  // Set force mode for sign in and sign up
18
18
  let force = false;
@@ -70,6 +70,23 @@ export interface IServerOptions {
70
70
  */
71
71
  automaticObjectIdFiltering?: boolean;
72
72
 
73
+ /**
74
+ * Configuration for Brevo
75
+ * See: https://developers.brevo.com/
76
+ */
77
+ brevo?: {
78
+ /**
79
+ * API key for Brevo
80
+ */
81
+ apiKey?: string;
82
+
83
+ /**
84
+ * Regular expression for excluding (test) users
85
+ * e.g. /@testuser.com$/i
86
+ */
87
+ exclude?: RegExp;
88
+ };
89
+
73
90
  /**
74
91
  * Whether to use the compression middleware package to enable gzip compression.
75
92
  * See: https://docs.nestjs.com/techniques/compression
@@ -0,0 +1,48 @@
1
+ import { Injectable } from '@nestjs/common';
2
+ import brevo = require('@getbrevo/brevo');
3
+ import { ConfigService } from './config.service';
4
+
5
+ @Injectable()
6
+ export class BrevoService {
7
+ constructor(protected configService: ConfigService) {
8
+ const defaultClient = brevo.ApiClient.instance;
9
+ const apiKey = defaultClient.authentications['api-key'];
10
+ apiKey.apiKey = configService.configFastButReadOnly.brevo?.apiKey;
11
+ if (!apiKey.apiKey) {
12
+ console.warn('Brevo API key not set!');
13
+ }
14
+ }
15
+
16
+ /**
17
+ * Send a transactional email via Brevo
18
+ */
19
+ async sendMail(to: string, templateId: number, params?: object): Promise<unknown> {
20
+
21
+ // Check input
22
+ if (!to || !templateId) {
23
+ return false;
24
+ }
25
+
26
+ // Exclude (test) users
27
+ if (this.configService.configFastButReadOnly.brevo?.exclude?.test?.(to)) {
28
+ return 'TEST_USER!';
29
+ }
30
+
31
+ // Prepare data
32
+ const apiInstance = new brevo.TransactionalEmailsApi();
33
+ const sendSmtpEmail = new brevo.SendSmtpEmail();
34
+ sendSmtpEmail.templateId = templateId;
35
+ sendSmtpEmail.to = [{ email: to }];
36
+ sendSmtpEmail.params = params;
37
+
38
+ // Send email
39
+ try {
40
+ return await apiInstance.sendTransacEmail(sendSmtpEmail);
41
+ } catch (error) {
42
+ console.error(error);
43
+ }
44
+
45
+ // Return null if error
46
+ return null;
47
+ }
48
+ }
@@ -1,5 +1,5 @@
1
1
  import { NotFoundException } from '@nestjs/common';
2
- import { Document, FilterQuery, PipelineStage, Query, QueryOptions } from 'mongoose';
2
+ import { AggregateOptions, Document, FilterQuery, PipelineStage, Query, QueryOptions } from 'mongoose';
3
3
  import { FilterArgs } from '../args/filter.args';
4
4
  import { getStringIds } from '../helpers/db.helper';
5
5
  import { convertFilterArgsToQuery } from '../helpers/filter.helper';
@@ -15,6 +15,51 @@ export abstract class CrudService<
15
15
  CreateInput = any,
16
16
  UpdateInput = any,
17
17
  > extends ModuleService<Model> {
18
+
19
+ /**
20
+ * Aggregate
21
+ * @param serviceOptions.aggregateOptions Aggregate options, see https://www.mongodb.com/docs/manual/core/aggregation-pipeline/
22
+ * @param serviceOptions.collation Collation, see https://www.mongodb.com/docs/manual/reference/collation/
23
+ * @param serviceOptions.outputPath Output path of items which should be prepared, e.g. 'items'
24
+ */
25
+ async aggregate<T = any>(
26
+ pipeline: PipelineStage[],
27
+ serviceOptions?: ServiceOptions & { aggregateOptions?: AggregateOptions },
28
+ ): Promise<T> {
29
+ return this.process(
30
+ async () => {
31
+ const aggregateOptions = serviceOptions?.aggregateOptions || {};
32
+ const collation = serviceOptions?.collation || ConfigService.get('mongoose.collation');
33
+ if (collation && !aggregateOptions.collation) {
34
+ aggregateOptions.collation = collation;
35
+ }
36
+ return this.mainDbModel.aggregate(pipeline, aggregateOptions).exec();
37
+ },
38
+ { serviceOptions },
39
+ );
40
+ }
41
+
42
+ /**
43
+ * Aggregate without checks or restrictions
44
+ * Warning: Disables the handling of rights and restrictions!
45
+ */
46
+ async aggregateForce<T = any>(pipeline: PipelineStage[], serviceOptions: ServiceOptions = {}): Promise<T> {
47
+ serviceOptions = serviceOptions || {};
48
+ serviceOptions.force = true;
49
+ return this.aggregate(pipeline, serviceOptions);
50
+ }
51
+
52
+ /**
53
+ * Aggregate without checks, restrictions or preparations
54
+ * Warning: Disables the handling of rights and restrictions! The raw data may contain secrets (such as passwords).
55
+ */
56
+ async aggregateRaw<T = any>(pipeline: PipelineStage[], serviceOptions: ServiceOptions = {}): Promise<T> {
57
+ serviceOptions = serviceOptions || {};
58
+ serviceOptions.prepareInput = null;
59
+ serviceOptions.prepareOutput = null;
60
+ return this.aggregateForce(pipeline, serviceOptions);
61
+ }
62
+
18
63
  /**
19
64
  * Create item
20
65
  */
@@ -311,6 +356,69 @@ export abstract class CrudService<
311
356
  return this.findAndUpdateForce(filter, update, serviceOptions);
312
357
  }
313
358
 
359
+ /**
360
+ * Find one item via filter
361
+ */
362
+ async findOne(
363
+ filter?: FilterArgs | { filterQuery?: FilterQuery<any>; queryOptions?: QueryOptions },
364
+ serviceOptions?: ServiceOptions,
365
+ ): Promise<Model> {
366
+ // If filter is not instance of FilterArgs a simple form with filterQuery and queryOptions is set
367
+ // and should not be processed as FilterArgs
368
+ if (!(filter instanceof FilterArgs) && serviceOptions?.inputType === FilterArgs) {
369
+ serviceOptions = Object.assign({ prepareInput: null }, serviceOptions, { inputType: null });
370
+ }
371
+
372
+ return this.process(
373
+ async (data) => {
374
+
375
+ // Prepare filter query
376
+ const filterQuery = { filterQuery: data?.input?.filterQuery, queryOptions: data?.input?.queryOptions };
377
+ if (data?.input instanceof FilterArgs) {
378
+ const converted = convertFilterArgsToQuery(data.input);
379
+ filterQuery.filterQuery = converted[0];
380
+ filterQuery.queryOptions = converted[1];
381
+ }
382
+
383
+ // Find in DB
384
+ let find = this.mainDbModel.findOne(filterQuery.filterQuery, null, filterQuery.queryOptions);
385
+ const collation = serviceOptions?.collation || ConfigService.get('mongoose.collation');
386
+ if (collation) {
387
+ find = find.collation(collation);
388
+ }
389
+ return find.exec();
390
+ },
391
+ { input: filter, serviceOptions },
392
+ );
393
+ }
394
+
395
+ /**
396
+ * Find one item via filter without checks or restrictions
397
+ * Warning: Disables the handling of rights and restrictions!
398
+ */
399
+ async findOneForce(
400
+ filter?: FilterArgs | { filterQuery?: FilterQuery<any>; queryOptions?: QueryOptions; samples?: number },
401
+ serviceOptions: ServiceOptions = {},
402
+ ): Promise<Model> {
403
+ serviceOptions = serviceOptions || {};
404
+ serviceOptions.force = true;
405
+ return this.findOne(filter, serviceOptions);
406
+ }
407
+
408
+ /**
409
+ * Find one item via filter without checks, restrictions or preparations
410
+ * Warning: Disables the handling of rights and restrictions! The raw data may contain secrets (such as passwords).
411
+ */
412
+ async findOneRaw(
413
+ filter?: FilterArgs | { filterQuery?: FilterQuery<any>; queryOptions?: QueryOptions; samples?: number },
414
+ serviceOptions: ServiceOptions = {},
415
+ ): Promise<Model> {
416
+ serviceOptions = serviceOptions || {};
417
+ serviceOptions.prepareInput = null;
418
+ serviceOptions.prepareOutput = null;
419
+ return this.findOneForce(filter, serviceOptions);
420
+ }
421
+
314
422
  /**
315
423
  * CRUD alias for get
316
424
  */
@@ -180,8 +180,14 @@ export abstract class ModuleService<T extends CoreModel = any> {
180
180
 
181
181
  // Pop and map main model
182
182
  if (config.processFieldSelection && config.fieldSelection && this.processFieldSelection) {
183
- const field = config.outputPath ? _.get(result, config.outputPath) : result;
184
- await this.processFieldSelection(field, config.fieldSelection, config.processFieldSelection);
183
+ let temps = result;
184
+ if (!Array.isArray(result)) {
185
+ temps = [result];
186
+ }
187
+ for (const temp of temps) {
188
+ const field = config.outputPath ? _.get(temp, config.outputPath) : temp;
189
+ await this.processFieldSelection(field, config.fieldSelection, config.processFieldSelection);
190
+ }
185
191
  }
186
192
 
187
193
  // Prepare output
@@ -191,7 +197,13 @@ export abstract class ModuleService<T extends CoreModel = any> {
191
197
  opts.targetModel = config.outputType;
192
198
  }
193
199
  if (config.outputPath) {
194
- _.set(result, config.outputPath, await this.prepareOutput(_.get(result, config.outputPath), opts));
200
+ let temps = result;
201
+ if (!Array.isArray(result)) {
202
+ temps = [result];
203
+ }
204
+ for (const temp of temps) {
205
+ _.set(temp, config.outputPath, await this.prepareOutput(_.get(temp, config.outputPath), opts));
206
+ }
195
207
  } else {
196
208
  result = await this.prepareOutput(result, config);
197
209
  }
package/src/index.ts CHANGED
@@ -33,6 +33,7 @@ export * from './core/common/helpers/graphql.helper';
33
33
  export * from './core/common/helpers/input.helper';
34
34
  export * from './core/common/helpers/model.helper';
35
35
  export * from './core/common/helpers/service.helper';
36
+ export * from './core/common/helpers/table.helper';
36
37
  export * from './core/common/inputs/combined-filter.input';
37
38
  export * from './core/common/inputs/core-input.input';
38
39
  export * from './core/common/inputs/filter.input';
@@ -61,6 +62,7 @@ export * from './core/common/scalars/any.scalar';
61
62
  export * from './core/common/scalars/date.scalar';
62
63
  export * from './core/common/scalars/date-timestamp.scalar';
63
64
  export * from './core/common/scalars/json.scalar';
65
+ export * from './core/common/services/brevo.service';
64
66
  export * from './core/common/services/config.service';
65
67
  export * from './core/common/services/core-cron-jobs.service';
66
68
  export * from './core/common/services/crud.service';