anote-server-libs 0.11.2 → 0.11.4
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/BaseModelRepository.js +8 -4
- package/models/repository/BaseModelRepository.ts +4 -4
- package/models/repository/CryptModelDao.js +82 -0
- 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 +24 -24
- package/services/utils.ts +269 -269
- package/tsconfig.json +29 -29
package/services/utils.ts
CHANGED
|
@@ -1,269 +1,269 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import {NextFunction, Request, Response} from 'express';
|
|
3
|
-
import {Logger} from 'winston';
|
|
4
|
-
import {BaseModelRepository} from '../models/repository/BaseModelRepository';
|
|
5
|
-
|
|
6
|
-
export function atob(str: string): string {
|
|
7
|
-
return Buffer.from(str, 'base64').toString('binary');
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function btoa(str: string): string {
|
|
11
|
-
return Buffer.from(str).toString('base64');
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function clientErrorHandle(this: Logger, err: any, dbClient: any) {
|
|
15
|
-
this.error('SQL [Client %d] ERROR: %j', dbClient.processID, err);
|
|
16
|
-
}
|
|
17
|
-
export const utils: {[id: string]: any} = {
|
|
18
|
-
clientErrorHandler: undefined
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
function gcdTwo(a: number, b: number): number {
|
|
22
|
-
if(a === 0)
|
|
23
|
-
return b;
|
|
24
|
-
return gcdTwo(b % a, a);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function gcd(values: number[]): number {
|
|
28
|
-
let result = values[0];
|
|
29
|
-
for(let i = 1; i < values.length; i++)
|
|
30
|
-
result = gcdTwo(values[i], result);
|
|
31
|
-
return result;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function lcm(values: number[]): number {
|
|
35
|
-
let l = 1, divisor = 2;
|
|
36
|
-
while(true) {
|
|
37
|
-
let counter = 0;
|
|
38
|
-
let divisible = false;
|
|
39
|
-
for(let i = 0; i < values.length; i++) {
|
|
40
|
-
if(values[i] === 0) {
|
|
41
|
-
return 0;
|
|
42
|
-
} else if(values[i] < 0) {
|
|
43
|
-
values[i] = values[i] * (-1);
|
|
44
|
-
}
|
|
45
|
-
if(values[i] === 1) {
|
|
46
|
-
counter++;
|
|
47
|
-
}
|
|
48
|
-
if(values[i] % divisor === 0) {
|
|
49
|
-
divisible = true;
|
|
50
|
-
values[i] = values[i] / divisor;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
if(divisible) {
|
|
54
|
-
l = l * divisor;
|
|
55
|
-
} else {
|
|
56
|
-
divisor++;
|
|
57
|
-
}
|
|
58
|
-
if(counter === values.length) {
|
|
59
|
-
return l;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function jsonStringify(obj: any): string {
|
|
65
|
-
const cache: any = {};
|
|
66
|
-
return JSON.stringify(obj, function(_, value) {
|
|
67
|
-
if(typeof value === 'object' && value !== null) {
|
|
68
|
-
if(cache[value] !== -1) {
|
|
69
|
-
try {
|
|
70
|
-
return JSON.parse(JSON.stringify(value));
|
|
71
|
-
} catch(error) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
cache[value] = true;
|
|
76
|
-
}
|
|
77
|
-
return value;
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function idempotent(repo: BaseModelRepository, debug: boolean, logger: Logger) {
|
|
82
|
-
return function(req: Request, res: Response, next: NextFunction) {
|
|
83
|
-
let idempotenceKey = req.header('x-idempotent-key');
|
|
84
|
-
if(idempotenceKey) {
|
|
85
|
-
idempotenceKey = idempotenceKey.substring(0, 40);
|
|
86
|
-
repo.ApiCall.get(idempotenceKey).then(call => {
|
|
87
|
-
if(!call) {
|
|
88
|
-
const jsonTerminator = res.json;
|
|
89
|
-
const endTerminator = res.end;
|
|
90
|
-
const writeTerminator = res.write;
|
|
91
|
-
let response = '';
|
|
92
|
-
res.json = (function(obj: any) {
|
|
93
|
-
repo.ApiCall.create({
|
|
94
|
-
id: idempotenceKey,
|
|
95
|
-
responseCode: res.statusCode,
|
|
96
|
-
responseJson: jsonStringify(obj),
|
|
97
|
-
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000)
|
|
98
|
-
}).then(() => jsonTerminator.call(res, obj), err => {
|
|
99
|
-
if(err) logger.warn('Cannot save idempotent key: %j', err);
|
|
100
|
-
jsonTerminator.call(res, obj);
|
|
101
|
-
});
|
|
102
|
-
}).bind(res);
|
|
103
|
-
res.end = (function(buf: string) {
|
|
104
|
-
if(buf) response = response + buf.toString();
|
|
105
|
-
repo.ApiCall.create({
|
|
106
|
-
id: idempotenceKey,
|
|
107
|
-
responseCode: res.statusCode,
|
|
108
|
-
responseJson: response,
|
|
109
|
-
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000)
|
|
110
|
-
}).then(() => endTerminator.call(res, buf), err => {
|
|
111
|
-
if(err) logger.warn('Cannot save idempotent key: %j', err);
|
|
112
|
-
endTerminator.call(res, buf);
|
|
113
|
-
});
|
|
114
|
-
}).bind(res);
|
|
115
|
-
res.write = (function(buf: string) {
|
|
116
|
-
if(buf) response = response + buf.toString();
|
|
117
|
-
writeTerminator.call(res, buf);
|
|
118
|
-
}).bind(res);
|
|
119
|
-
next();
|
|
120
|
-
} else res.status(417).json({
|
|
121
|
-
responseCode: call.responseCode,
|
|
122
|
-
responseBody: call.responseJson
|
|
123
|
-
});
|
|
124
|
-
}, err => res.status(500).json({
|
|
125
|
-
error: {
|
|
126
|
-
errorKey: 'internal.db',
|
|
127
|
-
additionalInfo: {message: err.message, stack: debug && err.stack}
|
|
128
|
-
}
|
|
129
|
-
}));
|
|
130
|
-
} else next();
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export function sendSelfPostableMessage(res: Response, code: number, messageType: string, err?: any) {
|
|
135
|
-
res.type('text/html').status(code).write(`
|
|
136
|
-
<!DOCTYPE HTML>
|
|
137
|
-
<html>
|
|
138
|
-
<head>
|
|
139
|
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
|
140
|
-
</head>
|
|
141
|
-
<body>
|
|
142
|
-
<script type="text/javascript">
|
|
143
|
-
window.parent.postMessage({
|
|
144
|
-
type: '${messageType}',
|
|
145
|
-
confirm: ${!err},
|
|
146
|
-
error: JSON.parse('${JSON.stringify(err) || 'null'}')
|
|
147
|
-
}, '*');
|
|
148
|
-
</script>
|
|
149
|
-
</body>
|
|
150
|
-
</html>
|
|
151
|
-
`);
|
|
152
|
-
res.end();
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export function fpEuros(n: number) {
|
|
156
|
-
return Math.round(n * 100) / 100;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export function digitize(value: number | string, opts?: {[key: string]: any}): string {
|
|
160
|
-
if(value === undefined || value === null) return 'undefined';
|
|
161
|
-
if(typeof value === 'number') {
|
|
162
|
-
if(isNaN(value)) return '-';
|
|
163
|
-
if(!isFinite(value)) return 'Infinite';
|
|
164
|
-
value = String(value);
|
|
165
|
-
} else if(typeof value === 'string') return value;
|
|
166
|
-
|
|
167
|
-
const parts = value.split('.');
|
|
168
|
-
const initialLength = parts[0].length;
|
|
169
|
-
for(let i = initialLength - 3; i > 0; i -= 3) {
|
|
170
|
-
parts[0] = parts[0].slice(0, i) + ',' + parts[0].slice(i);
|
|
171
|
-
}
|
|
172
|
-
if(parts[0].startsWith('-,'))
|
|
173
|
-
parts[0] = '-' + parts[0].slice(2);
|
|
174
|
-
if(parts[1]) {
|
|
175
|
-
const expDecimals = parts[1].split(/[eE]-/);
|
|
176
|
-
if (expDecimals.length > 1 && parseInt(expDecimals[1], 10) > 2) return '0.00';
|
|
177
|
-
let decimals = fpEuros(parseFloat('0.' + parts[1])).toString().substr(2, 2);
|
|
178
|
-
if (decimals.length === 1) decimals += '0';
|
|
179
|
-
return parts[0] + '.' + decimals;
|
|
180
|
-
}
|
|
181
|
-
return (opts && opts.currency)? (parts[0] + '.00') : parts[0];
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function loadEmailTemplates(path: string): Record<string, string> | undefined {
|
|
185
|
-
if(!path || !fs.existsSync(path)) return undefined;
|
|
186
|
-
const loadedEmailTemplates: {[id: string]: string} = {};
|
|
187
|
-
const fileNames = fs.readdirSync(path);
|
|
188
|
-
fileNames.forEach(fileName => {
|
|
189
|
-
if(!fileName.startsWith('template-') || !fileName.endsWith('.html')) return;
|
|
190
|
-
const templateIdentifier = fileName.slice('template-'.length, -'.html'.length);
|
|
191
|
-
loadedEmailTemplates[templateIdentifier] = fs.readFileSync(path + '/' + fileName).toString('utf8');
|
|
192
|
-
});
|
|
193
|
-
return loadedEmailTemplates;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
export function readEmailTemplates(path: string, config: Record<string, any>): {[id: string]: string} | undefined {
|
|
197
|
-
const {product, name} = config.app;
|
|
198
|
-
if(!product || !name) return loadEmailTemplates(path);
|
|
199
|
-
const mainPath = path + '/' + product;
|
|
200
|
-
const overridePath = product !== name ? path + `/${product}/${name}` : undefined;
|
|
201
|
-
const defaultTemplates = loadEmailTemplates(mainPath);
|
|
202
|
-
const overrideTemplates = overridePath ? loadEmailTemplates(overridePath) : {};
|
|
203
|
-
return {...defaultTemplates, ...overrideTemplates};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export function replacePlaceholders(data: Record<string, any>, keys: string[], depth: number): any {
|
|
207
|
-
if(!keys || depth >= keys.length) return undefined;
|
|
208
|
-
if(depth === keys.length - 1) return (data || {})[keys[depth]];
|
|
209
|
-
return replacePlaceholders((data || {})[keys[depth]], keys, depth + 1);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function getHtml(template: string, translationKeyValue: Record<string, any>): string | undefined {
|
|
213
|
-
if(!template) return undefined;
|
|
214
|
-
const pattern = new RegExp('{{\\s*([a-zA-Z._0-9]+)\\s*}}', 'g');
|
|
215
|
-
const replacer = (_: string, key: string) => replacePlaceholders(translationKeyValue, key.split('.'), 0) ?? '';
|
|
216
|
-
template = template.replace(pattern, replacer);
|
|
217
|
-
template = template.replace(pattern, replacer);
|
|
218
|
-
template = template.replace(new RegExp('\\[\\[\\s*([a-zA-Z._0-9:]+)\\s*\\]\\]', 'g'), (_, key) => {
|
|
219
|
-
const keyParts = key.split(':');
|
|
220
|
-
return (replacePlaceholders(translationKeyValue, keyParts[1].split('.'), 0) || [])
|
|
221
|
-
.map((subContext: any) => getHtml(keyParts[0], subContext)).join('');
|
|
222
|
-
});
|
|
223
|
-
template = template.replace(new RegExp('<<\\s*([a-zA-Z._0-9:]+)\\s*>>', 'g'), (_, key) => {
|
|
224
|
-
const keyParts = key.split(':');
|
|
225
|
-
if(replacePlaceholders(translationKeyValue, keyParts[1].split('.'), 0))
|
|
226
|
-
return getHtml(keyParts[0], translationKeyValue);
|
|
227
|
-
return '';
|
|
228
|
-
});
|
|
229
|
-
return template;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export function getHtmlReplaced(config: Record<string, any>, templates: Record<string, string>, key: string, lang: number, defaultTemplateTranslations: Record<number, any> = {}, envSpecificTranslations?: Record<string, any>, extraData?: Record<string, any>, newPage?: boolean): string {
|
|
233
|
-
const name = config.app.name;
|
|
234
|
-
const defaultTranslations = defaultTemplateTranslations?.[lang] || defaultTemplateTranslations?.[0] || {};
|
|
235
|
-
const envSpecificTrans = envSpecificTranslations?.[name]?.[key]?.[lang]
|
|
236
|
-
|| envSpecificTranslations?.[name]?.[key]?.[0]
|
|
237
|
-
|| {};
|
|
238
|
-
const translationKeyValue = {
|
|
239
|
-
...defaultTranslations,
|
|
240
|
-
...envSpecificTrans,
|
|
241
|
-
...extraData,
|
|
242
|
-
domain: config.frontend,
|
|
243
|
-
...config.app.emailConfig
|
|
244
|
-
};
|
|
245
|
-
let html = getHtml(templates[key], translationKeyValue);
|
|
246
|
-
if(newPage) {
|
|
247
|
-
html = html.replace('</head>', `<style>
|
|
248
|
-
@media print {
|
|
249
|
-
.new-page {
|
|
250
|
-
page-break-before: always;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
</style>
|
|
254
|
-
</head>`);
|
|
255
|
-
html = html.replace('</body>', `<p class="new-page">Provided by Satoris</p>
|
|
256
|
-
</body>`);
|
|
257
|
-
}
|
|
258
|
-
return html;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
export function getEmailTitle(key: string, lang: number, titles: Record<number, any>, overrideTitles?: Record<number, any>, data?: Record<string, string>): string | undefined {
|
|
262
|
-
const pattern = new RegExp('{{\\s*([a-zA-Z._0-9]+)\\s*}}', 'g');
|
|
263
|
-
const title = overrideTitles?.[lang]?.[key] ?? titles[lang]?.[key];
|
|
264
|
-
return title?.replace(pattern, (_: string, placeholderKey: string) => data?.[placeholderKey]);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export function getEmailTranslation(key: string, lang: number, translationKeyValue: Record<number, any>, defaultLang: number = 0): string | undefined {
|
|
268
|
-
return translationKeyValue?.[lang]?.[key] ?? translationKeyValue?.[defaultLang]?.[key];
|
|
269
|
-
}
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import {NextFunction, Request, Response} from 'express';
|
|
3
|
+
import {Logger} from 'winston';
|
|
4
|
+
import {BaseModelRepository} from '../models/repository/BaseModelRepository';
|
|
5
|
+
|
|
6
|
+
export function atob(str: string): string {
|
|
7
|
+
return Buffer.from(str, 'base64').toString('binary');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function btoa(str: string): string {
|
|
11
|
+
return Buffer.from(str).toString('base64');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function clientErrorHandle(this: Logger, err: any, dbClient: any) {
|
|
15
|
+
this.error('SQL [Client %d] ERROR: %j', dbClient.processID, err);
|
|
16
|
+
}
|
|
17
|
+
export const utils: {[id: string]: any} = {
|
|
18
|
+
clientErrorHandler: undefined
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function gcdTwo(a: number, b: number): number {
|
|
22
|
+
if(a === 0)
|
|
23
|
+
return b;
|
|
24
|
+
return gcdTwo(b % a, a);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function gcd(values: number[]): number {
|
|
28
|
+
let result = values[0];
|
|
29
|
+
for(let i = 1; i < values.length; i++)
|
|
30
|
+
result = gcdTwo(values[i], result);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function lcm(values: number[]): number {
|
|
35
|
+
let l = 1, divisor = 2;
|
|
36
|
+
while(true) {
|
|
37
|
+
let counter = 0;
|
|
38
|
+
let divisible = false;
|
|
39
|
+
for(let i = 0; i < values.length; i++) {
|
|
40
|
+
if(values[i] === 0) {
|
|
41
|
+
return 0;
|
|
42
|
+
} else if(values[i] < 0) {
|
|
43
|
+
values[i] = values[i] * (-1);
|
|
44
|
+
}
|
|
45
|
+
if(values[i] === 1) {
|
|
46
|
+
counter++;
|
|
47
|
+
}
|
|
48
|
+
if(values[i] % divisor === 0) {
|
|
49
|
+
divisible = true;
|
|
50
|
+
values[i] = values[i] / divisor;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if(divisible) {
|
|
54
|
+
l = l * divisor;
|
|
55
|
+
} else {
|
|
56
|
+
divisor++;
|
|
57
|
+
}
|
|
58
|
+
if(counter === values.length) {
|
|
59
|
+
return l;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function jsonStringify(obj: any): string {
|
|
65
|
+
const cache: any = {};
|
|
66
|
+
return JSON.stringify(obj, function(_, value) {
|
|
67
|
+
if(typeof value === 'object' && value !== null) {
|
|
68
|
+
if(cache[value] !== -1) {
|
|
69
|
+
try {
|
|
70
|
+
return JSON.parse(JSON.stringify(value));
|
|
71
|
+
} catch(error) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
cache[value] = true;
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function idempotent(repo: BaseModelRepository, debug: boolean, logger: Logger) {
|
|
82
|
+
return function(req: Request, res: Response, next: NextFunction) {
|
|
83
|
+
let idempotenceKey = req.header('x-idempotent-key');
|
|
84
|
+
if(idempotenceKey) {
|
|
85
|
+
idempotenceKey = idempotenceKey.substring(0, 40);
|
|
86
|
+
repo.ApiCall.get(idempotenceKey).then(call => {
|
|
87
|
+
if(!call) {
|
|
88
|
+
const jsonTerminator = res.json;
|
|
89
|
+
const endTerminator = res.end;
|
|
90
|
+
const writeTerminator = res.write;
|
|
91
|
+
let response = '';
|
|
92
|
+
res.json = (function(obj: any) {
|
|
93
|
+
repo.ApiCall.create({
|
|
94
|
+
id: idempotenceKey,
|
|
95
|
+
responseCode: res.statusCode,
|
|
96
|
+
responseJson: jsonStringify(obj),
|
|
97
|
+
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000)
|
|
98
|
+
}).then(() => jsonTerminator.call(res, obj), err => {
|
|
99
|
+
if(err) logger.warn('Cannot save idempotent key: %j', err);
|
|
100
|
+
jsonTerminator.call(res, obj);
|
|
101
|
+
});
|
|
102
|
+
}).bind(res);
|
|
103
|
+
res.end = (function(buf: string) {
|
|
104
|
+
if(buf) response = response + buf.toString();
|
|
105
|
+
repo.ApiCall.create({
|
|
106
|
+
id: idempotenceKey,
|
|
107
|
+
responseCode: res.statusCode,
|
|
108
|
+
responseJson: response,
|
|
109
|
+
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000)
|
|
110
|
+
}).then(() => endTerminator.call(res, buf), err => {
|
|
111
|
+
if(err) logger.warn('Cannot save idempotent key: %j', err);
|
|
112
|
+
endTerminator.call(res, buf);
|
|
113
|
+
});
|
|
114
|
+
}).bind(res);
|
|
115
|
+
res.write = (function(buf: string) {
|
|
116
|
+
if(buf) response = response + buf.toString();
|
|
117
|
+
writeTerminator.call(res, buf);
|
|
118
|
+
}).bind(res);
|
|
119
|
+
next();
|
|
120
|
+
} else res.status(417).json({
|
|
121
|
+
responseCode: call.responseCode,
|
|
122
|
+
responseBody: call.responseJson
|
|
123
|
+
});
|
|
124
|
+
}, err => res.status(500).json({
|
|
125
|
+
error: {
|
|
126
|
+
errorKey: 'internal.db',
|
|
127
|
+
additionalInfo: {message: err.message, stack: debug && err.stack}
|
|
128
|
+
}
|
|
129
|
+
}));
|
|
130
|
+
} else next();
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function sendSelfPostableMessage(res: Response, code: number, messageType: string, err?: any) {
|
|
135
|
+
res.type('text/html').status(code).write(`
|
|
136
|
+
<!DOCTYPE HTML>
|
|
137
|
+
<html>
|
|
138
|
+
<head>
|
|
139
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
|
140
|
+
</head>
|
|
141
|
+
<body>
|
|
142
|
+
<script type="text/javascript">
|
|
143
|
+
window.parent.postMessage({
|
|
144
|
+
type: '${messageType}',
|
|
145
|
+
confirm: ${!err},
|
|
146
|
+
error: JSON.parse('${JSON.stringify(err) || 'null'}')
|
|
147
|
+
}, '*');
|
|
148
|
+
</script>
|
|
149
|
+
</body>
|
|
150
|
+
</html>
|
|
151
|
+
`);
|
|
152
|
+
res.end();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function fpEuros(n: number) {
|
|
156
|
+
return Math.round(n * 100) / 100;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function digitize(value: number | string, opts?: {[key: string]: any}): string {
|
|
160
|
+
if(value === undefined || value === null) return 'undefined';
|
|
161
|
+
if(typeof value === 'number') {
|
|
162
|
+
if(isNaN(value)) return '-';
|
|
163
|
+
if(!isFinite(value)) return 'Infinite';
|
|
164
|
+
value = String(value);
|
|
165
|
+
} else if(typeof value === 'string') return value;
|
|
166
|
+
|
|
167
|
+
const parts = value.split('.');
|
|
168
|
+
const initialLength = parts[0].length;
|
|
169
|
+
for(let i = initialLength - 3; i > 0; i -= 3) {
|
|
170
|
+
parts[0] = parts[0].slice(0, i) + ',' + parts[0].slice(i);
|
|
171
|
+
}
|
|
172
|
+
if(parts[0].startsWith('-,'))
|
|
173
|
+
parts[0] = '-' + parts[0].slice(2);
|
|
174
|
+
if(parts[1]) {
|
|
175
|
+
const expDecimals = parts[1].split(/[eE]-/);
|
|
176
|
+
if (expDecimals.length > 1 && parseInt(expDecimals[1], 10) > 2) return '0.00';
|
|
177
|
+
let decimals = fpEuros(parseFloat('0.' + parts[1])).toString().substr(2, 2);
|
|
178
|
+
if (decimals.length === 1) decimals += '0';
|
|
179
|
+
return parts[0] + '.' + decimals;
|
|
180
|
+
}
|
|
181
|
+
return (opts && opts.currency)? (parts[0] + '.00') : parts[0];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function loadEmailTemplates(path: string): Record<string, string> | undefined {
|
|
185
|
+
if(!path || !fs.existsSync(path)) return undefined;
|
|
186
|
+
const loadedEmailTemplates: {[id: string]: string} = {};
|
|
187
|
+
const fileNames = fs.readdirSync(path);
|
|
188
|
+
fileNames.forEach(fileName => {
|
|
189
|
+
if(!fileName.startsWith('template-') || !fileName.endsWith('.html')) return;
|
|
190
|
+
const templateIdentifier = fileName.slice('template-'.length, -'.html'.length);
|
|
191
|
+
loadedEmailTemplates[templateIdentifier] = fs.readFileSync(path + '/' + fileName).toString('utf8');
|
|
192
|
+
});
|
|
193
|
+
return loadedEmailTemplates;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function readEmailTemplates(path: string, config: Record<string, any>): {[id: string]: string} | undefined {
|
|
197
|
+
const {product, name} = config.app;
|
|
198
|
+
if(!product || !name) return loadEmailTemplates(path);
|
|
199
|
+
const mainPath = path + '/' + product;
|
|
200
|
+
const overridePath = product !== name ? path + `/${product}/${name}` : undefined;
|
|
201
|
+
const defaultTemplates = loadEmailTemplates(mainPath);
|
|
202
|
+
const overrideTemplates = overridePath ? loadEmailTemplates(overridePath) : {};
|
|
203
|
+
return {...defaultTemplates, ...overrideTemplates};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function replacePlaceholders(data: Record<string, any>, keys: string[], depth: number): any {
|
|
207
|
+
if(!keys || depth >= keys.length) return undefined;
|
|
208
|
+
if(depth === keys.length - 1) return (data || {})[keys[depth]];
|
|
209
|
+
return replacePlaceholders((data || {})[keys[depth]], keys, depth + 1);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function getHtml(template: string, translationKeyValue: Record<string, any>): string | undefined {
|
|
213
|
+
if(!template) return undefined;
|
|
214
|
+
const pattern = new RegExp('{{\\s*([a-zA-Z._0-9]+)\\s*}}', 'g');
|
|
215
|
+
const replacer = (_: string, key: string) => replacePlaceholders(translationKeyValue, key.split('.'), 0) ?? '';
|
|
216
|
+
template = template.replace(pattern, replacer);
|
|
217
|
+
template = template.replace(pattern, replacer);
|
|
218
|
+
template = template.replace(new RegExp('\\[\\[\\s*([a-zA-Z._0-9:]+)\\s*\\]\\]', 'g'), (_, key) => {
|
|
219
|
+
const keyParts = key.split(':');
|
|
220
|
+
return (replacePlaceholders(translationKeyValue, keyParts[1].split('.'), 0) || [])
|
|
221
|
+
.map((subContext: any) => getHtml(keyParts[0], subContext)).join('');
|
|
222
|
+
});
|
|
223
|
+
template = template.replace(new RegExp('<<\\s*([a-zA-Z._0-9:]+)\\s*>>', 'g'), (_, key) => {
|
|
224
|
+
const keyParts = key.split(':');
|
|
225
|
+
if(replacePlaceholders(translationKeyValue, keyParts[1].split('.'), 0))
|
|
226
|
+
return getHtml(keyParts[0], translationKeyValue);
|
|
227
|
+
return '';
|
|
228
|
+
});
|
|
229
|
+
return template;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function getHtmlReplaced(config: Record<string, any>, templates: Record<string, string>, key: string, lang: number, defaultTemplateTranslations: Record<number, any> = {}, envSpecificTranslations?: Record<string, any>, extraData?: Record<string, any>, newPage?: boolean): string {
|
|
233
|
+
const name = config.app.name;
|
|
234
|
+
const defaultTranslations = defaultTemplateTranslations?.[lang] || defaultTemplateTranslations?.[0] || {};
|
|
235
|
+
const envSpecificTrans = envSpecificTranslations?.[name]?.[key]?.[lang]
|
|
236
|
+
|| envSpecificTranslations?.[name]?.[key]?.[0]
|
|
237
|
+
|| {};
|
|
238
|
+
const translationKeyValue = {
|
|
239
|
+
...defaultTranslations,
|
|
240
|
+
...envSpecificTrans,
|
|
241
|
+
...extraData,
|
|
242
|
+
domain: config.frontend,
|
|
243
|
+
...config.app.emailConfig
|
|
244
|
+
};
|
|
245
|
+
let html = getHtml(templates[key], translationKeyValue);
|
|
246
|
+
if(newPage) {
|
|
247
|
+
html = html.replace('</head>', `<style>
|
|
248
|
+
@media print {
|
|
249
|
+
.new-page {
|
|
250
|
+
page-break-before: always;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
</style>
|
|
254
|
+
</head>`);
|
|
255
|
+
html = html.replace('</body>', `<p class="new-page">Provided by Satoris</p>
|
|
256
|
+
</body>`);
|
|
257
|
+
}
|
|
258
|
+
return html;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function getEmailTitle(key: string, lang: number, titles: Record<number, any>, overrideTitles?: Record<number, any>, data?: Record<string, string>): string | undefined {
|
|
262
|
+
const pattern = new RegExp('{{\\s*([a-zA-Z._0-9]+)\\s*}}', 'g');
|
|
263
|
+
const title = overrideTitles?.[lang]?.[key] ?? titles[lang]?.[key];
|
|
264
|
+
return title?.replace(pattern, (_: string, placeholderKey: string) => data?.[placeholderKey]);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function getEmailTranslation(key: string, lang: number, translationKeyValue: Record<number, any>, defaultLang: number = 0): string | undefined {
|
|
268
|
+
return translationKeyValue?.[lang]?.[key] ?? translationKeyValue?.[defaultLang]?.[key];
|
|
269
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"allowUnusedLabels": false,
|
|
4
|
-
"emitDecoratorMetadata": true,
|
|
5
|
-
"experimentalDecorators": true,
|
|
6
|
-
"lib": ["es2015", "dom"],
|
|
7
|
-
"module": "commonjs",
|
|
8
|
-
"moduleResolution": "node",
|
|
9
|
-
"noImplicitAny": true,
|
|
10
|
-
"noImplicitReturns": true,
|
|
11
|
-
"noUnusedLocals": true,
|
|
12
|
-
"noUnusedParameters": true,
|
|
13
|
-
"removeComments": true,
|
|
14
|
-
"sourceMap": false,
|
|
15
|
-
"skipDefaultLibCheck": true,
|
|
16
|
-
"skipLibCheck": true,
|
|
17
|
-
"target": "es2021",
|
|
18
|
-
"typeRoots": [
|
|
19
|
-
"node_modules/@types"
|
|
20
|
-
]
|
|
21
|
-
},
|
|
22
|
-
"exclude": [
|
|
23
|
-
"node_modules",
|
|
24
|
-
"dist",
|
|
25
|
-
"**/*.spec.ts",
|
|
26
|
-
"example",
|
|
27
|
-
"tests"
|
|
28
|
-
]
|
|
29
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowUnusedLabels": false,
|
|
4
|
+
"emitDecoratorMetadata": true,
|
|
5
|
+
"experimentalDecorators": true,
|
|
6
|
+
"lib": ["es2015", "dom"],
|
|
7
|
+
"module": "commonjs",
|
|
8
|
+
"moduleResolution": "node",
|
|
9
|
+
"noImplicitAny": true,
|
|
10
|
+
"noImplicitReturns": true,
|
|
11
|
+
"noUnusedLocals": true,
|
|
12
|
+
"noUnusedParameters": true,
|
|
13
|
+
"removeComments": true,
|
|
14
|
+
"sourceMap": false,
|
|
15
|
+
"skipDefaultLibCheck": true,
|
|
16
|
+
"skipLibCheck": true,
|
|
17
|
+
"target": "es2021",
|
|
18
|
+
"typeRoots": [
|
|
19
|
+
"node_modules/@types"
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
"exclude": [
|
|
23
|
+
"node_modules",
|
|
24
|
+
"dist",
|
|
25
|
+
"**/*.spec.ts",
|
|
26
|
+
"example",
|
|
27
|
+
"tests"
|
|
28
|
+
]
|
|
29
|
+
}
|