@opengis/admin 0.2.123 → 0.2.124
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
@@ -1,118 +1,123 @@
|
|
1
|
-
import path from 'node:path';
|
2
|
-
|
3
|
-
import { getAccess, handlebars, setOpt, setToken, getTemplate, handlebarsSync, applyHook } 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
|
-
import conditions from './utils/conditions.js';
|
8
|
-
|
9
|
-
const components = {
|
10
|
-
'vs-widget-file': `select 'vs-widget-file' as component, count(*) from crm.files where entity_id=$1 and file_status<>3`,
|
11
|
-
'vs-widget-comments': `select 'vs-widget-comments' as component, count(*) from crm.communications where entity_id=$1`
|
12
|
-
};
|
13
|
-
|
14
|
-
export default async function getCardData(req) {
|
15
|
-
|
16
|
-
const {
|
17
|
-
pg, params = {}, session = {}, user
|
18
|
-
} = req;
|
19
|
-
const { table, id } = params;
|
20
|
-
const uid = session.passport?.user?.uid;
|
21
|
-
|
22
|
-
const res = await applyHook('cardData', { table, id, user });
|
23
|
-
if (res) return res;
|
24
|
-
|
25
|
-
|
26
|
-
const time = Date.now();
|
27
|
-
|
28
|
-
const template = await getTemplate('card', table);
|
29
|
-
const access = await getAccess({ table, user });
|
30
|
-
|
31
|
-
const tableTemplate = await getTemplate('table', table);
|
32
|
-
|
33
|
-
const index = template?.find(el => el[0] === 'index.yml')?.[1] || {};
|
34
|
-
|
35
|
-
const { message, rows = [] } = index.table && index.query ? await pg.query(`select * from ${index.table} where ${handlebarsSync.compile(index.query)({ uid, user })}`) :
|
36
|
-
await getTableData({ pg, params: { table, id }, session, user });
|
37
|
-
|
38
|
-
if (message) return { message };
|
39
|
-
|
40
|
-
// conditions
|
41
|
-
index.panels?.filter(el => el.items).forEach(el => {
|
42
|
-
el.items = el.items?.filter(el => conditions(el.conditions, rows[0]));
|
43
|
-
})
|
44
|
-
|
45
|
-
// title, count
|
46
|
-
index.panels?.filter(el => el.items).forEach(async el => {
|
47
|
-
const filtered = el.items.filter(el => el.count?.toLowerCase?.().includes('select'));
|
48
|
-
const data = await Promise.all(filtered.map(el => pg.query(el.count).then(el => el.rows[0] || {})))
|
49
|
-
filtered.forEach((el, i) => {
|
50
|
-
Object.assign(el, data[i] || {}, data[i].count ? {} : { count: undefined })
|
51
|
-
});
|
52
|
-
const q = el.items.map((el) => el.component ? components[el.component] : null).filter(el => el).join(' union all ');
|
53
|
-
const counts = q && id
|
54
|
-
? await pg.query(q, [id])
|
55
|
-
.then(e => e.rows.reduce((acc, curr) => Object.assign(acc, { [curr.component]: curr.count }), {}))
|
56
|
-
: {};
|
57
|
-
el.items?.filter?.(item => item.component)?.forEach(item => Object.assign(item, { count: counts?.[item.component] }));
|
58
|
-
})
|
59
|
-
|
60
|
-
|
61
|
-
// tokens result
|
62
|
-
const tokens = {};
|
63
|
-
if (
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
1
|
+
import path from 'node:path';
|
2
|
+
|
3
|
+
import { getAccess, handlebars, setOpt, setToken, getTemplate, handlebarsSync, applyHook } 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
|
+
import conditions from './utils/conditions.js';
|
8
|
+
|
9
|
+
const components = {
|
10
|
+
'vs-widget-file': `select 'vs-widget-file' as component, count(*) from crm.files where entity_id=$1 and file_status<>3`,
|
11
|
+
'vs-widget-comments': `select 'vs-widget-comments' as component, count(*) from crm.communications where entity_id=$1`
|
12
|
+
};
|
13
|
+
|
14
|
+
export default async function getCardData(req) {
|
15
|
+
|
16
|
+
const {
|
17
|
+
pg, params = {}, session = {}, user
|
18
|
+
} = req;
|
19
|
+
const { table, id } = params;
|
20
|
+
const uid = session.passport?.user?.uid;
|
21
|
+
|
22
|
+
const res = await applyHook('cardData', { table, id, user });
|
23
|
+
if (res) return res;
|
24
|
+
|
25
|
+
|
26
|
+
const time = Date.now();
|
27
|
+
|
28
|
+
const template = await getTemplate('card', table);
|
29
|
+
const access = await getAccess({ table, user });
|
30
|
+
|
31
|
+
const tableTemplate = await getTemplate('table', table);
|
32
|
+
|
33
|
+
const index = template?.find(el => el[0] === 'index.yml')?.[1] || {};
|
34
|
+
|
35
|
+
const { message, rows = [] } = index.table && index.query ? await pg.query(`select * from ${index.table} where ${handlebarsSync.compile(index.query)({ uid, user })}`) :
|
36
|
+
await getTableData({ pg, params: { table, id }, session, user });
|
37
|
+
|
38
|
+
if (message) return { message };
|
39
|
+
|
40
|
+
// conditions
|
41
|
+
index.panels?.filter(el => el.items).forEach(el => {
|
42
|
+
el.items = el.items?.filter(el => conditions(el.conditions, rows[0]));
|
43
|
+
})
|
44
|
+
|
45
|
+
// title, count
|
46
|
+
index.panels?.filter(el => el.items).forEach(async el => {
|
47
|
+
const filtered = el.items.filter(el => el.count?.toLowerCase?.().includes('select'));
|
48
|
+
const data = await Promise.all(filtered.map(el => pg.query(el.count).then(el => el.rows[0] || {})))
|
49
|
+
filtered.forEach((el, i) => {
|
50
|
+
Object.assign(el, data[i] || {}, data[i].count ? {} : { count: undefined })
|
51
|
+
});
|
52
|
+
const q = el.items.map((el) => el.component ? components[el.component] : null).filter(el => el).join(' union all ');
|
53
|
+
const counts = q && id
|
54
|
+
? await pg.query(q, [id])
|
55
|
+
.then(e => e.rows.reduce((acc, curr) => Object.assign(acc, { [curr.component]: curr.count }), {}))
|
56
|
+
: {};
|
57
|
+
el.items?.filter?.(item => item.component)?.forEach(item => Object.assign(item, { count: counts?.[item.component] }));
|
58
|
+
})
|
59
|
+
|
60
|
+
|
61
|
+
// tokens result
|
62
|
+
const tokens = {};
|
63
|
+
if (index?.tokens && typeof index?.tokens === 'object' && !Array.isArray(index?.tokens)) {
|
64
|
+
Object.keys(index.tokens || {})
|
65
|
+
.filter(key => index?.tokens[key]?.public
|
66
|
+
|| access.actions?.includes?.('edit')
|
67
|
+
|| access.actions?.includes?.('add')
|
68
|
+
)
|
69
|
+
.forEach(key => {
|
70
|
+
const item = index?.tokens[key];
|
71
|
+
Object.keys(item).filter(el => item[el]?.includes?.('{{')).forEach(el => {
|
72
|
+
item[el] = handlebarsSync.compile(item[el])({ user, uid: user?.uid, id, data: rows[0] });
|
73
|
+
});
|
74
|
+
|
75
|
+
const token = item.form && item.table ? setToken({
|
76
|
+
ids: [JSON.stringify(item)],
|
77
|
+
uid,
|
78
|
+
array: 1,
|
79
|
+
})[0] : setOpt(item, user.uid);
|
80
|
+
tokens[key] = token;
|
81
|
+
});
|
82
|
+
}
|
83
|
+
|
84
|
+
// vue result
|
85
|
+
const vue = template?.filter((el) => el[0].endsWith('.vue'))
|
86
|
+
.reduce((acc, curr) => Object.assign(acc, { [path.parse(curr[0]).name]: curr[1].match(/<template[^>]*>([\s\S]*?)<\/template>/)[1].trim() }), {});
|
87
|
+
|
88
|
+
// data result
|
89
|
+
const data = {};
|
90
|
+
if (index?.data && index?.data?.[0]?.name) {
|
91
|
+
await Promise.all(index.data.filter((el) => el?.name && el?.sql).map(async (el) => {
|
92
|
+
const q = handlebarsSync.compile(el.sql)({ data: rows[0], user, uid: user?.uid, id });
|
93
|
+
const { rows: sqlData } = await pg.query(q);
|
94
|
+
data[el.name] = sqlData;
|
95
|
+
}));
|
96
|
+
}
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
// html result
|
101
|
+
const html = {};
|
102
|
+
if (template) {
|
103
|
+
await Promise.all(template.filter(el => el[0].includes('.hbs')).map(async (el) => {
|
104
|
+
const htmlContent = await handlebars.compile(el[1])({ ...rows[0], user, data, tokens });
|
105
|
+
const name = el[0].substring(0, el[0].lastIndexOf('.'))
|
106
|
+
html[name] = htmlContent;
|
107
|
+
}));
|
108
|
+
}
|
109
|
+
|
110
|
+
return {
|
111
|
+
time: Date.now() - time,
|
112
|
+
...index,
|
113
|
+
tokens,
|
114
|
+
vue,
|
115
|
+
data,
|
116
|
+
html,
|
117
|
+
rows,
|
118
|
+
columns: tableTemplate?.columns || tableTemplate?.colModel,
|
119
|
+
table: undefined,
|
120
|
+
query: undefined,
|
121
|
+
};
|
122
|
+
|
118
123
|
}
|