@opengis/fastify-table 1.0.77 → 1.0.78

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 (41) hide show
  1. package/Changelog.md +4 -0
  2. package/crud/controllers/deleteCrud.js +19 -19
  3. package/crud/controllers/insert.js +54 -54
  4. package/crud/controllers/update.js +59 -59
  5. package/crud/funcs/dataInsert.js +24 -24
  6. package/crud/funcs/getAccess.js +53 -53
  7. package/crud/funcs/getOpt.js +10 -10
  8. package/crud/funcs/setOpt.js +16 -16
  9. package/helper.js +28 -28
  10. package/notification/controllers/userNotifications.js +19 -19
  11. package/notification/funcs/addNotification.js +8 -8
  12. package/package.json +1 -1
  13. package/pg/pgClients.js +20 -20
  14. package/policy/funcs/checkPolicy.js +82 -82
  15. package/policy/funcs/sqlInjection.js +33 -33
  16. package/policy/index.js +14 -14
  17. package/redis/client.js +8 -8
  18. package/redis/funcs/redisClients.js +2 -2
  19. package/redis/index.js +19 -19
  20. package/server/migrations/0.sql +64 -64
  21. package/server/templates/form/test.dataset.form.json +411 -411
  22. package/server/templates/select/test.storage.data.json +2 -2
  23. package/server/templates/table/test.dataset.table.json +24 -24
  24. package/server/templates/table/test.gis.map.table.json +44 -44
  25. package/table/controllers/data.js +95 -95
  26. package/table/controllers/utils/getSelect.js +20 -20
  27. package/table/controllers/utils/gisIRColumn.js +68 -68
  28. package/table/funcs/getFilterSQL/index.js +75 -75
  29. package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
  30. package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  31. package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
  32. package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  33. package/table/funcs/metaFormat/getSelectVal.js +20 -20
  34. package/test/api/crud.test.js +88 -88
  35. package/util/controllers/status.monitor.js +8 -8
  36. package/util/index.js +21 -21
  37. package/widget/controllers/utils/historyFormat.js +76 -76
  38. package/widget/controllers/utils/obj2db.js +13 -13
  39. package/widget/controllers/widget.del.js +44 -44
  40. package/widget/controllers/widget.get.js +96 -96
  41. package/widget/controllers/widget.set.js +70 -70
@@ -1,96 +1,96 @@
1
- import getToken from '../../crud/funcs/getToken.js';
2
- import getMeta from '../../pg/funcs/getMeta.js';
3
- import historyFormat from './utils/historyFormat.js';
4
-
5
- const galleryExtList = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'mp4', 'mov', 'avi'];
6
-
7
- /**
8
- * Дістає CRM для widget
9
- *
10
- */
11
-
12
- export default async function widgetGet({
13
- pg, session = {}, params = {}, query = {},
14
- }) {
15
- const { user = {} } = session.passport || {};
16
-
17
- const param = user?.uid ? await getToken({
18
- token: params.objectid, mode: 'w', uid: user.uid,
19
- }) : null;
20
-
21
- const objectid = param ? JSON.parse(param)?.id : params.objectid;
22
-
23
- if (!objectid) return { error: 'id required', status: 400 };
24
-
25
- const sqls = {
26
- comment: pg.pk['admin.users']
27
- ? `select communication_id, entity_id, body, subject, c.cdate, c.uid,
28
- coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, avatar
29
- from crm.communications c left join admin.users u on u.uid=c.uid where entity_id=$1 order by cdate desc`
30
- : 'select communication_id, entity_id, body, subject, cdate, uid from crm.communications where entity_id=$1 order by cdate desc',
31
-
32
- history: `SELECT table_change_id, entity_id, entity_type, change_key, change_date, json_old, json_new, date_old,
33
- date_new, number_old, number_new, bool_old, bool_new, text_old,
34
- text_new, uid, cdate FROM log.table_changes where entity_id=$1 order by cdate desc, change_key limit 100`,
35
-
36
- checklist: pg.pk['admin.users']
37
- ? `SELECT checklist_id, entity_id, subject, is_done, done_date, c.uid, c.cdate, coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username,
38
- avatar FROM crm.checklists c left join admin.users u on u.uid=c.uid where entity_id=$1 order by cdate desc`
39
- : 'SELECT checklist_id, entity_id, subject, is_done, done_date, uid, cdate FROM crm.checklists where entity_id=$1 order by cdate desc',
40
-
41
- file: pg.pk['admin.users']
42
- ? `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
43
- coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, isverified,
44
- avatar, c.uid as author, file_status FROM crm.files c left join admin.users u on u.uid=c.uid
45
- where entity_id=$1 and file_status<>3 order by cdate desc`
46
- : `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, uid, cdate, file_type, ismain,
47
- isverified, uid as author, file_status FROM crm.files c where entity_id=$1 and file_status<>3 order by cdate desc`,
48
- gallery: pg.pk['admin.users']
49
- ? `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
50
- coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, isverified,
51
- avatar, c.uid as author, file_status FROM crm.files c left join admin.users u on u.uid=c.uid
52
- where entity_id=$1 and file_status<>3 and ext = any($2) order by cdate desc`
53
- : `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, ismain,
54
- isverified, uid as author, file_status FROM crm.files c where entity_id=$1 and file_status<>3 and ext = any($2) order by cdate desc`,
55
-
56
- };
57
- const sql = sqls[params.type];
58
- if (!sql) {
59
- return { error: 'param type not valid', status: 400 };
60
- }
61
-
62
- try {
63
- /* data */
64
- const time = [Date.now()];
65
- const { rows } = await pg.query(sql, [objectid, params.type === 'gallery' ? galleryExtList : null].filter((el) => el));
66
- time.push(Date.now());
67
-
68
- /* Object info */
69
- const { tableName } = pg.pk['log.table_changes'] ? await pg.one('select entity_type as "tableName" from log.table_changes where entity_id=$1 limit 1', [objectid]) : {};
70
- const { pk } = await getMeta({ table: tableName });
71
-
72
- const q = tableName && pg.pk['admin.users'] ? `select coalesce(b.user_name,'')||coalesce(' '||b.sur_name,'') as author, a.cdate, a.editor_date from ${tableName} a
73
- left join admin.users b on a.uid=b.uid where a.${pk}=$1 limit 1` : undefined;
74
- const data = pk && q ? await pg.one(q, [objectid]) : {};
75
-
76
- if (query.debug && user?.user_type === 'admin') {
77
- return {
78
- sql, type: params.type, q, id: objectid, data,
79
- };
80
- }
81
-
82
- time.push(Date.now());
83
- return {
84
- time: { data: time[1] - time[0], format: time[2] - time[1] },
85
- rows: params.type === 'history' ? await historyFormat(rows, tableName, pg) : rows,
86
- user: { uid: user?.uid, name: user?.user_name },
87
- data: { author: data?.author, cdate: data?.cdate, edate: data?.editor_date },
88
- objectid: params.objectid,
89
- };
90
- }
91
- catch (err) {
92
- // 'history', 'file', 'checklist'
93
- /* 'document', 'image' - Макс на клиенте */
94
- return { message: err.toString(), status: 500 };
95
- }
96
- }
1
+ import getToken from '../../crud/funcs/getToken.js';
2
+ import getMeta from '../../pg/funcs/getMeta.js';
3
+ import historyFormat from './utils/historyFormat.js';
4
+
5
+ const galleryExtList = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'mp4', 'mov', 'avi'];
6
+
7
+ /**
8
+ * Дістає CRM для widget
9
+ *
10
+ */
11
+
12
+ export default async function widgetGet({
13
+ pg, session = {}, params = {}, query = {},
14
+ }) {
15
+ const { user = {} } = session.passport || {};
16
+
17
+ const param = user?.uid ? await getToken({
18
+ token: params.objectid, mode: 'w', uid: user.uid,
19
+ }) : null;
20
+
21
+ const objectid = param ? JSON.parse(param)?.id : params.objectid;
22
+
23
+ if (!objectid) return { error: 'id required', status: 400 };
24
+
25
+ const sqls = {
26
+ comment: pg.pk['admin.users']
27
+ ? `select communication_id, entity_id, body, subject, c.cdate, c.uid,
28
+ coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, avatar
29
+ from crm.communications c left join admin.users u on u.uid=c.uid where entity_id=$1 order by cdate desc`
30
+ : 'select communication_id, entity_id, body, subject, cdate, uid from crm.communications where entity_id=$1 order by cdate desc',
31
+
32
+ history: `SELECT table_change_id, entity_id, entity_type, change_key, change_date, json_old, json_new, date_old,
33
+ date_new, number_old, number_new, bool_old, bool_new, text_old,
34
+ text_new, uid, cdate FROM log.table_changes where entity_id=$1 order by cdate desc, change_key limit 100`,
35
+
36
+ checklist: pg.pk['admin.users']
37
+ ? `SELECT checklist_id, entity_id, subject, is_done, done_date, c.uid, c.cdate, coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username,
38
+ avatar FROM crm.checklists c left join admin.users u on u.uid=c.uid where entity_id=$1 order by cdate desc`
39
+ : 'SELECT checklist_id, entity_id, subject, is_done, done_date, uid, cdate FROM crm.checklists where entity_id=$1 order by cdate desc',
40
+
41
+ file: pg.pk['admin.users']
42
+ ? `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
43
+ coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, isverified,
44
+ avatar, c.uid as author, file_status FROM crm.files c left join admin.users u on u.uid=c.uid
45
+ where entity_id=$1 and file_status<>3 order by cdate desc`
46
+ : `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, uid, cdate, file_type, ismain,
47
+ isverified, uid as author, file_status FROM crm.files c where entity_id=$1 and file_status<>3 order by cdate desc`,
48
+ gallery: pg.pk['admin.users']
49
+ ? `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
50
+ coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, isverified,
51
+ avatar, c.uid as author, file_status FROM crm.files c left join admin.users u on u.uid=c.uid
52
+ where entity_id=$1 and file_status<>3 and ext = any($2) order by cdate desc`
53
+ : `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, ismain,
54
+ isverified, uid as author, file_status FROM crm.files c where entity_id=$1 and file_status<>3 and ext = any($2) order by cdate desc`,
55
+
56
+ };
57
+ const sql = sqls[params.type];
58
+ if (!sql) {
59
+ return { error: 'param type not valid', status: 400 };
60
+ }
61
+
62
+ try {
63
+ /* data */
64
+ const time = [Date.now()];
65
+ const { rows } = await pg.query(sql, [objectid, params.type === 'gallery' ? galleryExtList : null].filter((el) => el));
66
+ time.push(Date.now());
67
+
68
+ /* Object info */
69
+ const { tableName } = pg.pk['log.table_changes'] ? await pg.one('select entity_type as "tableName" from log.table_changes where entity_id=$1 limit 1', [objectid]) : {};
70
+ const { pk } = await getMeta({ table: tableName });
71
+
72
+ const q = tableName && pg.pk['admin.users'] ? `select coalesce(b.user_name,'')||coalesce(' '||b.sur_name,'') as author, a.cdate, a.editor_date from ${tableName} a
73
+ left join admin.users b on a.uid=b.uid where a.${pk}=$1 limit 1` : undefined;
74
+ const data = pk && q ? await pg.one(q, [objectid]) : {};
75
+
76
+ if (query.debug && user?.user_type === 'admin') {
77
+ return {
78
+ sql, type: params.type, q, id: objectid, data,
79
+ };
80
+ }
81
+
82
+ time.push(Date.now());
83
+ return {
84
+ time: { data: time[1] - time[0], format: time[2] - time[1] },
85
+ rows: params.type === 'history' ? await historyFormat(rows, tableName, pg) : rows,
86
+ user: { uid: user?.uid, name: user?.user_name },
87
+ data: { author: data?.author, cdate: data?.cdate, edate: data?.editor_date },
88
+ objectid: params.objectid,
89
+ };
90
+ }
91
+ catch (err) {
92
+ // 'history', 'file', 'checklist'
93
+ /* 'document', 'image' - Макс на клиенте */
94
+ return { message: err.toString(), status: 500 };
95
+ }
96
+ }
@@ -1,70 +1,70 @@
1
- import path from 'path';
2
-
3
- import getMeta from '../../pg/funcs/getMeta.js';
4
- import dataInsert from '../../crud/funcs/dataInsert.js';
5
- import dataUpdate from '../../crud/funcs/dataUpdate.js';
6
-
7
- const tableList = {
8
- comment: 'crm.communications',
9
- checklist: 'crm.checklists',
10
- };
11
- const pkList = {
12
- comment: 'communication_id',
13
- checklist: 'checklist_id',
14
- };
15
-
16
- const galleryExtList = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'mp4', 'mov', 'avi'];
17
-
18
- export default async function widgetSet(req) {
19
- const {
20
- pg, params = {}, session = {}, body = {}, funcs, log,
21
- } = req;
22
- const { user = {} } = session.passport || {};
23
- const { type, id, objectid } = params;
24
- if (!['comment', 'checklist', 'file', 'gallery'].includes(type)) return { error: 'param type not valid', status: 400 };
25
- if (!objectid) return { error: 'id required', status: 400 };
26
-
27
- const table = tableList[type];
28
-
29
- try {
30
- if (['gallery', 'file'].includes(type)) {
31
- const file = await funcs.uploadMultiPart(req);
32
- const extName = path.extname(file.filepath).slice(1).toLowerCase();
33
-
34
- const data = {
35
- uploaded_name: file?.originalFilename?.toLocaleLowerCase()?.replace(/'/g, '\'\''),
36
- file_path: file?.relativeFilepath?.replace(/\\/g, '/'),
37
- ext: extName,
38
- size: file?.size,
39
- file_status: 1,
40
- uid: user?.uid || 1,
41
- entity_id: objectid,
42
- };
43
-
44
- if (type === 'gallery' && !galleryExtList.includes(extName.toLowerCase())) {
45
- return { message: 'invalid file extension', status: 400 };
46
- }
47
-
48
- const { rows = [] } = await dataInsert({ table: 'crm.files', data });
49
- return {
50
- rowCount: 1, data: 'ok', command: 'UPLOAD', id: rows[0]?.file_id, entity_id: rows[0]?.entity_id,
51
- };
52
- }
53
- const { pk } = await getMeta({ pg, table });
54
- if (!pk) return { message: 'table not found', status: 404 };
55
-
56
- const data = { ...body, uid: user?.uid, entity_id: objectid };
57
-
58
- const result = id
59
- ? await dataUpdate({ table, data, id })
60
- : await dataInsert({ table, data });
61
-
62
- return {
63
- rowCount: result.rowCount, data: 'ok', command: result.command, id: result.rows?.[0]?.[pkList[type]] || result?.[pkList[type]],
64
- };
65
- }
66
- catch (err) {
67
- log.error('widget/upload', { error: err.toString(), params });
68
- return { error: err.toString(), status: 500 };
69
- }
70
- }
1
+ import path from 'path';
2
+
3
+ import getMeta from '../../pg/funcs/getMeta.js';
4
+ import dataInsert from '../../crud/funcs/dataInsert.js';
5
+ import dataUpdate from '../../crud/funcs/dataUpdate.js';
6
+
7
+ const tableList = {
8
+ comment: 'crm.communications',
9
+ checklist: 'crm.checklists',
10
+ };
11
+ const pkList = {
12
+ comment: 'communication_id',
13
+ checklist: 'checklist_id',
14
+ };
15
+
16
+ const galleryExtList = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'mp4', 'mov', 'avi'];
17
+
18
+ export default async function widgetSet(req) {
19
+ const {
20
+ pg, params = {}, session = {}, body = {}, funcs, log,
21
+ } = req;
22
+ const { user = {} } = session.passport || {};
23
+ const { type, id, objectid } = params;
24
+ if (!['comment', 'checklist', 'file', 'gallery'].includes(type)) return { error: 'param type not valid', status: 400 };
25
+ if (!objectid) return { error: 'id required', status: 400 };
26
+
27
+ const table = tableList[type];
28
+
29
+ try {
30
+ if (['gallery', 'file'].includes(type)) {
31
+ const file = await funcs.uploadMultiPart(req);
32
+ const extName = path.extname(file.filepath).slice(1).toLowerCase();
33
+
34
+ const data = {
35
+ uploaded_name: file?.originalFilename?.toLocaleLowerCase()?.replace(/'/g, '\'\''),
36
+ file_path: file?.relativeFilepath?.replace(/\\/g, '/'),
37
+ ext: extName,
38
+ size: file?.size,
39
+ file_status: 1,
40
+ uid: user?.uid || 1,
41
+ entity_id: objectid,
42
+ };
43
+
44
+ if (type === 'gallery' && !galleryExtList.includes(extName.toLowerCase())) {
45
+ return { message: 'invalid file extension', status: 400 };
46
+ }
47
+
48
+ const { rows = [] } = await dataInsert({ table: 'crm.files', data });
49
+ return {
50
+ rowCount: 1, data: 'ok', command: 'UPLOAD', id: rows[0]?.file_id, entity_id: rows[0]?.entity_id,
51
+ };
52
+ }
53
+ const { pk } = await getMeta({ pg, table });
54
+ if (!pk) return { message: 'table not found', status: 404 };
55
+
56
+ const data = { ...body, uid: user?.uid, entity_id: objectid };
57
+
58
+ const result = id
59
+ ? await dataUpdate({ table, data, id })
60
+ : await dataInsert({ table, data });
61
+
62
+ return {
63
+ rowCount: result.rowCount, data: 'ok', command: result.command, id: result.rows?.[0]?.[pkList[type]] || result?.[pkList[type]],
64
+ };
65
+ }
66
+ catch (err) {
67
+ log.error('widget/upload', { error: err.toString(), params });
68
+ return { error: err.toString(), status: 500 };
69
+ }
70
+ }