@opengis/admin 0.2.72 → 0.2.73

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.
@@ -0,0 +1,121 @@
1
+ import { createHash } from 'node:crypto';
2
+
3
+ import { config, getFilterSQL, getTemplate, handlebars, pgClients } from '@opengis/fastify-table/utils.js';
4
+ import { grpc } from '@opengis/fastify-file/utils.js';
5
+
6
+ import printTemplates from './printTemplates.js';
7
+
8
+ const { htmlToPdf } = grpc();
9
+
10
+ export default async function printTemplate(req, reply) {
11
+ const {
12
+ pg = pgClients.client,
13
+ params = {},
14
+ query = {},
15
+ user = {},
16
+ } = req;
17
+
18
+ if (!params?.name) {
19
+ return { message: 'not enough params: name', status: 400 };
20
+ }
21
+
22
+ const printBody = await getTemplate('print', params.name);
23
+
24
+ /* -- params.name === interface -- */
25
+ if (!printBody?.route) {
26
+ const where = user?.user_type?.includes('admin')
27
+ ? '1=1'
28
+ : `coalesce(b.user_uid, d.user_uid)='${user?.uid?.replace(/'/g, "''")}'::text`;
29
+
30
+ const { route_id: routeId, alias } = await pg.query(`select a.route_id, alias
31
+ from admin.routes a
32
+ left join admin.role_access b on
33
+ a.route_id=b.route_id
34
+ left join admin.roles c on
35
+ b.role_id=c.role_id
36
+ and c.enabled
37
+ left join admin.user_roles d on
38
+ c.role_id=d.role_id
39
+ and ( case when
40
+ d.expiration is not null
41
+ then d.expiration > CURRENT_DATE
42
+ else 1=1
43
+ end )
44
+ where $1 in (a.route_id, a.alias, a.table_name) and ${where}`, [params.name])
45
+ .then(el => el.rows?.[0] || {});
46
+
47
+ if (!routeId || !alias) {
48
+ return { message: `access restricted: route (${params.name}/${routeId})`, status: 403 };
49
+ }
50
+
51
+ const rows = Object.keys(printTemplates)
52
+ .filter(key => printTemplates[key]?.route === routeId)
53
+ .map(key => ({ key, title: printTemplates[key]?.title }));
54
+
55
+ return { rows };
56
+ }
57
+
58
+ if (!query.preview && !query?.demo && !params?.id) {
59
+ return { message: 'not enough params: id', status: 400 };
60
+ }
61
+
62
+ /* -- params.name === document template -- */
63
+ const format = query.format || (config.debug ? 'html' : 'pdf');
64
+
65
+ const hash = createHash('md5')
66
+ .update([query?.preview, query?.demo, params?.name, params?.id].join())
67
+ .digest('hex');
68
+
69
+ const headers = format === 'pdf'
70
+ ? { 'Content-Disposition': `inline; filename=${hash}.pdf`, 'Content-Type': 'application/pdf' }
71
+ : { 'Content-Type': 'text/html; charset=utf-8' };
72
+
73
+ const pt = printBody?.hbs || printBody?.html;
74
+
75
+ if (query?.preview) {
76
+ const matches = pt.match(/{{(?!\!)([^}]*)}}/g);
77
+ const preview = `<style> #toggle { background: yellow; } </style>`
78
+ + matches.reduce((acc, curr) => acc.replace(curr, curr.replace(/{{(?!\!)([^}]*)}}/g, `<div id="toggle">${curr.replace(/{/g, '%7B').replace(/}/g, '%7D')}</div>`)), pt);
79
+
80
+ const html = await handlebars.compile(preview)({});
81
+
82
+ if (format == 'html') {
83
+ return reply.headers(headers).send(html.replace(/%7B/g, '{').replace(/%7D/g, '}'));
84
+ }
85
+
86
+ const result = await htmlToPdf({ html: html.replace(/%7B/g, '{').replace(/%7D/g, '}') });
87
+ const buffer = Buffer.from(result.result, 'base64');
88
+ return reply.headers(headers).send(buffer);
89
+ }
90
+
91
+ if (query?.demo || params?.id) {
92
+ const table = await pg.query(`select alias from admin.routes where route_id=$1`, [printBody.route])
93
+ .then(el => el.rows?.[0]?.alias);
94
+
95
+ if (!table) {
96
+ return { message: 'route table not found', status: 404 };
97
+ }
98
+
99
+ const loadTable = await getTemplate('table', table);
100
+ const { optimizedSQL } = await getFilterSQL({ table, pg });
101
+
102
+ const where = query.demo
103
+ ? ' 1=1 limit 1'
104
+ : `${loadTable?.key || pg.pk?.[loadTable?.table || table]}=$1`;
105
+
106
+ const q = `select * from (${optimizedSQL})q where ${loadTable?.query || '1=1'} and ${where}`;
107
+
108
+ const obj = await pg.query(q, [params.id].filter(el => el))
109
+ .then(el => el.rows?.[0] || {});
110
+
111
+ const html = await handlebars.compile(pt)(obj);
112
+
113
+ if (format == 'html') {
114
+ return reply.headers(headers).send(html);
115
+ }
116
+
117
+ const result = await htmlToPdf({ html });
118
+ const buffer = Buffer.from(result.result, 'base64');
119
+ return reply.headers(headers).send(buffer);
120
+ }
121
+ }
@@ -0,0 +1 @@
1
+ export default {}
@@ -1,5 +1,7 @@
1
1
  import cardPrint from './controllers/cardPrint.js';
2
+ import printTemplate from './controllers/printTemplate.js';
2
3
 
3
4
  export default async function route(app) {
4
5
  app.get(`/card-print/:table/:id`, cardPrint);
6
+ app.get('/print-template/:name/:id?', printTemplate);
5
7
  }