@opengis/fastify-table 1.2.17 → 1.2.19

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.2.17",
3
+ "version": "1.2.19",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -1,7 +1,14 @@
1
1
  import {
2
- config, getTemplate, getFilterSQL, getMeta, metaFormat, getAccess, setToken, gisIRColumn, applyHook, handlebars, getSelect,
2
+ config, getTemplate, getFilterSQL, getMeta, metaFormat, getAccess, setToken, gisIRColumn, applyHook, handlebars, handlebarsSync, getSelect, setOpt,
3
3
  } from '../../../../utils.js';
4
4
 
5
+ import conditions from './utils/conditions.js';
6
+
7
+ const components = {
8
+ 'vs-widget-file': `select 'vs-widget-file' as component, count(*) from crm.files where entity_id=$1 and file_status<>3`,
9
+ 'vs-widget-comments': `select 'vs-widget-comments' as component, count(*) from crm.communications where entity_id=$1`
10
+ };
11
+
5
12
  const maxLimit = 100;
6
13
  export default async function dataAPI(req) {
7
14
  const {
@@ -85,7 +92,9 @@ export default async function dataAPI(req) {
85
92
  // id, query, filter
86
93
  const [orderColumn, orderDir] = (query.order || loadTable.order || '').split(/[- ]/);
87
94
 
88
- const order = columnList.includes(orderColumn) && orderColumn?.length ? `order by ${orderColumn} ${query.desc || orderDir === 'desc' ? 'desc' : ''}` : '';
95
+ const order = query.order && columnList.includes(orderColumn) && orderColumn?.length
96
+ ? `order by ${orderColumn} ${query.desc || orderDir === 'desc' ? 'desc' : ''}`
97
+ : `order by ${(loadTable.order || 'true')}`;
89
98
  const search = loadTable.meta?.search && query.search
90
99
  ? `(${loadTable.meta?.search.split(',').map(el => `${el} ilike '%${query.search.replace(/%/g, '\\%').replace(/'/g, "''")}%'`).join(' or ')})`
91
100
  : null;
@@ -150,8 +159,97 @@ export default async function dataAPI(req) {
150
159
  ?.forEach(el => status.push({ ...el, count: rows.filter(row => el.id === row[statusColumn.name]).length }));
151
160
  }
152
161
 
162
+ const template = await getTemplate('card', hookData?.table || params.table);
163
+ const index = template?.find(el => el[0] === 'index.yml')?.[1] || {};
164
+
165
+ const html = {};
166
+ const { panels = [] } = index;
167
+
168
+ if (template && (hookData?.id || params.id)) {
169
+
170
+ // tokens result
171
+ const tokens = {};
172
+ if (index?.tokens && typeof index?.tokens === 'object' && !Array.isArray(index?.tokens)) {
173
+ Object.keys(index.tokens || {})
174
+ .filter(key => index?.tokens[key]?.public
175
+ || actions?.includes?.('edit')
176
+ || actions?.includes?.('add')
177
+ || !index?.tokens[key]?.table
178
+ )
179
+ .forEach(key => {
180
+ const item = index?.tokens[key];
181
+ Object.keys(item).filter(el => item[el]?.includes?.('{{')).forEach(el => {
182
+ item[el] = handlebarsSync.compile(item[el])({ user, uid: user?.uid, id, data: rows[0] });
183
+ });
184
+
185
+ const token = item.form && item.table ? setToken({
186
+ ids: [JSON.stringify(item)],
187
+ uid,
188
+ array: 1,
189
+ })[0] : setOpt(item, user.uid);
190
+ tokens[key] = token;
191
+ });
192
+ }
193
+
194
+ // conditions
195
+ panels?.filter(el => el.items).forEach(el => {
196
+ el.items = el.items?.filter(el => conditions(el.conditions, rows[0]));
197
+ });
198
+
199
+ // title, count
200
+ panels?.filter(el => el.items).forEach(async el => {
201
+ const filtered = el.items.filter(el => el.count?.toLowerCase?.().includes('select'));
202
+ const data = await Promise.all(filtered.map(el => pg.query(el.count).then(el => el.rows[0] || {})))
203
+ filtered.forEach((el, i) => {
204
+ Object.assign(el, data[i] || {}, data[i].count ? {} : { count: undefined })
205
+ });
206
+ const q = el.items.map((el) => el.component ? components[el.component] : null).filter(el => el).join(' union all ');
207
+ const counts = q && id
208
+ ? await pg.query(q, [id])
209
+ .then(e => e.rows.reduce((acc, curr) => Object.assign(acc, { [curr.component]: curr.count }), {}))
210
+ : {};
211
+ el.items?.filter?.(item => item.component)?.forEach(item => Object.assign(item, { count: counts?.[item.component] }));
212
+ });
213
+
214
+ // data result
215
+ const data = {};
216
+ const route = await pg.query(`select route_id as path, title from admin.routes where enabled and alias=$1 limit 1`, [table])
217
+ .then(el => el.rows?.[0] || {});
218
+ Object.assign(route, { tableTitle: loadTable?.title });
219
+ if (index?.data && index?.data?.[0]?.name) {
220
+ await Promise.all(index.data.filter((el) => el?.name && el?.sql).map(async (el) => {
221
+ const q = handlebarsSync.compile(el.sql)({ data: rows[0], user, uid: user?.uid, id });
222
+ const { rows: sqlData } = await pg.query(q);
223
+ data[el.name] = sqlData;
224
+ }));
225
+ }
226
+
227
+ // html
228
+ await Promise.all(template.filter(el => el[0].includes('.hbs')).map(async (el) => {
229
+ const htmlContent = await handlebars.compile(el[1])({ ...rows[0], user, data, tokens });
230
+ const name = el[0].substring(0, el[0].lastIndexOf('.'))
231
+ html[name] = htmlContent;
232
+ }));
233
+ }
234
+
153
235
  const res = {
154
- time: Date.now() - time, public: ispublic, card: loadTable.card, actions, total, filtered, count: rows.length, pk, form, agg, status, rows, meta, columns, filters,
236
+ time: Date.now() - time,
237
+ public: ispublic,
238
+ card: loadTable.card,
239
+ actions,
240
+ total,
241
+ filtered,
242
+ count: rows.length,
243
+ pk,
244
+ form,
245
+ agg,
246
+ status,
247
+ panels,
248
+ html,
249
+ rows,
250
+ meta,
251
+ columns,
252
+ filters,
155
253
  };
156
254
 
157
255
  // console.log({ add: loadTable.table, form: loadTable.form });
@@ -0,0 +1,21 @@
1
+ function onCheck(rule, data) {
2
+ const val = data[rule[0]];
3
+ // console.log(val, rule[1], rule[2])
4
+ if (rule[1] === '==') return val === rule[2];
5
+ if (rule[1] === '!=') return val !== rule[2];
6
+ if (rule[1] === 'in' && rule[2].split) return rule[2].split(',').includes(val);
7
+ if (rule[1] === 'in' && rule[2].includes) return rule[2].includes(val);
8
+
9
+ if (rule[1] === 'not in' && rule[2].split) return !rule[2].split(',').includes(val);
10
+ if (rule[1] === 'not in' && rule[2].includes) return !rule[2].includes(val);
11
+
12
+ if (rule[1] === '>') return val > rule[2];
13
+ if (rule[1] === '<') return val < rule[2];
14
+ }
15
+ export default function conditions(rules, data) {
16
+ if (!rules?.length) return true;
17
+ const result = Array.isArray(rules[0]) ? !rules.filter(el => !onCheck(el, data)).length : onCheck(rules, data)
18
+ // console.log(rules, result)
19
+ return result;
20
+
21
+ }