@opengis/fastify-table 1.0.74 → 1.0.75

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.
Files changed (96) hide show
  1. package/.eslintrc.cjs +42 -42
  2. package/Changelog.md +233 -229
  3. package/README.md +26 -26
  4. package/config.js +10 -10
  5. package/cron/controllers/cronApi.js +22 -22
  6. package/cron/controllers/utils/cronList.js +1 -1
  7. package/cron/funcs/addCron.js +131 -131
  8. package/cron/index.js +10 -10
  9. package/crud/controllers/deleteCrud.js +19 -19
  10. package/crud/controllers/insert.js +54 -54
  11. package/crud/controllers/update.js +59 -59
  12. package/crud/controllers/utils/checkXSS.js +45 -45
  13. package/crud/controllers/utils/xssInjection.js +72 -72
  14. package/crud/funcs/dataDelete.js +15 -15
  15. package/crud/funcs/dataInsert.js +24 -24
  16. package/crud/funcs/dataUpdate.js +24 -24
  17. package/crud/funcs/getAccess.js +53 -52
  18. package/crud/funcs/getOpt.js +10 -10
  19. package/crud/funcs/getToken.js +27 -27
  20. package/crud/funcs/isFileExists.js +13 -13
  21. package/crud/funcs/setOpt.js +16 -16
  22. package/crud/funcs/setToken.js +53 -53
  23. package/crud/index.js +36 -36
  24. package/helper.js +28 -28
  25. package/index.js +97 -97
  26. package/migration/exec.migrations.js +75 -75
  27. package/notification/controllers/testEmail.js +49 -49
  28. package/notification/controllers/userNotifications.js +19 -19
  29. package/notification/funcs/addNotification.js +8 -8
  30. package/notification/funcs/sendNotification.js +111 -111
  31. package/notification/funcs/utils/sendEmail.js +39 -39
  32. package/notification/index.js +38 -38
  33. package/package.json +26 -26
  34. package/pg/funcs/getPG.js +30 -30
  35. package/pg/pgClients.js +20 -20
  36. package/policy/funcs/checkPolicy.js +82 -82
  37. package/policy/funcs/sqlInjection.js +33 -33
  38. package/policy/index.js +14 -14
  39. package/redis/client.js +8 -8
  40. package/redis/funcs/getRedis.js +23 -23
  41. package/redis/funcs/redisClients.js +2 -2
  42. package/redis/index.js +19 -19
  43. package/server/migrations/0.sql +64 -13
  44. package/server/migrations/crm.sql +150 -150
  45. package/server/migrations/log.sql +43 -43
  46. package/server/templates/form/test.dataset.form.json +411 -411
  47. package/server/templates/select/test.storage.data.json +2 -2
  48. package/server/templates/table/test.dataset.table.json +24 -24
  49. package/server/templates/table/test.gis.map.table.json +44 -44
  50. package/server.js +14 -14
  51. package/table/controllers/card.js +44 -44
  52. package/table/controllers/data.js +95 -95
  53. package/table/controllers/filter.js +37 -37
  54. package/table/controllers/form.js +28 -28
  55. package/table/controllers/search.js +72 -72
  56. package/table/controllers/suggest.js +62 -62
  57. package/table/controllers/table.js +44 -44
  58. package/table/controllers/utils/getSelect.js +20 -20
  59. package/table/controllers/utils/getSelectMeta.js +66 -66
  60. package/table/controllers/utils/getTemplate.js +28 -28
  61. package/table/controllers/utils/getTemplates.js +18 -18
  62. package/table/controllers/utils/gisIRColumn.js +68 -68
  63. package/table/funcs/getFilterSQL/index.js +75 -75
  64. package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
  65. package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  66. package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
  67. package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  68. package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  69. package/table/funcs/metaFormat/getSelectVal.js +20 -20
  70. package/table/funcs/metaFormat/index.js +27 -27
  71. package/table/index.js +78 -78
  72. package/test/api/crud.test.js +88 -81
  73. package/test/api/crud.xss.test.js +72 -72
  74. package/test/api/notification.test.js +37 -37
  75. package/test/api/table.test.js +57 -57
  76. package/test/api/widget.test.js +114 -114
  77. package/test/config.example +18 -18
  78. package/test/funcs/crud.test.js +76 -76
  79. package/test/funcs/notification.test.js +31 -31
  80. package/test/funcs/pg.test.js +34 -34
  81. package/test/funcs/redis.test.js +19 -19
  82. package/test/templates/cls/test.json +9 -9
  83. package/test/templates/form/cp_building.form.json +32 -32
  84. package/test/templates/select/account_id.json +3 -3
  85. package/test/templates/select/storage.data.json +2 -2
  86. package/test/templates/table/gis.dataset.table.json +20 -20
  87. package/util/controllers/next.id.js +4 -4
  88. package/util/controllers/properties.add.js +50 -50
  89. package/util/controllers/properties.get.js +19 -19
  90. package/util/index.js +20 -20
  91. package/widget/controllers/utils/historyFormat.js +76 -76
  92. package/widget/controllers/utils/obj2db.js +13 -13
  93. package/widget/controllers/widget.del.js +44 -44
  94. package/widget/controllers/widget.get.js +96 -96
  95. package/widget/controllers/widget.set.js +70 -70
  96. package/widget/index.js +40 -40
package/crud/index.js CHANGED
@@ -1,36 +1,36 @@
1
- import getOpt from './funcs/getOpt.js';
2
- import setOpt from './funcs/setOpt.js';
3
- import isFileExists from './funcs/isFileExists.js';
4
- import dataUpdate from './funcs/dataUpdate.js';
5
- import dataInsert from './funcs/dataInsert.js';
6
-
7
- import update from './controllers/update.js';
8
- import insert from './controllers/insert.js';
9
- import deleteCrud from './controllers/deleteCrud.js';
10
- import getAccessFunc from './funcs/getAccess.js';
11
-
12
- const tableSchema = {
13
- params: {
14
- id: { type: 'string', pattern: '^([\\d\\w]+)$' },
15
- table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
16
- },
17
- };
18
-
19
- async function plugin(fastify, config = {}) {
20
- const prefix = config.prefix || '/api';
21
- // funcs
22
- fastify.decorate('setOpt', setOpt);
23
- fastify.decorate('getOpt', getOpt);
24
- fastify.decorate('dataUpdate', dataUpdate);
25
- fastify.decorate('dataInsert', dataInsert);
26
- fastify.decorate('getAccess', getAccessFunc);
27
-
28
- fastify.decorate('isFileExists', isFileExists);
29
-
30
- // api
31
- fastify.put(`${prefix}/table/:table/:id`, { schema: tableSchema }, update);
32
- fastify.delete(`${prefix}/table/:table/:id`, { schema: tableSchema }, deleteCrud);
33
- fastify.post(`${prefix}/table/:table`, { schema: tableSchema }, insert);
34
- }
35
-
36
- export default plugin;
1
+ import getOpt from './funcs/getOpt.js';
2
+ import setOpt from './funcs/setOpt.js';
3
+ import isFileExists from './funcs/isFileExists.js';
4
+ import dataUpdate from './funcs/dataUpdate.js';
5
+ import dataInsert from './funcs/dataInsert.js';
6
+
7
+ import update from './controllers/update.js';
8
+ import insert from './controllers/insert.js';
9
+ import deleteCrud from './controllers/deleteCrud.js';
10
+ import getAccessFunc from './funcs/getAccess.js';
11
+
12
+ const tableSchema = {
13
+ params: {
14
+ id: { type: 'string', pattern: '^([\\d\\w]+)$' },
15
+ table: { type: 'string', pattern: '^([\\w\\d_.]+)$' },
16
+ },
17
+ };
18
+
19
+ async function plugin(fastify, config = {}) {
20
+ const prefix = config.prefix || '/api';
21
+ // funcs
22
+ fastify.decorate('setOpt', setOpt);
23
+ fastify.decorate('getOpt', getOpt);
24
+ fastify.decorate('dataUpdate', dataUpdate);
25
+ fastify.decorate('dataInsert', dataInsert);
26
+ fastify.decorate('getAccess', getAccessFunc);
27
+
28
+ fastify.decorate('isFileExists', isFileExists);
29
+
30
+ // api
31
+ fastify.put(`${prefix}/table/:table/:id`, { schema: tableSchema }, update);
32
+ fastify.delete(`${prefix}/table/:table/:id`, { schema: tableSchema }, deleteCrud);
33
+ fastify.post(`${prefix}/table/:table`, { schema: tableSchema }, insert);
34
+ }
35
+
36
+ export default plugin;
package/helper.js CHANGED
@@ -1,28 +1,28 @@
1
- // This file contains code that we reuse
2
- // between our tests.
3
- import Fastify from 'fastify';
4
- import config from './test/config.js';
5
- import appService from './index.js';
6
-
7
- import rclient from './redis/client.js';
8
- import pgClients from './pg/pgClients.js';
9
-
10
- // automatically build and tear down our instance
11
- async function build(t) {
12
- // you can set all the options supported by the fastify CLI command
13
- // const argv = [AppPath]
14
- process.env.NODE_ENV = 'production';
15
- const app = Fastify({ logger: false });
16
- app.register(appService, config);
17
- // close the app after we are done
18
- t.after(() => {
19
- // console.log('close app');
20
- pgClients.client.end();
21
- rclient.quit();
22
- app.close();
23
- });
24
-
25
- return app;
26
- }
27
-
28
- export default build;
1
+ // This file contains code that we reuse
2
+ // between our tests.
3
+ import Fastify from 'fastify';
4
+ import config from './test/config.js';
5
+ import appService from './index.js';
6
+
7
+ import rclient from './redis/client.js';
8
+ import pgClients from './pg/pgClients.js';
9
+
10
+ // automatically build and tear down our instance
11
+ async function build(t) {
12
+ // you can set all the options supported by the fastify CLI command
13
+ // const argv = [AppPath]
14
+ process.env.NODE_ENV = 'production';
15
+ const app = Fastify({ logger: false });
16
+ app.register(appService, config);
17
+ // close the app after we are done
18
+ t.after(() => {
19
+ // console.log('close app');
20
+ pgClients.client.end();
21
+ rclient.quit();
22
+ app.close();
23
+ });
24
+
25
+ return app;
26
+ }
27
+
28
+ export default build;
package/index.js CHANGED
@@ -1,97 +1,97 @@
1
- import path from 'path';
2
- import { existsSync, readdirSync, readFileSync } from 'fs';
3
-
4
- import fp from 'fastify-plugin';
5
- import config from './config.js';
6
- // import rclient from './redis/client.js';
7
-
8
- import redisPlugin from './redis/index.js';
9
- import pgPlugin from './pg/index.js';
10
- import tablePlugin from './table/index.js';
11
- import notificationPlugin from './notification/index.js';
12
- import widgetPlugin from './widget/index.js';
13
- import crudPlugin from './crud/index.js';
14
- import policyPlugin from './policy/index.js';
15
- import utilPlugin from './util/index.js';
16
- import cronPlugin from './cron/index.js';
17
-
18
- import pgClients from './pg/pgClients.js';
19
-
20
- import execMigrations from './migration/exec.migrations.js';
21
-
22
- async function plugin(fastify, opt) {
23
- // console.log(opt);
24
- config.pg = opt.pg;
25
- config.redis = opt.redis;
26
- config.root = opt.root;
27
- config.mapServerRoot = opt.mapServerRoot;
28
-
29
- // independent npm start / unit test
30
- if (!fastify.config) {
31
- fastify.decorate('config', config);
32
- }
33
-
34
- fastify.register(import('@fastify/sensible'), {
35
- errorHandler: false,
36
- });
37
-
38
- fastify.register(import('@fastify/url-data'), {
39
- errorHandler: false,
40
- });
41
-
42
- fastify.register(import('@opengis/fastify-hb'));
43
- fastify.decorate('getFolder', (req, type = 'server') => {
44
- if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
45
- const types = { local: req.root, server: req.mapServerRoot };
46
- const filepath = path.posix.join(types[type] || '/data/local', req.folder || config.folder || '');
47
- return filepath;
48
- });
49
-
50
- fastify.addHook('onListen', async () => {
51
- const { client } = pgClients;
52
- if (client?.pk?.['crm.cls']) {
53
- const clsDir = path.join(process.cwd(), 'server/templates/cls');
54
- const files = existsSync(clsDir) ? readdirSync(clsDir) : [];
55
- if (files.length) {
56
- const res = await Promise.all(files.map(async (filename) => {
57
- const filepath = path.join(clsDir, filename);
58
- const data = JSON.parse(readFileSync(filepath));
59
- return { name: path.parse(filename).name, data };
60
- }));
61
- await client.query('truncate table crm.cls');
62
- const { rows } = await client.query(`insert into crm.cls(name, type)
63
- select value->>'name', 'json' from json_array_elements($1) returning cls_id as id, name`, [JSON.stringify(res).replace(/'/g, "''")]);
64
- rows.forEach((row) => Object.assign(row, { data: res.find((cls) => row.name === cls.name)?.data }));
65
- const sql = `insert into crm.cls(code, name, parent)
66
- select json_array_elements(value->'data')->>'id', json_array_elements(value->'data')->>'text', value->>'name' from json_array_elements($1)`;
67
- await client.query(sql, [JSON.stringify(rows).replace(/'/g, "''")]);
68
- }
69
- }
70
- // call from another repo / project
71
- fastify.execMigrations = execMigrations;
72
- // execute core migrations
73
- await fastify.execMigrations();
74
- });
75
- if (!fastify.funcs) {
76
- fastify.addHook('onRequest', async (req) => {
77
- req.funcs = fastify;
78
- if (!req.user && req.session?.passport?.user) {
79
- const { user } = req.session?.passport || {};
80
- req.user = user;
81
- }
82
- });
83
- // fastify.decorateRequest('funcs', fastify);
84
- }
85
-
86
- policyPlugin(fastify);
87
- redisPlugin(fastify);
88
- await pgPlugin(fastify, opt);
89
- tablePlugin(fastify, opt);
90
- crudPlugin(fastify, opt);
91
- notificationPlugin(fastify, opt);
92
- widgetPlugin(fastify, opt);
93
- utilPlugin(fastify, opt);
94
- cronPlugin(fastify, opt);
95
- }
96
- export default fp(plugin);
97
- // export { rclient };
1
+ import path from 'path';
2
+ import { existsSync, readdirSync, readFileSync } from 'fs';
3
+
4
+ import fp from 'fastify-plugin';
5
+ import config from './config.js';
6
+ // import rclient from './redis/client.js';
7
+
8
+ import redisPlugin from './redis/index.js';
9
+ import pgPlugin from './pg/index.js';
10
+ import tablePlugin from './table/index.js';
11
+ import notificationPlugin from './notification/index.js';
12
+ import widgetPlugin from './widget/index.js';
13
+ import crudPlugin from './crud/index.js';
14
+ import policyPlugin from './policy/index.js';
15
+ import utilPlugin from './util/index.js';
16
+ import cronPlugin from './cron/index.js';
17
+
18
+ import pgClients from './pg/pgClients.js';
19
+
20
+ import execMigrations from './migration/exec.migrations.js';
21
+
22
+ async function plugin(fastify, opt) {
23
+ // console.log(opt);
24
+ config.pg = opt.pg;
25
+ config.redis = opt.redis;
26
+ config.root = opt.root;
27
+ config.mapServerRoot = opt.mapServerRoot;
28
+
29
+ // independent npm start / unit test
30
+ if (!fastify.config) {
31
+ fastify.decorate('config', config);
32
+ }
33
+
34
+ fastify.register(import('@fastify/sensible'), {
35
+ errorHandler: false,
36
+ });
37
+
38
+ fastify.register(import('@fastify/url-data'), {
39
+ errorHandler: false,
40
+ });
41
+
42
+ fastify.register(import('@opengis/fastify-hb'));
43
+ fastify.decorate('getFolder', (req, type = 'server') => {
44
+ if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
45
+ const types = { local: req.root, server: req.mapServerRoot };
46
+ const filepath = path.posix.join(types[type] || '/data/local', req.folder || config.folder || '');
47
+ return filepath;
48
+ });
49
+
50
+ fastify.addHook('onListen', async () => {
51
+ const { client } = pgClients;
52
+ if (client?.pk?.['crm.cls']) {
53
+ const clsDir = path.join(process.cwd(), 'server/templates/cls');
54
+ const files = existsSync(clsDir) ? readdirSync(clsDir) : [];
55
+ if (files.length) {
56
+ const res = await Promise.all(files.map(async (filename) => {
57
+ const filepath = path.join(clsDir, filename);
58
+ const data = JSON.parse(readFileSync(filepath));
59
+ return { name: path.parse(filename).name, data };
60
+ }));
61
+ await client.query('truncate table crm.cls');
62
+ const { rows } = await client.query(`insert into crm.cls(name, type)
63
+ select value->>'name', 'json' from json_array_elements($1) returning cls_id as id, name`, [JSON.stringify(res).replace(/'/g, "''")]);
64
+ rows.forEach((row) => Object.assign(row, { data: res.find((cls) => row.name === cls.name)?.data }));
65
+ const sql = `insert into crm.cls(code, name, parent)
66
+ select json_array_elements(value->'data')->>'id', json_array_elements(value->'data')->>'text', value->>'name' from json_array_elements($1)`;
67
+ await client.query(sql, [JSON.stringify(rows).replace(/'/g, "''")]);
68
+ }
69
+ }
70
+ // call from another repo / project
71
+ fastify.execMigrations = execMigrations;
72
+ // execute core migrations
73
+ await fastify.execMigrations();
74
+ });
75
+ if (!fastify.funcs) {
76
+ fastify.addHook('onRequest', async (req) => {
77
+ req.funcs = fastify;
78
+ if (!req.user && req.session?.passport?.user) {
79
+ const { user } = req.session?.passport || {};
80
+ req.user = user;
81
+ }
82
+ });
83
+ // fastify.decorateRequest('funcs', fastify);
84
+ }
85
+
86
+ policyPlugin(fastify);
87
+ redisPlugin(fastify);
88
+ await pgPlugin(fastify, opt);
89
+ tablePlugin(fastify, opt);
90
+ crudPlugin(fastify, opt);
91
+ notificationPlugin(fastify, opt);
92
+ widgetPlugin(fastify, opt);
93
+ utilPlugin(fastify, opt);
94
+ cronPlugin(fastify, opt);
95
+ }
96
+ export default fp(plugin);
97
+ // export { rclient };
@@ -1,76 +1,76 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
-
4
- const time = Date.now();
5
-
6
- import getPG from '../pg/funcs/getPG.js';
7
-
8
- export default async function execMigrations(opt) {
9
- try {
10
- const pg = opt?.pg || getPG({ name: 'client' });
11
- const rootDir = getCallerDir();
12
- const dir = path.join(rootDir.replace(/\\/g, '/').replace(/^file:\/\/\//, ''), rootDir.endsWith('plugins') ? '../..' : '', 'server/migrations');
13
-
14
- console.log('migrations start', dir, Date.now() - time);
15
- const exists = fs.existsSync(dir);
16
- if (exists) {
17
- // get directory sql file list
18
- const content = fs.readdirSync(dir, { withFileTypes: true })
19
- ?.filter((el) => el.isFile() && path.extname(el.name) === '.sql')
20
- ?.map((el) => el.name) || [];
21
-
22
- // execute sql files
23
- if (content?.length) {
24
- await sequence(content, { pg, dir }, execSql);
25
- }
26
- }
27
- console.log('migrations finish', dir, exists, Date.now() - time);
28
- } catch(err) {
29
- console.error('migrations error', err.toString(), Date.now() - time);
30
- }
31
- }
32
-
33
- function getCallerDir() {
34
- const originalFunc = Error.prepareStackTrace;
35
-
36
- let callerfile;
37
- try {
38
- const err = new Error();
39
- let currentfile;
40
-
41
- Error.prepareStackTrace = function (err, stack) { return stack; };
42
-
43
- currentfile = err.stack.shift().getFileName();
44
-
45
- while (err.stack.length) {
46
- callerfile = err.stack.shift().getFileName();
47
-
48
- if(currentfile !== callerfile) break;
49
- }
50
- } catch (err) { }
51
-
52
- Error.prepareStackTrace = originalFunc;
53
-
54
- return path.dirname(callerfile);
55
- }
56
-
57
- function sequence(files, data, fn) {
58
- return files.reduce((promise, filename) => promise.then(() => fn({
59
- ...data, filename,
60
- })), Promise.resolve());
61
- }
62
-
63
- async function execSql({
64
- pg, dir, filename,
65
- }) {
66
- const start = Date.now();
67
- const filepath = path.join(dir, filename);
68
- const sql = fs.readFileSync(filepath, 'utf-8');
69
- try {
70
- console.log(filename, 'start', Date.now() - start);
71
- await pg.query(sql);
72
- console.log(filename, 'finish', Date.now() - start);
73
- } catch (err) {
74
- console.log(filepath, 'error', err.toString(), Date.now() - start);
75
- }
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ const time = Date.now();
5
+
6
+ import getPG from '../pg/funcs/getPG.js';
7
+
8
+ export default async function execMigrations(opt) {
9
+ try {
10
+ const pg = opt?.pg || getPG({ name: 'client' });
11
+ const rootDir = getCallerDir();
12
+ const dir = path.join(rootDir.replace(/\\/g, '/').replace(/^file:\/\/\//, ''), rootDir.endsWith('plugins') ? '../..' : '', 'server/migrations');
13
+
14
+ console.log('migrations start', dir, Date.now() - time);
15
+ const exists = fs.existsSync(dir);
16
+ if (exists) {
17
+ // get directory sql file list
18
+ const content = fs.readdirSync(dir, { withFileTypes: true })
19
+ ?.filter((el) => el.isFile() && path.extname(el.name) === '.sql')
20
+ ?.map((el) => el.name) || [];
21
+
22
+ // execute sql files
23
+ if (content?.length) {
24
+ await sequence(content, { pg, dir }, execSql);
25
+ }
26
+ }
27
+ console.log('migrations finish', dir, exists, Date.now() - time);
28
+ } catch(err) {
29
+ console.error('migrations error', err.toString(), Date.now() - time);
30
+ }
31
+ }
32
+
33
+ function getCallerDir() {
34
+ const originalFunc = Error.prepareStackTrace;
35
+
36
+ let callerfile;
37
+ try {
38
+ const err = new Error();
39
+ let currentfile;
40
+
41
+ Error.prepareStackTrace = function (err, stack) { return stack; };
42
+
43
+ currentfile = err.stack.shift().getFileName();
44
+
45
+ while (err.stack.length) {
46
+ callerfile = err.stack.shift().getFileName();
47
+
48
+ if(currentfile !== callerfile) break;
49
+ }
50
+ } catch (err) { }
51
+
52
+ Error.prepareStackTrace = originalFunc;
53
+
54
+ return path.dirname(callerfile);
55
+ }
56
+
57
+ function sequence(files, data, fn) {
58
+ return files.reduce((promise, filename) => promise.then(() => fn({
59
+ ...data, filename,
60
+ })), Promise.resolve());
61
+ }
62
+
63
+ async function execSql({
64
+ pg, dir, filename,
65
+ }) {
66
+ const start = Date.now();
67
+ const filepath = path.join(dir, filename);
68
+ const sql = fs.readFileSync(filepath, 'utf-8');
69
+ try {
70
+ console.log(filename, 'start', Date.now() - start);
71
+ await pg.query(sql);
72
+ console.log(filename, 'finish', Date.now() - start);
73
+ } catch (err) {
74
+ console.log(filepath, 'error', err.toString(), Date.now() - start);
75
+ }
76
76
  }
@@ -1,49 +1,49 @@
1
- import path from 'path';
2
- import { existsSync } from 'fs';
3
- import { fileURLToPath } from 'url';
4
-
5
- const fileName = fileURLToPath(import.meta.url);
6
- const dirName = path.dirname(fileName);
7
-
8
- import notification from '../funcs/sendNotification.js';
9
-
10
- export default async function testNotification({
11
- pg, funcs = {}, log, query = {}, session = {},
12
- }) {
13
- const { local } = funcs.config || {};
14
- if (!session?.passport?.user?.user_type?.includes('admin') && !local) {
15
- return { message: 'Forbidden', status: 403 };
16
- }
17
-
18
- const date = new Date().toISOString().split('T')[0];
19
- if (!query.to) {
20
- return { message: 'param to is required', status: 400 };
21
- }
22
-
23
- try {
24
- const {
25
- to, template, table, id, nocache,
26
- } = query;
27
- const file = [path.join(dirName, '../../', 'changelog.md'), path.join(dirName, 'utils', 'pin-m-ty-media-record-outline+303070.png')].filter((el) => existsSync(el));
28
- const data = await notification({
29
- pg,
30
- funcs,
31
- log,
32
- to,
33
- template,
34
- title: `Test Softpro ${date}`,
35
- table,
36
- nocache,
37
- file,
38
- id,
39
- message: `Test mail Softpro ${date} Lorem Ipsum Lorem Ipsum`,
40
- });
41
-
42
- return {
43
- message: data || 'ok',
44
- };
45
- }
46
- catch (err) {
47
- return { error: err.toString(), status: 500 };
48
- }
49
- }
1
+ import path from 'path';
2
+ import { existsSync } from 'fs';
3
+ import { fileURLToPath } from 'url';
4
+
5
+ const fileName = fileURLToPath(import.meta.url);
6
+ const dirName = path.dirname(fileName);
7
+
8
+ import notification from '../funcs/sendNotification.js';
9
+
10
+ export default async function testNotification({
11
+ pg, funcs = {}, log, query = {}, session = {},
12
+ }) {
13
+ const { local } = funcs.config || {};
14
+ if (!session?.passport?.user?.user_type?.includes('admin') && !local) {
15
+ return { message: 'Forbidden', status: 403 };
16
+ }
17
+
18
+ const date = new Date().toISOString().split('T')[0];
19
+ if (!query.to) {
20
+ return { message: 'param to is required', status: 400 };
21
+ }
22
+
23
+ try {
24
+ const {
25
+ to, template, table, id, nocache,
26
+ } = query;
27
+ const file = [path.join(dirName, '../../', 'changelog.md'), path.join(dirName, 'utils', 'pin-m-ty-media-record-outline+303070.png')].filter((el) => existsSync(el));
28
+ const data = await notification({
29
+ pg,
30
+ funcs,
31
+ log,
32
+ to,
33
+ template,
34
+ title: `Test Softpro ${date}`,
35
+ table,
36
+ nocache,
37
+ file,
38
+ id,
39
+ message: `Test mail Softpro ${date} Lorem Ipsum Lorem Ipsum`,
40
+ });
41
+
42
+ return {
43
+ message: data || 'ok',
44
+ };
45
+ }
46
+ catch (err) {
47
+ return { error: err.toString(), status: 500 };
48
+ }
49
+ }
@@ -1,19 +1,19 @@
1
- export default async function userNotifications({
2
- pg, session = {}, query = {},
3
- }) {
4
- const time = Date.now();
5
- try {
6
- const { uid } = session.passport?.user || {};
7
- if (!uid) return { error: 'access restricted', status: 403 };
8
-
9
- const queryFunc = query.nocache ? pg.query : pg.queryCache;
10
-
11
- // queryCache not supports $1 params
12
- const { rows } = await queryFunc(`select notification_id as id, notification_type as type,
13
- notification_status as status, title, body, link, uid from crm.notification where uid='${uid}'`);
14
- return { time: Date.now() - time, total: rows?.length, rows };
15
- }
16
- catch (err) {
17
- return { error: err.toString(), status: 500 };
18
- }
19
- }
1
+ export default async function userNotifications({
2
+ pg, session = {}, query = {},
3
+ }) {
4
+ const time = Date.now();
5
+ try {
6
+ const { uid } = session.passport?.user || {};
7
+ if (!uid) return { error: 'access restricted', status: 403 };
8
+
9
+ const queryFunc = query.nocache ? pg.query : pg.queryCache;
10
+
11
+ // queryCache not supports $1 params
12
+ const { rows } = await queryFunc(`select notification_id as id, notification_type as type,
13
+ notification_status as status, title, body, link, uid from crm.notification where uid='${uid}'`);
14
+ return { time: Date.now() - time, total: rows?.length, rows };
15
+ }
16
+ catch (err) {
17
+ return { error: err.toString(), status: 500 };
18
+ }
19
+ }
@@ -1,8 +1,8 @@
1
- export default async function addNotification({
2
- pg, session = {}, title, body, link, notificationType, uid: uid1,
3
- }) {
4
- const uid = uid1 || session.passport?.user?.uid || {};
5
- const { id, status } = await pg.one(`insert into crm.notification(title, body, link, notification_type, uid)
6
- values($1,$2,$3,$4,$5) returning notification_id as id, notification_status as status`, [title, body, link, notificationType, uid]);
7
- return { id, status };
8
- }
1
+ export default async function addNotification({
2
+ pg, session = {}, title, body, link, notificationType, uid: uid1,
3
+ }) {
4
+ const uid = uid1 || session.passport?.user?.uid || {};
5
+ const { id, status } = await pg.one(`insert into crm.notification(title, body, link, notification_type, uid)
6
+ values($1,$2,$3,$4,$5) returning notification_id as id, notification_status as status`, [title, body, link, notificationType, uid]);
7
+ return { id, status };
8
+ }