@opengis/admin 0.1.29 → 0.1.30
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/package.json +1 -1
- package/plugin.js +79 -2
- package/server/plugins/hook.js +1 -1
package/package.json
CHANGED
package/plugin.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import fp from 'fastify-plugin';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
import { userTemplateDir, getTemplate, pgClients } from '@opengis/fastify-table/utils.js';
|
|
6
|
+
|
|
2
7
|
import config from './config.js';
|
|
3
|
-
|
|
8
|
+
import getMenu from './server/routes/menu/controllers/getMenu.js';
|
|
4
9
|
|
|
10
|
+
config.prefix = config.prefix || '/api';
|
|
5
11
|
|
|
6
12
|
async function plugin(fastify, opts = config) {
|
|
7
13
|
// const prefix = config.prefix || '/api';
|
|
@@ -12,7 +18,78 @@ async function plugin(fastify, opts = config) {
|
|
|
12
18
|
fastify.register(import('./server/helpers/index.mjs'), opts);
|
|
13
19
|
// fastify.register(import('@opengis/fastify-auth'), config);
|
|
14
20
|
|
|
15
|
-
|
|
21
|
+
fastify.addHook('onListen', async () => {
|
|
22
|
+
const { client } = pgClients;
|
|
23
|
+
const json = await getMenu();
|
|
24
|
+
// insert interface list to db (user access management)
|
|
25
|
+
if (client?.pk?.['admin.routes'] && json?.length) {
|
|
26
|
+
const menuList = json.filter((el) => el?.menu?.length && el?.ua || el?.en || el?.name);
|
|
27
|
+
const interfaces = menuList.reduce((acc, curr) => { curr.menu.forEach((el) => acc.push(el.path)); return acc; }, []);
|
|
28
|
+
await client.query('update admin.routes set enabled=false where not array[route_id] <@ $1::text[]', [interfaces]);
|
|
29
|
+
|
|
30
|
+
const q = `insert into admin.menu(name, ord) values${
|
|
31
|
+
menuList.map((el, i) => `('${(el?.ua || el?.en || el?.name).replace(/'/g, '’')}', ${i}) `).join(',')
|
|
32
|
+
} on conflict (name) do update set ord=excluded.ord, enabled=true returning name, menu_id`;
|
|
33
|
+
const { rows = [] } = await client.query(q);
|
|
34
|
+
await client.query('update admin.menu set enabled=false where not array[menu_id] <@ $1::text[]', [rows.map((el) => el.menu_id)]);
|
|
35
|
+
|
|
36
|
+
const menus = rows.reduce((acc, curr) => Object.assign(acc, { [curr.menu_id]: menuList.find((item) => (item?.ua || item?.en || item?.name) === curr.name) }), {});
|
|
37
|
+
const values = Object.entries(menus).reduce((acc, curr) => { curr[1]?.menu?.forEach((el) => acc.push({ ...el, menuId: curr[0] })); return acc; }, []);
|
|
38
|
+
|
|
39
|
+
await Promise.all(values.filter((el) => el?.table).map(async (el) => Object.assign(el, { table: (await getTemplate('table', el.table))?.table || el.table })));
|
|
40
|
+
|
|
41
|
+
const q1 = `insert into admin.routes(route_id, alias, title, menu_id, table_name) values ${values.map((el) => `('${el.path}', '${el.path}', '${el.title}', '${el.menuId}', '${el.table}')`).join(',')}
|
|
42
|
+
on conflict (route_id) do update set menu_id=excluded.menu_id, title=excluded.title, enabled=true,
|
|
43
|
+
table_name=excluded.table_name returning route_id, table_name`;
|
|
44
|
+
try {
|
|
45
|
+
const { rowCount } = await client.query(q1);
|
|
46
|
+
console.log('interface insert ok', values, rowCount);
|
|
47
|
+
} catch (err) {
|
|
48
|
+
console.log('interface insert error', values, q1, err);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
fastify.addHook('onListen', async () => {
|
|
54
|
+
const { client: pg } = pgClients;
|
|
55
|
+
const clsQuery = [];
|
|
56
|
+
if (!pg.pk?.['admin.cls']) return;
|
|
57
|
+
|
|
58
|
+
const cls = userTemplateDir
|
|
59
|
+
?.reduce((acc, curr) => { [`${curr}/cls`, `${curr}/select`].forEach((el) => acc.push(el)); return acc; }, [])
|
|
60
|
+
?.filter((el) => fs.existsSync(el))
|
|
61
|
+
?.map((el) => ({ module: path.basename(path.dirname(el)), files: fs.readdirSync(el)?.filter((item) => el === 'cls' ? true : path.extname(item) === '.sql') }))
|
|
62
|
+
?.filter((el) => el.files?.length);
|
|
63
|
+
if (!cls?.length) return;
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
await Promise.all(cls.map(async (el1) => {
|
|
67
|
+
const { module, files } = el1;
|
|
68
|
+
await Promise.all((files.map(async (el) => {
|
|
69
|
+
const { ext, name } = path.parse(el);
|
|
70
|
+
const type = { '.json': 'cls', '.sql': 'select' }[ext];
|
|
71
|
+
const loadTemplate = await getTemplate(type, name);
|
|
72
|
+
console.log(name, type);
|
|
73
|
+
if (type === 'select') {
|
|
74
|
+
clsQuery.push(`insert into admin.cls(name,type,data,module) values('${name}','sql','${(loadTemplate?.sql || loadTemplate)?.replace(/'/g, "''")}', '${module?.replace(/'/g, "''")}')`);
|
|
75
|
+
} else if (type === 'cls') {
|
|
76
|
+
clsQuery.push(`insert into admin.cls(name,type) values('${name}','json');
|
|
77
|
+
insert into admin.cls(code,name,parent,icon)
|
|
78
|
+
select value->>'id',value->>'text','${name}',value->>'icon'
|
|
79
|
+
from json_array_elements('${JSON.stringify(loadTemplate).replace(/'/g, "''")}'::json)`);
|
|
80
|
+
}
|
|
81
|
+
})));
|
|
82
|
+
}));
|
|
83
|
+
|
|
84
|
+
await pg.query('truncate admin.cls');
|
|
85
|
+
if (clsQuery.filter((el) => el).length) {
|
|
86
|
+
await pg.query(clsQuery.filter((el) => el).join(';'));
|
|
87
|
+
console.log('cls insert ok', clsQuery?.length);
|
|
88
|
+
}
|
|
89
|
+
} catch (err) {
|
|
90
|
+
console.error('cls insert error', err.toString());
|
|
91
|
+
}
|
|
92
|
+
});
|
|
16
93
|
|
|
17
94
|
|
|
18
95
|
// API
|
package/server/plugins/hook.js
CHANGED
|
@@ -53,7 +53,7 @@ async function plugin(fastify) {
|
|
|
53
53
|
const cls = userTemplateDir
|
|
54
54
|
?.reduce((acc, curr) => { [`${curr}/cls`, `${curr}/select`].forEach((el) => acc.push(el)); return acc; }, [])
|
|
55
55
|
?.filter((el) => fs.existsSync(el))
|
|
56
|
-
?.map((el) => ({ module: path.basename(el), files: fs.readdirSync(el)?.filter((item) => el === 'cls' ? true : path.extname(item) === '.sql') }))
|
|
56
|
+
?.map((el) => ({ module: path.basename(path.dirname(el)), files: fs.readdirSync(el)?.filter((item) => el === 'cls' ? true : path.extname(item) === '.sql') }))
|
|
57
57
|
?.filter((el) => el.files?.length);
|
|
58
58
|
if (!cls?.length) return;
|
|
59
59
|
|