@opengis/fastify-table 1.0.94 → 1.0.96

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/Changelog.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # fastify-table
2
2
 
3
+ ## 1.0.96 - 12.09.2024
4
+
5
+ - minor refactor of email notifications
6
+
7
+ ## 1.0.95 - 12.09.2024
8
+
9
+ - add dbname to getFolder func
10
+
3
11
  ## 1.0.94 - 11.09.2024
4
12
 
5
13
  - fix getMeta (public schema)
package/index.js CHANGED
@@ -43,7 +43,7 @@ async function plugin(fastify, opt) {
43
43
  fastify.decorate('getFolder', (req, type = 'server') => {
44
44
  if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
45
45
  const types = { local: req.root || config.root, server: req.mapServerRoot || config.mapServerRoot };
46
- const filepath = path.posix.join(types[type] || '/data/local', req.folder || config.folder || '');
46
+ const filepath = path.posix.join(types[type] || `/data/local/${req.pg?.options?.database || ''}`, req.folder || config.folder || '');
47
47
  return filepath;
48
48
  });
49
49
 
@@ -1,111 +1,111 @@
1
- // eslint-disable-next-line max-len, no-control-regex
2
- const emailReg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g;
3
-
4
- // eslint-disable-next-line import/no-relative-packages
5
- // import downloadFile from '../../../fastify-file/file/funcs/downloadFile.js'; // test only
6
- import pgClients from '../../pg/pgClients.js';
7
- import sendEmail from './utils/sendEmail.js';
8
- import getTemplate from '../../table/controllers/utils/getTemplate.js';
9
- import getRedis from '../../redis/funcs/getRedis.js';
10
-
11
- async function generateNotificationContent({
12
- pg, funcs, table, template, id, message, data: data1,
13
- }) {
14
- if (template) {
15
- const data = table && id ? await pg.one(`select * from ${table} where ${pg.pk[table]}=$1`, [id]) : data1;
16
- // console.log(data);
17
- const body = await getTemplate('pt', template);
18
- // console.log(body);
19
- const html = funcs.handlebars.compile(body || 'template not found')(data || {});
20
- // console.log(html);
21
- return html;
22
- }
23
- return message;
24
- }
25
-
26
- export default async function notification({
27
- pg: pg1,
28
- funcs,
29
- log,
30
- provider = ['email'],
31
- from,
32
- to,
33
- template,
34
- table,
35
- message,
36
- title,
37
- file,
38
- data,
39
- id,
40
- nocache,
41
- }) {
42
- const rclient = getRedis();
43
- const pg = pg1 || pgClients.client;
44
-
45
- if (!pg) throw new Error('need pg');
46
-
47
- if (pg?.readonly) {
48
- return null;
49
- }
50
-
51
- const keyTo = `${pg.options?.database}:mail:${provider[0]}:${to || ''}${id || ''}${table || ''}${title || ''}`;
52
- const uniqueTo = await rclient.setnx(keyTo, 1);
53
- log.info('notification/sent', { keyTo, send: uniqueTo, nocache });
54
-
55
- if (!uniqueTo && !nocache) {
56
- return `already sent: ${keyTo}`;
57
- }
58
- await rclient.expire(keyTo, 1000 * 600);
59
-
60
- if (!to.length) {
61
- return null;
62
- }
63
-
64
- if (!(Array.isArray(provider) && provider.length)) {
65
- return 'notification provider - must be array and not empty';
66
- }
67
-
68
- try {
69
- const html = await generateNotificationContent({
70
- pg, funcs, table, template, id, message, data,
71
- });
72
-
73
- // return html;
74
-
75
- if (provider.includes('email')) {
76
- const files = Array.isArray(file) ? file : [file];
77
- const attachments = files?.length ? await Promise.all(files?.filter((el) => el).map(async (el) => {
78
- const content = await funcs.downloadFile(el, { funcs, buffer: true }); // ?
79
- return {
80
- filename: el.split('/').pop(),
81
- encoding: 'base64',
82
- content,
83
- };
84
- })) : [];
85
-
86
- const toEmail = Array.isArray(to) ? to.map((emails) => emails.match(emailReg)?.join(',')) : to;
87
-
88
- const result = await sendEmail({
89
- funcs,
90
- attachments,
91
- html,
92
- subject: title,
93
- from,
94
- to: toEmail,
95
- });
96
-
97
- log.info('notification', {
98
- provider, title, to: toEmail, from, result, len: message?.length, files,
99
- });
100
-
101
- return result;
102
- }
103
- return null;
104
- }
105
- catch (err) {
106
- log.info('notification/error', {
107
- provider, from, to, err: err.toString(),
108
- });
109
- throw err;
110
- }
111
- }
1
+ // eslint-disable-next-line max-len, no-control-regex
2
+ const emailReg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g;
3
+
4
+ // eslint-disable-next-line import/no-relative-packages
5
+ // import downloadFile from '../../../fastify-file/file/funcs/downloadFile.js'; // test only
6
+ import pgClients from '../../pg/pgClients.js';
7
+ import sendEmail from './utils/sendEmail.js';
8
+ import getTemplate from '../../table/controllers/utils/getTemplate.js';
9
+ import getRedis from '../../redis/funcs/getRedis.js';
10
+
11
+ async function generateNotificationContent({
12
+ pg, funcs, table, template, id, message, data: data1,
13
+ }) {
14
+ if (template) {
15
+ const data = table && id ? await pg.one(`select * from ${table} where ${pg.pk[table]}=$1`, [id]) : data1;
16
+ // console.log(data);
17
+ const body = await getTemplate('pt', template);
18
+ // console.log(body);
19
+ const html = funcs.handlebars.compile(body || template || 'template not found')(data || {});
20
+ // console.log(html);
21
+ return html;
22
+ }
23
+ return message;
24
+ }
25
+
26
+ export default async function notification({
27
+ pg: pg1,
28
+ funcs,
29
+ log,
30
+ provider = ['email'],
31
+ from,
32
+ to,
33
+ template,
34
+ table,
35
+ message,
36
+ title,
37
+ file,
38
+ data,
39
+ id,
40
+ nocache,
41
+ }) {
42
+ const rclient = getRedis();
43
+ const pg = pg1 || pgClients.client;
44
+
45
+ if (!pg) throw new Error('need pg');
46
+
47
+ if (pg?.readonly) {
48
+ return null;
49
+ }
50
+
51
+ const keyTo = `${pg.options?.database}:mail:${provider[0]}:${to || ''}${id || ''}${table || ''}${title || ''}`;
52
+ const uniqueTo = await rclient.setnx(keyTo, 1);
53
+ log.info('notification/sent', { keyTo, send: uniqueTo, nocache });
54
+
55
+ if (!uniqueTo && !nocache) {
56
+ return `already sent: ${keyTo}`;
57
+ }
58
+ await rclient.expire(keyTo, 1000 * 600);
59
+
60
+ if (!to.length) {
61
+ return null;
62
+ }
63
+
64
+ if (!(Array.isArray(provider) && provider.length)) {
65
+ return 'notification provider - must be array and not empty';
66
+ }
67
+
68
+ try {
69
+ const html = await generateNotificationContent({
70
+ pg, funcs, table, template, id, message, data,
71
+ });
72
+
73
+ // return html;
74
+
75
+ if (provider.includes('email')) {
76
+ const files = Array.isArray(file) ? file : [file];
77
+ const attachments = files?.length ? await Promise.all(files?.filter((el) => el).map(async (el) => {
78
+ const content = await funcs.downloadFile(el, { funcs, buffer: true }); // ?
79
+ return {
80
+ filename: el.split('/').pop(),
81
+ encoding: 'base64',
82
+ content,
83
+ };
84
+ })) : [];
85
+
86
+ const toEmail = Array.isArray(to) ? to.map((emails) => emails.match(emailReg)?.join(',')) : to;
87
+
88
+ const result = await sendEmail({
89
+ funcs,
90
+ attachments,
91
+ html,
92
+ subject: title,
93
+ from,
94
+ to: toEmail,
95
+ });
96
+
97
+ log.info('notification', {
98
+ provider, title, to: toEmail, from, result, len: message?.length, files,
99
+ });
100
+
101
+ return result;
102
+ }
103
+ return null;
104
+ }
105
+ catch (err) {
106
+ log.info('notification/error', {
107
+ provider, from, to, err: err.toString(),
108
+ });
109
+ throw err;
110
+ }
111
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.94",
3
+ "version": "1.0.96",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",