anote-server-libs 0.11.5 → 0.11.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/.vscode/settings.json +2 -2
- package/models/ApiCall.ts +40 -40
- package/models/Migration.ts +43 -43
- package/models/repository/MemoryCache.ts +87 -87
- package/models/repository/ModelDao.ts +268 -268
- package/package.json +35 -35
- package/services/WithBody.ts +65 -65
- package/services/WithTransaction.ts +161 -161
- package/services/utils.js +43 -42
- package/services/utils.ts +287 -281
- package/tsconfig.json +29 -29
- package/models/repository/CryptModelDao.js +0 -82
package/services/WithBody.ts
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
import {Request, Response} from 'express';
|
|
2
|
-
import Ajv, {_} from 'ajv';
|
|
3
|
-
|
|
4
|
-
export const ajv = new Ajv();
|
|
5
|
-
ajv.addKeyword({
|
|
6
|
-
keyword: 'maxDigits',
|
|
7
|
-
type: 'number',
|
|
8
|
-
schemaType: 'number',
|
|
9
|
-
validate: (schema: number, data: any) => typeof data === 'number' && Math.round(data * 10 ** schema) / (10 ** schema) === data
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
export function WithBody(schema: any, collapseTopLevelNulls = true) {
|
|
13
|
-
if(schema.type === 'object') {
|
|
14
|
-
const required: string[] = [];
|
|
15
|
-
const keys = Object.getOwnPropertyNames(schema.properties);
|
|
16
|
-
keys.forEach(key => {
|
|
17
|
-
if(schema.properties[key].required === true) {
|
|
18
|
-
required.push(key);
|
|
19
|
-
}
|
|
20
|
-
if(schema.properties[key].required === true || schema.properties[key].required === false) {
|
|
21
|
-
delete schema.properties[key].required;
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
schema.required = required;
|
|
25
|
-
}
|
|
26
|
-
const validate = ajv.compile(schema);
|
|
27
|
-
return function(_: any, __: string, descriptor: PropertyDescriptor) {
|
|
28
|
-
if(typeof descriptor.value === 'function') {
|
|
29
|
-
const previousMethod = descriptor.value;
|
|
30
|
-
descriptor.value = function(this: any, req: Request, res: Response) {
|
|
31
|
-
if(req.method.toUpperCase() !== 'POST' && req.method.toUpperCase() !== 'PUT') {
|
|
32
|
-
return previousMethod.call(this, req, res);
|
|
33
|
-
}
|
|
34
|
-
if(!req.body) {
|
|
35
|
-
res.status(400).json({
|
|
36
|
-
error: {
|
|
37
|
-
errorKey: 'client.body.missing',
|
|
38
|
-
additionalInfo: 'client.extended.badPayload'
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
} else {
|
|
42
|
-
if(collapseTopLevelNulls) {
|
|
43
|
-
Object.keys(req.body).forEach(k => {
|
|
44
|
-
if(req.body[k] === null) delete req.body[k];
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
const valid = validate(req.body);
|
|
48
|
-
if(valid) {
|
|
49
|
-
return previousMethod.call(this, req, res);
|
|
50
|
-
} else {
|
|
51
|
-
res.status(400).json({
|
|
52
|
-
error: {
|
|
53
|
-
errorKey: 'client.body.missing',
|
|
54
|
-
additionalInfo: 'client.extended.badPayload',
|
|
55
|
-
detailedInfo: validate.errors
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
return descriptor;
|
|
62
|
-
}
|
|
63
|
-
return undefined;
|
|
64
|
-
};
|
|
65
|
-
}
|
|
1
|
+
import {Request, Response} from 'express';
|
|
2
|
+
import Ajv, {_} from 'ajv';
|
|
3
|
+
|
|
4
|
+
export const ajv = new Ajv();
|
|
5
|
+
ajv.addKeyword({
|
|
6
|
+
keyword: 'maxDigits',
|
|
7
|
+
type: 'number',
|
|
8
|
+
schemaType: 'number',
|
|
9
|
+
validate: (schema: number, data: any) => typeof data === 'number' && Math.round(data * 10 ** schema) / (10 ** schema) === data
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export function WithBody(schema: any, collapseTopLevelNulls = true) {
|
|
13
|
+
if(schema.type === 'object') {
|
|
14
|
+
const required: string[] = [];
|
|
15
|
+
const keys = Object.getOwnPropertyNames(schema.properties);
|
|
16
|
+
keys.forEach(key => {
|
|
17
|
+
if(schema.properties[key].required === true) {
|
|
18
|
+
required.push(key);
|
|
19
|
+
}
|
|
20
|
+
if(schema.properties[key].required === true || schema.properties[key].required === false) {
|
|
21
|
+
delete schema.properties[key].required;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
schema.required = required;
|
|
25
|
+
}
|
|
26
|
+
const validate = ajv.compile(schema);
|
|
27
|
+
return function(_: any, __: string, descriptor: PropertyDescriptor) {
|
|
28
|
+
if(typeof descriptor.value === 'function') {
|
|
29
|
+
const previousMethod = descriptor.value;
|
|
30
|
+
descriptor.value = function(this: any, req: Request, res: Response) {
|
|
31
|
+
if(req.method.toUpperCase() !== 'POST' && req.method.toUpperCase() !== 'PUT') {
|
|
32
|
+
return previousMethod.call(this, req, res);
|
|
33
|
+
}
|
|
34
|
+
if(!req.body) {
|
|
35
|
+
res.status(400).json({
|
|
36
|
+
error: {
|
|
37
|
+
errorKey: 'client.body.missing',
|
|
38
|
+
additionalInfo: 'client.extended.badPayload'
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
if(collapseTopLevelNulls) {
|
|
43
|
+
Object.keys(req.body).forEach(k => {
|
|
44
|
+
if(req.body[k] === null) delete req.body[k];
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const valid = validate(req.body);
|
|
48
|
+
if(valid) {
|
|
49
|
+
return previousMethod.call(this, req, res);
|
|
50
|
+
} else {
|
|
51
|
+
res.status(400).json({
|
|
52
|
+
error: {
|
|
53
|
+
errorKey: 'client.body.missing',
|
|
54
|
+
additionalInfo: 'client.extended.badPayload',
|
|
55
|
+
detailedInfo: validate.errors
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
return descriptor;
|
|
62
|
+
}
|
|
63
|
+
return undefined;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -1,161 +1,161 @@
|
|
|
1
|
-
import {NextFunction, Request, Response} from 'express';
|
|
2
|
-
import {Logger} from 'winston';
|
|
3
|
-
import {BaseModelRepository} from '../models/repository/BaseModelRepository';
|
|
4
|
-
import {jsonStringify, utils} from './utils';
|
|
5
|
-
|
|
6
|
-
export const enum SystemLock {
|
|
7
|
-
CHECK_CROSSING = 1,
|
|
8
|
-
FLUSH_CALLS = 2,
|
|
9
|
-
TX_CHECK = 3
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const withTransactionConfig = {
|
|
13
|
-
logQueries: false,
|
|
14
|
-
timeoutMillis: 15000
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export function withTransaction(repo: BaseModelRepository, logger: Logger, previousMethod: (req: Request, res: Response, next: NextFunction) => void, lock?: SystemLock, commitIfLost = true) {
|
|
18
|
-
return function(req: Request, res: Response, next: NextFunction) {
|
|
19
|
-
let commit = true;
|
|
20
|
-
if(!commitIfLost) {
|
|
21
|
-
res.once('close', () => commit = false);
|
|
22
|
-
}
|
|
23
|
-
const endTerminator = res.end.bind(res);
|
|
24
|
-
const jsonTerminator = (obj: any) => {
|
|
25
|
-
try { res.set('Content-Type', 'application/json'); } catch(_) {}
|
|
26
|
-
res.write(jsonStringify(obj) || '{}');
|
|
27
|
-
endTerminator();
|
|
28
|
-
};
|
|
29
|
-
const connectTimeoutHandler = setTimeout(() => {
|
|
30
|
-
// Timed out getting a client, restart worker or process...
|
|
31
|
-
logger.error('Error timed out getting a client, exiting...');
|
|
32
|
-
process.exit(22);
|
|
33
|
-
}, withTransactionConfig.timeoutMillis);
|
|
34
|
-
Promise.all([
|
|
35
|
-
repo.db ? repo.db.connect() : Promise.resolve(undefined),
|
|
36
|
-
repo.dbMssql ? Promise.resolve(repo.dbMssql.transaction()) : Promise.resolve(undefined)
|
|
37
|
-
]).then(([c1, c2]) => {
|
|
38
|
-
const dbClient = c1 || c2;
|
|
39
|
-
clearTimeout(connectTimeoutHandler);
|
|
40
|
-
// On error, will rollback...
|
|
41
|
-
utils.logger = logger;
|
|
42
|
-
if(withTransactionConfig.logQueries && !dbClient.queryPatched) {
|
|
43
|
-
const originalQuery = dbClient.query.bind(dbClient);
|
|
44
|
-
dbClient.query = (...args: any[]) => {
|
|
45
|
-
logger.debug('SQL [Client %d] QUERY: %s', dbClient.processID, args[0]);
|
|
46
|
-
return originalQuery(...args);
|
|
47
|
-
};
|
|
48
|
-
dbClient.queryPatched = true;
|
|
49
|
-
}
|
|
50
|
-
dbClient.removeAllListeners('error');
|
|
51
|
-
dbClient.on('error', (err: any) => utils.clientErrorHandler(err, dbClient));
|
|
52
|
-
|
|
53
|
-
res.locals.dbClient = dbClient;
|
|
54
|
-
res.locals.dbClientCommited = false;
|
|
55
|
-
res.locals.dbClientCommit = (cb: (err: any) => any) => {
|
|
56
|
-
if(!res.locals.dbClientCommited) {
|
|
57
|
-
res.locals.dbClientCommited = true;
|
|
58
|
-
(commit ? (repo.db ? dbClient.query('COMMIT') : dbClient.commit()) : Promise.reject(new Error('Client lost'))).catch((err: any) => err).then((err: any) => {
|
|
59
|
-
if(repo.db) dbClient.release();
|
|
60
|
-
if(!(err instanceof Error)) {
|
|
61
|
-
for(let i = 0; i < res.locals.dbClientOnCommit.length; i++) {
|
|
62
|
-
res.locals.dbClientOnCommit[i]();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
cb(err instanceof Error ? err : undefined);
|
|
66
|
-
});
|
|
67
|
-
} else {
|
|
68
|
-
cb(undefined);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
res.locals.dbClientOnCommit = [];
|
|
72
|
-
return (repo.db ? dbClient.query('BEGIN') : dbClient.begin()).then(() => {
|
|
73
|
-
const finish = () => {
|
|
74
|
-
res.send = () => {
|
|
75
|
-
throw new Error('res.send() should not be used within transactions. Use res.json() instead.');
|
|
76
|
-
};
|
|
77
|
-
res.json = (obj: any) => {
|
|
78
|
-
if(res.statusCode > 303 && res.statusCode !== 412) {
|
|
79
|
-
if(logger) {
|
|
80
|
-
if(res.statusCode > 499) {
|
|
81
|
-
logger.error('Uncaught 500: %j', obj?.error?.additionalInfo);
|
|
82
|
-
} else {
|
|
83
|
-
logger.warn('Client error 4XX: %j', obj?.error);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err: any) => obj && obj.error && (obj.error.additionalInfo2 = {message: err.message})).then(() => {
|
|
87
|
-
if(repo.db) dbClient.release();
|
|
88
|
-
jsonTerminator(obj);
|
|
89
|
-
});
|
|
90
|
-
} else {
|
|
91
|
-
res.locals.dbClientCommit((err: any) => {
|
|
92
|
-
if(err && commitIfLost) {
|
|
93
|
-
res.status(500);
|
|
94
|
-
jsonTerminator({
|
|
95
|
-
error: {
|
|
96
|
-
errorKey: 'internal.db',
|
|
97
|
-
additionalInfo: {message: err.message}
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
} else jsonTerminator(obj);
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
return res;
|
|
104
|
-
};
|
|
105
|
-
res.end = () => {
|
|
106
|
-
if(res.statusCode > 303 && res.statusCode !== 412) {
|
|
107
|
-
if(logger && res.statusCode > 499) {
|
|
108
|
-
logger.error('Uncaught 500 with no details...');
|
|
109
|
-
}
|
|
110
|
-
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((): any => undefined).then(() => {
|
|
111
|
-
if(repo.db) dbClient.release();
|
|
112
|
-
endTerminator();
|
|
113
|
-
});
|
|
114
|
-
} else {
|
|
115
|
-
res.locals.dbClientCommit((err: any) => {
|
|
116
|
-
if(err && commitIfLost) {
|
|
117
|
-
res.status(500);
|
|
118
|
-
jsonTerminator({
|
|
119
|
-
error: {
|
|
120
|
-
errorKey: 'internal.db',
|
|
121
|
-
additionalInfo: {message: err.message}
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
} else endTerminator();
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
return res;
|
|
128
|
-
};
|
|
129
|
-
return previousMethod.call(this, req, res, next);
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
if(lock) {
|
|
133
|
-
dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err: any) => {
|
|
134
|
-
res.status(500).json({
|
|
135
|
-
error: {
|
|
136
|
-
errorKey: 'internal.db',
|
|
137
|
-
additionalInfo: {message: err.message}
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
dbClient.release(); // Lock acquisition failed, release will also rollback
|
|
141
|
-
});
|
|
142
|
-
} else {
|
|
143
|
-
finish();
|
|
144
|
-
}
|
|
145
|
-
}).catch((err: any) => {
|
|
146
|
-
// Error beginning transaction
|
|
147
|
-
dbClient.release();
|
|
148
|
-
throw err;
|
|
149
|
-
});
|
|
150
|
-
}).catch(err => {
|
|
151
|
-
// Error connecting to database for other reason than timeout or beginning transaction
|
|
152
|
-
clearTimeout(connectTimeoutHandler);
|
|
153
|
-
res.status(500).json({
|
|
154
|
-
error: {
|
|
155
|
-
errorKey: 'internal.db',
|
|
156
|
-
additionalInfo: {message: err.message}
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
};
|
|
161
|
-
}
|
|
1
|
+
import {NextFunction, Request, Response} from 'express';
|
|
2
|
+
import {Logger} from 'winston';
|
|
3
|
+
import {BaseModelRepository} from '../models/repository/BaseModelRepository';
|
|
4
|
+
import {jsonStringify, utils} from './utils';
|
|
5
|
+
|
|
6
|
+
export const enum SystemLock {
|
|
7
|
+
CHECK_CROSSING = 1,
|
|
8
|
+
FLUSH_CALLS = 2,
|
|
9
|
+
TX_CHECK = 3
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const withTransactionConfig = {
|
|
13
|
+
logQueries: false,
|
|
14
|
+
timeoutMillis: 15000
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function withTransaction(repo: BaseModelRepository, logger: Logger, previousMethod: (req: Request, res: Response, next: NextFunction) => void, lock?: SystemLock, commitIfLost = true) {
|
|
18
|
+
return function(req: Request, res: Response, next: NextFunction) {
|
|
19
|
+
let commit = true;
|
|
20
|
+
if(!commitIfLost) {
|
|
21
|
+
res.once('close', () => commit = false);
|
|
22
|
+
}
|
|
23
|
+
const endTerminator = res.end.bind(res);
|
|
24
|
+
const jsonTerminator = (obj: any) => {
|
|
25
|
+
try { res.set('Content-Type', 'application/json'); } catch(_) {}
|
|
26
|
+
res.write(jsonStringify(obj) || '{}');
|
|
27
|
+
endTerminator();
|
|
28
|
+
};
|
|
29
|
+
const connectTimeoutHandler = setTimeout(() => {
|
|
30
|
+
// Timed out getting a client, restart worker or process...
|
|
31
|
+
logger.error('Error timed out getting a client, exiting...');
|
|
32
|
+
process.exit(22);
|
|
33
|
+
}, withTransactionConfig.timeoutMillis);
|
|
34
|
+
Promise.all([
|
|
35
|
+
repo.db ? repo.db.connect() : Promise.resolve(undefined),
|
|
36
|
+
repo.dbMssql ? Promise.resolve(repo.dbMssql.transaction()) : Promise.resolve(undefined)
|
|
37
|
+
]).then(([c1, c2]) => {
|
|
38
|
+
const dbClient = c1 || c2;
|
|
39
|
+
clearTimeout(connectTimeoutHandler);
|
|
40
|
+
// On error, will rollback...
|
|
41
|
+
utils.logger = logger;
|
|
42
|
+
if(withTransactionConfig.logQueries && !dbClient.queryPatched) {
|
|
43
|
+
const originalQuery = dbClient.query.bind(dbClient);
|
|
44
|
+
dbClient.query = (...args: any[]) => {
|
|
45
|
+
logger.debug('SQL [Client %d] QUERY: %s', dbClient.processID, args[0]);
|
|
46
|
+
return originalQuery(...args);
|
|
47
|
+
};
|
|
48
|
+
dbClient.queryPatched = true;
|
|
49
|
+
}
|
|
50
|
+
dbClient.removeAllListeners('error');
|
|
51
|
+
dbClient.on('error', (err: any) => utils.clientErrorHandler(err, dbClient));
|
|
52
|
+
|
|
53
|
+
res.locals.dbClient = dbClient;
|
|
54
|
+
res.locals.dbClientCommited = false;
|
|
55
|
+
res.locals.dbClientCommit = (cb: (err: any) => any) => {
|
|
56
|
+
if(!res.locals.dbClientCommited) {
|
|
57
|
+
res.locals.dbClientCommited = true;
|
|
58
|
+
(commit ? (repo.db ? dbClient.query('COMMIT') : dbClient.commit()) : Promise.reject(new Error('Client lost'))).catch((err: any) => err).then((err: any) => {
|
|
59
|
+
if(repo.db) dbClient.release();
|
|
60
|
+
if(!(err instanceof Error)) {
|
|
61
|
+
for(let i = 0; i < res.locals.dbClientOnCommit.length; i++) {
|
|
62
|
+
res.locals.dbClientOnCommit[i]();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
cb(err instanceof Error ? err : undefined);
|
|
66
|
+
});
|
|
67
|
+
} else {
|
|
68
|
+
cb(undefined);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
res.locals.dbClientOnCommit = [];
|
|
72
|
+
return (repo.db ? dbClient.query('BEGIN') : dbClient.begin()).then(() => {
|
|
73
|
+
const finish = () => {
|
|
74
|
+
res.send = () => {
|
|
75
|
+
throw new Error('res.send() should not be used within transactions. Use res.json() instead.');
|
|
76
|
+
};
|
|
77
|
+
res.json = (obj: any) => {
|
|
78
|
+
if(res.statusCode > 303 && res.statusCode !== 412) {
|
|
79
|
+
if(logger) {
|
|
80
|
+
if(res.statusCode > 499) {
|
|
81
|
+
logger.error('Uncaught 500: %j', obj?.error?.additionalInfo);
|
|
82
|
+
} else {
|
|
83
|
+
logger.warn('Client error 4XX: %j', obj?.error);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err: any) => obj && obj.error && (obj.error.additionalInfo2 = {message: err.message})).then(() => {
|
|
87
|
+
if(repo.db) dbClient.release();
|
|
88
|
+
jsonTerminator(obj);
|
|
89
|
+
});
|
|
90
|
+
} else {
|
|
91
|
+
res.locals.dbClientCommit((err: any) => {
|
|
92
|
+
if(err && commitIfLost) {
|
|
93
|
+
res.status(500);
|
|
94
|
+
jsonTerminator({
|
|
95
|
+
error: {
|
|
96
|
+
errorKey: 'internal.db',
|
|
97
|
+
additionalInfo: {message: err.message}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
} else jsonTerminator(obj);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return res;
|
|
104
|
+
};
|
|
105
|
+
res.end = () => {
|
|
106
|
+
if(res.statusCode > 303 && res.statusCode !== 412) {
|
|
107
|
+
if(logger && res.statusCode > 499) {
|
|
108
|
+
logger.error('Uncaught 500 with no details...');
|
|
109
|
+
}
|
|
110
|
+
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((): any => undefined).then(() => {
|
|
111
|
+
if(repo.db) dbClient.release();
|
|
112
|
+
endTerminator();
|
|
113
|
+
});
|
|
114
|
+
} else {
|
|
115
|
+
res.locals.dbClientCommit((err: any) => {
|
|
116
|
+
if(err && commitIfLost) {
|
|
117
|
+
res.status(500);
|
|
118
|
+
jsonTerminator({
|
|
119
|
+
error: {
|
|
120
|
+
errorKey: 'internal.db',
|
|
121
|
+
additionalInfo: {message: err.message}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
} else endTerminator();
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return res;
|
|
128
|
+
};
|
|
129
|
+
return previousMethod.call(this, req, res, next);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
if(lock) {
|
|
133
|
+
dbClient.query('SELECT pg_advisory_xact_lock(' + lock + ')').then(() => finish()).catch((err: any) => {
|
|
134
|
+
res.status(500).json({
|
|
135
|
+
error: {
|
|
136
|
+
errorKey: 'internal.db',
|
|
137
|
+
additionalInfo: {message: err.message}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
dbClient.release(); // Lock acquisition failed, release will also rollback
|
|
141
|
+
});
|
|
142
|
+
} else {
|
|
143
|
+
finish();
|
|
144
|
+
}
|
|
145
|
+
}).catch((err: any) => {
|
|
146
|
+
// Error beginning transaction
|
|
147
|
+
dbClient.release();
|
|
148
|
+
throw err;
|
|
149
|
+
});
|
|
150
|
+
}).catch(err => {
|
|
151
|
+
// Error connecting to database for other reason than timeout or beginning transaction
|
|
152
|
+
clearTimeout(connectTimeoutHandler);
|
|
153
|
+
res.status(500).json({
|
|
154
|
+
error: {
|
|
155
|
+
errorKey: 'internal.db',
|
|
156
|
+
additionalInfo: {message: err.message}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
}
|
package/services/utils.js
CHANGED
|
@@ -12,9 +12,8 @@ exports.sendSelfPostableMessage = sendSelfPostableMessage;
|
|
|
12
12
|
exports.fpEuros = fpEuros;
|
|
13
13
|
exports.digitize = digitize;
|
|
14
14
|
exports.readEmailTemplates = readEmailTemplates;
|
|
15
|
-
exports.replacePlaceholders = replacePlaceholders;
|
|
16
15
|
exports.getHtmlReplaced = getHtmlReplaced;
|
|
17
|
-
exports.
|
|
16
|
+
exports.getEmailOneSimpleValue = getEmailOneSimpleValue;
|
|
18
17
|
const fs = require("fs");
|
|
19
18
|
function atob(str) {
|
|
20
19
|
return Buffer.from(str, 'base64').toString('binary');
|
|
@@ -148,22 +147,22 @@ function idempotent(repo, debug, logger) {
|
|
|
148
147
|
};
|
|
149
148
|
}
|
|
150
149
|
function sendSelfPostableMessage(res, code, messageType, err) {
|
|
151
|
-
res.type('text/html').status(code).write(`
|
|
152
|
-
<!DOCTYPE HTML>
|
|
153
|
-
<html>
|
|
154
|
-
<head>
|
|
155
|
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
|
156
|
-
</head>
|
|
157
|
-
<body>
|
|
158
|
-
<script type="text/javascript">
|
|
159
|
-
window.parent.postMessage({
|
|
160
|
-
type: '${messageType}',
|
|
161
|
-
confirm: ${!err},
|
|
162
|
-
error: JSON.parse('${JSON.stringify(err) || 'null'}')
|
|
163
|
-
}, '*');
|
|
164
|
-
</script>
|
|
165
|
-
</body>
|
|
166
|
-
</html>
|
|
150
|
+
res.type('text/html').status(code).write(`
|
|
151
|
+
<!DOCTYPE HTML>
|
|
152
|
+
<html>
|
|
153
|
+
<head>
|
|
154
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
|
155
|
+
</head>
|
|
156
|
+
<body>
|
|
157
|
+
<script type="text/javascript">
|
|
158
|
+
window.parent.postMessage({
|
|
159
|
+
type: '${messageType}',
|
|
160
|
+
confirm: ${!err},
|
|
161
|
+
error: JSON.parse('${JSON.stringify(err) || 'null'}')
|
|
162
|
+
}, '*');
|
|
163
|
+
</script>
|
|
164
|
+
</body>
|
|
165
|
+
</html>
|
|
167
166
|
`);
|
|
168
167
|
res.end();
|
|
169
168
|
}
|
|
@@ -233,9 +232,8 @@ function replacePlaceholders(data, keys, depth) {
|
|
|
233
232
|
function getHtml(template, translationKeyValue) {
|
|
234
233
|
if (!template)
|
|
235
234
|
return undefined;
|
|
236
|
-
const pattern = new RegExp('{{\\s*([
|
|
237
|
-
const replacer = (_, key) => replacePlaceholders(translationKeyValue, key.split('.'), 0) ??
|
|
238
|
-
template = template.replace(pattern, replacer);
|
|
235
|
+
const pattern = new RegExp('{{\\s*([^{}]+?)\\s*}}', 'g');
|
|
236
|
+
const replacer = (_, key) => replacePlaceholders(translationKeyValue, key.split('.'), 0) ?? key;
|
|
239
237
|
template = template.replace(pattern, replacer);
|
|
240
238
|
template = template.replace(pattern, replacer);
|
|
241
239
|
template = template.replace(new RegExp('\\[\\[\\s*([a-zA-Z._0-9:]+)\\s*\\]\\]', 'g'), (_, key) => {
|
|
@@ -251,38 +249,41 @@ function getHtml(template, translationKeyValue) {
|
|
|
251
249
|
});
|
|
252
250
|
return template;
|
|
253
251
|
}
|
|
254
|
-
function getHtmlReplaced(config, templates, key, lang,
|
|
255
|
-
const
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
252
|
+
function getHtmlReplaced(config, templates, key, lang, translations, extraData, newPage) {
|
|
253
|
+
const { product, name } = config.app;
|
|
254
|
+
const merged = {
|
|
255
|
+
...(translations.common[lang] ?? translations.common[0] ?? {}),
|
|
256
|
+
...(translations.byProduct[product]?.[lang] ?? translations.byProduct[product]?.[0] ?? {}),
|
|
257
|
+
...(name && name !== product ? (translations.byEnv[name]?.[lang] ?? translations.byEnv[name]?.[0] ?? {}) : {}),
|
|
258
|
+
};
|
|
259
259
|
const translationKeyValue = {
|
|
260
|
-
...
|
|
261
|
-
...projectSpecificTrans,
|
|
262
|
-
...envSpecificTrans,
|
|
260
|
+
...merged,
|
|
263
261
|
...extraData,
|
|
264
262
|
domain: config.frontend,
|
|
265
263
|
...config.app.emailConfig
|
|
266
264
|
};
|
|
267
265
|
let html = getHtml(templates[key], translationKeyValue);
|
|
268
266
|
if (newPage) {
|
|
269
|
-
html = html.replace('</head>', `<style>
|
|
270
|
-
@media print {
|
|
271
|
-
.new-page {
|
|
272
|
-
page-break-before: always;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
</style>
|
|
267
|
+
html = html.replace('</head>', `<style>
|
|
268
|
+
@media print {
|
|
269
|
+
.new-page {
|
|
270
|
+
page-break-before: always;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
</style>
|
|
276
274
|
</head>`);
|
|
277
|
-
html = html.replace('</body>', `<p class="new-page">Provided by Satoris</p>
|
|
275
|
+
html = html.replace('</body>', `<p class="new-page">Provided by Satoris</p>
|
|
278
276
|
</body>`);
|
|
279
277
|
}
|
|
280
278
|
return html;
|
|
281
279
|
}
|
|
282
|
-
function
|
|
280
|
+
function getEmailOneSimpleValue(config, key, lang, translations, extraData = {}) {
|
|
281
|
+
const { product, name } = config.app;
|
|
282
|
+
const merged = {
|
|
283
|
+
...(translations.common[lang] ?? translations.common[0] ?? {}),
|
|
284
|
+
...(translations.byProduct[product]?.[lang] ?? translations.byProduct[product]?.[0] ?? {}),
|
|
285
|
+
...(name && name !== product ? (translations.byEnv[name]?.[lang] ?? translations.byEnv[name]?.[0] ?? {}) : {}),
|
|
286
|
+
};
|
|
283
287
|
const pattern = new RegExp('{{\\s*([a-zA-Z._0-9]+)\\s*}}', 'g');
|
|
284
|
-
|
|
285
|
-
|| projectSpecificTitles?.[config.app.product]?.[lang]?.[key]
|
|
286
|
-
|| defaultTitles?.[lang]?.[key];
|
|
287
|
-
return title?.replace(pattern, (_, placeholderKey) => extraData?.[placeholderKey] || config.app.emailConfig?.[placeholderKey] || placeholderKey);
|
|
288
|
+
return merged[key]?.replace(pattern, (_, placeholderKey) => extraData[placeholderKey] ?? placeholderKey);
|
|
288
289
|
}
|