@opengis/admin 0.2.122 → 0.2.124
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/dist/{add-page-D9BweG1f.js → add-page-CNaov3n_.js} +1 -1
- package/dist/{admin-interface-Bq8kf59d.js → admin-interface-Bfl5z6ck.js} +582 -732
- package/dist/{admin-view-pmWjnncn.js → admin-view-DYePX_Un.js} +1 -1
- package/dist/admin.js +2 -2
- package/dist/admin.umd.cjs +50 -50
- package/dist/{card-view-DNKItKZ_.js → card-view-OkM8SWMi.js} +1 -1
- package/dist/{edit-page-DmanLFQC.js → edit-page-BXf-hnHj.js} +1 -1
- package/dist/{import-file-B3hz_TTe.js → import-file-Cds3w-U5.js} +15024 -14822
- package/dist/{profile-page-CnXrMOV_.js → profile-page-B7ofAYKr.js} +1 -1
- package/dist/style.css +1 -1
- package/module/settings/menu.json +21 -13
- package/package.json +3 -3
- package/server/plugins/hook.js +13 -1
- package/server/routes/data/controllers/cardData.js +122 -117
- package/server/routes/menu/controllers/getMenu.js +9 -3
- package/server/routes/print/controllers/printTemplate.add.js +37 -0
- package/server/routes/print/controllers/printTemplate.delete.js +29 -0
- package/server/routes/print/controllers/printTemplate.edit.js +42 -0
- package/server/routes/print/controllers/printTemplate.js +24 -79
- package/server/routes/print/controllers/printTemplateList.js +20 -0
- package/server/routes/print/controllers/printTemplatePreview.js +81 -0
- package/server/routes/print/index.mjs +14 -2
- package/server/routes/properties/controllers/admin.properties.post.js +2 -2
- package/server/routes/properties/index.mjs +1 -1
- package/server/routes/report/controllers/data.js +76 -0
- package/server/routes/report/controllers/list.js +18 -0
- package/server/routes/report/index.mjs +7 -0
- package/server/routes/report/utils/formatValue.js +179 -0
- package/server/routes/report/utils/getFilterQuery.js +67 -0
@@ -0,0 +1,81 @@
|
|
1
|
+
import { createHash } from 'node:crypto';
|
2
|
+
|
3
|
+
import { config, getFilterSQL, getTemplate, handlebars, pgClients } from '@opengis/fastify-table/utils.js';
|
4
|
+
import { grpc } from '@opengis/fastify-file/utils.js';
|
5
|
+
|
6
|
+
const { htmlToPdf } = grpc();
|
7
|
+
|
8
|
+
export default async function printTemplate(req, reply) {
|
9
|
+
const {
|
10
|
+
pg = pgClients.client,
|
11
|
+
params = {},
|
12
|
+
query = {},
|
13
|
+
} = req;
|
14
|
+
|
15
|
+
if (!params?.name) {
|
16
|
+
return reply.status(404).send('not enough params: name');
|
17
|
+
}
|
18
|
+
|
19
|
+
const { id, name, title, body = '', route } = await pg.query(`select template_id as id, name, title, body, route_id as route from admin.templates where $1 in (template_id,name)`, [params.name])
|
20
|
+
.then(el => el.rows?.[0] || {});
|
21
|
+
|
22
|
+
if (!id) {
|
23
|
+
return reply.status(404).send('template not found');
|
24
|
+
}
|
25
|
+
|
26
|
+
/* -- params.name === document template -- */
|
27
|
+
const format = query.format || (config.debug ? 'html' : 'pdf');
|
28
|
+
|
29
|
+
const hash = createHash('md5')
|
30
|
+
.update([query?.preview, query?.demo, params?.name].join())
|
31
|
+
.digest('hex');
|
32
|
+
|
33
|
+
const headers = format === 'pdf'
|
34
|
+
? { 'Content-Disposition': `inline; filename=${hash}.pdf`, 'Content-Type': 'application/pdf' }
|
35
|
+
: { 'Content-Type': 'text/html; charset=utf-8' };
|
36
|
+
|
37
|
+
if (query?.preview) {
|
38
|
+
const matches = body?.match?.(/{{(?!\!)([^}]*)}}/g) || [];
|
39
|
+
const preview = `<style> #toggle { background: yellow; } </style>`
|
40
|
+
+ matches.reduce((acc, curr) => acc.replace(curr, curr.replace(/{{(?!\!)([^}]*)}}/g, `<div id="toggle">${curr.replace(/{/g, '%7B').replace(/}/g, '%7D')}</div>`)), body);
|
41
|
+
|
42
|
+
const html = await handlebars.compile(preview)({});
|
43
|
+
|
44
|
+
if (format == 'html') {
|
45
|
+
return reply.headers(headers).send(html.replace(/%7B/g, '{').replace(/%7D/g, '}'));
|
46
|
+
}
|
47
|
+
|
48
|
+
const result = await htmlToPdf({ html: html.replace(/%7B/g, '{').replace(/%7D/g, '}') });
|
49
|
+
const buffer = Buffer.from(result.result, 'base64');
|
50
|
+
return reply.headers(headers).send(buffer);
|
51
|
+
}
|
52
|
+
|
53
|
+
if (query?.demo) {
|
54
|
+
const table = await pg.query(`select alias from admin.routes where route_id=$1`, [route])
|
55
|
+
.then(el => el.rows?.[0]?.alias);
|
56
|
+
|
57
|
+
if (!table) {
|
58
|
+
return reply.status(404).send('route table not found');
|
59
|
+
}
|
60
|
+
|
61
|
+
const loadTable = await getTemplate('table', table);
|
62
|
+
const { optimizedSQL } = await getFilterSQL({ table, pg });
|
63
|
+
|
64
|
+
const q = `select * from (${optimizedSQL})q where ${loadTable?.query || '1=1'} limit 1`;
|
65
|
+
|
66
|
+
const obj = await pg.query(q).then(el => el.rows?.[0] || {});
|
67
|
+
|
68
|
+
const html = await handlebars.compile(body)(obj);
|
69
|
+
|
70
|
+
if (format == 'html') {
|
71
|
+
return reply.headers(headers).send(html);
|
72
|
+
}
|
73
|
+
|
74
|
+
const result = await htmlToPdf({ html });
|
75
|
+
const buffer = Buffer.from(result.result, 'base64');
|
76
|
+
return reply.headers(headers).send(buffer);
|
77
|
+
}
|
78
|
+
|
79
|
+
// for body edit
|
80
|
+
return { name, title, html: body, route };
|
81
|
+
}
|
@@ -1,7 +1,19 @@
|
|
1
1
|
import cardPrint from './controllers/cardPrint.js';
|
2
|
+
import printTemplateAdd from './controllers/printTemplate.add.js';
|
3
|
+
import printTemplateEdit from './controllers/printTemplate.edit.js';
|
4
|
+
import printTemplateDelete from './controllers/printTemplate.delete.js';
|
2
5
|
import printTemplate from './controllers/printTemplate.js';
|
6
|
+
import printTemplatePreview from './controllers/printTemplatePreview.js';
|
7
|
+
import printTemplateList from './controllers/printTemplateList.js';
|
8
|
+
|
9
|
+
const policy = ['user'];
|
3
10
|
|
4
11
|
export default async function route(app) {
|
5
|
-
app.get(`/card-print/:table/:id`, cardPrint);
|
6
|
-
app.get('/print-template/:name/:id
|
12
|
+
app.get(`/card-print/:table/:id`, { config: { policy } }, cardPrint);
|
13
|
+
app.get('/print-template/:name/:id', { config: { policy } }, printTemplate);
|
14
|
+
app.get('/print-template/:name', { config: { policy } }, printTemplatePreview);
|
15
|
+
app.get('/print-template', { config: { policy } }, printTemplateList);
|
16
|
+
app.post('/print-template', { config: { policy } }, printTemplateAdd);
|
17
|
+
app.put('/print-template/:id', { config: { policy } }, printTemplateEdit);
|
18
|
+
app.delete('/print-template/:id', { config: { policy } }, printTemplateDelete);
|
7
19
|
}
|
@@ -3,9 +3,9 @@ const table = 'admin.properties';
|
|
3
3
|
import { setSettings } from '../../../../utils.js';
|
4
4
|
|
5
5
|
export default async function postSettingsApp({
|
6
|
-
pg,
|
6
|
+
pg, user = {}, body = {},
|
7
7
|
}) {
|
8
|
-
if (
|
8
|
+
if (!user?.user_type?.includes?.('admin')) {
|
9
9
|
return { message: 'access restricted', status: 403 };
|
10
10
|
}
|
11
11
|
const { key, val } = body;
|
@@ -11,7 +11,7 @@ import { propertiesSchema } from './schema.js';
|
|
11
11
|
|
12
12
|
export default async function route(fastify) {
|
13
13
|
fastify.get('/settings-app/:key?', { scheme: propertiesSchema }, getSettingsApp);
|
14
|
-
fastify.post('/settings-app', { config: { policy: ['
|
14
|
+
fastify.post('/settings-app', { config: { policy: ['admin'] } }, postSettingsApp);
|
15
15
|
|
16
16
|
fastify.get('/settings-user/:key?', { scheme: propertiesSchema }, getSettingsUser);
|
17
17
|
fastify.post('/settings-user', {}, postSettingsUser);
|
@@ -0,0 +1,76 @@
|
|
1
|
+
import { handlebars, pgClients, getTemplate, metaFormat, handlebarsSync } from '@opengis/fastify-table/utils.js';
|
2
|
+
|
3
|
+
import getFilterQuery from '../utils/getFilterQuery.js';
|
4
|
+
|
5
|
+
const maxLimit = 100;
|
6
|
+
|
7
|
+
export default async function reportData({
|
8
|
+
pg = pgClients.client, params = {}, query = {}, user = {},
|
9
|
+
}) {
|
10
|
+
if (!params?.name) {
|
11
|
+
return { message: 'not enough params: name', status: 400 };
|
12
|
+
}
|
13
|
+
|
14
|
+
const loadTemplate = await getTemplate('report', params.name);
|
15
|
+
|
16
|
+
if (!loadTemplate?.sql) {
|
17
|
+
return { message: `report not found: ${params.name}`, status: 404 };
|
18
|
+
}
|
19
|
+
|
20
|
+
const { uid } = user;
|
21
|
+
|
22
|
+
const { kpi, sql, meta, filters } = loadTemplate;
|
23
|
+
const { date, columns: metaColumns } = meta || {};
|
24
|
+
|
25
|
+
const granularity = query.granularity && date && false ? `date_trunc('${query.granularity}',${date})::date::text` : null;
|
26
|
+
const groupby = [meta?.groupby, granularity].filter(el => el).join(',');
|
27
|
+
|
28
|
+
const period = query.period && date ? `${date}=${query.period}` : null;
|
29
|
+
const filter = [query.filter, period].filter(el => el).join(';');
|
30
|
+
|
31
|
+
const limit = Math.min(maxLimit, +(query.limit || 20));
|
32
|
+
const offset = query.page && query.page > 0 ? (query.page - 1) * limit : 0;
|
33
|
+
|
34
|
+
const kpiColumns = kpi ? kpi?.map(el => `${el.sql || 'count(*)'} as "${el.name}"`)?.join(',') : '';
|
35
|
+
const columnsList = metaColumns || loadTemplate.columns?.map(el => el.name)?.join(',');
|
36
|
+
|
37
|
+
const qMeta = await handlebars.compile(sql)({ user, uid });
|
38
|
+
const { fields = [] } = await pg.query(`select * from (${qMeta})q limit 0`);
|
39
|
+
|
40
|
+
const where = filter ? getFilterQuery({ pg, filter, fields, filterList: filters })?.map?.(el => el.query)?.join(' and ') : null;
|
41
|
+
|
42
|
+
const qAgg = `select ${kpiColumns || ''} from (${qMeta})q where ${where || '1=1'}`;
|
43
|
+
const q = `select ${columnsList || '*'} from (${qMeta})q where ${where || '1=1'} ${groupby ? `group by ${groupby}` : ''} limit ${limit} offset ${offset}`;
|
44
|
+
|
45
|
+
if (query.sql && user?.user_type?.includes('admin')) return `${qAgg};${q}`;
|
46
|
+
|
47
|
+
const kpiData = await pg.query(qAgg).then(el => el.rows?.[0] || {});
|
48
|
+
kpi?.forEach(el => Object.assign(el, { count: kpiData[el.name] || '0' }));
|
49
|
+
|
50
|
+
const { rows = [] } = await pg.query(q);
|
51
|
+
|
52
|
+
const cls = meta?.cls || loadTemplate.columns
|
53
|
+
?.filter(el => (el.option || el.data))
|
54
|
+
?.reduce((acc, curr) => Object.assign(acc, { [curr.name]: (curr.option || curr.data) }), {});
|
55
|
+
|
56
|
+
const titles = meta?.titles
|
57
|
+
|| loadTemplate.columns?.reduce((acc, curr) => Object.assign(acc, { [curr.name]: curr.title }), {});
|
58
|
+
|
59
|
+
await metaFormat({ rows, cls, sufix: true });
|
60
|
+
|
61
|
+
const columns = loadTemplate.columns
|
62
|
+
|| fields.map(el => ({
|
63
|
+
name: el.name,
|
64
|
+
title: titles?.[el.name] || el.name,
|
65
|
+
type: cls?.[el.name] ? 'Autocomplete' : 'Text',
|
66
|
+
format: pg.pgType?.[el.dataTypeID],
|
67
|
+
data: cls?.[el.name],
|
68
|
+
}));
|
69
|
+
|
70
|
+
return {
|
71
|
+
q: user?.user_type?.includes('admin') ? q : undefined,
|
72
|
+
kpi,
|
73
|
+
rows,
|
74
|
+
columns,
|
75
|
+
};
|
76
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { getTemplatePath, getTemplate } from '@opengis/fastify-table/utils.js';
|
2
|
+
|
3
|
+
export default async function reportList({
|
4
|
+
user = {},
|
5
|
+
}) {
|
6
|
+
const arr = getTemplatePath('report').filter(el => el[2] === 'json').map(el => el[0]);
|
7
|
+
|
8
|
+
const rows = await Promise.all(arr.map(async (el) => {
|
9
|
+
const loadTemplate = await getTemplate('report', el);
|
10
|
+
return {
|
11
|
+
name: el,
|
12
|
+
filters: loadTemplate?.filters || [],
|
13
|
+
title: loadTemplate?.title || el,
|
14
|
+
sql: user.user_type?.includes('admin') ? loadTemplate?.sql : undefined,
|
15
|
+
};
|
16
|
+
}));
|
17
|
+
return { rows };
|
18
|
+
}
|
@@ -0,0 +1,179 @@
|
|
1
|
+
const dateTypeList = ['date', 'timestamp', 'timestamp without time zone', 'timestamp with time zone'];
|
2
|
+
const numberTypeList = ['float8', 'int4', 'int8', 'numeric', 'double precision', 'integer'];
|
3
|
+
const isValidDate = (dateStr) => {
|
4
|
+
const [dd, mm, yyyy] = dateStr.split('.');
|
5
|
+
return new Date(mm + '/' + dd + '/' + yyyy).toString() !== 'Invalid Date';
|
6
|
+
};
|
7
|
+
|
8
|
+
function dt(y, m, d) {
|
9
|
+
return new Date(Date.UTC(y, m, d)).toISOString().slice(0, 10);
|
10
|
+
}
|
11
|
+
const dp = {
|
12
|
+
d: new Date().getDate(),
|
13
|
+
w: new Date().getDate() - (new Date().getDay() || 7) + 1,
|
14
|
+
m: new Date().getMonth(),
|
15
|
+
q: (new Date().getMonth() / 4).toFixed() * 3,
|
16
|
+
y: new Date().getFullYear(),
|
17
|
+
};
|
18
|
+
|
19
|
+
function formatDateISOString(date) {
|
20
|
+
if (!date?.includes('.')) return date;
|
21
|
+
const [day, month, year] = date.split('.');
|
22
|
+
return `${year}-${month}-${day}`;
|
23
|
+
}
|
24
|
+
|
25
|
+
function formatValue({
|
26
|
+
pg, filter = {}, name, value, operator = '=', dataTypeID, uid = 1, optimize,
|
27
|
+
}) {
|
28
|
+
const { data, sql, extra } = filter;
|
29
|
+
const pk = false;
|
30
|
+
|
31
|
+
if (!dataTypeID && !extra) return {};
|
32
|
+
const fieldType = extra ? pg.pgType?.[{ Date: 1114 }[filter?.type] || 25] : pg.pgType?.[dataTypeID];
|
33
|
+
if (!name || !value || !fieldType) return {};
|
34
|
+
const filterType = filter.type?.toLowerCase();
|
35
|
+
|
36
|
+
// current day, week, month, year etc.
|
37
|
+
if (dateTypeList.includes(fieldType) && !value?.includes('_') && ['cd', 'cw', 'cm', 'cq', 'cy'].includes(value)) {
|
38
|
+
const query = {
|
39
|
+
cd: `${name}::date = '${dt(dp.y, dp.m, dp.d)}'::date`,
|
40
|
+
cw: `${name}::date >= '${dt(dp.y, dp.m, dp.w)}'::date and ${name} <= '${dt(dp.y, dp.m, dp.w + 6)}'::date`,
|
41
|
+
cm: `${name}::date >= '${dt(dp.y, dp.m, 1)}'::date and ${name} <= '${dt(dp.y, dp.m + 1, 0)}'::date`,
|
42
|
+
cq: `${name}::date >= '${dt(dp.y, dp.q, 1)}'::date and ${name} <= '${dt(dp.y, dp.q + 3, 0)}'::date`,
|
43
|
+
cy: `${name}::date >= '${dt(dp.y, 0, 1)}'::date and ${name}::date <= '${dt(dp.y, 11, 31)}'::date`,
|
44
|
+
}[value];
|
45
|
+
return { op: '=', query, extra };
|
46
|
+
}
|
47
|
+
|
48
|
+
// date range
|
49
|
+
if (dateTypeList.includes(fieldType) && value?.includes('_')) {
|
50
|
+
const [min, max] = value.split('_');
|
51
|
+
const query = `${name} >= '${min}'::date and ${name} <= '${max}'::date`;
|
52
|
+
return { op: 'between', query, extra };
|
53
|
+
}
|
54
|
+
|
55
|
+
// v3 filter date range, example - "01.01.2024-31.12.2024"
|
56
|
+
if (dateTypeList.includes(fieldType) && value?.includes('.') && value?.indexOf('-') === 10 && value?.length === 21) {
|
57
|
+
const [startDate, endDate] = value.split('-');
|
58
|
+
const min = formatDateISOString(startDate);
|
59
|
+
const max = formatDateISOString(endDate);
|
60
|
+
|
61
|
+
if (!isValidDate(startDate) || !isValidDate(endDate)) {
|
62
|
+
return { op: 'between', query: 'false', extra };
|
63
|
+
}
|
64
|
+
const query = extra && pk
|
65
|
+
? `${pk} in (select object_id from crm.extra_data where property_key='${name}' and value_date::date >= '${min}'::date and value_date::date <= '${max}'::date)`
|
66
|
+
: `${name}::date >= '${min}'::date and ${name}::date <= '${max}'::date`;
|
67
|
+
return { op: 'between', query, extra };
|
68
|
+
}
|
69
|
+
|
70
|
+
// my rows
|
71
|
+
if (value === 'me' && uid && fieldType === 'text') {
|
72
|
+
return { op: '=', query: extra ? `uid = '${uid}'` : `${name}::text = '${uid}'`, extra };
|
73
|
+
}
|
74
|
+
|
75
|
+
const formatType = {
|
76
|
+
float8: 'numeric',
|
77
|
+
int4: 'numeric',
|
78
|
+
int8: 'numeric',
|
79
|
+
varchar: 'text',
|
80
|
+
bool: 'boolean',
|
81
|
+
geometry: 'geom',
|
82
|
+
}[fieldType] || 'text';
|
83
|
+
|
84
|
+
if (optimize && optimize.name !== optimize.pk) {
|
85
|
+
const val = filterType === 'text' ? `ilike '%${value}%'` : `= any('{${value}}')`;
|
86
|
+
return {
|
87
|
+
op: '~',
|
88
|
+
query: fieldType?.includes('[]')
|
89
|
+
? `${optimize.pk} && (select array_agg(${optimize.pk}) from ${optimize.table} where ${name} ${val} )`
|
90
|
+
: `${optimize.pk} in (select ${optimize.pk} from ${optimize.table} where ${name} ${val} )`,
|
91
|
+
extra,
|
92
|
+
};
|
93
|
+
}
|
94
|
+
|
95
|
+
if (fieldType?.includes('[]')) {
|
96
|
+
return { op: 'in', query: `'{${value}}'::text[] && ${name}::text[]`, extra };
|
97
|
+
}
|
98
|
+
|
99
|
+
// multiple items of 1 param
|
100
|
+
if (value?.indexOf(',') !== -1) {
|
101
|
+
const values = value.split(',').filter((el) => el !== 'null');
|
102
|
+
if (extra && pk) {
|
103
|
+
const query = value?.indexOf('null') !== -1
|
104
|
+
? `${pk} in (select object_id from crm.extra_data where property_key='${name}' and ( value_text is null or value_text in (${values?.map((el) => `'"${el}"'`).join(',')}) ) )`
|
105
|
+
: `${pk} in (select object_id from crm.extra_data where property_key='${name}' and value_text in (${values?.map((el) => `'"${el}"'`).join(',')}) )`;
|
106
|
+
return { op: 'in', query, extra };
|
107
|
+
}
|
108
|
+
const query = value?.indexOf('null') !== -1
|
109
|
+
? `( ${name} is null or ${name}::text in (${values?.map((el) => `'${el}'`).join(',')}) )`
|
110
|
+
: `${name}::text in (${value.split(',')?.map((el) => `'${el}'`).join(',')})`;
|
111
|
+
return { op: 'in', query, extra };
|
112
|
+
}
|
113
|
+
|
114
|
+
// v3 filter number range, example - "100_500"
|
115
|
+
if (numberTypeList.includes(fieldType) && value?.indexOf('_') !== -1) {
|
116
|
+
const [min, max] = value.split('_');
|
117
|
+
const query = (max === 'max' ? `${name} > ${min}` : null) || (min === 'min' ? `${name} <= ${max}` : null) || `${name} between ${min} and ${max}`;
|
118
|
+
return { op: 'between', query, extra };
|
119
|
+
}
|
120
|
+
|
121
|
+
// number range
|
122
|
+
if (numberTypeList.includes(fieldType) && value?.indexOf('-') !== -1) {
|
123
|
+
const [min, max] = value.split('-');
|
124
|
+
if (min === 'min' && max === 'max') return {};
|
125
|
+
const query = (max === 'max' ? `${name} > ${min}` : null) || (min === 'min' ? `${name} < ${max}` : null) || `${name} between ${min} and ${max}`;
|
126
|
+
return { op: 'between', query, extra };
|
127
|
+
}
|
128
|
+
|
129
|
+
if (['<', '>'].includes(operator)) {
|
130
|
+
const query = `${name} ${operator} '${value}'::${formatType}`;
|
131
|
+
return { op: operator, query, extra };
|
132
|
+
}
|
133
|
+
|
134
|
+
if (operator === '=' && filterType !== 'text' && !filter?.data) {
|
135
|
+
const query = {
|
136
|
+
null: `${name} is null`,
|
137
|
+
notnull: `${name} is not null`,
|
138
|
+
}[value] || `${name}::${formatType}='${value}'::${formatType}`;
|
139
|
+
return { op: '=', query, extra };
|
140
|
+
}
|
141
|
+
|
142
|
+
if (['~', '='].includes(operator)) {
|
143
|
+
const operator1 = (filterType === 'text' && (filter?.id || filter?.name) && operator === '=' ? '~' : operator);
|
144
|
+
const matchNull = { null: 'is null', notnull: 'is not null' }[value];
|
145
|
+
const match = matchNull || ((operator1 === '=' || filterType === 'autocomplete') ? `='${value}'` : `ilike '%${value}%'`);
|
146
|
+
if (extra && pk) {
|
147
|
+
const query = data && sql
|
148
|
+
? `${pk} in (select object_id from crm.extra_data where property_key='${name}' and value_text in ( ( with q(id,name) as (${sql}) select id from q where ${filterType === 'autocomplete' ? 'id' : 'name'} ${match})))`
|
149
|
+
: `${pk} in (select object_id from crm.extra_data where property_key='${name}' and value_text ${match})`;
|
150
|
+
return { op: 'ilike', query, extra };
|
151
|
+
}
|
152
|
+
|
153
|
+
const query = filter?.data && filter?.sql
|
154
|
+
? `${filter?.name || filter?.id} in ( ( with q(id,name) as (${filter?.sql}) select id from q where ${filterType === 'autocomplete' ? 'id' : 'name'}::text ${match}) )` // filter with cls
|
155
|
+
: `${name}::text ${match}`; // simple filter
|
156
|
+
// console.log(query);
|
157
|
+
return { op: 'ilike', query };
|
158
|
+
}
|
159
|
+
|
160
|
+
// json
|
161
|
+
if (name.includes('.')) {
|
162
|
+
const [col, prop] = name.split('.');
|
163
|
+
const query = ` ${col}->>'${prop}' in ('${value.join("','")}')`;
|
164
|
+
return { op: 'in', query, extra };
|
165
|
+
}
|
166
|
+
|
167
|
+
// geometry
|
168
|
+
if (['geometry'].includes(fieldType)) {
|
169
|
+
const bbox = value[0].split('_');
|
170
|
+
|
171
|
+
if (bbox?.length === 4) {
|
172
|
+
const query = ` ${name} && 'box(${bbox[0]} ${bbox[1]},${bbox[2]} ${bbox[3]})'::box2d `;
|
173
|
+
return { op: '&&', query, extra };
|
174
|
+
}
|
175
|
+
}
|
176
|
+
return {};
|
177
|
+
}
|
178
|
+
|
179
|
+
export default formatValue;
|
@@ -0,0 +1,67 @@
|
|
1
|
+
/* eslint-disable no-continue */
|
2
|
+
|
3
|
+
/**
|
4
|
+
* @param {Number} opt.json - (1|0) 1 - Результат - Object, 0 - String
|
5
|
+
* @param {String} opt.query - запит до таблиці
|
6
|
+
* @param {String} opt.hash - інформація з хешу по запиту
|
7
|
+
*/
|
8
|
+
|
9
|
+
import formatValue from './formatValue.js';
|
10
|
+
|
11
|
+
function getFilterQuery({ pg, filter: filterStr, fields, filterList }) {
|
12
|
+
if (!filterStr) return null; // filter list API
|
13
|
+
|
14
|
+
const mainOperators = ['=', '~', '>', '<'];
|
15
|
+
|
16
|
+
const filterQueryArray = decodeURIComponent(filterStr?.replace(/%/g, '%25').replace(/%/g, '\\%')?.replace(/(^,)|(,$)/g, '')).replace(/'/g, '').split(/[;|]/);
|
17
|
+
|
18
|
+
const resultList = [];
|
19
|
+
|
20
|
+
for (let i = 0; i < filterQueryArray.length; i += 1) {
|
21
|
+
const item = filterQueryArray[i];
|
22
|
+
const operator = mainOperators?.find((el) => item.indexOf(el) !== -1) || '=';
|
23
|
+
const [name] = item.split(operator);
|
24
|
+
|
25
|
+
// skip already added filter
|
26
|
+
if (resultList.find((el) => el.name === name)) {
|
27
|
+
continue;
|
28
|
+
}
|
29
|
+
|
30
|
+
// filter
|
31
|
+
const filter = filterList?.find((el) => [el.id, el.name].includes(name)) || { type: 'text' };
|
32
|
+
|
33
|
+
// find all value
|
34
|
+
const value = filterQueryArray.filter((el) => el.startsWith(name)).map((el) => el.substring(name.length + 1)).join(',');
|
35
|
+
|
36
|
+
if (filter?.query) {
|
37
|
+
resultList.push({
|
38
|
+
name, value, query: filter?.query, operator: '=', filterType: filter.type, type: 'text',
|
39
|
+
});
|
40
|
+
continue;
|
41
|
+
}
|
42
|
+
|
43
|
+
// find field and skip not exists
|
44
|
+
const { dataTypeID } = fields?.find((el) => el.name === name) || {};
|
45
|
+
|
46
|
+
// format query
|
47
|
+
const {
|
48
|
+
op, query, filterType, fieldType,
|
49
|
+
} = formatValue({
|
50
|
+
pg,
|
51
|
+
filter,
|
52
|
+
name,
|
53
|
+
value,
|
54
|
+
operator,
|
55
|
+
dataTypeID,
|
56
|
+
}) || {};
|
57
|
+
if (!query) continue;
|
58
|
+
|
59
|
+
resultList.push({
|
60
|
+
name, value, query, operator: op, filterType, type: fieldType,
|
61
|
+
});
|
62
|
+
}
|
63
|
+
|
64
|
+
return resultList;
|
65
|
+
}
|
66
|
+
|
67
|
+
export default getFilterQuery;
|