@opengis/admin 0.2.148 → 0.2.149

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/admin",
3
- "version": "0.2.148",
3
+ "version": "0.2.149",
4
4
  "description": "This project Softpro Admin",
5
5
  "main": "dist/admin.js",
6
6
  "type": "module",
@@ -1,11 +1,11 @@
1
- import { readFile } from 'fs/promises';
2
- import { getTemplatePath } from '@opengis/fastify-table/utils.js';
1
+ import { readFile } from 'node:fs/promises';
2
+ import { getTemplatePath, pgClients } from '@opengis/fastify-table/utils.js';
3
3
 
4
4
  export default async function userCls(req) {
5
5
  const {
6
- pg, params = {}, query = {}, session = {},
6
+ pg = pgClients.client, params = {}, query = {}, user = {},
7
7
  } = req;
8
- const { uid } = session.passport?.user || {};
8
+ const { uid } = user || {};
9
9
 
10
10
  if (!uid) {
11
11
  return { message: 'access restricted', status: 403 };
@@ -18,7 +18,7 @@ export default async function userCls(req) {
18
18
  const q = `select user_clsid as id, name, type,
19
19
  case when type='json' then (
20
20
  ${params?.id
21
- ? `(with recursive rows as (
21
+ ? `(with recursive rows as (
22
22
  select user_clsid, code as id, name as text, icon, color, parent
23
23
  from admin.user_cls a
24
24
  where name=u.name
@@ -28,7 +28,7 @@ export default async function userCls(req) {
28
28
  join rows b on a.parent=b.text
29
29
  ) select json_agg(row_to_json(q)) from rows q where text<>u.name
30
30
  )`
31
- : 'select count(*)::int from admin.user_cls where parent=u.name'} ) else null end as children,
31
+ : 'select count(*)::int from admin.user_cls where parent=u.name'} ) else null end as children,
32
32
  case when type='sql' then data else null end as sql from admin.user_cls u
33
33
  where (case when type='json' then parent is null else true end)
34
34
  and uid=(select uid from admin.users where $1 in (login,uid) limit 1)
@@ -37,35 +37,35 @@ export default async function userCls(req) {
37
37
  if (query?.sql) return q;
38
38
 
39
39
 
40
- const { rows = [] } = await pg.query(q, [uid, params.id].filter((el) => el));
40
+ const { rows = [] } = await pg.query(q, [uid, params.id].filter((el) => el));
41
41
 
42
- rows.forEach((row) => {
43
- if (row.type === 'sql') delete row.children;
44
- if (row.type === 'json') { delete row.sql; Object.assign(row, { children: row.children || (params?.id ? [] : 0) }); }
45
- });
42
+ rows.forEach((row) => {
43
+ if (row.type === 'sql') delete row.children;
44
+ if (row.type === 'json') { delete row.sql; Object.assign(row, { children: row.children || (params?.id ? [] : 0) }); }
45
+ });
46
46
 
47
- const clsList = query.type === 'sql' ? [] : getTemplatePath('cls'); // skip cls if type sql
48
- const selectList = query.type === 'json' ? [] : getTemplatePath('select'); // skip sql if type json
47
+ const clsList = query.type === 'sql' ? [] : getTemplatePath('cls'); // skip cls if type sql
48
+ const selectList = query.type === 'json' ? [] : getTemplatePath('select'); // skip sql if type json
49
49
 
50
- const userClsNames = rows.map((el) => el.name);
51
- const selectNames = selectList.map((el) => el[0]);
50
+ const userClsNames = rows.map((el) => el.name);
51
+ const selectNames = selectList.map((el) => el[0]);
52
52
 
53
- const arr = (clsList || []).concat(selectList || []).map((el) => ({ name: el[0], path: el[1], type: el[2] }))
54
- ?.filter((el) => (params?.id ? el.name === params?.id : true))
55
- ?.filter((el) => ['sql', 'json'].includes(el?.type) && !userClsNames.includes(el.name))
56
- ?.filter((el) => (el?.type === 'json' ? !selectNames?.includes(el?.name) : true));
53
+ const arr = (clsList || []).concat(selectList || []).map((el) => ({ name: el[0], path: el[1], type: el[2] }))
54
+ ?.filter((el) => (params?.id ? el.name === params?.id : true))
55
+ ?.filter((el) => ['sql', 'json'].includes(el?.type) && !userClsNames.includes(el.name))
56
+ ?.filter((el) => (el?.type === 'json' ? !selectNames?.includes(el?.name) : true));
57
57
 
58
- const res = await Promise.all(arr?.map(async (el) => {
59
- const type = { json: 'cls', sql: 'select' }[el.type] || el.type;
60
- // const clsData = await getSelect(type, el.name);
61
- const str = await readFile(el.path, 'utf-8');
62
- const clsData = type === 'cls' ? JSON.parse(str) : str;
63
- if (type === 'cls') {
64
- const children = params?.id ? clsData : clsData?.length || 0;
65
- return { name: el.name, type: el.type, children };
66
- }
67
- return { name: el.name, type: el.type, sql: clsData?.sql || clsData };
68
- }));
58
+ const res = await Promise.all(arr?.map(async (el) => {
59
+ const type = { json: 'cls', sql: 'select' }[el.type] || el.type;
60
+ // const clsData = await getSelect(type, el.name);
61
+ const str = await readFile(el.path, 'utf-8');
62
+ const clsData = type === 'cls' ? JSON.parse(str) : str;
63
+ if (type === 'cls') {
64
+ const children = params?.id ? clsData : clsData?.length || 0;
65
+ return { name: el.name, type: el.type, children };
66
+ }
67
+ return { name: el.name, type: el.type, sql: clsData?.sql || clsData };
68
+ }));
69
69
 
70
- return { message: { rows: rows.concat(res) }, status: 200 };
70
+ return { message: { rows: rows.concat(res) }, status: 200 };
71
71
  }
@@ -1,7 +1,9 @@
1
+ import { pgClients } from "@opengis/fastify-table/utils.js";
2
+
1
3
  export default async function userClsPost({
2
- pg, body = {}, session = {},
4
+ pg = pgClients.client, body = {}, user = {},
3
5
  }) {
4
- const { uid } = session.passport?.user || {};
6
+ const { uid } = user || {};
5
7
  const {
6
8
  name, type = 'json', children = [], sql,
7
9
  } = body;
@@ -23,7 +25,7 @@ export default async function userClsPost({
23
25
  }
24
26
 
25
27
 
26
- const { rowCount = 0 } = await pg.query(`with recursive rows as (
28
+ const { rowCount = 0 } = await pg.query(`with recursive rows as (
27
29
  select user_clsid, name, code, icon, color, parent
28
30
  from admin.user_cls a
29
31
  where name=$1
@@ -32,21 +34,21 @@ export default async function userClsPost({
32
34
  from admin.user_cls a
33
35
  join rows b on a.parent=b.name
34
36
  ) delete from admin.user_cls where user_clsid in (select user_clsid from rows )`, [name]);
35
- console.log('delete old user cls', name, rowCount);
37
+ console.log('delete old user cls', name, rowCount);
36
38
 
37
- const { id, data } = await pg.query('insert into admin.user_cls(name,type,data,uid) values($1,$2,$3,$4) returning user_clsid as id, data', [name, type, sql, uid])
38
- .then((res) => res.rows?.[0] || {});
39
- if (type === 'json' && children?.length) {
40
- if (!id) { return { error: 'insert user cls error', status: 500 }; }
41
- const q1 = `insert into admin.user_cls(code,name,color,icon,parent,uid)
39
+ const { id, data } = await pg.query('insert into admin.user_cls(name,type,data,uid) values($1,$2,$3,$4) returning user_clsid as id, data', [name, type, sql, uid])
40
+ .then((res) => res.rows?.[0] || {});
41
+ if (type === 'json' && children?.length) {
42
+ if (!id) { return { error: 'insert user cls error', status: 500 }; }
43
+ const q1 = `insert into admin.user_cls(code,name,color,icon,parent,uid)
42
44
 
43
45
  select value->>'id',value->>'text',value->>'color',value->>'icon', '${name.replace(/'/g, "''")}', '${uid}'
44
46
  from json_array_elements('${JSON.stringify(children).replace(/'/g, "''")}'::json)
45
47
 
46
48
  returning user_clsid as id, code, name, parent`;
47
- const { rows = [] } = await pg.query(q1);
48
- return { id, name, type, children: rows };
49
- }
50
- return { id, name, type, data };
49
+ const { rows = [] } = await pg.query(q1);
50
+ return { id, name, type, children: rows };
51
+ }
52
+ return { id, name, type, data };
51
53
 
52
54
  }