@opengis/admin 0.1.84 → 0.1.85

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. package/README.md +29 -29
  2. package/config.js +4 -4
  3. package/dist/{IconChevronDown-DoqykhvB.js → IconChevronDown-IrRpX-i0.js} +1 -1
  4. package/dist/{add-page-ChBpMfRU.js → add-page-DRXx4iOi.js} +1 -1
  5. package/dist/{admin-interface-SDtt7rqS.js → admin-interface-BY-D9Jvu.js} +4 -4
  6. package/dist/{admin-view-CKTriO6z.js → admin-view-BZ75rygK.js} +44 -44
  7. package/dist/admin.js +1 -1
  8. package/dist/admin.umd.cjs +7 -7
  9. package/dist/assets/logo.svg +41 -41
  10. package/dist/{card-page-BaSbgjMn.js → card-page-BAeSkHUO.js} +2 -2
  11. package/dist/{card-view-B8ohi0lw.js → card-view-p73B3Gbl.js} +1 -1
  12. package/dist/{edit-page-DLlbyyd_.js → edit-page-CZ8SDvjo.js} +1 -1
  13. package/dist/{import-file-CV5CU5lC.js → import-file-f46xrNIz.js} +22 -15
  14. package/dist/style.css +1 -1
  15. package/module/settings/card/admin.roles.table/access.hbs +27 -27
  16. package/module/settings/card/admin.roles.table/general_info.hbs +16 -16
  17. package/module/settings/card/admin.roles.table/index.yml +14 -14
  18. package/module/settings/card/admin.roles.table/users.hbs +27 -27
  19. package/module/settings/card/admin.routes.table/general_info.hbs +40 -40
  20. package/module/settings/card/admin.routes.table/index.yml +8 -8
  21. package/module/settings/card/admin.routes.table/users.hbs +33 -33
  22. package/module/settings/card/admin.users.table/general_info.hbs +25 -25
  23. package/module/settings/card/admin.users.table/index.yml +12 -12
  24. package/module/settings/card/admin.users.table/logs.hbs +30 -30
  25. package/module/settings/card/admin.users.table/user_roles.hbs +24 -24
  26. package/module/settings/cls/core.actions.json +17 -17
  27. package/module/settings/cls/core.scope.json +13 -13
  28. package/module/settings/cls/properties.site_status.json +13 -13
  29. package/module/settings/cls/properties.widget_status.json +13 -13
  30. package/module/settings/cls/users.user_type.json +13 -13
  31. package/module/settings/form/admin.access.form.json +36 -36
  32. package/module/settings/form/admin.custom_column.form.json +71 -71
  33. package/module/settings/form/admin.properties.form.json +15 -15
  34. package/module/settings/form/admin.roles.form.json +19 -19
  35. package/module/settings/form/admin.routes.form.json +25 -25
  36. package/module/settings/form/admin.user_properties.form.json +15 -15
  37. package/module/settings/form/admin.user_roles.form.json +21 -21
  38. package/module/settings/form/admin.users.form.json +153 -153
  39. package/module/settings/form/user.user_roles.form.json +13 -13
  40. package/module/settings/interface/admin.properties.json +4 -4
  41. package/module/settings/interface/admin.roles.json +4 -4
  42. package/module/settings/interface/admin.routes.json +4 -4
  43. package/module/settings/interface/admin.users.json +4 -4
  44. package/module/settings/menu.json +50 -50
  45. package/module/settings/select/core.roles.json +2 -2
  46. package/module/settings/select/core.routes.sql +1 -1
  47. package/module/settings/select/core.user_mentioned.sql +1 -1
  48. package/module/settings/select/core.user_uid.sql +1 -1
  49. package/module/settings/table/admin.access.table.json +83 -83
  50. package/module/settings/table/admin.custom_column.table.json +99 -99
  51. package/module/settings/table/admin.properties.table.json +39 -39
  52. package/module/settings/table/admin.roles.table.json +64 -64
  53. package/module/settings/table/admin.routes.table.json +68 -68
  54. package/module/settings/table/admin.user_properties.table.json +34 -34
  55. package/module/settings/table/admin.user_roles.table.json +72 -72
  56. package/module/settings/table/admin.users.table.json +121 -121
  57. package/package.json +82 -82
  58. package/plugin.js +29 -29
  59. package/server/helpers/controllers/badge.js +11 -11
  60. package/server/helpers/controllers/hb.js +2 -2
  61. package/server/helpers/controllers/map.js +2 -2
  62. package/server/helpers/controllers/mls.js +2 -2
  63. package/server/helpers/controllers/token.js +16 -16
  64. package/server/helpers/controllers/vue.js +2 -2
  65. package/server/helpers/index.mjs +29 -17
  66. package/server/helpers/list/buttonHelper.js +11 -0
  67. package/server/helpers/list/descriptionList.js +34 -0
  68. package/server/helpers/list/tableList.js +66 -0
  69. package/server/helpers/list/utils/button.js +6 -0
  70. package/server/plugins/access/funcs/getAdminAccess.js +14 -14
  71. package/server/plugins/access/index.mjs +6 -6
  72. package/server/plugins/adminHook.js +81 -93
  73. package/server/plugins/cron.js +10 -10
  74. package/server/plugins/docs.js +28 -28
  75. package/server/plugins/hook.js +223 -223
  76. package/server/plugins/vite.js +71 -71
  77. package/server/routes/access/controllers/access.group.js +29 -29
  78. package/server/routes/access/controllers/access.group.post.js +43 -43
  79. package/server/routes/access/index.mjs +63 -63
  80. package/server/routes/calendar/controllers/calendar.data.js +87 -87
  81. package/server/routes/calendar/index.mjs +7 -7
  82. package/server/routes/calendar/schema.js +21 -21
  83. package/server/routes/data/controllers/cardData.js +81 -81
  84. package/server/routes/data/controllers/cardTabData.js +49 -49
  85. package/server/routes/data/controllers/funcs/getFilterSQL/index.js +92 -92
  86. package/server/routes/data/controllers/funcs/getFilterSQL/util/formatValue.js +170 -170
  87. package/server/routes/data/controllers/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  88. package/server/routes/data/controllers/funcs/getFilterSQL/util/getFilterQuery.js +64 -64
  89. package/server/routes/data/controllers/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  90. package/server/routes/data/controllers/funcs/getFilterSQL/util/getTableSql.js +34 -34
  91. package/server/routes/data/controllers/tableData.js +20 -20
  92. package/server/routes/data/controllers/tableDataId.js +27 -27
  93. package/server/routes/data/controllers/tableFilter.js +63 -63
  94. package/server/routes/data/controllers/tokenInfo.js +9 -9
  95. package/server/routes/data/controllers/utils/assignTokens.js +30 -30
  96. package/server/routes/data/controllers/utils/getColumns.js +8 -8
  97. package/server/routes/data/index.mjs +17 -17
  98. package/server/routes/data/schema.js +54 -54
  99. package/server/routes/menu/controllers/getMenu.js +67 -67
  100. package/server/routes/menu/index.mjs +5 -5
  101. package/server/routes/notifications/controllers/readNotifications.js +27 -27
  102. package/server/routes/notifications/controllers/testEmail.js +35 -35
  103. package/server/routes/notifications/controllers/userNotifications.js +53 -53
  104. package/server/routes/notifications/funcs/addNotification.js +21 -21
  105. package/server/routes/notifications/funcs/sendNotification.js +105 -105
  106. package/server/routes/notifications/hook/onWidgetSet.js +57 -57
  107. package/server/routes/notifications/index.mjs +27 -27
  108. package/server/routes/notifications/schema.js +16 -16
  109. package/server/routes/properties/controllers/admin.properties.get.js +29 -29
  110. package/server/routes/properties/controllers/user.properties.get.js +30 -30
  111. package/server/routes/properties/controllers/user.properties.post.js +30 -30
  112. package/server/routes/properties/funcs/getSettings.js +56 -56
  113. package/server/routes/properties/funcs/setSettings.js +44 -44
  114. package/server/routes/properties/funcs/utils/dataInsert.js +26 -26
  115. package/server/routes/properties/index.mjs +14 -14
  116. package/server/routes/properties/schema.js +10 -10
  117. package/server/routes/root.mjs +3 -3
  118. package/server/routes/templates/controllers/getTemplate.js +26 -26
  119. package/server/routes/templates/index.mjs +16 -16
  120. package/server/routes/templates/schema.js +8 -8
  121. package/server/routes/user/controllers/user.cls.id.js +14 -14
  122. package/server/routes/user/controllers/user.cls.js +71 -71
  123. package/server/routes/user/controllers/user.cls.post.js +52 -52
  124. package/server/routes/user/controllers/user.info.js +17 -17
  125. package/server/routes/user/schema.js +14 -14
  126. package/server/routes/widget/controllers/utils/historyFormat.js +75 -75
  127. package/server/routes/widget/controllers/utils/obj2db.js +13 -13
  128. package/server/routes/widget/controllers/widget.del.js +41 -41
  129. package/server/routes/widget/controllers/widget.get.js +96 -96
  130. package/server/routes/widget/controllers/widget.set.js +76 -76
  131. package/server/routes/widget/index.mjs +11 -11
  132. package/server/routes/widget/schema.js +12 -12
  133. package/server/templates/cls/itree.recrzone_category.json +73 -73
  134. package/server/templates/cls/test.json +9 -9
  135. package/server/templates/form/admin.user_cls.data.form.json +49 -49
  136. package/server/templates/form/admin.user_group_rel.form.json +21 -21
  137. package/server/templates/form/cp_building.form.json +32 -32
  138. package/server/templates/form/form-user-pass.json +10 -10
  139. package/server/templates/form/form-user_group.json +39 -39
  140. package/server/templates/form/form-users.json +156 -156
  141. package/server/templates/form/user_group_access.form.json +22 -22
  142. package/server/templates/select/account_id.json +2 -2
  143. package/server/templates/table/gis.dataset.table.json +43 -43
  144. package/server/templates/table/management.user_group.table.json +112 -112
  145. package/server/templates/table/management.users.table.json +126 -126
  146. package/utils.js +29 -29
@@ -1,82 +1,82 @@
1
- import path from 'node:path';
2
-
3
- import { config, handlebars, setOpt, setToken, getTemplate, handlebarsSync } from "@opengis/fastify-table/utils.js";
4
- //import { setOpt, setToken } from "../../../../../fastify-table/utils.js";
5
- // import yaml from 'js-yaml';
6
- import getTableData from "./tableData.js";
7
-
8
- export default async function getCardData(req) {
9
- const {
10
- pg, params = {}, session = {}, user
11
- } = req;
12
- const { table, id } = params;
13
- const uid = config?.auth?.disable ? '1' : session.passport?.user?.uid;
14
-
15
- const time = Date.now();
16
-
17
- const template = await getTemplate('card', table);
18
-
19
- const tableTemplate = await getTemplate('table', table);
20
- const index = template?.find(el => el[0] === 'index.yml')?.[1] || {};
21
-
22
- const { message, rows = [] } = await getTableData({ pg, params: { table, id }, session, user });
23
-
24
- if (message) return { message };
25
-
26
- // tokens result
27
- const tokens = {};
28
- if (index?.tokens && typeof index?.tokens === 'object' && !Array.isArray(index?.tokens)) {
29
- for (let key in index?.tokens) {
30
- const item = index?.tokens[key];
31
- Object.keys(item).filter(el => item[el]?.includes?.('{{')).forEach(el => {
32
- item[el] = handlebarsSync.compile(item[el])({ user, uid: user?.uid, id, data: rows[0] });
33
- });
34
-
35
- const token = item.form && item.table ? setToken({
36
- ids: [JSON.stringify(item)],
37
- uid,
38
- array: 1,
39
- })[0] : setOpt(item, user.uid);
40
- tokens[key] = token;
41
- }
42
- }
43
-
44
- // vue result
45
- const vue = template
46
- .filter((el) => el[0].endsWith('.vue'))
47
- .reduce((acc, curr) => Object.assign(acc, { [path.parse(curr[0]).name]: curr[1].match(/<template[^>]*>([\s\S]*?)<\/template>/)[1].trim() }), {});
48
-
49
- // data result
50
- const data = {};
51
- if (index?.data && index?.data?.[0]?.name) {
52
- await Promise.all(index.data.filter((el) => el?.name && el?.sql).map(async (el) => {
53
- const q = handlebarsSync.compile(el.sql)({ user, uid: user?.uid, id });
54
- const { rows = [] } = await pg.query(q);
55
- data[el.name] = rows;
56
- }));
57
- }
58
-
59
-
60
-
61
- // html result
62
- const html = {};
63
- if (template) {
64
- await Promise.all(template.filter(el => el[0].includes('.hbs')).map(async (el) => {
65
- const htmlContent = await handlebars.compile(el[1])({ ...rows[0], user, data, tokens });
66
- const name = el[0].substring(0, el[0].lastIndexOf('.'))
67
- html[name] = htmlContent;
68
- }));
69
- }
70
-
71
- return {
72
- time: Date.now() - time,
73
- ...index,
74
- tokens,
75
- vue,
76
- data,
77
- html,
78
- rows,
79
- columns: tableTemplate?.columns || tableTemplate?.colModel,
80
- };
81
-
1
+ import path from 'node:path';
2
+
3
+ import { config, handlebars, setOpt, setToken, getTemplate, handlebarsSync } from "@opengis/fastify-table/utils.js";
4
+ //import { setOpt, setToken } from "../../../../../fastify-table/utils.js";
5
+ // import yaml from 'js-yaml';
6
+ import getTableData from "./tableData.js";
7
+
8
+ export default async function getCardData(req) {
9
+ const {
10
+ pg, params = {}, session = {}, user
11
+ } = req;
12
+ const { table, id } = params;
13
+ const uid = session.passport?.user?.uid;
14
+
15
+ const time = Date.now();
16
+
17
+ const template = await getTemplate('card', table);
18
+
19
+ const tableTemplate = await getTemplate('table', table);
20
+ const index = template?.find(el => el[0] === 'index.yml')?.[1] || {};
21
+
22
+ const { message, rows = [] } = await getTableData({ pg, params: { table, id }, session, user });
23
+
24
+ if (message) return { message };
25
+
26
+ // tokens result
27
+ const tokens = {};
28
+ if (index?.tokens && typeof index?.tokens === 'object' && !Array.isArray(index?.tokens)) {
29
+ for (let key in index?.tokens) {
30
+ const item = index?.tokens[key];
31
+ Object.keys(item).filter(el => item[el]?.includes?.('{{')).forEach(el => {
32
+ item[el] = handlebarsSync.compile(item[el])({ user, uid: user?.uid, id, data: rows[0] });
33
+ });
34
+
35
+ const token = item.form && item.table ? setToken({
36
+ ids: [JSON.stringify(item)],
37
+ uid,
38
+ array: 1,
39
+ })[0] : setOpt(item, user.uid);
40
+ tokens[key] = token;
41
+ }
42
+ }
43
+
44
+ // vue result
45
+ const vue = template
46
+ .filter((el) => el[0].endsWith('.vue'))
47
+ .reduce((acc, curr) => Object.assign(acc, { [path.parse(curr[0]).name]: curr[1].match(/<template[^>]*>([\s\S]*?)<\/template>/)[1].trim() }), {});
48
+
49
+ // data result
50
+ const data = {};
51
+ if (index?.data && index?.data?.[0]?.name) {
52
+ await Promise.all(index.data.filter((el) => el?.name && el?.sql).map(async (el) => {
53
+ const q = handlebarsSync.compile(el.sql)({ user, uid: user?.uid, id });
54
+ const { rows = [] } = await pg.query(q);
55
+ data[el.name] = rows;
56
+ }));
57
+ }
58
+
59
+
60
+
61
+ // html result
62
+ const html = {};
63
+ if (template) {
64
+ await Promise.all(template.filter(el => el[0].includes('.hbs')).map(async (el) => {
65
+ const htmlContent = await handlebars.compile(el[1])({ ...rows[0], user, data, tokens });
66
+ const name = el[0].substring(0, el[0].lastIndexOf('.'))
67
+ html[name] = htmlContent;
68
+ }));
69
+ }
70
+
71
+ return {
72
+ time: Date.now() - time,
73
+ ...index,
74
+ tokens,
75
+ vue,
76
+ data,
77
+ html,
78
+ rows,
79
+ columns: tableTemplate?.columns || tableTemplate?.colModel,
80
+ };
81
+
82
82
  }
@@ -1,50 +1,50 @@
1
- import { config, handlebars, getToken, getTemplate, getMeta } from "@opengis/fastify-table/utils.js";
2
-
3
- import getTableData from "./tableData.js";
4
-
5
- export default async function cardTabData({
6
- pg, query = {}, params = {}, session = {},
7
- }) {
8
- const { token } = params;
9
- const { sql } = query;
10
-
11
- if (!token) {
12
- return { message: 'not enough params: token', status: 400 };
13
- }
14
-
15
- const time = Date.now();
16
- const { uid } = config?.auth?.disable ? { uid: '1' } : (session.passport?.user || {});
17
-
18
- const props = await getToken({
19
- token,
20
- mode: 'w',
21
- uid,
22
- json: 1,
23
- });
24
-
25
- if (!props || !props?.table || !props) {
26
- return { message: { error: 'token is invalid', props }, status: 403 };
27
- }
28
-
29
- const tableTemplate = await getTemplate('table', props.table);
30
-
31
- const { columns = [] } = tableTemplate?.columns || tableTemplate?.colModel
32
- ? { columns: tableTemplate?.columns || tableTemplate?.colModel }
33
- : await getMeta({ table: props.table });
34
-
35
- const filter = await handlebars.compile(props.query.replace(/@id/g, '{{id}}'))(props);
36
- const { message, rows = [] } = await getTableData({ pg, params: { table: props.table }, session, query: { filter, sql } });
37
- if (message) return { message };
38
-
39
- if (props.columns?.length && rows?.length) {
40
- const allowList = ['id', 'token'].concat(props.columns).map((el) => [el, `${el}_text`]).flat();
41
- rows.forEach((row) => Object.keys(row).forEach((el) => { if (!allowList.includes(el)) delete row[el]; }));
42
-
43
- return {
44
- time: Date.now() - time,
45
- rows,
46
- columns: columns.filter((col) => props.columns.includes(col.name)),
47
- };
48
- }
49
- return { time: Date.now() - time, rows, columns };
1
+ import { config, handlebars, getToken, getTemplate, getMeta } from "@opengis/fastify-table/utils.js";
2
+
3
+ import getTableData from "./tableData.js";
4
+
5
+ export default async function cardTabData({
6
+ pg, query = {}, params = {}, session = {},
7
+ }) {
8
+ const { token } = params;
9
+ const { sql } = query;
10
+
11
+ if (!token) {
12
+ return { message: 'not enough params: token', status: 400 };
13
+ }
14
+
15
+ const time = Date.now();
16
+ const { uid } = config?.auth?.disable ? { uid: '1' } : (session.passport?.user || {});
17
+
18
+ const props = await getToken({
19
+ token,
20
+ mode: 'w',
21
+ uid,
22
+ json: 1,
23
+ });
24
+
25
+ if (!props || !props?.table || !props) {
26
+ return { message: { error: 'token is invalid', props }, status: 403 };
27
+ }
28
+
29
+ const tableTemplate = await getTemplate('table', props.table);
30
+
31
+ const { columns = [] } = tableTemplate?.columns || tableTemplate?.colModel
32
+ ? { columns: tableTemplate?.columns || tableTemplate?.colModel }
33
+ : await getMeta({ table: props.table });
34
+
35
+ const filter = await handlebars.compile(props.query.replace(/@id/g, '{{id}}'))(props);
36
+ const { message, rows = [] } = await getTableData({ pg, params: { table: props.table }, session, query: { filter, sql } });
37
+ if (message) return { message };
38
+
39
+ if (props.columns?.length && rows?.length) {
40
+ const allowList = ['id', 'token'].concat(props.columns).map((el) => [el, `${el}_text`]).flat();
41
+ rows.forEach((row) => Object.keys(row).forEach((el) => { if (!allowList.includes(el)) delete row[el]; }));
42
+
43
+ return {
44
+ time: Date.now() - time,
45
+ rows,
46
+ columns: columns.filter((col) => props.columns.includes(col.name)),
47
+ };
48
+ }
49
+ return { time: Date.now() - time, rows, columns };
50
50
  }
@@ -1,92 +1,92 @@
1
- import { getPG, getTemplate } from "@opengis/fastify-table/utils.js";
2
- // import pgClients from '@opengis/fastify-table/pg/funcs/pgClients.js';
3
-
4
- import getTableSql from './util/getTableSql.js';
5
- import getFilterQuery from './util/getFilterQuery.js';
6
- import getOptimizedQuery from './util/getOptimizedQuery.js';
7
-
8
- async function getFilterSQL({
9
- pg: pg1, table, filter, search, filterList, query, uid,
10
- }) {
11
- if (!table) return { error: 'param table is required', status: 400 };
12
-
13
- const pg = pg1 || getPG();
14
- const body = await getTemplate('table', table);
15
-
16
- const sqlList = body?.sql?.length
17
- ? body?.sql?.filter((el) => !el.disabled && el?.sql?.replace)
18
- .map((el, i) => {
19
- Object.assign(el, { name: el.name || `t${i + 1}` });
20
- return ` left join lateral (${el.filter ? el.sql.replace(/limit 1/ig, '') : el.sql}) as ${el.name} on 1=1 `;
21
- }).join(' ')
22
- : '';
23
- const fieldQuery = `select * from ${body?.table || table} ${sqlList ? ` t ${sqlList}` : ''} where 1=1 limit 0`;
24
- const { fields = [] } = await pg.query(fieldQuery);
25
-
26
- const { fields: fieldsModel } = pg.pk[body?.table] ? await pg.query(`select * from ${body.table} limit 0`) : {};
27
-
28
- const autoSearchColumn = fields?.filter((el) => pg.pgType?.[el.dataTypeID] === 'text')?.map((el) => el.name).join(',');
29
- const searchColumn = body?.search_column || autoSearchColumn;
30
- const fieldsList = (fieldsModel || fields)?.map((el) => el.name);
31
- try {
32
- const tableSQL = await getTableSql({
33
- pg, body, table, fields,
34
- });
35
- const sval = `ilike '%${decodeURIComponent(search).replace(/'/g, "''")}%'`;
36
- const searchQuery = search && searchColumn
37
- ? ` (${searchColumn.split(',')?.map((name) => {
38
- const { pk } = tableSQL.find((el) => el.name === name) || {};
39
- return pk && !fieldsList.includes(name) ? `${pk} in (select ${pk} from (${fieldQuery})q where ${name} ${sval})` : `${name} ${sval}`;
40
- }).join(' or ')} )` : '';
41
-
42
- // table_properties - user filter data
43
- const { rows: filterProperties = [] } = await pg.query(`select column_id, name, title, format, data from admin.custom_column where entity=$1 and uid=$2`, [table, uid]);
44
- const extraFilters = filterProperties.map((row) => ({ ua: row.title, name: row.name, type: row.format, data: row.data, extra: true }));
45
- const customFilters = [];
46
-
47
- const filterList1 = await Promise.all((filterList || (body?.filter_list || []).concat(body?.filterInline || []).concat(body?.filterCustom || []).concat(body?.filterState || []).concat(body?.filterList || []).concat(body?.filters || [])).concat(extraFilters || []).concat(customFilters || [])
48
-
49
- ?.map(async (el) => {
50
- if (!el?.data) return el;
51
- const cls = await getTemplate(['cls', 'select'], el.data);
52
- if (Array.isArray(cls) && cls?.length) {
53
- Object.assign(el, { options: cls });
54
- } else if (typeof (cls?.sql || cls) === 'string') {
55
- Object.assign(el, { sql: cls?.sql || cls });
56
- }
57
- return el;
58
- })
59
- );
60
- const filters = getFilterQuery({
61
- pg,
62
- filter,
63
- table: body?.table || table,
64
- tableSQL,
65
- fields,
66
- filterList: filterList1,
67
- });
68
-
69
- // filter
70
- const filterQuery = filters?.filter((el) => el.query)?.map((el) => `${el.query} `).join(' and ');
71
- const q = [body?.query, query, searchQuery, filterQuery].filter((el) => el).join(' and ');
72
-
73
- // table
74
- const modelQuery = body?.model || body?.table || table;
75
- const optimizedSQL = `select * from ${getOptimizedQuery({ body, table, q })} `;
76
- const tableCount = getOptimizedQuery({ body, table, q }, true);
77
- // console.log(optimizedSQL);
78
- return {
79
- filterList,
80
- q,
81
- optimizedSQL,
82
- tableCount,
83
- table: modelQuery,
84
- searchQuery,
85
- };
86
- }
87
- catch (err) {
88
- throw new Error(err.toString());
89
- }
90
- }
91
-
92
- export default getFilterSQL;
1
+ import { getPG, getTemplate } from "@opengis/fastify-table/utils.js";
2
+ // import pgClients from '@opengis/fastify-table/pg/funcs/pgClients.js';
3
+
4
+ import getTableSql from './util/getTableSql.js';
5
+ import getFilterQuery from './util/getFilterQuery.js';
6
+ import getOptimizedQuery from './util/getOptimizedQuery.js';
7
+
8
+ async function getFilterSQL({
9
+ pg: pg1, table, filter, search, filterList, query, uid,
10
+ }) {
11
+ if (!table) return { error: 'param table is required', status: 400 };
12
+
13
+ const pg = pg1 || getPG();
14
+ const body = await getTemplate('table', table);
15
+
16
+ const sqlList = body?.sql?.length
17
+ ? body?.sql?.filter((el) => !el.disabled && el?.sql?.replace)
18
+ .map((el, i) => {
19
+ Object.assign(el, { name: el.name || `t${i + 1}` });
20
+ return ` left join lateral (${el.filter ? el.sql.replace(/limit 1/ig, '') : el.sql}) as ${el.name} on 1=1 `;
21
+ }).join(' ')
22
+ : '';
23
+ const fieldQuery = `select * from ${body?.table || table} ${sqlList ? ` t ${sqlList}` : ''} where 1=1 limit 0`;
24
+ const { fields = [] } = await pg.query(fieldQuery);
25
+
26
+ const { fields: fieldsModel } = pg.pk[body?.table] ? await pg.query(`select * from ${body.table} limit 0`) : {};
27
+
28
+ const autoSearchColumn = fields?.filter((el) => pg.pgType?.[el.dataTypeID] === 'text')?.map((el) => el.name).join(',');
29
+ const searchColumn = body?.search_column || autoSearchColumn;
30
+ const fieldsList = (fieldsModel || fields)?.map((el) => el.name);
31
+ try {
32
+ const tableSQL = await getTableSql({
33
+ pg, body, table, fields,
34
+ });
35
+ const sval = `ilike '%${decodeURIComponent(search).replace(/'/g, "''")}%'`;
36
+ const searchQuery = search && searchColumn
37
+ ? ` (${searchColumn.split(',')?.map((name) => {
38
+ const { pk } = tableSQL.find((el) => el.name === name) || {};
39
+ return pk && !fieldsList.includes(name) ? `${pk} in (select ${pk} from (${fieldQuery})q where ${name} ${sval})` : `${name} ${sval}`;
40
+ }).join(' or ')} )` : '';
41
+
42
+ // table_properties - user filter data
43
+ const { rows: filterProperties = [] } = await pg.query(`select column_id, name, title, format, data from admin.custom_column where entity=$1 and uid=$2`, [table, uid]);
44
+ const extraFilters = filterProperties.map((row) => ({ ua: row.title, name: row.name, type: row.format, data: row.data, extra: true }));
45
+ const customFilters = [];
46
+
47
+ const filterList1 = await Promise.all((filterList || (body?.filter_list || []).concat(body?.filterInline || []).concat(body?.filterCustom || []).concat(body?.filterState || []).concat(body?.filterList || []).concat(body?.filters || [])).concat(extraFilters || []).concat(customFilters || [])
48
+
49
+ ?.map(async (el) => {
50
+ if (!el?.data) return el;
51
+ const cls = await getTemplate(['cls', 'select'], el.data);
52
+ if (Array.isArray(cls) && cls?.length) {
53
+ Object.assign(el, { options: cls });
54
+ } else if (typeof (cls?.sql || cls) === 'string') {
55
+ Object.assign(el, { sql: cls?.sql || cls });
56
+ }
57
+ return el;
58
+ })
59
+ );
60
+ const filters = getFilterQuery({
61
+ pg,
62
+ filter,
63
+ table: body?.table || table,
64
+ tableSQL,
65
+ fields,
66
+ filterList: filterList1,
67
+ });
68
+
69
+ // filter
70
+ const filterQuery = filters?.filter((el) => el.query)?.map((el) => `${el.query} `).join(' and ');
71
+ const q = [body?.query, query, searchQuery, filterQuery].filter((el) => el).join(' and ');
72
+
73
+ // table
74
+ const modelQuery = body?.model || body?.table || table;
75
+ const optimizedSQL = `select * from ${getOptimizedQuery({ body, table, q })} `;
76
+ const tableCount = getOptimizedQuery({ body, table, q }, true);
77
+ // console.log(optimizedSQL);
78
+ return {
79
+ filterList,
80
+ q,
81
+ optimizedSQL,
82
+ tableCount,
83
+ table: modelQuery,
84
+ searchQuery,
85
+ };
86
+ }
87
+ catch (err) {
88
+ throw new Error(err.toString());
89
+ }
90
+ }
91
+
92
+ export default getFilterSQL;