@opengis/cms 0.0.26 → 0.0.28

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 (90) hide show
  1. package/README.md +98 -98
  2. package/dist/index-W-qQIppj-DunG40EG.js +2452 -0
  3. package/dist/index.html +28 -28
  4. package/dist/index.js +12683 -6702
  5. package/dist/index.umd.cjs +87 -41
  6. package/dist/style.css +1 -0
  7. package/package.json +68 -68
  8. package/plugin.js +76 -76
  9. package/server/app.js +35 -35
  10. package/server/config.js +4 -4
  11. package/server/functions/getDraftKey.js +22 -22
  12. package/server/index.js +22 -22
  13. package/server/migrations/fixes.sql +123 -123
  14. package/server/migrations/site.sql +545 -545
  15. package/server/plugins/adminHook.js +78 -78
  16. package/server/plugins/hook.js +59 -59
  17. package/server/plugins/vite.js +84 -84
  18. package/server/routes/category/controllers/cms.category.delete.js +21 -21
  19. package/server/routes/category/controllers/cms.category.get.js +17 -17
  20. package/server/routes/category/controllers/cms.category.list.js +16 -16
  21. package/server/routes/category/controllers/cms.category.post.js +21 -21
  22. package/server/routes/category/controllers/cms.category.put.js +23 -23
  23. package/server/routes/category/index.mjs +22 -22
  24. package/server/routes/cms/controllers/cmsStat.js +55 -55
  25. package/server/routes/cms/controllers/cmsSuggest.js +57 -57
  26. package/server/routes/cms/controllers/deleteContent.js +114 -114
  27. package/server/routes/cms/controllers/deleteMedia.js +75 -75
  28. package/server/routes/cms/controllers/downloadMedia.js +48 -48
  29. package/server/routes/cms/controllers/getContent.js +110 -110
  30. package/server/routes/cms/controllers/getContentBySlug.js +95 -95
  31. package/server/routes/cms/controllers/getPermissions.js +15 -15
  32. package/server/routes/cms/controllers/insertContent.js +218 -218
  33. package/server/routes/cms/controllers/listMedia.js +93 -93
  34. package/server/routes/cms/controllers/metadataMedia.js +38 -38
  35. package/server/routes/cms/controllers/properties.get.js +53 -53
  36. package/server/routes/cms/controllers/properties.post.js +99 -99
  37. package/server/routes/cms/controllers/searchContent.js +205 -205
  38. package/server/routes/cms/controllers/setPermissions.js +49 -49
  39. package/server/routes/cms/controllers/translate.js +89 -89
  40. package/server/routes/cms/controllers/updateContent.js +238 -238
  41. package/server/routes/cms/controllers/uploadMedia.js +78 -78
  42. package/server/routes/cms/index.mjs +114 -114
  43. package/server/routes/cms/utils/additionalData.js +35 -35
  44. package/server/routes/cms/utils/getCollection.js +81 -81
  45. package/server/routes/cms/utils/getSingle.js +187 -187
  46. package/server/routes/cms/utils/insertContentLocalization.js +86 -86
  47. package/server/routes/cms/utils/requestTranslation.js +85 -85
  48. package/server/routes/cms/utils/updateLocalization.js +47 -47
  49. package/server/routes/cmsSpace/controllers/deleteSpace.js +25 -25
  50. package/server/routes/cmsSpace/controllers/getSpaces.js +27 -27
  51. package/server/routes/cmsSpace/controllers/insertSpace.js +21 -21
  52. package/server/routes/cmsSpace/controllers/updateSpace.js +23 -23
  53. package/server/routes/cmsSpace/index.mjs +20 -20
  54. package/server/routes/contentType/controllers/addContentType.js +162 -162
  55. package/server/routes/contentType/controllers/contentTypeList.js +54 -54
  56. package/server/routes/contentType/controllers/delContentType.js +75 -75
  57. package/server/routes/contentType/controllers/editContentType.js +61 -61
  58. package/server/routes/contentType/controllers/getContentType.js +37 -37
  59. package/server/routes/contentType/index.mjs +35 -35
  60. package/server/routes/contentType/utils/updateContents.js +28 -28
  61. package/server/routes/contentType/utils/updateCustomContentTable.js +55 -55
  62. package/server/routes/feedback/controllers/email.list.js +24 -24
  63. package/server/routes/feedback/controllers/feedback.js +48 -48
  64. package/server/routes/feedback/controllers/feedback.list.js +37 -37
  65. package/server/routes/feedback/controllers/news.subscriptions.js +44 -44
  66. package/server/routes/feedback/index.mjs +71 -71
  67. package/server/routes/logs/controllers/export.user.logs.js +77 -77
  68. package/server/routes/logs/controllers/user.logs.js +44 -44
  69. package/server/routes/logs/index.mjs +9 -9
  70. package/server/routes/menu/controllers/addMenu.js +37 -37
  71. package/server/routes/menu/controllers/delMenu.js +31 -31
  72. package/server/routes/menu/controllers/editMenu.js +41 -41
  73. package/server/routes/menu/controllers/getMenu.js +42 -42
  74. package/server/routes/menu/index.mjs +13 -13
  75. package/server/routes/migration/controllers/collectionToCustom.js +137 -137
  76. package/server/routes/migration/index.mjs +8 -8
  77. package/server/routes/root.mjs +8 -8
  78. package/server/routes/tags/controllers/add.tags.js +24 -24
  79. package/server/routes/tags/controllers/del.tags.js +19 -19
  80. package/server/routes/tags/controllers/edit.tags.js +25 -25
  81. package/server/routes/tags/controllers/get.tags.js +15 -15
  82. package/server/routes/tags/index.mjs +14 -14
  83. package/server/templates/cls/cms.category_type.json +9 -9
  84. package/server/templates/cls/cms.content_review_status.json +9 -9
  85. package/server/templates/cls/cms.content_status.json +9 -9
  86. package/server/templates/cls/cms.content_type.json +9 -9
  87. package/server/templates/cls/cms.lang.json +9 -9
  88. package/server/templates/page/login.html +126 -126
  89. package/server/templates/select/core.user_mentioned.sql +1 -1
  90. package/dist/index-W-qQIppj-DRzFSjU1.js +0 -2452
@@ -1,205 +1,205 @@
1
- import { config, pgClients, getFilterSQL } from '@opengis/fastify-table/utils.js';
2
-
3
- const filterList = [
4
- {
5
- ua: 'Статус',
6
- name: 'status',
7
- type: 'Check'
8
- },
9
- {
10
- name: 'published_at',
11
- type: 'Date'
12
- },
13
- {
14
- name: 'search',
15
- columns: 'slug,title',
16
- type: 'Text'
17
- },
18
- ];
19
-
20
- const matchFilterType = {
21
- select: 'check',
22
- datetime: 'date'
23
- };
24
-
25
- const maxLimit = 100;
26
-
27
- export default async function searchContent({
28
- pg = pgClients.client, query = {}, user = {},
29
- }, reply) {
30
- if (!pg?.pk?.['site.contents']) {
31
- return reply.status(404).send('table not found');
32
- }
33
-
34
- const t1 = Date.now();
35
-
36
- const {
37
- locale, // to do?
38
- filter,
39
- contentType,
40
- asc,
41
- tags,
42
- } = query;
43
-
44
- if (contentType && typeof contentType !== 'string') {
45
- return reply.status(400).send('invalid query params: contentType must be a string');
46
- }
47
-
48
- if (tags && typeof tags !== 'string') {
49
- return reply.status(400).send('invalid query params: tags must be a string');
50
- }
51
-
52
- // add for singletone offset
53
- const page = Math.max(1, parseInt(query.page || 1, 10));
54
- const limit = Math.min(query.limit || 16, maxLimit);
55
- const offset = (page - 1) * limit;
56
-
57
- const { q } = filter ? await getFilterSQL({
58
- pg,
59
- table: 'site.contents',
60
- filter,
61
- query: `status='published'`,
62
- filterList,
63
- }) : { q: `status='published'` };
64
-
65
- // 1. Query all metadata
66
- const q1 = `
67
- SELECT a.title, a.slug, a.status, a.content_type_id, b.name as content_type_name, b.title as content_type_title, a.content_id,
68
- b.type as content_type, b.columns, b.table_name, case when b.type = 'single' then main_image else null end as main_image
69
- FROM site.contents a
70
- LEFT JOIN site.content_types b ON a.content_type_id = b.content_type_id
71
- WHERE b.visible
72
- AND a.slug IS NOT NULL
73
- ${tags ? `AND case when b.type = 'single' then a.content_id in (SELECT data_id FROM site.tag_data td left join site.tags ts on td.tag_id=ts.tag_id WHERE ts.slug = any($1) or ts.tag_id = any($1)) else true end` : ''}
74
- ${contentType ? `and ((b.name=any($${tags ? 2 : 1}) or a.content_type_id=any($${tags ? 2 : 1}) or b.title=any($${tags ? 2 : 1})) ${contentType.includes('pages') ? `or b.type='single')` : ')'}` : ''}
75
- ${q ? `AND (CASE WHEN b.type = 'single' THEN ${q.replace(/"title"/g, 'a.title').replace(/"slug"/g, 'a.slug').replace(/status/g, 'a.status')} ELSE TRUE END)` : ''}
76
- `;
77
-
78
- const { rows: contents = [] } = await pg.query(q1, [(tags ? tags.split(',') : null), (contentType ? contentType.split(',') : null)].filter(Boolean))
79
- .catch(err => {
80
- console.error(q1, err.toString(), err.stack);
81
- return {};
82
- }) || {};
83
-
84
- if (!contents.length) {
85
- return reply.status(404).send('content not found');
86
- }
87
-
88
- const singles = contents.filter(el => el.content_type === 'single');
89
- const collections = contents.filter(el => el.content_type === 'collection' && el.table_name);
90
-
91
- // const totals = await pg.query(`select json_object_agg(oid::regclass, reltuples) from pg_class`).then(el => el.rows?.[0]?.json_object_agg || {});
92
- const allSingles = await pg.query(`select count(*)::int from site.contents where content_type_id in (select content_type_id from site.content_types where type = 'single' )`).then(el => el.rows?.[0]?.count || 0);
93
- const allCollectionTables = await pg.query(`select array_agg(table_name) from site.content_types where type = 'collection' and table_name is not null`).then(el => el.rows?.[0]?.array_agg || []);
94
-
95
- const allCollections = await Promise.all(allCollectionTables.map(async tablename => {
96
- const total = pg.queryCache ? await pg.queryCache(`select count(*)::int from data."${tablename}"`, { table: `data.${tablename}` }).then(el => el.rows?.[0]?.count || 0) : 0;
97
- return total;
98
- })).then(el => el.reduce((acc, curr) => acc + (curr || 0), 0) || 0);
99
-
100
- // collections
101
- const collectionResults = await Promise.all(collections.map(async row => {
102
- const { table_name: table, columns = [] } = row;
103
-
104
- const collectionFilters = columns.filter(el =>
105
- ['title', 'status', 'published_at'].includes(el.name) || el.type === 'text'
106
- ).map(({ name, type }) => ({ name, type: matchFilterType[type] || type })); // select column => check type filter
107
-
108
- collectionFilters.push({
109
- name: 'search',
110
- columns: collectionFilters
111
- .filter(({ type }) => type === 'text')
112
- .map(({ name }) => name).join(','),
113
- type: 'Text',
114
- });
115
-
116
- const order = {
117
- true: 'published_at ASC NULLS LAST',
118
- false: 'published_at DESC NULLS LAST'
119
- }[!!asc];
120
-
121
- const { optimizedSQL = `SELECT * FROM data."${table}" where status='published' ORDER BY ${order}` } =
122
- filter ? await getFilterSQL({
123
- pg,
124
- table: `data."${table}"`,
125
- filter,
126
- query: `status='published'`,
127
- filterList: collectionFilters,
128
- order: 'published_at DESC NULLS LAST'
129
- }) : {};
130
-
131
- const q2 = `select id, ${columns.filter(el =>
132
- ['title', 'status', 'published_at', 'slug', 'main_image'].includes(el.name) || el.type === 'text'
133
- ).map(({ name }) => name === 'published_at' ? name : `"${name}"`).join(',')} from (${optimizedSQL}) q where ${tags ? 'id in (SELECT data_id FROM site.tag_data td left join site.tags ts on td.tag_id=ts.tag_id WHERE ts.slug = any($1))' : '1=1'} limit ${offset + limit}`;
134
-
135
- // const total = pg.queryCache ? await pg.queryCache(`select count(*)::int from data."${table}"`, { table: `data.${table}` }).then(el => el.rows?.[0]?.count || 0) : 0;
136
-
137
- const { rows: items = [] } = await pg.query(q2, [tags ? tags.split(',') : null].filter(Boolean)).catch(err => {
138
- console.error(q2, err.toString(), err.stack);
139
- return { rows: [], /*total: total // totals[`data.${table}`] || totals[`data."${table}"`] || 0*/ };
140
- });
141
-
142
- return {
143
- rows: items.map(el => ({
144
- ...row,
145
- ...el,
146
- slug: el.slug,
147
- title: el.title,
148
- status: el.status,
149
- published_at: el.published_at,
150
- table_name: undefined,
151
- columns: undefined
152
- })),
153
- // total: total /*totals[`data.${table}`] || totals[`data."${table}"`] || 0,*/
154
- };
155
- }));
156
-
157
- const allRows = [
158
- ...singles.map(row => ({
159
- ...row,
160
- columns: undefined,
161
- table_name: undefined
162
- })),
163
- ...collectionResults.map(el => el.rows || []).flat()
164
- ];
165
-
166
- const collectionIds = pg.queryCache ? await Promise.all(contents.filter(el => el.content_type === 'collection' && el.table_name).map(async row => pg.queryCache(`select array_agg(id) from data."${row.table_name}"`))).then(el => el.map(res => res.rows[0]?.array_agg || []).flat()) : [];
167
-
168
- // add tags etc. for current page of rows
169
- const allTags = pg.queryCache ? await pg.queryCache(
170
- `select a.data_id, b.tag_id, b.value, b.color, b.slug from site.tag_data a left join site.tags b on a.tag_id=b.tag_id
171
- where a.data_id = any($1)`,
172
- { args: [contents.map(el => el.content_id).concat(collectionIds)], table: 'site.tag_data' },
173
- ).then(el => el.rows || []) : [];
174
-
175
- // sort
176
- const sorted = allRows.sort((a, b) => {
177
- const aDate = new Date(a.published_at || 0);
178
- const bDate = new Date(b.published_at || 0);
179
- return bDate - aDate;
180
- });
181
-
182
- const paginated = sorted.slice(offset, offset + limit);
183
-
184
- paginated.forEach(row => {
185
- if (row.content_type === 'single' && row.content_id) {
186
- row.tag_list = allTags
187
- .filter(tag => tag.data_id === row.content_id)
188
- .map(({ tag_id: id, value: text, color, slug }) => ({ id, text, color, slug }));
189
- } else if (row.content_type === 'collection' && row.id) {
190
- row.tag_list = allTags
191
- .filter(tag => tag.data_id === row.id)
192
- .map(({ tag_id: id, value: text, color, slug }) => ({ id, text, color, slug }));
193
- }
194
- });
195
-
196
- return reply.status(200).send({
197
- time: Date.now() - t1,
198
- page,
199
- limit,
200
- total: allSingles + allCollections,
201
- filtered: sorted.length,
202
- count: paginated.length,
203
- rows: paginated,
204
- });
205
- }
1
+ import { config, pgClients, getFilterSQL } from '@opengis/fastify-table/utils.js';
2
+
3
+ const filterList = [
4
+ {
5
+ ua: 'Статус',
6
+ name: 'status',
7
+ type: 'Check'
8
+ },
9
+ {
10
+ name: 'published_at',
11
+ type: 'Date'
12
+ },
13
+ {
14
+ name: 'search',
15
+ columns: 'slug,title',
16
+ type: 'Text'
17
+ },
18
+ ];
19
+
20
+ const matchFilterType = {
21
+ select: 'check',
22
+ datetime: 'date'
23
+ };
24
+
25
+ const maxLimit = 100;
26
+
27
+ export default async function searchContent({
28
+ pg = pgClients.client, query = {}, user = {},
29
+ }, reply) {
30
+ if (!pg?.pk?.['site.contents']) {
31
+ return reply.status(404).send('table not found');
32
+ }
33
+
34
+ const t1 = Date.now();
35
+
36
+ const {
37
+ locale, // to do?
38
+ filter,
39
+ contentType,
40
+ asc,
41
+ tags,
42
+ } = query;
43
+
44
+ if (contentType && typeof contentType !== 'string') {
45
+ return reply.status(400).send('invalid query params: contentType must be a string');
46
+ }
47
+
48
+ if (tags && typeof tags !== 'string') {
49
+ return reply.status(400).send('invalid query params: tags must be a string');
50
+ }
51
+
52
+ // add for singletone offset
53
+ const page = Math.max(1, parseInt(query.page || 1, 10));
54
+ const limit = Math.min(query.limit || 16, maxLimit);
55
+ const offset = (page - 1) * limit;
56
+
57
+ const { q } = filter ? await getFilterSQL({
58
+ pg,
59
+ table: 'site.contents',
60
+ filter,
61
+ query: `status='published'`,
62
+ filterList,
63
+ }) : { q: `status='published'` };
64
+
65
+ // 1. Query all metadata
66
+ const q1 = `
67
+ SELECT a.title, a.slug, a.status, a.content_type_id, b.name as content_type_name, b.title as content_type_title, a.content_id,
68
+ b.type as content_type, b.columns, b.table_name, case when b.type = 'single' then main_image else null end as main_image
69
+ FROM site.contents a
70
+ LEFT JOIN site.content_types b ON a.content_type_id = b.content_type_id
71
+ WHERE b.visible
72
+ AND a.slug IS NOT NULL
73
+ ${tags ? `AND case when b.type = 'single' then a.content_id in (SELECT data_id FROM site.tag_data td left join site.tags ts on td.tag_id=ts.tag_id WHERE ts.slug = any($1) or ts.tag_id = any($1)) else true end` : ''}
74
+ ${contentType ? `and ((b.name=any($${tags ? 2 : 1}) or a.content_type_id=any($${tags ? 2 : 1}) or b.title=any($${tags ? 2 : 1})) ${contentType.includes('pages') ? `or b.type='single')` : ')'}` : ''}
75
+ ${q ? `AND (CASE WHEN b.type = 'single' THEN ${q.replace(/"title"/g, 'a.title').replace(/"slug"/g, 'a.slug').replace(/status/g, 'a.status')} ELSE TRUE END)` : ''}
76
+ `;
77
+
78
+ const { rows: contents = [] } = await pg.query(q1, [(tags ? tags.split(',') : null), (contentType ? contentType.split(',') : null)].filter(Boolean))
79
+ .catch(err => {
80
+ console.error(q1, err.toString(), err.stack);
81
+ return {};
82
+ }) || {};
83
+
84
+ if (!contents.length) {
85
+ return reply.status(404).send('content not found');
86
+ }
87
+
88
+ const singles = contents.filter(el => el.content_type === 'single');
89
+ const collections = contents.filter(el => el.content_type === 'collection' && el.table_name);
90
+
91
+ // const totals = await pg.query(`select json_object_agg(oid::regclass, reltuples) from pg_class`).then(el => el.rows?.[0]?.json_object_agg || {});
92
+ const allSingles = await pg.query(`select count(*)::int from site.contents where content_type_id in (select content_type_id from site.content_types where type = 'single' )`).then(el => el.rows?.[0]?.count || 0);
93
+ const allCollectionTables = await pg.query(`select array_agg(table_name) from site.content_types where type = 'collection' and table_name is not null`).then(el => el.rows?.[0]?.array_agg || []);
94
+
95
+ const allCollections = await Promise.all(allCollectionTables.map(async tablename => {
96
+ const total = pg.queryCache ? await pg.queryCache(`select count(*)::int from data."${tablename}"`, { table: `data.${tablename}` }).then(el => el.rows?.[0]?.count || 0) : 0;
97
+ return total;
98
+ })).then(el => el.reduce((acc, curr) => acc + (curr || 0), 0) || 0);
99
+
100
+ // collections
101
+ const collectionResults = await Promise.all(collections.map(async row => {
102
+ const { table_name: table, columns = [] } = row;
103
+
104
+ const collectionFilters = columns.filter(el =>
105
+ ['title', 'status', 'published_at'].includes(el.name) || el.type === 'text'
106
+ ).map(({ name, type }) => ({ name, type: matchFilterType[type] || type })); // select column => check type filter
107
+
108
+ collectionFilters.push({
109
+ name: 'search',
110
+ columns: collectionFilters
111
+ .filter(({ type }) => type === 'text')
112
+ .map(({ name }) => name).join(','),
113
+ type: 'Text',
114
+ });
115
+
116
+ const order = {
117
+ true: 'published_at ASC NULLS LAST',
118
+ false: 'published_at DESC NULLS LAST'
119
+ }[!!asc];
120
+
121
+ const { optimizedSQL = `SELECT * FROM data."${table}" where status='published' ORDER BY ${order}` } =
122
+ filter ? await getFilterSQL({
123
+ pg,
124
+ table: `data."${table}"`,
125
+ filter,
126
+ query: `status='published'`,
127
+ filterList: collectionFilters,
128
+ order: 'published_at DESC NULLS LAST'
129
+ }) : {};
130
+
131
+ const q2 = `select id, ${columns.filter(el =>
132
+ ['title', 'status', 'published_at', 'slug', 'main_image'].includes(el.name) || el.type === 'text'
133
+ ).map(({ name }) => name === 'published_at' ? name : `"${name}"`).join(',')} from (${optimizedSQL}) q where ${tags ? 'id in (SELECT data_id FROM site.tag_data td left join site.tags ts on td.tag_id=ts.tag_id WHERE ts.slug = any($1))' : '1=1'} limit ${offset + limit}`;
134
+
135
+ // const total = pg.queryCache ? await pg.queryCache(`select count(*)::int from data."${table}"`, { table: `data.${table}` }).then(el => el.rows?.[0]?.count || 0) : 0;
136
+
137
+ const { rows: items = [] } = await pg.query(q2, [tags ? tags.split(',') : null].filter(Boolean)).catch(err => {
138
+ console.error(q2, err.toString(), err.stack);
139
+ return { rows: [], /*total: total // totals[`data.${table}`] || totals[`data."${table}"`] || 0*/ };
140
+ });
141
+
142
+ return {
143
+ rows: items.map(el => ({
144
+ ...row,
145
+ ...el,
146
+ slug: el.slug,
147
+ title: el.title,
148
+ status: el.status,
149
+ published_at: el.published_at,
150
+ table_name: undefined,
151
+ columns: undefined
152
+ })),
153
+ // total: total /*totals[`data.${table}`] || totals[`data."${table}"`] || 0,*/
154
+ };
155
+ }));
156
+
157
+ const allRows = [
158
+ ...singles.map(row => ({
159
+ ...row,
160
+ columns: undefined,
161
+ table_name: undefined
162
+ })),
163
+ ...collectionResults.map(el => el.rows || []).flat()
164
+ ];
165
+
166
+ const collectionIds = pg.queryCache ? await Promise.all(contents.filter(el => el.content_type === 'collection' && el.table_name).map(async row => pg.queryCache(`select array_agg(id) from data."${row.table_name}"`))).then(el => el.map(res => res.rows[0]?.array_agg || []).flat()) : [];
167
+
168
+ // add tags etc. for current page of rows
169
+ const allTags = pg.queryCache ? await pg.queryCache(
170
+ `select a.data_id, b.tag_id, b.value, b.color, b.slug from site.tag_data a left join site.tags b on a.tag_id=b.tag_id
171
+ where a.data_id = any($1)`,
172
+ { args: [contents.map(el => el.content_id).concat(collectionIds)], table: 'site.tag_data' },
173
+ ).then(el => el.rows || []) : [];
174
+
175
+ // sort
176
+ const sorted = allRows.sort((a, b) => {
177
+ const aDate = new Date(a.published_at || 0);
178
+ const bDate = new Date(b.published_at || 0);
179
+ return bDate - aDate;
180
+ });
181
+
182
+ const paginated = sorted.slice(offset, offset + limit);
183
+
184
+ paginated.forEach(row => {
185
+ if (row.content_type === 'single' && row.content_id) {
186
+ row.tag_list = allTags
187
+ .filter(tag => tag.data_id === row.content_id)
188
+ .map(({ tag_id: id, value: text, color, slug }) => ({ id, text, color, slug }));
189
+ } else if (row.content_type === 'collection' && row.id) {
190
+ row.tag_list = allTags
191
+ .filter(tag => tag.data_id === row.id)
192
+ .map(({ tag_id: id, value: text, color, slug }) => ({ id, text, color, slug }));
193
+ }
194
+ });
195
+
196
+ return reply.status(200).send({
197
+ time: Date.now() - t1,
198
+ page,
199
+ limit,
200
+ total: allSingles + allCollections,
201
+ filtered: sorted.length,
202
+ count: paginated.length,
203
+ rows: paginated,
204
+ });
205
+ }
@@ -1,50 +1,50 @@
1
- import { logger, pgClients, dataInsert } from '@opengis/fastify-table/utils.js';
2
-
3
- export default async function setPermissions(req, reply) {
4
- const { pg = pgClients.client, params = {}, user = {}, body = {} } = req;
5
-
6
- if (!user?.uid) {
7
- return reply.status(401).send('unauthorized');
8
- }
9
-
10
- if (!params.id) {
11
- return reply.status(400).send('not enough params: id');
12
- }
13
-
14
- const client = await pg.connect()
15
- const result = {};
16
- try {
17
- await client.query('BEGIN');
18
-
19
- const { rowCount = 0 } = await client.query(
20
- `delete from site.permissions where user_id=$1`,
21
- [params.id].filter(Boolean),
22
- );
23
-
24
- Object.assign(result, { deleted: rowCount });
25
-
26
- if (Array.isArray(body.permissions) && body.permissions?.length) {
27
- body.permissions.forEach((el) => {
28
- Object.assign(el, { user_id: el.user_id || params.id });
29
- });
30
-
31
- await Promise.all(body.permissions.map(async (el) => dataInsert({
32
- pg: client,
33
- table: 'site.permissions',
34
- data: el,
35
- uid: user.uid,
36
- })));
37
-
38
- Object.assign(result, { inserted: body.permissions.length });
39
- }
40
-
41
- await client.query('COMMIT');
42
- return reply.status(200).send(result);
43
- } catch (err) {
44
- await client.query('ROLLBACK');
45
- logger.file('cms/permissions', { error: err.toString(), stack: err.stack });
46
- return reply.status(500).send('set permissions error');
47
- } finally {
48
- client.release();
49
- }
1
+ import { logger, pgClients, dataInsert } from '@opengis/fastify-table/utils.js';
2
+
3
+ export default async function setPermissions(req, reply) {
4
+ const { pg = pgClients.client, params = {}, user = {}, body = {} } = req;
5
+
6
+ if (!user?.uid) {
7
+ return reply.status(401).send('unauthorized');
8
+ }
9
+
10
+ if (!params.id) {
11
+ return reply.status(400).send('not enough params: id');
12
+ }
13
+
14
+ const client = await pg.connect()
15
+ const result = {};
16
+ try {
17
+ await client.query('BEGIN');
18
+
19
+ const { rowCount = 0 } = await client.query(
20
+ `delete from site.permissions where user_id=$1`,
21
+ [params.id].filter(Boolean),
22
+ );
23
+
24
+ Object.assign(result, { deleted: rowCount });
25
+
26
+ if (Array.isArray(body.permissions) && body.permissions?.length) {
27
+ body.permissions.forEach((el) => {
28
+ Object.assign(el, { user_id: el.user_id || params.id });
29
+ });
30
+
31
+ await Promise.all(body.permissions.map(async (el) => dataInsert({
32
+ pg: client,
33
+ table: 'site.permissions',
34
+ data: el,
35
+ uid: user.uid,
36
+ })));
37
+
38
+ Object.assign(result, { inserted: body.permissions.length });
39
+ }
40
+
41
+ await client.query('COMMIT');
42
+ return reply.status(200).send(result);
43
+ } catch (err) {
44
+ await client.query('ROLLBACK');
45
+ logger.file('cms/permissions', { error: err.toString(), stack: err.stack });
46
+ return reply.status(500).send('set permissions error');
47
+ } finally {
48
+ client.release();
49
+ }
50
50
  }