@opengis/fastify-table 1.2.67 → 1.2.69
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/README.md +86 -86
- package/package.json +1 -1
- package/server/migrations/cls.sql +39 -39
- package/server/plugins/cron/funcs/addCron.js +130 -130
- package/server/plugins/cron/index.js +6 -6
- package/server/plugins/crud/funcs/getOpt.js +13 -13
- package/server/plugins/crud/funcs/setOpt.js +21 -21
- package/server/plugins/crud/funcs/setToken.js +44 -44
- package/server/plugins/crud/funcs/utils/getFolder.js +11 -11
- package/server/plugins/crud/funcs/validateData.js +3 -3
- package/server/plugins/crud/index.js +23 -23
- package/server/plugins/hook/index.js +8 -8
- package/server/plugins/logger/errorStatus.js +19 -19
- package/server/plugins/logger/index.js +26 -26
- package/server/plugins/migration/index.js +7 -7
- package/server/plugins/pg/funcs/autoIndex.js +2 -2
- package/server/plugins/policy/sqlInjection.js +33 -33
- package/server/plugins/redis/client.js +8 -8
- package/server/plugins/redis/funcs/redisClients.js +3 -3
- package/server/plugins/redis/index.js +17 -17
- package/server/plugins/table/funcs/getData.js +3 -3
- package/server/plugins/table/funcs/getFilterSQL/index.js +68 -50
- package/server/plugins/table/funcs/getFilterSQL/util/formatValue.js +143 -119
- package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/server/plugins/table/funcs/getFilterSQL/util/getFilterQuery.js +9 -6
- package/server/plugins/table/funcs/getFilterSQL/util/getOptimizedQuery.js +5 -11
- package/server/plugins/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/server/plugins/table/funcs/getTemplates.js +19 -19
- package/server/plugins/table/funcs/gisIRColumn.js +82 -82
- package/server/plugins/table/funcs/loadTemplate.js +1 -1
- package/server/plugins/table/funcs/loadTemplatePath.js +1 -1
- package/server/plugins/table/funcs/userTemplateDir.js +1 -1
- package/server/plugins/table/index.js +13 -13
- package/server/plugins/util/index.js +7 -7
- package/server/routes/cron/index.js +14 -14
- package/server/routes/crud/controllers/table.js +92 -88
- package/server/routes/logger/controllers/logger.file.js +92 -92
- package/server/routes/logger/controllers/utils/checkUserAccess.js +19 -19
- package/server/routes/logger/controllers/utils/getRootDir.js +26 -26
- package/server/routes/logger/index.js +17 -17
- package/server/routes/properties/controllers/properties.add.js +55 -55
- package/server/routes/properties/controllers/properties.get.js +17 -17
- package/server/routes/properties/index.js +16 -16
- package/server/routes/table/controllers/data.js +12 -7
- package/server/routes/table/controllers/filter.js +21 -4
- package/server/routes/table/controllers/form.js +42 -42
- package/server/routes/table/controllers/search.js +74 -74
- package/server/routes/table/controllers/suggest.js +2 -2
- package/server/routes/table/index.js +29 -29
- package/server/routes/table/schema.js +64 -64
- package/server/routes/util/controllers/status.monitor.js +8 -8
- package/server/routes/util/index.js +11 -11
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import config from '../../../../../config.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
* @summary check user access to logger interface - per admin user type or user group
|
|
6
|
-
* @returns {Object} message, status
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export default function checkUserAccess({ user = {} }) {
|
|
10
|
-
// console.log(user);
|
|
11
|
-
if (user.user_type !== 'admin' && !config?.local) {
|
|
12
|
-
return { message: 'access restricted', status: 403 };
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/* if (!['admin', 'superadmin']?.includes(user.user_type) && count === '0') {
|
|
16
|
-
return { message: 'access restricted', status: 403 };
|
|
17
|
-
} */
|
|
18
|
-
return { message: 'access granted', status: 200 };
|
|
19
|
-
}
|
|
1
|
+
import config from '../../../../../config.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @summary check user access to logger interface - per admin user type or user group
|
|
6
|
+
* @returns {Object} message, status
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export default function checkUserAccess({ user = {} }) {
|
|
10
|
+
// console.log(user);
|
|
11
|
+
if (user.user_type !== 'admin' && !config?.local) {
|
|
12
|
+
return { message: 'access restricted', status: 403 };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/* if (!['admin', 'superadmin']?.includes(user.user_type) && count === '0') {
|
|
16
|
+
return { message: 'access restricted', status: 403 };
|
|
17
|
+
} */
|
|
18
|
+
return { message: 'access granted', status: 200 };
|
|
19
|
+
}
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
|
|
4
|
-
import config from '../../../../../config.js';
|
|
5
|
-
|
|
6
|
-
// import { existsSync } from 'fs';
|
|
7
|
-
let logDir = null;
|
|
8
|
-
export default function getRootDir() {
|
|
9
|
-
// absolute / relative path
|
|
10
|
-
if (logDir) return logDir;
|
|
11
|
-
const file = ['config.json', '/data/local/config.json'].find(el => (fs.existsSync(el) ? el : null));
|
|
12
|
-
const root = file === 'config.json' ? process.cwd() : '/data/local';
|
|
13
|
-
logDir = config.logDir || path.join(root, config.log?.dir || 'log');
|
|
14
|
-
console.log({ logDir });
|
|
15
|
-
return logDir;
|
|
16
|
-
|
|
17
|
-
// windows debug support
|
|
18
|
-
/* const customLogDir = process.cwd().includes(':') ? 'c:/data/local' : '/data/local';
|
|
19
|
-
// docker default path
|
|
20
|
-
if (existsSync(customLogDir)) {
|
|
21
|
-
return path.join(customLogDir, config.folder || '', 'log');
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// non-docker default path
|
|
25
|
-
return path.join(config.root || '/data/local', config.folder || '', 'log'); */
|
|
26
|
-
}
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import config from '../../../../../config.js';
|
|
5
|
+
|
|
6
|
+
// import { existsSync } from 'fs';
|
|
7
|
+
let logDir = null;
|
|
8
|
+
export default function getRootDir() {
|
|
9
|
+
// absolute / relative path
|
|
10
|
+
if (logDir) return logDir;
|
|
11
|
+
const file = ['config.json', '/data/local/config.json'].find(el => (fs.existsSync(el) ? el : null));
|
|
12
|
+
const root = file === 'config.json' ? process.cwd() : '/data/local';
|
|
13
|
+
logDir = config.logDir || path.join(root, config.log?.dir || 'log');
|
|
14
|
+
console.log({ logDir });
|
|
15
|
+
return logDir;
|
|
16
|
+
|
|
17
|
+
// windows debug support
|
|
18
|
+
/* const customLogDir = process.cwd().includes(':') ? 'c:/data/local' : '/data/local';
|
|
19
|
+
// docker default path
|
|
20
|
+
if (existsSync(customLogDir)) {
|
|
21
|
+
return path.join(customLogDir, config.folder || '', 'log');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// non-docker default path
|
|
25
|
+
return path.join(config.root || '/data/local', config.folder || '', 'log'); */
|
|
26
|
+
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import loggerFile from './controllers/logger.file.js';
|
|
2
|
-
|
|
3
|
-
// import loggerTest from './controllers/logger.test.api.js';
|
|
4
|
-
|
|
5
|
-
const loggerSchema = {
|
|
6
|
-
querystring: {
|
|
7
|
-
download: { type: 'string', pattern: '^(\\d+)$' },
|
|
8
|
-
full: { type: 'string', pattern: '^(\\d+)$' },
|
|
9
|
-
dir: { type: 'string', pattern: '^(\\d+)$' },
|
|
10
|
-
},
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
async function plugin(fastify) {
|
|
14
|
-
fastify.get('/logger-file/*', { config: { policy: ['site'] }, schema: loggerSchema }, loggerFile);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default plugin;
|
|
1
|
+
import loggerFile from './controllers/logger.file.js';
|
|
2
|
+
|
|
3
|
+
// import loggerTest from './controllers/logger.test.api.js';
|
|
4
|
+
|
|
5
|
+
const loggerSchema = {
|
|
6
|
+
querystring: {
|
|
7
|
+
download: { type: 'string', pattern: '^(\\d+)$' },
|
|
8
|
+
full: { type: 'string', pattern: '^(\\d+)$' },
|
|
9
|
+
dir: { type: 'string', pattern: '^(\\d+)$' },
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
async function plugin(fastify) {
|
|
14
|
+
fastify.get('/logger-file/*', { config: { policy: ['site'] }, schema: loggerSchema }, loggerFile);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default plugin;
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import { dataInsert } from '../../../../utils.js';
|
|
2
|
-
|
|
3
|
-
const table = 'crm.properties';
|
|
4
|
-
|
|
5
|
-
function checkKeyType({ body, key }) {
|
|
6
|
-
if (typeof body[key] === 'number' && (!/\D/.test(body[key].toString()) && body[key].toString().length < 10)) {
|
|
7
|
-
return { [key]: 'int' };
|
|
8
|
-
} if (typeof body[key] === 'object') {
|
|
9
|
-
return { [key]: 'json' };
|
|
10
|
-
}
|
|
11
|
-
if (Date.parse(body[key], 'yyyy/MM/ddTHH:mm:ss.000Z')) {
|
|
12
|
-
return { [key]: 'date' };
|
|
13
|
-
}
|
|
14
|
-
return { [key]: 'text' };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default async function addExtraProperties({
|
|
18
|
-
pg, params = {}, body = {}, user = {},
|
|
19
|
-
}) {
|
|
20
|
-
const { id } = params;
|
|
21
|
-
const { uid } = user;
|
|
22
|
-
if (!uid) {
|
|
23
|
-
return { message: 'access restricted: uid', status: 403 };
|
|
24
|
-
}
|
|
25
|
-
if (!id) {
|
|
26
|
-
return { message: 'not enougn params: 1', status: 400 };
|
|
27
|
-
}
|
|
28
|
-
const extraProperties = Object.keys(body);
|
|
29
|
-
if (!extraProperties.length) {
|
|
30
|
-
return { message: 'not enougn params: 2', status: 400 };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (!pg.pk?.[table]) {
|
|
34
|
-
return { message: 'table not found: crm.properties', status: 400 };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
await pg.query('delete from crm.properties where object_id=$1', [id]); // rewrite?
|
|
38
|
-
const keyTypeMatch = extraProperties.filter((key) => body[key]).reduce((acc, curr) => Object.assign(acc, checkKeyType({ body, key: curr })), {});
|
|
39
|
-
const res = await Promise.all(Object.keys(keyTypeMatch).map(async (key) => {
|
|
40
|
-
const propertyType = keyTypeMatch[key];
|
|
41
|
-
const { rows = [] } = await dataInsert({
|
|
42
|
-
pg,
|
|
43
|
-
table,
|
|
44
|
-
data: {
|
|
45
|
-
property_type: propertyType,
|
|
46
|
-
property_key: key,
|
|
47
|
-
object_id: id,
|
|
48
|
-
[`property_${propertyType}`]: body[key],
|
|
49
|
-
},
|
|
50
|
-
uid,
|
|
51
|
-
});
|
|
52
|
-
return { id: rows[0]?.property_id, type: propertyType, value: body[key] };
|
|
53
|
-
}));
|
|
54
|
-
return { message: { rows: res }, status: 200 };
|
|
55
|
-
}
|
|
1
|
+
import { dataInsert } from '../../../../utils.js';
|
|
2
|
+
|
|
3
|
+
const table = 'crm.properties';
|
|
4
|
+
|
|
5
|
+
function checkKeyType({ body, key }) {
|
|
6
|
+
if (typeof body[key] === 'number' && (!/\D/.test(body[key].toString()) && body[key].toString().length < 10)) {
|
|
7
|
+
return { [key]: 'int' };
|
|
8
|
+
} if (typeof body[key] === 'object') {
|
|
9
|
+
return { [key]: 'json' };
|
|
10
|
+
}
|
|
11
|
+
if (Date.parse(body[key], 'yyyy/MM/ddTHH:mm:ss.000Z')) {
|
|
12
|
+
return { [key]: 'date' };
|
|
13
|
+
}
|
|
14
|
+
return { [key]: 'text' };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default async function addExtraProperties({
|
|
18
|
+
pg, params = {}, body = {}, user = {},
|
|
19
|
+
}) {
|
|
20
|
+
const { id } = params;
|
|
21
|
+
const { uid } = user;
|
|
22
|
+
if (!uid) {
|
|
23
|
+
return { message: 'access restricted: uid', status: 403 };
|
|
24
|
+
}
|
|
25
|
+
if (!id) {
|
|
26
|
+
return { message: 'not enougn params: 1', status: 400 };
|
|
27
|
+
}
|
|
28
|
+
const extraProperties = Object.keys(body);
|
|
29
|
+
if (!extraProperties.length) {
|
|
30
|
+
return { message: 'not enougn params: 2', status: 400 };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!pg.pk?.[table]) {
|
|
34
|
+
return { message: 'table not found: crm.properties', status: 400 };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
await pg.query('delete from crm.properties where object_id=$1', [id]); // rewrite?
|
|
38
|
+
const keyTypeMatch = extraProperties.filter((key) => body[key]).reduce((acc, curr) => Object.assign(acc, checkKeyType({ body, key: curr })), {});
|
|
39
|
+
const res = await Promise.all(Object.keys(keyTypeMatch).map(async (key) => {
|
|
40
|
+
const propertyType = keyTypeMatch[key];
|
|
41
|
+
const { rows = [] } = await dataInsert({
|
|
42
|
+
pg,
|
|
43
|
+
table,
|
|
44
|
+
data: {
|
|
45
|
+
property_type: propertyType,
|
|
46
|
+
property_key: key,
|
|
47
|
+
object_id: id,
|
|
48
|
+
[`property_${propertyType}`]: body[key],
|
|
49
|
+
},
|
|
50
|
+
uid,
|
|
51
|
+
});
|
|
52
|
+
return { id: rows[0]?.property_id, type: propertyType, value: body[key] };
|
|
53
|
+
}));
|
|
54
|
+
return { message: { rows: res }, status: 200 };
|
|
55
|
+
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
export default async function getExtraProperties({
|
|
2
|
-
pg, params = {},
|
|
3
|
-
}) {
|
|
4
|
-
const { id } = params;
|
|
5
|
-
if (!id) {
|
|
6
|
-
return { message: 'not enougn params', status: 400 };
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const { rows = [] } = pg.pk?.['crm.properties']
|
|
10
|
-
? await pg.query(`select property_key, property_type, property_text, property_int,
|
|
11
|
-
property_json, property_date from crm.properties where property_key is not null and object_id=$1`, [id])
|
|
12
|
-
: {};
|
|
13
|
-
if (!rows.length) return {};
|
|
14
|
-
|
|
15
|
-
const data = rows.reduce((acc, curr) => Object.assign(acc, { [curr.property_key]: curr[`property_${curr.property_type}`] }), {});
|
|
16
|
-
return { message: data, status: 200 };
|
|
17
|
-
}
|
|
1
|
+
export default async function getExtraProperties({
|
|
2
|
+
pg, params = {},
|
|
3
|
+
}) {
|
|
4
|
+
const { id } = params;
|
|
5
|
+
if (!id) {
|
|
6
|
+
return { message: 'not enougn params', status: 400 };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const { rows = [] } = pg.pk?.['crm.properties']
|
|
10
|
+
? await pg.query(`select property_key, property_type, property_text, property_int,
|
|
11
|
+
property_json, property_date from crm.properties where property_key is not null and object_id=$1`, [id])
|
|
12
|
+
: {};
|
|
13
|
+
if (!rows.length) return {};
|
|
14
|
+
|
|
15
|
+
const data = rows.reduce((acc, curr) => Object.assign(acc, { [curr.property_key]: curr[`property_${curr.property_type}`] }), {});
|
|
16
|
+
return { message: data, status: 200 };
|
|
17
|
+
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import getExtraProperties from './controllers/properties.get.js';
|
|
2
|
-
import addExtraProperties from './controllers/properties.add.js';
|
|
3
|
-
|
|
4
|
-
const propertiesSchema = {
|
|
5
|
-
params: {
|
|
6
|
-
id: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
7
|
-
},
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
async function plugin(fastify, config = {}) {
|
|
11
|
-
const prefix = config.prefix || '/api';
|
|
12
|
-
fastify.get(`${prefix}/properties/:id`, { schema: propertiesSchema }, getExtraProperties);
|
|
13
|
-
fastify.post(`${prefix}/properties/:id`, { schema: propertiesSchema }, addExtraProperties);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export default plugin;
|
|
1
|
+
import getExtraProperties from './controllers/properties.get.js';
|
|
2
|
+
import addExtraProperties from './controllers/properties.add.js';
|
|
3
|
+
|
|
4
|
+
const propertiesSchema = {
|
|
5
|
+
params: {
|
|
6
|
+
id: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
7
|
+
},
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
async function plugin(fastify, config = {}) {
|
|
11
|
+
const prefix = config.prefix || '/api';
|
|
12
|
+
fastify.get(`${prefix}/properties/:id`, { schema: propertiesSchema }, getExtraProperties);
|
|
13
|
+
fastify.post(`${prefix}/properties/:id`, { schema: propertiesSchema }, addExtraProperties);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default plugin;
|
|
@@ -34,9 +34,10 @@ export default async function dataAPI(req, reply, called) {
|
|
|
34
34
|
const loadTable = await getTemplate('table', tokenData?.table || hookData?.table || params.table);
|
|
35
35
|
|
|
36
36
|
// check sql inline fields count
|
|
37
|
-
if (!checkInline[params?.table] && loadTable
|
|
38
|
-
const filterSql = loadTable.sql.filter(el => el.inline ?? true);
|
|
39
|
-
const
|
|
37
|
+
if (!checkInline[params?.table] && loadTable?.sql?.length && loadTable.table) {
|
|
38
|
+
const filterSql = loadTable.sql.filter(el => !el?.disabled && (el.inline ?? true));
|
|
39
|
+
const sqlTable = filterSql.map((el, i) => ` left join lateral (${el.sql}) ${el.name || `t${i}`} on 1=1 `)?.join('') || '';
|
|
40
|
+
const d = await Promise.all(filterSql.map((el, i) => pg.query(`select ${el.name || `t${i}`}.* from(select * from ${loadTable.table})t ${sqlTable} limit 0`).then(el => el.fields)))
|
|
40
41
|
d.forEach((el, i) => {
|
|
41
42
|
filterSql[i].inline = el.length == 1 ? true : false;
|
|
42
43
|
filterSql[i].fields = el.map(f => f.name);
|
|
@@ -46,6 +47,8 @@ export default async function dataAPI(req, reply, called) {
|
|
|
46
47
|
loadTable.sql = checkInline[params?.table]
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
if (query.sql === '0') return loadTable;
|
|
51
|
+
|
|
49
52
|
if (!loadTable && !(tokenData?.table && pg.pk?.[tokenData?.table])) { return { message: 'template not found', status: 404 }; }
|
|
50
53
|
|
|
51
54
|
const id = tokenData?.id || hookData?.id || params?.id;
|
|
@@ -103,6 +106,7 @@ export default async function dataAPI(req, reply, called) {
|
|
|
103
106
|
search: query.search,
|
|
104
107
|
state: query.state,
|
|
105
108
|
custom: query.custom,
|
|
109
|
+
uid,
|
|
106
110
|
json: 1,
|
|
107
111
|
}) : {};
|
|
108
112
|
|
|
@@ -131,10 +135,11 @@ export default async function dataAPI(req, reply, called) {
|
|
|
131
135
|
const q = `select ${pk ? `"${pk}" as id,` : ''}
|
|
132
136
|
${params.id || query.key ? '*' : sqlColumns || cols || '*'}
|
|
133
137
|
${metaCols}
|
|
134
|
-
${sql?.filter(el => el.inline
|
|
135
|
-
|
|
138
|
+
${sql?.filter?.(el => el.inline && el.fields?.length)?.length
|
|
139
|
+
? `,${sql?.filter(el => el.inline && el.fields).map(el => el.fields).join(',')}` || ''
|
|
140
|
+
: ''}
|
|
136
141
|
${dbColumns.find((el) => el.name === 'geom' && pg.pgType?.[el.dataTypeID] === 'geometry') ? ',st_asgeojson(geom)::json as geom' : ''}
|
|
137
|
-
from (select * from ${table} where ${sqlTable ? 'true' : (where.join(' and ') || 'true')} ) t
|
|
142
|
+
from (select * ${sql?.filter(el => el.inline).map(el => `,(${el.sql})`).join('') || ''} from ${table} t where ${sqlTable ? 'true' : (where.join(' and ') || 'true')} ) t
|
|
138
143
|
${sqlTable}
|
|
139
144
|
${params.id ? cardSqlTable : ''}
|
|
140
145
|
where ${where.join(' and ') || 'true'}
|
|
@@ -165,7 +170,7 @@ export default async function dataAPI(req, reply, called) {
|
|
|
165
170
|
count(*)::int as total,
|
|
166
171
|
count(*) FILTER(WHERE ${filterWhere.join(' and ') || 'true'})::int as filtered
|
|
167
172
|
${aggregates.length ? `,${aggregates.map((el) => `${aggColumns[el.name]}(${el.name}) FILTER(WHERE ${filterWhere.join(' and ') || 'true'}) as ${el.name}`).join(',')}` : ''}
|
|
168
|
-
from ${table} t ${sqlTable}
|
|
173
|
+
from (select * ${sql?.filter(el => el.inline).map(el => `,(${el.sql})`).join('') || ''} from ${table} t ${sqlTable})q
|
|
169
174
|
where ${[loadTable?.query, tokenData?.query, accessQuery].filter(el => el).join(' and ') || 'true'} `
|
|
170
175
|
.replace(/{{uid}}/g, uid);
|
|
171
176
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { logger, autoIndex, getSelect, getFilterSQL, getTemplate, getSelectVal, pgClients } from '../../../../utils.js';
|
|
2
2
|
|
|
3
|
+
const checkInline = {};
|
|
4
|
+
|
|
3
5
|
export default async function filterAPI(req) {
|
|
4
6
|
const time = Date.now();
|
|
5
7
|
|
|
@@ -10,8 +12,22 @@ export default async function filterAPI(req) {
|
|
|
10
12
|
const loadTable = await getTemplate('table', params.table);
|
|
11
13
|
if (!loadTable) { return { status: 404, message: 'not found' }; }
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
// check sql inline fields count
|
|
16
|
+
if (!checkInline[params.table] && loadTable.sql?.length && loadTable.table) {
|
|
17
|
+
const filterSql = loadTable.sql.filter(el => el.inline ?? true);
|
|
18
|
+
const d = await Promise.all(filterSql.map(el => pg.query(`select q.* from(select * from ${loadTable.table})t left join lateral(${el.sql})q on 1=1 limit 0`).then(el => el.fields)))
|
|
19
|
+
d.forEach((el, i) => {
|
|
20
|
+
filterSql[i].inline = el.length == 1 ? true : false;
|
|
21
|
+
filterSql[i].fields = el.map(f => f.name);
|
|
22
|
+
});
|
|
23
|
+
checkInline[params?.table] = loadTable.sql;
|
|
24
|
+
} else if (checkInline[params?.table]) {
|
|
25
|
+
loadTable.sql = checkInline[params?.table]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const sqlTable = false ? loadTable.sql?.filter?.((el) => !el.inline && !el?.disabled && el?.sql?.replace).map((el, i) => ` left join lateral (${el.sql.replace('{{uid}}', user?.uid)}) ${el.name || `t${i}`} on 1=1 `)?.join('') || '' : '';
|
|
29
|
+
const sqlInline = loadTable.sql?.filter?.(el => el.inline).map(el => `,(${el.sql})`).join('');
|
|
30
|
+
const { fields: columns = [] } = await pg.query(`select q.* from (select * ${sqlInline || ''} from ${loadTable.table} t ${sqlTable} limit 0)q`);
|
|
15
31
|
const { fields = [] } = await pg.query(`select * from ${loadTable.table} t limit 0`);
|
|
16
32
|
|
|
17
33
|
const {
|
|
@@ -20,13 +36,14 @@ export default async function filterAPI(req) {
|
|
|
20
36
|
|
|
21
37
|
const { extra } = loadTable?.form ? await getTemplate('form', loadTable?.form) || {} : {};
|
|
22
38
|
|
|
23
|
-
const { optimizedSQL = `select * from ${loadTable.table}` } = loadTable?.sql || filter || custom || state || search || extra ? await getFilterSQL({
|
|
39
|
+
const { optimizedSQL = `select * ${sqlInline || ''} from ${loadTable.table}` } = loadTable?.sql || filter || custom || state || search || extra ? await getFilterSQL({
|
|
24
40
|
pg,
|
|
25
41
|
table: params.table,
|
|
26
42
|
filter,
|
|
27
43
|
custom,
|
|
28
44
|
state,
|
|
29
45
|
search,
|
|
46
|
+
uid: user?.uid,
|
|
30
47
|
}) : {};
|
|
31
48
|
|
|
32
49
|
const filters = (loadTable?.filter_list || loadTable?.filters || loadTable?.filterList || []).concat(loadTable?.filterSql || []);
|
|
@@ -47,8 +64,8 @@ export default async function filterAPI(req) {
|
|
|
47
64
|
await Promise.all(filters.filter((el) => el.data && el.id && el.type !== 'Autocomplete').map(async (el) => {
|
|
48
65
|
const cls = await getSelect(el.data, pg);
|
|
49
66
|
|
|
50
|
-
if (!cls || !loadTable.table) return;
|
|
51
67
|
const { dataTypeID } = columns.find((item) => item.name === el.id) || {};
|
|
68
|
+
if (!cls || !loadTable.table || !dataTypeID) return;
|
|
52
69
|
|
|
53
70
|
if (el.extra && el.type === 'select' && Array.isArray(cls)) {
|
|
54
71
|
const countArr = await pg.query(`select value_text as id, count(*) from crm.extra_data where property_key=$1 and property_entity=$2 group by value_text`, [el.id, params.table]);
|
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import { applyHook, getTemplate } from '../../../../utils.js';
|
|
2
|
-
|
|
3
|
-
const sql = `select property_key as key, property_json as json, property_int as int,
|
|
4
|
-
property_text as text from admin.properties where 1=1`;
|
|
5
|
-
|
|
6
|
-
async function getSettings({ pg }) {
|
|
7
|
-
const { rows = [] } = await pg.query(sql);
|
|
8
|
-
const data = rows.reduce((acc, curr) => Object.assign(acc, { [curr.key]: curr.json || curr.int || curr.text }), {});
|
|
9
|
-
return data;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default async function formFunction(req) {
|
|
13
|
-
const time = Date.now();
|
|
14
|
-
|
|
15
|
-
const { pg, params, user } = req;
|
|
16
|
-
const hookData = await applyHook('preForm', { form: params?.form, user });
|
|
17
|
-
|
|
18
|
-
if (hookData?.message && hookData?.status) {
|
|
19
|
-
return { message: hookData?.message, status: hookData?.status };
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const form = await getTemplate('form', hookData?.form || params?.form);
|
|
23
|
-
if (!form) { return { status: 404, message: 'not found' }; }
|
|
24
|
-
|
|
25
|
-
// replace settings
|
|
26
|
-
const arr = JSON.stringify(form).match(/{{settings.([^}]*)}}/g);
|
|
27
|
-
if (arr?.length) {
|
|
28
|
-
const string = JSON.stringify(form);
|
|
29
|
-
const settings = await getSettings({ pg });
|
|
30
|
-
const match = arr.reduce((acc, curr) => Object.assign(acc, { [curr]: settings[curr.replace(/^{{settings./g, '').replace(/}}$/, '')] }), {});
|
|
31
|
-
const res = Object.keys(match).reduce((s, m) => s.replace(m, match[m]), string);
|
|
32
|
-
return { time: Date.now() - time, form: JSON.parse(res) };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const res = { time: Date.now() - time, form };
|
|
36
|
-
const res1 = await applyHook('afterForm', {
|
|
37
|
-
form: hookData?.form || params?.form,
|
|
38
|
-
payload: res,
|
|
39
|
-
user,
|
|
40
|
-
});
|
|
41
|
-
return res1 || { time: Date.now() - time, form };
|
|
42
|
-
}
|
|
1
|
+
import { applyHook, getTemplate } from '../../../../utils.js';
|
|
2
|
+
|
|
3
|
+
const sql = `select property_key as key, property_json as json, property_int as int,
|
|
4
|
+
property_text as text from admin.properties where 1=1`;
|
|
5
|
+
|
|
6
|
+
async function getSettings({ pg }) {
|
|
7
|
+
const { rows = [] } = await pg.query(sql);
|
|
8
|
+
const data = rows.reduce((acc, curr) => Object.assign(acc, { [curr.key]: curr.json || curr.int || curr.text }), {});
|
|
9
|
+
return data;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default async function formFunction(req) {
|
|
13
|
+
const time = Date.now();
|
|
14
|
+
|
|
15
|
+
const { pg, params, user } = req;
|
|
16
|
+
const hookData = await applyHook('preForm', { form: params?.form, user });
|
|
17
|
+
|
|
18
|
+
if (hookData?.message && hookData?.status) {
|
|
19
|
+
return { message: hookData?.message, status: hookData?.status };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const form = await getTemplate('form', hookData?.form || params?.form);
|
|
23
|
+
if (!form) { return { status: 404, message: 'not found' }; }
|
|
24
|
+
|
|
25
|
+
// replace settings
|
|
26
|
+
const arr = JSON.stringify(form).match(/{{settings.([^}]*)}}/g);
|
|
27
|
+
if (arr?.length) {
|
|
28
|
+
const string = JSON.stringify(form);
|
|
29
|
+
const settings = await getSettings({ pg });
|
|
30
|
+
const match = arr.reduce((acc, curr) => Object.assign(acc, { [curr]: settings[curr.replace(/^{{settings./g, '').replace(/}}$/, '')] }), {});
|
|
31
|
+
const res = Object.keys(match).reduce((s, m) => s.replace(m, match[m]), string);
|
|
32
|
+
return { time: Date.now() - time, form: JSON.parse(res) };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const res = { time: Date.now() - time, form };
|
|
36
|
+
const res1 = await applyHook('afterForm', {
|
|
37
|
+
form: hookData?.form || params?.form,
|
|
38
|
+
payload: res,
|
|
39
|
+
user,
|
|
40
|
+
});
|
|
41
|
+
return res1 || { time: Date.now() - time, form };
|
|
42
|
+
}
|