@opengis/admin 0.4.5 → 0.4.7

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.
Files changed (86) hide show
  1. package/README.md +97 -97
  2. package/dist/{add-page-DCJ-Yp0v.js → add-page-DD0eulsx.js} +1 -1
  3. package/dist/{admin-interface-BZK7WQza.js → admin-interface-DnTsbYY6.js} +2 -2
  4. package/dist/{admin-view-B0_hyDn5.js → admin-view-CyTFtjjj.js} +3 -3
  5. package/dist/admin.js +1 -1
  6. package/dist/admin.umd.cjs +74 -74
  7. package/dist/assets/logo.svg +41 -41
  8. package/dist/{card-view-DXKWi1Ty.js → card-view-CEJ_VxwZ.js} +1 -1
  9. package/dist/{edit-page-DO8gx1TX.js → edit-page-BVD8qIXT.js} +1 -1
  10. package/dist/{import-file-BlI2DAMT.js → import-file-CHNARxht.js} +11506 -11502
  11. package/dist/{profile-page-9oNOPlYV.js → profile-page-C3JDls-o.js} +1 -1
  12. package/dist/style.css +1 -1
  13. package/module/settings/card/admin.accounts.table/index.yml +7 -7
  14. package/module/settings/card/admin.accounts.table/rules.hbs +18 -18
  15. package/module/settings/card/admin.accounts.table/users.hbs +13 -13
  16. package/module/settings/card/admin.routes.table/groups.hbs +11 -11
  17. package/module/settings/card/admin.routes.table/users.hbs +16 -16
  18. package/module/settings/cls/core.actions.json +17 -17
  19. package/module/settings/cls/core.scope.json +13 -13
  20. package/module/settings/cls/properties.site_status.json +13 -13
  21. package/module/settings/cls/properties.widget_status.json +13 -13
  22. package/module/settings/cls/yes_no.json +11 -11
  23. package/module/settings/form/admin.accounts.form.json +13 -13
  24. package/module/settings/form/admin.properties.form.json +15 -15
  25. package/module/settings/form/admin.roles.form.json +21 -21
  26. package/module/settings/form/admin.user_properties.form.json +15 -15
  27. package/module/settings/form/admin.user_roles_card.form.json +13 -13
  28. package/module/settings/interface/admin.properties.json +4 -4
  29. package/module/settings/interface/admin.roles.json +4 -4
  30. package/module/settings/interface/admin.routes.json +4 -4
  31. package/module/settings/interface/admin.users.json +4 -4
  32. package/module/settings/select/core.routes.sql +1 -1
  33. package/module/settings/select/core.user_mentioned.sql +1 -1
  34. package/module/settings/select/core.user_uid.sql +1 -1
  35. package/module/settings/table/admin.properties.table.json +39 -39
  36. package/module/settings/table/admin.user_properties.table.json +34 -34
  37. package/package.json +8 -16
  38. package/server/helpers/core/coalesce.js +7 -7
  39. package/server/helpers/core/select.js +48 -48
  40. package/server/helpers/core/token.js +18 -18
  41. package/server/helpers/list/buttonHelper.js +21 -21
  42. package/server/helpers/list/utils/button.js +5 -5
  43. package/server/helpers/temp/contentList.js +58 -58
  44. package/server/helpers/temp/ifCond.js +101 -101
  45. package/server/helpers/utils/button.js +5 -5
  46. package/server/helpers/utils/mdToHTML.js +17 -17
  47. package/server/plugins/access/index.mjs +6 -6
  48. package/server/plugins/cron.js +10 -10
  49. package/server/plugins/docs.js +28 -28
  50. package/server/routes/access/controllers/access.group.js +29 -29
  51. package/server/routes/access/controllers/access.group.post.js +54 -54
  52. package/server/routes/access/index.mjs +11 -11
  53. package/server/routes/access/schema.mjs +67 -67
  54. package/server/routes/calendar/controllers/calendar.data.js +125 -125
  55. package/server/routes/calendar/index.mjs +7 -7
  56. package/server/routes/notifications/controllers/readNotifications.js +18 -18
  57. package/server/routes/notifications/controllers/testEmail.js +35 -35
  58. package/server/routes/notifications/controllers/userNotifications.js +53 -53
  59. package/server/routes/notifications/hook/onWidgetSet.js +56 -56
  60. package/server/routes/notifications/index.mjs +26 -26
  61. package/server/routes/properties/schema.js +10 -10
  62. package/server/routes/root.mjs +3 -3
  63. package/server/routes/user/controllers/user.cls.id.js +14 -14
  64. package/server/routes/user/controllers/user.cls.js +72 -72
  65. package/server/routes/user/controllers/user.info.js +17 -17
  66. package/server/routes/util/controllers/code.generator.js +89 -89
  67. package/server/routes/widget/controllers/file.edit.js +1 -3
  68. package/server/routes/widget/controllers/widget.del.js +79 -80
  69. package/server/routes/widget/controllers/widget.set.js +99 -101
  70. package/server/routes/widget/hook/onWidgetSet.js +12 -12
  71. package/server/routes/widget/index.mjs +20 -20
  72. package/server/routes/widget/schema.js +12 -12
  73. package/server/templates/cls/itree.recrzone_category.json +73 -73
  74. package/server/templates/cls/test.json +9 -9
  75. package/server/templates/form/admin.user_cls.data.form.json +49 -49
  76. package/server/templates/form/admin.user_group_rel.form.json +21 -21
  77. package/server/templates/form/form-user-pass.json +10 -10
  78. package/server/templates/form/form-user_group.json +39 -39
  79. package/server/templates/form/form-users.json +156 -156
  80. package/server/templates/form/user_group_access.form.json +22 -22
  81. package/server/templates/select/account_id.json +2 -2
  82. package/server/templates/table/gis.dataset.table.json +43 -43
  83. package/server/templates/table/management.user_group.table.json +112 -112
  84. package/server/templates/table/management.users.table.json +126 -126
  85. package/server/utils/addNotification.js +21 -21
  86. package/server/utils/sendNotification.js +89 -89
@@ -1,18 +1,18 @@
1
- export default async function readNotifications({
2
- pg, params = {}, query = {}, user = {},
3
- }, reply) {
4
- const { uid } = user || {};
5
-
6
- if (!uid) {
7
- return reply.status(401).send('access restricted: user ');
8
- }
9
-
10
- const q = `update crm.notifications set read=true where read is not true
11
- and ${params?.id ? 'notification_id=$2' : '1=1'} and addressee_id=$1`;
12
-
13
- if (query.sql) return q;
14
-
15
- const { rowCount = 0 } = await pg.query(q, [uid, params?.id].filter((el) => el));
16
-
17
- return reply.status(200).send(`${rowCount} unread notifications marked as read`);
18
- }
1
+ export default async function readNotifications({
2
+ pg, params = {}, query = {}, user = {},
3
+ }, reply) {
4
+ const { uid } = user || {};
5
+
6
+ if (!uid) {
7
+ return reply.status(401).send('access restricted: user ');
8
+ }
9
+
10
+ const q = `update crm.notifications set read=true where read is not true
11
+ and ${params?.id ? 'notification_id=$2' : '1=1'} and addressee_id=$1`;
12
+
13
+ if (query.sql) return q;
14
+
15
+ const { rowCount = 0 } = await pg.query(q, [uid, params?.id].filter((el) => el));
16
+
17
+ return reply.status(200).send(`${rowCount} unread notifications marked as read`);
18
+ }
@@ -1,35 +1,35 @@
1
- import { config } from '@opengis/fastify-table/utils.js';
2
-
3
- import { sendNotification } from '../../../../utils.js';
4
-
5
- export default async function testNotification({
6
- pg, query = {}, session = {},
7
- }) {
8
- const { local } = config || {};
9
-
10
- if (!session?.passport?.user?.user_type?.includes('admin') && !local) {
11
- return { message: 'Forbidden', status: 403 };
12
- }
13
-
14
- const date = new Date().toISOString().split('T')[0];
15
- if (!query.to) {
16
- return { message: 'param to is required', status: 400 };
17
- }
18
-
19
- const {
20
- to, template, table, id, nocache,
21
- } = query;
22
-
23
- const data = await sendNotification({
24
- pg,
25
- to,
26
- template,
27
- title: `Test Softpro ${date}`,
28
- table,
29
- nocache,
30
- id,
31
- message: `Test mail Softpro ${date} Lorem Ipsum Lorem Ipsum`,
32
- });
33
-
34
- return { message: data || 'ok' };
35
- }
1
+ import { config } from '@opengis/fastify-table/utils.js';
2
+
3
+ import { sendNotification } from '../../../../utils.js';
4
+
5
+ export default async function testNotification({
6
+ pg, query = {}, session = {},
7
+ }) {
8
+ const { local } = config || {};
9
+
10
+ if (!session?.passport?.user?.user_type?.includes('admin') && !local) {
11
+ return { message: 'Forbidden', status: 403 };
12
+ }
13
+
14
+ const date = new Date().toISOString().split('T')[0];
15
+ if (!query.to) {
16
+ return { message: 'param to is required', status: 400 };
17
+ }
18
+
19
+ const {
20
+ to, template, table, id, nocache,
21
+ } = query;
22
+
23
+ const data = await sendNotification({
24
+ pg,
25
+ to,
26
+ template,
27
+ title: `Test Softpro ${date}`,
28
+ table,
29
+ nocache,
30
+ id,
31
+ message: `Test mail Softpro ${date} Lorem Ipsum Lorem Ipsum`,
32
+ });
33
+
34
+ return { message: data || 'ok' };
35
+ }
@@ -1,53 +1,53 @@
1
- // for example only
2
- /*
3
- const res = await dataInsert({
4
- pg,
5
- table: 'crm.notifications',
6
- data: {
7
- subject: 'notif title',
8
- body: 'notif body',
9
- link: 'http://localhost:3000/api/notification',
10
- addressee_id: userId,
11
- author_id: userId,
12
- },
13
- });
14
- */
15
-
16
- import { getSelectVal } from '@opengis/fastify-table/utils.js';
17
-
18
- const maxLimit = 100;
19
-
20
- export default async function userNotifications({
21
- pg, query = {}, session = {},
22
- }) {
23
- const time = Date.now();
24
-
25
- const { uid } = session.passport?.user || {};
26
-
27
- if (!uid) {
28
- return { message: 'access restricted', status: 403 };
29
- }
30
-
31
- const limit = Math.min(maxLimit, +(query.limit || 5));
32
- const offset = query.page && query.page > 0 ? (query.page - 1) * limit : 0;
33
-
34
- const q = `select notification_id as id, subject, body, cdate,
35
- author_id, read, link, entity_id, (select avatar from admin.users where uid=a.author_id limit 1) as avatar from crm.notifications a where addressee_id=$1 order by cdate desc limit $2 offset $3`;
36
-
37
- if (query.sql) return q;
38
-
39
- const { rows = [] } = pg.pk?.['crm.notifications'] ? await pg.query(q, [uid, limit, offset]) : {};
40
-
41
- const values = rows.map((el) => el.author_id)
42
- ?.filter((el, idx, arr) => el && arr.indexOf(el) === idx);
43
-
44
- if (values?.length) {
45
- const vals = await getSelectVal({ name: 'core.user_mentioned', values });
46
- rows.forEach((row) => {
47
- Object.assign(row, { author: vals?.[row.author_id] });
48
- });
49
- }
50
-
51
- return { time: Date.now() - time, total: rows?.length, rows };
52
-
53
- }
1
+ // for example only
2
+ /*
3
+ const res = await dataInsert({
4
+ pg,
5
+ table: 'crm.notifications',
6
+ data: {
7
+ subject: 'notif title',
8
+ body: 'notif body',
9
+ link: 'http://localhost:3000/api/notification',
10
+ addressee_id: userId,
11
+ author_id: userId,
12
+ },
13
+ });
14
+ */
15
+
16
+ import { getSelectVal } from '@opengis/fastify-table/utils.js';
17
+
18
+ const maxLimit = 100;
19
+
20
+ export default async function userNotifications({
21
+ pg, query = {}, session = {},
22
+ }) {
23
+ const time = Date.now();
24
+
25
+ const { uid } = session.passport?.user || {};
26
+
27
+ if (!uid) {
28
+ return { message: 'access restricted', status: 403 };
29
+ }
30
+
31
+ const limit = Math.min(maxLimit, +(query.limit || 5));
32
+ const offset = query.page && query.page > 0 ? (query.page - 1) * limit : 0;
33
+
34
+ const q = `select notification_id as id, subject, body, cdate,
35
+ author_id, read, link, entity_id, (select avatar from admin.users where uid=a.author_id limit 1) as avatar from crm.notifications a where addressee_id=$1 order by cdate desc limit $2 offset $3`;
36
+
37
+ if (query.sql) return q;
38
+
39
+ const { rows = [] } = pg.pk?.['crm.notifications'] ? await pg.query(q, [uid, limit, offset]) : {};
40
+
41
+ const values = rows.map((el) => el.author_id)
42
+ ?.filter((el, idx, arr) => el && arr.indexOf(el) === idx);
43
+
44
+ if (values?.length) {
45
+ const vals = await getSelectVal({ name: 'core.user_mentioned', values });
46
+ rows.forEach((row) => {
47
+ Object.assign(row, { author: vals?.[row.author_id] });
48
+ });
49
+ }
50
+
51
+ return { time: Date.now() - time, total: rows?.length, rows };
52
+
53
+ }
@@ -1,56 +1,56 @@
1
- /* eslint-disable camelcase */
2
- import { config, pgClients, getSelect } from '@opengis/fastify-table/utils.js';
3
-
4
- import { addNotification, sendNotification } from '../../../../utils.js';
5
-
6
- function sequence(arr, data, fn) {
7
- return arr.reduce((promise, row) => promise.then(() => fn({
8
- ...data, ...row,
9
- })), Promise.resolve());
10
- }
11
-
12
- export default async function onWidgetSet({ pg = pgClients?.client, link, session, type, payload: data = {} }) {
13
- const values = data.body?.match(/\B@[a-zA-z0-9а-яА-яіїІЇєЄ]+ [a-zA-z0-9а-яА-яіїІЇєЄ]+/g)
14
- ?.filter((el, idx, arr) => el?.replace && arr.indexOf(el) === idx)
15
- ?.map((el) => el.replace(/@/g, ''));
16
-
17
- if (type !== 'comment' || !values?.length) {
18
- return null;
19
- }
20
-
21
- const { sql } = await getSelect('core.user_mentioned');
22
-
23
- const { rows = [] } = await pg.query(`with data (id,name,email) as (${sql})
24
- select id, name, email from data where name = any($1)`, [values]);
25
-
26
- if (!rows?.length) {
27
- return null;
28
- }
29
-
30
- const message = `${data.body?.substring(0, 50)}...`;
31
- const subject = 'You were mentioned';
32
-
33
- await sequence(rows, {}, async ({ id, name, email }) => {
34
- const res = await addNotification({
35
- pg, session, subject, entity: data.entity_id, body: message, link, uid: id,
36
- });
37
- try {
38
- const res1 = await sendNotification({
39
- pg,
40
- to: email,
41
- // template,
42
- message,
43
- title: subject,
44
- data,
45
- nocache: 1,
46
- });
47
- if (config.local) console.info('comment notification', name, id, email, res1);
48
- await pg.query('update crm.notifications set sent=true where notification_id=$1', [res?.notification_id]);
49
- }
50
- catch (err) {
51
- console.error('comment notification send error', err.toString());
52
- }
53
- });
54
- console.log('comment notification add', rows);
55
- return null;
56
- }
1
+ /* eslint-disable camelcase */
2
+ import { config, pgClients, getSelect } from '@opengis/fastify-table/utils.js';
3
+
4
+ import { addNotification, sendNotification } from '../../../../utils.js';
5
+
6
+ function sequence(arr, data, fn) {
7
+ return arr.reduce((promise, row) => promise.then(() => fn({
8
+ ...data, ...row,
9
+ })), Promise.resolve());
10
+ }
11
+
12
+ export default async function onWidgetSet({ pg = pgClients?.client, link, session, type, payload: data = {} }) {
13
+ const values = data.body?.match(/\B@[a-zA-z0-9а-яА-яіїІЇєЄ]+ [a-zA-z0-9а-яА-яіїІЇєЄ]+/g)
14
+ ?.filter((el, idx, arr) => el?.replace && arr.indexOf(el) === idx)
15
+ ?.map((el) => el.replace(/@/g, ''));
16
+
17
+ if (type !== 'comment' || !values?.length) {
18
+ return null;
19
+ }
20
+
21
+ const { sql } = await getSelect('core.user_mentioned');
22
+
23
+ const { rows = [] } = await pg.query(`with data (id,name,email) as (${sql})
24
+ select id, name, email from data where name = any($1)`, [values]);
25
+
26
+ if (!rows?.length) {
27
+ return null;
28
+ }
29
+
30
+ const message = `${data.body?.substring(0, 50)}...`;
31
+ const subject = 'You were mentioned';
32
+
33
+ await sequence(rows, {}, async ({ id, name, email }) => {
34
+ const res = await addNotification({
35
+ pg, session, subject, entity: data.entity_id, body: message, link, uid: id,
36
+ });
37
+ try {
38
+ const res1 = await sendNotification({
39
+ pg,
40
+ to: email,
41
+ // template,
42
+ message,
43
+ title: subject,
44
+ data,
45
+ nocache: 1,
46
+ });
47
+ if (config.local) console.info('comment notification', name, id, email, res1);
48
+ await pg.query('update crm.notifications set sent=true where notification_id=$1', [res?.notification_id]);
49
+ }
50
+ catch (err) {
51
+ console.error('comment notification send error', err.toString());
52
+ }
53
+ });
54
+ console.log('comment notification add', rows);
55
+ return null;
56
+ }
@@ -1,26 +1,26 @@
1
- import { addHook } from '@opengis/fastify-table/utils.js';
2
-
3
- // api
4
- import testEmail from './controllers/testEmail.js';
5
- import readNotifications from './controllers/readNotifications.js'; // mark as read
6
- import userNotifications from './controllers/userNotifications.js'; // check all, backend pagination
7
-
8
- // hook
9
- import onWidgetSet from './hook/onWidgetSet.js'; // send notification on comment
10
-
11
- // funcs
12
- // import addNotification from '../notifications/funcs/addNotification.js'; // add to db
13
- // import notification from '../notifications/funcs/sendNotification.js'; // send notification
14
-
15
- import { notificationSchema, emailSchema } from './schema.js';
16
-
17
- export default async function route(fastify) {
18
-
19
- // fastify.decorate('addNotification', addNotification);
20
- // fastify.decorate('notification', notification);
21
-
22
- fastify.get('/test-email', { config: { policy: ['user'] }, schema: emailSchema }, testEmail);
23
- fastify.get('/notification', { config: { policy: ['user'] }, schema: notificationSchema }, userNotifications);
24
- fastify.get('/notification-read/:id?', { config: { policy: ['user'] }, schema: notificationSchema }, readNotifications);
25
- addHook('onWidgetSet', onWidgetSet);
26
- }
1
+ import { addHook } from '@opengis/fastify-table/utils.js';
2
+
3
+ // api
4
+ import testEmail from './controllers/testEmail.js';
5
+ import readNotifications from './controllers/readNotifications.js'; // mark as read
6
+ import userNotifications from './controllers/userNotifications.js'; // check all, backend pagination
7
+
8
+ // hook
9
+ import onWidgetSet from './hook/onWidgetSet.js'; // send notification on comment
10
+
11
+ // funcs
12
+ // import addNotification from '../notifications/funcs/addNotification.js'; // add to db
13
+ // import notification from '../notifications/funcs/sendNotification.js'; // send notification
14
+
15
+ import { notificationSchema, emailSchema } from './schema.js';
16
+
17
+ export default async function route(fastify) {
18
+
19
+ // fastify.decorate('addNotification', addNotification);
20
+ // fastify.decorate('notification', notification);
21
+
22
+ fastify.get('/test-email', { config: { policy: ['user'] }, schema: emailSchema }, testEmail);
23
+ fastify.get('/notification', { config: { policy: ['user'] }, schema: notificationSchema }, userNotifications);
24
+ fastify.get('/notification-read/:id?', { config: { policy: ['user'] }, schema: notificationSchema }, readNotifications);
25
+ addHook('onWidgetSet', onWidgetSet);
26
+ }
@@ -1,11 +1,11 @@
1
- const propertiesSchema = {
2
- params: {
3
- key: { type: 'string', pattern: '^([\\d\\w._]+)$' },
4
- },
5
- querystring: {
6
- json: { type: 'string', pattern: '^([\\d\\w]+)$' },
7
- },
8
- };
9
-
10
- export default null;
1
+ const propertiesSchema = {
2
+ params: {
3
+ key: { type: 'string', pattern: '^([\\d\\w._]+)$' },
4
+ },
5
+ querystring: {
6
+ json: { type: 'string', pattern: '^([\\d\\w]+)$' },
7
+ },
8
+ };
9
+
10
+ export default null;
11
11
  export { propertiesSchema }
@@ -1,3 +1,3 @@
1
- export default async function (fastify, opts) {
2
- fastify.get(`/test`, () => { return { test: true } });
3
- }
1
+ export default async function (fastify, opts) {
2
+ fastify.get(`/test`, () => { return { test: true } });
3
+ }
@@ -1,14 +1,14 @@
1
- import userCls from './user.cls.js';
2
-
3
- export default async function userClsId(req) {
4
- const { id } = req.params || {};
5
- const res = await userCls(req);
6
- if (req.query?.sql || res?.error) {
7
- return res;
8
- }
9
- const { rows = [] } = res?.message || {};
10
- if (!rows?.length) {
11
- return { message: `cls not found: ${id}`, status: 404 };
12
- }
13
- return { message: rows?.[0], status: 200 };
14
- }
1
+ import userCls from './user.cls.js';
2
+
3
+ export default async function userClsId(req) {
4
+ const { id } = req.params || {};
5
+ const res = await userCls(req);
6
+ if (req.query?.sql || res?.error) {
7
+ return res;
8
+ }
9
+ const { rows = [] } = res?.message || {};
10
+ if (!rows?.length) {
11
+ return { message: `cls not found: ${id}`, status: 404 };
12
+ }
13
+ return { message: rows?.[0], status: 200 };
14
+ }
@@ -1,72 +1,72 @@
1
- import { readFile } from 'node:fs/promises';
2
- import { getTemplatePath, pgClients } from '@opengis/fastify-table/utils.js';
3
-
4
- export default async function userCls(req) {
5
- const {
6
- pg = pgClients.client, params = {}, query = {}, user = {},
7
- } = req;
8
- const { uid } = user || {};
9
-
10
- if (!uid) {
11
- return { message: 'access restricted', status: 403 };
12
- }
13
-
14
- if (query.type && !['json', 'sql'].includes(query.type)) {
15
- return { message: 'param type is invalid', status: 400 };
16
- }
17
-
18
- const q = `select user_clsid as id, name, type,
19
- case when type='json' then (
20
- ${params?.id
21
- ? `(with recursive rows as (
22
- select user_clsid, code as id, name as text, icon, color, parent
23
- from admin.user_cls a
24
- where name=u.name
25
- union all
26
- select a.user_clsid, a.code, a.name, a.icon, a.color, a.parent
27
- from admin.user_cls a
28
- join rows b on a.parent=b.text
29
- ) select json_agg(row_to_json(q)) from rows q where text<>u.name
30
- )`
31
- : 'select count(*)::int from admin.user_cls where parent=u.name'} ) else null end as children,
32
- case when type='sql' then data else null end as sql from admin.user_cls u
33
- where (case when type='json' then parent is null else true end)
34
- and uid=(select uid from admin.users where $1 in (login,uid) limit 1)
35
- and parent is null and ${query.type ? `type='${query.type}'` : '1=1'} and ${params?.id ? 'u.name=$2' : '1=1'}
36
- order by cdate desc`;
37
-
38
- if (query?.sql) return q;
39
-
40
-
41
- const { rows = [] } = await pg.query(q, [uid, params.id].filter((el) => el));
42
-
43
- rows.forEach((row) => {
44
- if (row.type === 'sql') delete row.children;
45
- if (row.type === 'json') { delete row.sql; Object.assign(row, { children: row.children || (params?.id ? [] : 0) }); }
46
- });
47
-
48
- const clsList = query.type === 'sql' ? [] : getTemplatePath('cls'); // skip cls if type sql
49
- const selectList = query.type === 'json' ? [] : getTemplatePath('select'); // skip sql if type json
50
-
51
- const userClsNames = rows.map((el) => el.name);
52
- const selectNames = selectList.map((el) => el[0]);
53
-
54
- const arr = (clsList || []).concat(selectList || []).map((el) => ({ name: el[0], path: el[1], type: el[2] }))
55
- ?.filter((el) => (params?.id ? el.name === params?.id : true))
56
- ?.filter((el) => ['sql', 'json'].includes(el?.type) && !userClsNames.includes(el.name))
57
- ?.filter((el) => (el?.type === 'json' ? !selectNames?.includes(el?.name) : true));
58
-
59
- const res = await Promise.all(arr?.map(async (el) => {
60
- const type = { json: 'cls', sql: 'select' }[el.type] || el.type;
61
- // const clsData = await getSelect(type, el.name);
62
- const str = await readFile(el.path, 'utf-8');
63
- const clsData = type === 'cls' ? JSON.parse(str) : str;
64
- if (type === 'cls') {
65
- const children = params?.id ? clsData : clsData?.length || 0;
66
- return { name: el.name, type: el.type, children };
67
- }
68
- return { name: el.name, type: el.type, sql: clsData?.sql || clsData };
69
- }));
70
-
71
- return { message: { rows: rows.concat(res) }, status: 200 };
72
- }
1
+ import { readFile } from 'node:fs/promises';
2
+ import { getTemplatePath, pgClients } from '@opengis/fastify-table/utils.js';
3
+
4
+ export default async function userCls(req) {
5
+ const {
6
+ pg = pgClients.client, params = {}, query = {}, user = {},
7
+ } = req;
8
+ const { uid } = user || {};
9
+
10
+ if (!uid) {
11
+ return { message: 'access restricted', status: 403 };
12
+ }
13
+
14
+ if (query.type && !['json', 'sql'].includes(query.type)) {
15
+ return { message: 'param type is invalid', status: 400 };
16
+ }
17
+
18
+ const q = `select user_clsid as id, name, type,
19
+ case when type='json' then (
20
+ ${params?.id
21
+ ? `(with recursive rows as (
22
+ select user_clsid, code as id, name as text, icon, color, parent
23
+ from admin.user_cls a
24
+ where name=u.name
25
+ union all
26
+ select a.user_clsid, a.code, a.name, a.icon, a.color, a.parent
27
+ from admin.user_cls a
28
+ join rows b on a.parent=b.text
29
+ ) select json_agg(row_to_json(q)) from rows q where text<>u.name
30
+ )`
31
+ : 'select count(*)::int from admin.user_cls where parent=u.name'} ) else null end as children,
32
+ case when type='sql' then data else null end as sql from admin.user_cls u
33
+ where (case when type='json' then parent is null else true end)
34
+ and uid=(select uid from admin.users where $1 in (login,uid) limit 1)
35
+ and parent is null and ${query.type ? `type='${query.type}'` : '1=1'} and ${params?.id ? 'u.name=$2' : '1=1'}
36
+ order by cdate desc`;
37
+
38
+ if (query?.sql) return q;
39
+
40
+
41
+ const { rows = [] } = await pg.query(q, [uid, params.id].filter((el) => el));
42
+
43
+ rows.forEach((row) => {
44
+ if (row.type === 'sql') delete row.children;
45
+ if (row.type === 'json') { delete row.sql; Object.assign(row, { children: row.children || (params?.id ? [] : 0) }); }
46
+ });
47
+
48
+ const clsList = query.type === 'sql' ? [] : getTemplatePath('cls'); // skip cls if type sql
49
+ const selectList = query.type === 'json' ? [] : getTemplatePath('select'); // skip sql if type json
50
+
51
+ const userClsNames = rows.map((el) => el.name);
52
+ const selectNames = selectList.map((el) => el[0]);
53
+
54
+ const arr = (clsList || []).concat(selectList || []).map((el) => ({ name: el[0], path: el[1], type: el[2] }))
55
+ ?.filter((el) => (params?.id ? el.name === params?.id : true))
56
+ ?.filter((el) => ['sql', 'json'].includes(el?.type) && !userClsNames.includes(el.name))
57
+ ?.filter((el) => (el?.type === 'json' ? !selectNames?.includes(el?.name) : true));
58
+
59
+ const res = await Promise.all(arr?.map(async (el) => {
60
+ const type = { json: 'cls', sql: 'select' }[el.type] || el.type;
61
+ // const clsData = await getSelect(type, el.name);
62
+ const str = await readFile(el.path, 'utf-8');
63
+ const clsData = type === 'cls' ? JSON.parse(str) : str;
64
+ if (type === 'cls') {
65
+ const children = params?.id ? clsData : clsData?.length || 0;
66
+ return { name: el.name, type: el.type, children };
67
+ }
68
+ return { name: el.name, type: el.type, sql: clsData?.sql || clsData };
69
+ }));
70
+
71
+ return { message: { rows: rows.concat(res) }, status: 200 };
72
+ }
@@ -1,17 +1,17 @@
1
- export default async function userInfo({
2
- pg, session = {},
3
- }) {
4
- const { uid } = session.passport?.user || {};
5
-
6
- if (!uid) {
7
- return { message: 'access restricted', status: 403 };
8
- }
9
-
10
- const data = await pg.query(`select user_name, sur_name, father_name, user_rnokpp, user_type, email, login from admin.users
11
- where uid=$1`, [uid]).then((res) => res.rows?.[0] || {});
12
-
13
- const { notifications = 0 } = await pg.query(`select count(*)::int as notifications from crm.notifications
14
- where addressee_id=$1 and read is not true`, [uid]).then((res) => res.rows?.[0] || {});
15
- return { uid, ...data, notifications };
16
-
17
- }
1
+ export default async function userInfo({
2
+ pg, session = {},
3
+ }) {
4
+ const { uid } = session.passport?.user || {};
5
+
6
+ if (!uid) {
7
+ return { message: 'access restricted', status: 403 };
8
+ }
9
+
10
+ const data = await pg.query(`select user_name, sur_name, father_name, user_rnokpp, user_type, email, login from admin.users
11
+ where uid=$1`, [uid]).then((res) => res.rows?.[0] || {});
12
+
13
+ const { notifications = 0 } = await pg.query(`select count(*)::int as notifications from crm.notifications
14
+ where addressee_id=$1 and read is not true`, [uid]).then((res) => res.rows?.[0] || {});
15
+ return { uid, ...data, notifications };
16
+
17
+ }