@opengis/admin 0.3.33 → 0.3.35

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 (27) hide show
  1. package/dist/{add-page-DEMB0NVp.js → add-page-DmhwcAMY.js} +1 -1
  2. package/dist/{admin-interface-hLLnl1qT.js → admin-interface-5dHYgR-t.js} +1 -1
  3. package/dist/{admin-view-BxAU72CU.js → admin-view-C7JteOit.js} +1 -1
  4. package/dist/admin.js +1 -1
  5. package/dist/admin.umd.cjs +63 -63
  6. package/dist/{card-view-DdCCgDuV.js → card-view-Cey1LE5b.js} +1 -1
  7. package/dist/{edit-page-Dw2VGB_7.js → edit-page-C-cpKJC2.js} +1 -1
  8. package/dist/{import-file-Brsexl6w.js → import-file-B53vJ2ve.js} +11780 -11682
  9. package/dist/{profile-page-C_aOC-wH.js → profile-page-BqYeCCjU.js} +1 -1
  10. package/dist/style.css +1 -1
  11. package/package.json +2 -2
  12. package/server/plugins/hook.js +1 -0
  13. package/server/routes/notifications/controllers/readNotifications.js +9 -18
  14. package/server/routes/notifications/controllers/testEmail.js +2 -2
  15. package/server/routes/notifications/controllers/userNotifications.js +1 -1
  16. package/server/routes/widget/controllers/widget.del.js +32 -15
  17. package/server/routes/widget/controllers/widget.get.js +12 -7
  18. package/server/routes/widget/controllers/widget.set.js +19 -10
  19. package/server/utils/isAdmin.js +8 -0
  20. package/server/{routes/notifications/funcs → utils}/sendNotification.js +5 -8
  21. package/utils.js +4 -3
  22. package/server/routes/dblist/controllers/readItems.js +0 -20
  23. package/server/routes/dblist/controllers/setItem.js +0 -22
  24. package/server/routes/dblist/index.mjs +0 -18
  25. package/server/routes/dblist/utils/formatData.js +0 -7
  26. /package/server/{routes/notifications/funcs → utils}/addNotification.js +0 -0
  27. /package/server/{routes/notifications/funcs/utils → utils}/sendEmail.js +0 -0
@@ -36,7 +36,7 @@ export default async function userNotifications({
36
36
 
37
37
  if (query.sql) return q;
38
38
 
39
- const { rows = [] } = await pg.query(q, [uid, limit, offset]);
39
+ const { rows = [] } = pg.pk?.['crm.notifications'] ? await pg.query(q, [uid, limit, offset]) : {};
40
40
 
41
41
  const values = rows.map((el) => el.author_id)
42
42
  ?.filter((el, idx, arr) => el && arr.indexOf(el) === idx);
@@ -1,5 +1,8 @@
1
+ import { pgClients, logChanges } from "@opengis/fastify-table/utils.js";
1
2
  import { isFileExists } from "@opengis/fastify-file/utils.js";
2
3
 
4
+ import { isAdmin } from "../../../../utils.js";
5
+
3
6
  async function checkAccess(pg, objectid, id) {
4
7
  const { uid, filepath } = await pg.query(`select uid, file_path as filepath from crm.files where entity_id=$1 and file_id=$2`, [objectid, id])
5
8
  .then(el => el.rows?.[0] || {});
@@ -25,39 +28,53 @@ async function checkAccess(pg, objectid, id) {
25
28
  * @returns {String|Object} message Повідомлення про успішне виконання або об'єкт з параметрами
26
29
  */
27
30
 
28
- import { pgClients, logChanges } from "@opengis/fastify-table/utils.js";
31
+ export default async function widgetDel(req, reply) {
32
+ const {
33
+ pg = pgClients.client, params = {}, user = {},
34
+ } = req;
35
+
36
+ if (!user?.uid) {
37
+ return reply.status(401).send('access restricted: user not authorized');
38
+ }
29
39
 
30
- export default async function widgetDel({
31
- pg = pgClients.client, params = {}, session = {},
32
- }, reply) {
33
- const { user = {} } = session.passport || {};
34
- if (!user.uid) return { error: 'access restricted', status: 403 };
35
40
  const { type, objectid, id } = params;
36
41
 
37
- if (!objectid) return { error: 'id required', status: 400 };
42
+ if (!objectid) {
43
+ return reply.status(400).send('not enough params: id');
44
+ }
38
45
 
39
46
  // force delete db entry if file not exists
40
47
  const { exists, uid } = ['file', 'gallery'].includes(type) ? await checkAccess(pg, objectid, id) : {};
41
48
 
42
- if (exists && !user?.user_type?.includes?.('admin') && uid && user?.uid !== uid) {
49
+ if (exists && !isAdmin(req) && uid && user?.uid !== uid) {
43
50
  return reply.status(403).send('access restricted: file exists, not an author');
44
51
  }
45
52
 
46
53
  const sqls = {
47
- comment: 'delete from crm.communications where entity_id=$1 and uid=$2 and communication_id=$3',
48
- checklist: 'delete from crm.checklists where entity_id=$1 and uid=$2 and checklist_id=$3',
49
- file: `update crm.files set file_status=3 where entity_id=$1 and ${!exists || user?.user_type?.includes?.('admin') ? '$2=$2' : 'uid=$2'} and file_id=$3 returning uploaded_name`,
50
- gallery: `update crm.files set file_status=3 where entity_id=$1 and ${!exists || user?.user_type?.includes?.('admin') ? '$2=$2' : 'uid=$2'} and file_id=$3 returning uploaded_name`,
54
+ comment: `delete from crm.communications where entity_id=$1 and ${isAdmin(req) ? '$2=$2' : 'uid=$2'} and communication_id=$3`,
55
+ checklist: `delete from crm.checklists where entity_id=$1 and ${isAdmin(req) ? '$2=$2' : 'uid=$2'} and checklist_id=$3`,
56
+ file: `update crm.files set file_status=3 where entity_id=$1 and ${!exists || isAdmin(req) ? '$2=$2' : 'uid=$2'} and file_id=$3 returning uploaded_name`,
57
+ gallery: `update crm.files set file_status=3 where entity_id=$1 and ${!exists || isAdmin(req) ? '$2=$2' : 'uid=$2'} and file_id=$3 returning uploaded_name`,
51
58
  };
59
+
52
60
  const sql = sqls[type];
53
- // console.log(sql);
54
- if (!sql) return reply.status(400).send('type not valid');
61
+ const table = {
62
+ comment: 'crm.communications',
63
+ checklist: 'crm.checklists',
64
+ file: 'crm.files',
65
+ gallery: 'crm.files',
66
+ }[type];
67
+
68
+ if (!sql) {
69
+ return reply.status(400).send('invalid widget type');
70
+ }
55
71
 
56
72
  const { rows = [] } = await pg.query(sql, [objectid, user.uid, id]);
57
- const table = { comment: 'crm.communications', checklist: 'crm.checklists', file: 'crm.files', gallery: 'crm.files' }[type];
73
+
58
74
  await logChanges({
59
75
  pg, table, id, data: rows[0], uid: user?.uid, type: 'DELETE',
60
76
  });
77
+
61
78
  return { data: { id }, user: { uid: user.uid, name: user.user_name } };
62
79
 
63
80
  }
@@ -9,8 +9,8 @@ const username = `coalesce(u.sur_name,'')||coalesce(' '||u.user_name,'') ||coale
9
9
  */
10
10
 
11
11
  export default async function widgetGet({
12
- pg = pgClients.client, user = {}, params = {}, query = {},
13
- }) {
12
+ pg = pgClients.client, user = {}, params = {}, query = {}, unittest,
13
+ }, reply) {
14
14
  const param = user?.uid ? await getToken({
15
15
  token: params.objectid, mode: 'w', uid: user.uid,
16
16
  }) : null;
@@ -18,7 +18,7 @@ export default async function widgetGet({
18
18
  const objectid = param ? JSON.parse(param)?.id : params.objectid;
19
19
 
20
20
  if (!objectid) {
21
- return { message: 'id required', status: 400 };
21
+ return reply.status(400).send('not enough params: id');
22
22
  }
23
23
 
24
24
  const sqls = {
@@ -87,10 +87,11 @@ export default async function widgetGet({
87
87
  isverified, uid as author, file_status FROM crm.files c where entity_id=$1 and file_status<>3 and ext = any($2) order by cdate desc`,
88
88
 
89
89
  };
90
+
90
91
  const sql = sqls[params.type];
91
92
 
92
93
  if (!sql) {
93
- return { message: 'param type not valid', status: 400 };
94
+ return reply.status(400).send('invalid widget type');
94
95
  }
95
96
 
96
97
 
@@ -101,13 +102,17 @@ export default async function widgetGet({
101
102
  time.push(Date.now());
102
103
 
103
104
  /* Object info */
104
- const { tableName } = pg.pk['log.table_changes'] ? await pg.one('select entity_type as "tableName" from log.table_changes where entity_id=$1 limit 1', [objectid]) : {};
105
- const { pk } = await getMeta({ table: tableName });
105
+ const { tableName } = pg.pk['log.table_changes'] ? await pg.query('select entity_type as "tableName" from log.table_changes where entity_id=$1 limit 1', [objectid]).then(el => el.rows?.[0] || {}) : {};
106
+ const { pk } = await getMeta({ pg, table: tableName });
107
+
108
+ if (!pk && !unittest) {
109
+ return reply.status(404).send('table not found');
110
+ }
106
111
 
107
112
  const q = tableName && pg.pk['admin.users'] ? `select ${username} as author, u.login, a.cdate, a.editor_date from ${tableName} a
108
113
  left join admin.users u on a.uid=u.uid where a.${pk}=$1 limit 1` : undefined;
109
114
 
110
- const data = pk && q ? await pg.one(q, [objectid]) : {};
115
+ const data = pk && q ? await pg.query(q, [objectid]).then(el => el.rows?.[0] || {}) : {};
111
116
 
112
117
  if (query.debug && user?.user_type === 'admin') {
113
118
  return {
@@ -19,14 +19,19 @@ const pkList = {
19
19
 
20
20
  const galleryExtList = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'mp4', 'mov', 'avi'];
21
21
 
22
- export default async function widgetSet(req) {
22
+ export default async function widgetSet(req, reply) {
23
23
  const {
24
- pg, params = {}, session = {}, body = {},
24
+ pg, params = {}, session = {}, body = {}, user = {},
25
25
  } = req;
26
- const { user = {} } = session.passport || {};
27
26
  const { type, id, objectid } = params;
28
- if (!['comment', 'checklist', 'file', 'gallery'].includes(type)) return { message: 'param type not valid', status: 400 };
29
- if (!objectid) return { message: 'id required', status: 400 };
27
+
28
+ if (!['comment', 'checklist', 'file', 'gallery'].includes(type)) {
29
+ return reply.status(400).send('param type not valid');
30
+ }
31
+
32
+ if (!objectid) {
33
+ return reply.status(400).send('not enough params: id');
34
+ }
30
35
 
31
36
  const table = tableList[type];
32
37
 
@@ -46,22 +51,26 @@ export default async function widgetSet(req) {
46
51
  };
47
52
 
48
53
  if (type === 'gallery' && !galleryExtList.includes(extName.toLowerCase())) {
49
- return { message: 'invalid file extension', status: 400 };
54
+ return reply.status(400).send('invalid file extension');
50
55
  }
51
56
 
52
57
  const { rows = [] } = await dataInsert({
53
- table: 'crm.files', data, uid: user?.uid,
58
+ pg, table: 'crm.files', data, uid: user?.uid,
54
59
  });
55
60
  return {
56
61
  rowCount: 1, data: 'ok', command: 'UPLOAD', id: rows[0]?.file_id, entity_id: rows[0]?.entity_id,
57
62
  };
58
63
  }
59
64
  const { pk } = await getMeta({ pg, table });
60
- if (!pk) return { message: 'table not found', status: 404 };
65
+
66
+ if (!pk) {
67
+ return reply.status(404).send('table not found');
68
+ }
61
69
 
62
70
  const data = { ...body, uid: user?.uid, entity_id: objectid };
63
71
 
64
72
  await applyHook('onWidgetSet', {
73
+ pg,
65
74
  link: req.path,
66
75
  id,
67
76
  objectid,
@@ -72,10 +81,10 @@ export default async function widgetSet(req) {
72
81
 
73
82
  const result = id
74
83
  ? await dataUpdate({
75
- table, data, id, uid: user?.uid,
84
+ pg, table, data, id, uid: user?.uid,
76
85
  })
77
86
  : await dataInsert({
78
- table, data, uid: user?.uid,
87
+ pg, table, data, uid: user?.uid,
79
88
  });
80
89
 
81
90
  return {
@@ -0,0 +1,8 @@
1
+ import { config } from '@opengis/fastify-table/utils.js';
2
+
3
+ const isAdmin = (req) => process.env.NODE_ENV === 'admin'
4
+ || config.admin
5
+ || req?.hostname?.split?.(':')?.shift?.() === config.adminDomain
6
+ || req?.hostname?.startsWith?.('admin');
7
+
8
+ export default isAdmin;
@@ -1,9 +1,11 @@
1
+ import { handlebars, pgClients, getTemplate, getRedis, logger } from '@opengis/fastify-table/utils.js';
2
+
3
+ import sendEmail from './sendEmail.js';
4
+
1
5
  // eslint-disable-next-line max-len, no-control-regex
2
6
  const emailReg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g;
3
7
 
4
- import sendEmail from './utils/sendEmail.js';
5
-
6
- import { handlebars, pgClients, getTemplate, getRedis, logger } from '@opengis/fastify-table/utils.js';
8
+ const rclient = getRedis();
7
9
 
8
10
  async function generateNotificationContent({
9
11
  pg, table, template, id, message, data: data1,
@@ -35,10 +37,6 @@ export default async function notification({
35
37
  id,
36
38
  nocache,
37
39
  }) {
38
- const rclient = getRedis();
39
-
40
- if (!pg) throw new Error('need pg');
41
-
42
40
  if (pg?.readonly) {
43
41
  return null;
44
42
  }
@@ -46,7 +44,6 @@ export default async function notification({
46
44
  const keyTo = `${pg.options?.database}:mail:${provider[0]}:${to || ''}${id || ''}${table || ''}${title || ''}`;
47
45
  const uniqueTo = await rclient.setnx(keyTo, 1);
48
46
 
49
-
50
47
  if (!uniqueTo && !nocache) {
51
48
  logger.file('notification/sent', { keyTo, send: uniqueTo, nocache });
52
49
  return `already sent: ${keyTo}`;
package/utils.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import yamlSafe from 'js-yaml';
2
2
 
3
3
 
4
-
5
- import addNotification from './server/routes/notifications/funcs/addNotification.js';
6
- import sendNotification from './server/routes/notifications/funcs/sendNotification.js';
4
+ import isAdmin from './server/utils/isAdmin.js';
5
+ import addNotification from './server/utils/addNotification.js';
6
+ import sendNotification from './server/utils/sendNotification.js';
7
7
 
8
8
  import getAdminAccess from './server/plugins/access/funcs/getAdminAccess.js';
9
9
 
@@ -19,6 +19,7 @@ Object.assign(yamlSafe, { loadSafe });
19
19
 
20
20
  export default null;
21
21
  export {
22
+ isAdmin,
22
23
  yamlSafe,
23
24
  addNotification,
24
25
  sendNotification,
@@ -1,20 +0,0 @@
1
- import { dblist, getRedis } from '@opengis/fastify-table/utils.js';
2
-
3
- import formatData from '../utils/formatData.js';
4
-
5
- const rclient = getRedis();
6
-
7
- export default async function readItemList(req) {
8
- const rows = formatData(dblist);
9
- const uid = req.session.passport?.user?.uid // login db
10
- || req.session.passport?.user?.username // login passwd
11
- || '2';
12
-
13
- const key = `current-db:${uid}`;
14
- const ttl = await rclient.ttl(key);
15
- const currentId = await rclient.get(key);
16
- rclient.setex(key, 60 * 60 * 10000, currentId);
17
-
18
- const { originalMaxAge, expires } = req.session?.cookie || {};
19
- return { ttl, current: currentId || rows[0]?.id, rows, user: { ...req.user, originalMaxAge, expires, uid }, };
20
- }
@@ -1,22 +0,0 @@
1
- import { dblist, getRedis } from '@opengis/fastify-table/utils.js';
2
-
3
- export default async function setItem(req) {
4
- const { params = {} } = req;
5
- const { id } = params;
6
-
7
- if (!id) return { error: 'not enough params', status: 400 };
8
-
9
- const current = dblist.find((el) => [el.id, el.key].includes(id));
10
- if (!current?.database) {
11
- return { error: 'invalid param id', status: 400 };
12
- }
13
-
14
- const rclient = getRedis();
15
- const uid = req.session.passport?.user?.uid // login db
16
- || req.session.passport?.user?.username // login passwd
17
- || '2';
18
-
19
- await rclient.setex(`current-db:${uid}`, 60 * 60 * 10000, id);
20
-
21
- return { current: id };
22
- }
@@ -1,18 +0,0 @@
1
- import readItemList from './controllers/readItems.js';
2
- import setItem from './controllers/setItem.js';
3
-
4
- export default async function plugin(fastify) {
5
-
6
- fastify.route({
7
- method: 'GET',
8
- url: '/db-list',
9
- config: { policy: ['site'] },
10
- handler: readItemList,
11
- });
12
- fastify.route({
13
- method: 'GET',
14
- url: '/db-list/:id',
15
- config: { policy: ['site'] },
16
- handler: setItem,
17
- });
18
- }
@@ -1,7 +0,0 @@
1
- const showKeys = ['id', 'key', 'title'];
2
-
3
- export default function formatData(data = []) {
4
- return data?.length
5
- ? data.map((el) => Object.keys(el).filter((key) => showKeys.includes(key)).reduce((acc, curr) => Object.assign(acc, { [curr]: el[curr] }), {}))
6
- : [];
7
- }