@opengis/fastify-table 1.1.114 → 1.1.116

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/fastify-table",
3
- "version": "1.1.114",
3
+ "version": "1.1.116",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -1,6 +1,4 @@
1
- import config from '../../../../config.js';
2
1
  import pgClients from '../../pg/pgClients.js';
3
-
4
2
  import getTemplate from '../../table/funcs/getTemplate.js';
5
3
  import applyHook from '../../hook/funcs/applyHook.js';
6
4
 
@@ -26,33 +24,29 @@ export default async function getAccess({ table, user = {} }) {
26
24
  const hookData = await applyHook('getAccess', { table, user });
27
25
  if (hookData) return hookData;
28
26
 
29
- const { uid, user_type: userType } = user;
27
+ const { uid, user_type: userType = 'regular' } = user;
30
28
 
31
- if (config.auth?.disable || userType?.includes?.('admin')) {
29
+ if (userType.includes('admin')) {
32
30
  return { actions: ['view', 'edit', 'add', 'del'], query: '1=1' };
33
31
  }
34
32
 
35
- const body = await getTemplate('table', table) || {};
33
+ const body = await getTemplate('table', table);
34
+ const tableActions = ['view'].concat(body?.actions || body?.action_default || []);
36
35
 
37
- if (body.access === 'admin' && !userType?.includes?.('admin')) {
38
- const { userActions = [] } = uid ? await pgClients.client.query(q, [table, uid]).then(el => el.rows?.[0] || {}) : {};
39
- const customActions = userActions.filter((el => ['view'].concat(body.actions || body.action_default || []).includes(el)));
40
- return { actions: customActions, query: '1=1' };
36
+ if (body?.public || body?.access === 'public') {
37
+ return { actions: tableActions, query: '1=1' };
41
38
  }
42
39
 
43
- const actions = uid ? ['view'].concat(body.actions || body.action_default || []) : [];
44
-
45
- if (body?.public || body.access === 'public') {
46
- return { actions, query: '1=1' };
40
+ if (body?.access === 'user' && uid) {
41
+ return { actions: tableActions, query: '1=1' };
47
42
  }
48
43
 
49
- const dbTable = pgClients.client?.pk?.[table] ? table : body?.table;
50
- if (!uid || !dbTable) return null;
44
+ const { userScope, userActions = [] } = uid
45
+ ? await pgClients.client.query(q, [table, uid]).then(el => el.rows?.[0] || {})
46
+ : {};
51
47
 
52
- const { userScope, userActions = [] } = await pgClients.client.query(q, [table, uid]).then(el => el.rows?.[0] || {});
53
- const query = body.access === 'user' || userScope === 'my' ? `uid='${uid}'` : null;
54
- const customActions = userActions.filter((el => actions.includes(el)));
55
- const scope = body.access === 'user' ? 'my' : userScope;
48
+ const query = uid && userScope === 'my' ? `uid='${uid}'` : '1=1';
49
+ const actions = userActions.filter((el => tableActions.includes(el)));
56
50
 
57
- return { scope, actions: customActions, query };
51
+ return { scope: userScope, actions, query };
58
52
  }
@@ -3,16 +3,16 @@ import { handlebars } from '@opengis/fastify-hb/utils.js';
3
3
  import getTemplate from '../getTemplate.js';
4
4
  import getSelectVal from './getSelectVal.js';
5
5
 
6
- // import { getTemplate, getSelectVal } from '../../../../../utils.js';
7
-
8
- export default async function metaFormat({ rows, table }) {
9
- const loadTable = await getTemplate('table', table);
10
- const selectCols = loadTable?.columns?.filter((e) => e.data);
6
+ export default async function metaFormat({
7
+ rows, table, cls, sufix = true,
8
+ }) {
9
+ const loadTable = table ? await getTemplate('table', table) : {};
10
+ const selectCols = Object.keys(cls || {}).map(key => ({ name: key, data: cls[key] })).concat(loadTable?.columns?.filter((e) => e.data) || []);
11
11
  const metaCls = Object.keys(loadTable?.meta?.cls || {}).map((el) => ({ name: el, data: loadTable?.meta?.cls[el] }));
12
- const htmlCols = loadTable?.columns?.filter((e) => e.name && e.format === 'html' && e.html);
12
+ const htmlCols = loadTable?.columns?.filter?.((e) => e.name && e.format === 'html' && e.html) || [];
13
13
  if (!selectCols?.length && !metaCls?.length && !htmlCols?.length) return rows;
14
14
 
15
- await Promise.all(htmlCols?.map(async (attr) => {
15
+ await Promise.all(htmlCols.map(async (attr) => {
16
16
  await Promise.all(rows.filter(row => row?.[attr.name])?.map(async (row) => {
17
17
  const html = await handlebars.compile(attr.html)(row);
18
18
  Object.assign(row, { [attr.name]: html?.replaceAll(/<[^>]*>/g, '')?.replace(/\n/g, '')?.trim() });
@@ -24,13 +24,18 @@ export default async function metaFormat({ rows, table }) {
24
24
  const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => (typeof el === 'boolean' ? true : el));
25
25
  if (!values.length) return null;
26
26
 
27
- const cls = await getSelectVal({ name: attr.data, values });
28
- if (!cls) return null;
27
+ const clsValues = await getSelectVal({ name: attr.data, values });
28
+ if (!clsValues) return null;
29
29
 
30
30
  rows.forEach(el => {
31
- const val = el[attr.name]?.map?.(c => cls[c.toString()] || cls[c] || c) || cls[el[attr.name]?.toString()] || cls[el[attr.name]] || el[attr.name];
31
+ const val = el[attr.name]?.map?.(c => clsValues[c.toString()] || clsValues[c] || c) || clsValues[el[attr.name]?.toString()] || clsValues[el[attr.name]] || el[attr.name];
32
32
  if (!val) return;
33
- Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
33
+ if (!sufix) {
34
+ Object.assign(el, { [attr.name]: val.text || val });
35
+ }
36
+ else {
37
+ Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
38
+ }
34
39
  });
35
40
 
36
41
  return null;
@@ -5,7 +5,9 @@ import config from '../../../../config.js';
5
5
  import insert from './insert.js';
6
6
 
7
7
  export default async function update(req) {
8
- const { user, params = {}, body = {} } = req;
8
+ const {
9
+ pg = pgClients.client, user, params = {}, body = {},
10
+ } = req;
9
11
  if (!user) return { message: 'access restricted', status: 403 };
10
12
  const hookData = await applyHook('preUpdate', {
11
13
  table: params?.table, id: params?.id, user,
@@ -69,7 +71,7 @@ export default async function update(req) {
69
71
  if (extraKeys?.length) {
70
72
  res.extra = {};
71
73
  await Promise.all(extraKeys?.map(async (key) => {
72
- const objId = body[schema[key].parent_id] || body?.id || res?.[schema[key]?.parent_id];
74
+ const objId = body[schema[key].parent_id] || body?.id || res?.[schema[key]?.parent_id] || res?.[pg.pk?.[loadTemplate?.table || table] || ''];
73
75
  // delete old extra data
74
76
  await pgClients.client.query(`delete from ${schema[key].table} where ${schema[key].parent_id}=$1`, [objId]); // rewrite?
75
77
  // insert new extra data