@lenne.tech/nest-server 10.2.6 → 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/dist/core/common/helpers/table.helper.d.ts +9 -0
- package/dist/core/common/helpers/table.helper.js +28 -0
- package/dist/core/common/helpers/table.helper.js.map +1 -0
- package/dist/core/common/interfaces/server-options.interface.d.ts +4 -0
- package/dist/core/common/services/brevo.service.d.ts +6 -0
- package/dist/core/common/services/brevo.service.js +52 -0
- package/dist/core/common/services/brevo.service.js.map +1 -0
- package/dist/core/common/services/crud.service.d.ts +6 -1
- package/dist/core/common/services/crud.service.js +21 -0
- package/dist/core/common/services/crud.service.js.map +1 -1
- package/dist/core/common/services/module.service.js +15 -3
- package/dist/core/common/services/module.service.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/src/core/common/helpers/table.helper.ts +35 -0
- package/src/core/common/interfaces/server-options.interface.ts +17 -0
- package/src/core/common/services/brevo.service.ts +48 -0
- package/src/core/common/services/crud.service.ts +46 -1
- package/src/core/common/services/module.service.ts +15 -3
- package/src/index.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nest-server",
|
|
3
|
-
"version": "10.2.
|
|
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
|
+
}
|
|
@@ -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
|
*/
|
|
@@ -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
|
-
|
|
184
|
-
|
|
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
|
-
|
|
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';
|