@opengis/cms 0.0.43 → 0.0.44

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 (97) hide show
  1. package/README.md +98 -98
  2. package/dist/{en-CzEziHMG.js → en-OTy187va.js} +1 -0
  3. package/dist/index.html +29 -29
  4. package/dist/index.js +6167 -16007
  5. package/dist/index.umd.cjs +75 -78
  6. package/dist/{uk-TyVZmyOZ.js → uk-Dy2psXBp.js} +1 -0
  7. package/input-types.json +9 -9
  8. package/module/cms/cls/content.status.json +17 -17
  9. package/module/cms/cls/user_type.json +9 -9
  10. package/module/cms/form/admin.users.form.json +77 -77
  11. package/module/cms/select/cms.page_type.sql +1 -1
  12. package/module/cms/select/news_tag_id.sql +11 -11
  13. package/module/cms/table/admin.users.table.json +53 -53
  14. package/module/cms/table/collection.default.table.json +96 -96
  15. package/module/cms/table/single.default.table.json +116 -114
  16. package/package.json +65 -65
  17. package/plugin.js +43 -43
  18. package/server/app.js +35 -35
  19. package/server/config.js +4 -4
  20. package/server/functions/getDraftKey.js +22 -22
  21. package/server/index.js +22 -22
  22. package/server/migrations/fixes.sql +124 -124
  23. package/server/migrations/site.sql +595 -595
  24. package/server/plugins/adminHook.js +78 -78
  25. package/server/plugins/hook.js +59 -59
  26. package/server/plugins/vite.js +75 -75
  27. package/server/routes/category/controllers/cms.category.delete.js +21 -21
  28. package/server/routes/category/controllers/cms.category.get.js +17 -17
  29. package/server/routes/category/controllers/cms.category.list.js +16 -16
  30. package/server/routes/category/controllers/cms.category.post.js +21 -21
  31. package/server/routes/category/controllers/cms.category.put.js +23 -23
  32. package/server/routes/category/index.mjs +22 -22
  33. package/server/routes/cms/controllers/cmsStat.js +55 -55
  34. package/server/routes/cms/controllers/cmsSuggest.js +57 -57
  35. package/server/routes/cms/controllers/deleteContent.js +113 -113
  36. package/server/routes/cms/controllers/deleteMedia.js +76 -76
  37. package/server/routes/cms/controllers/downloadMedia.js +49 -49
  38. package/server/routes/cms/controllers/getContent.js +110 -110
  39. package/server/routes/cms/controllers/getContentBySlug.js +93 -93
  40. package/server/routes/cms/controllers/insertContent.js +217 -217
  41. package/server/routes/cms/controllers/listMedia.js +94 -94
  42. package/server/routes/cms/controllers/metadataMedia.js +39 -39
  43. package/server/routes/cms/controllers/properties.get.js +53 -53
  44. package/server/routes/cms/controllers/properties.post.js +99 -99
  45. package/server/routes/cms/controllers/searchContent.js +205 -205
  46. package/server/routes/cms/controllers/translate.js +89 -89
  47. package/server/routes/cms/controllers/updateContent.js +238 -238
  48. package/server/routes/cms/controllers/uploadMedia.js +79 -79
  49. package/server/routes/cms/index.mjs +112 -112
  50. package/server/routes/cms/utils/additionalData.js +35 -35
  51. package/server/routes/cms/utils/getCollection.js +82 -82
  52. package/server/routes/cms/utils/getSingle.js +187 -187
  53. package/server/routes/cms/utils/inputTypes.js +5 -5
  54. package/server/routes/cms/utils/insertContentLocalization.js +86 -86
  55. package/server/routes/cms/utils/requestTranslation.js +85 -85
  56. package/server/routes/cms/utils/updateLocalization.js +47 -47
  57. package/server/routes/cmsSpace/controllers/deleteSpace.js +25 -25
  58. package/server/routes/cmsSpace/controllers/getSpaces.js +27 -27
  59. package/server/routes/cmsSpace/controllers/insertSpace.js +21 -21
  60. package/server/routes/cmsSpace/controllers/updateSpace.js +23 -23
  61. package/server/routes/cmsSpace/index.mjs +20 -20
  62. package/server/routes/contentType/controllers/addContentType.js +162 -162
  63. package/server/routes/contentType/controllers/contentTypeList.js +54 -54
  64. package/server/routes/contentType/controllers/delContentType.js +75 -75
  65. package/server/routes/contentType/controllers/editContentType.js +61 -61
  66. package/server/routes/contentType/controllers/getContentType.js +37 -37
  67. package/server/routes/contentType/index.mjs +35 -35
  68. package/server/routes/contentType/utils/updateContents.js +28 -28
  69. package/server/routes/contentType/utils/updateCustomContentTable.js +53 -53
  70. package/server/routes/feedback/controllers/email.list.js +24 -24
  71. package/server/routes/feedback/controllers/feedback.js +48 -48
  72. package/server/routes/feedback/controllers/feedback.list.js +37 -37
  73. package/server/routes/feedback/controllers/news.subscriptions.js +44 -44
  74. package/server/routes/feedback/index.mjs +71 -71
  75. package/server/routes/logs/controllers/export.user.logs.js +77 -77
  76. package/server/routes/logs/controllers/user.logs.js +44 -44
  77. package/server/routes/logs/index.mjs +9 -9
  78. package/server/routes/menu/controllers/addMenu.js +37 -37
  79. package/server/routes/menu/controllers/delMenu.js +31 -31
  80. package/server/routes/menu/controllers/editMenu.js +41 -41
  81. package/server/routes/menu/controllers/getMenu.js +42 -42
  82. package/server/routes/menu/index.mjs +13 -13
  83. package/server/routes/migration/controllers/collectionToCustom.js +137 -137
  84. package/server/routes/migration/index.mjs +8 -8
  85. package/server/routes/root.mjs +8 -8
  86. package/server/routes/tags/controllers/add.tags.js +24 -24
  87. package/server/routes/tags/controllers/del.tags.js +19 -19
  88. package/server/routes/tags/controllers/edit.tags.js +25 -25
  89. package/server/routes/tags/controllers/get.tags.js +15 -15
  90. package/server/routes/tags/index.mjs +14 -14
  91. package/server/templates/cls/cms.category_type.json +9 -9
  92. package/server/templates/cls/cms.content_review_status.json +9 -9
  93. package/server/templates/cls/cms.content_status.json +9 -9
  94. package/server/templates/cls/cms.content_type.json +9 -9
  95. package/server/templates/cls/cms.lang.json +9 -9
  96. package/server/templates/page/login.html +126 -126
  97. 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,90 +1,90 @@
1
- import { pgClients, eventStream } from "@opengis/fastify-table/utils.js";
2
-
3
- import insertContentLocalization from "../utils/insertContentLocalization.js";
4
-
5
- const getCollectionId = async (id, pg) => pg.query(`select content_type_id as id, table_name as table, columns FROM site.content_types
6
- where $1 in (content_type_id, name)`, [id]).then(el => el.rows?.[0] || {});
7
-
8
- function sequence(arr, data, fn) {
9
- return arr.reduce((promise, id) => promise.then(() => fn({
10
- ...data, id,
11
- })), Promise.resolve());
12
- }
13
-
14
- export async function translateContent({
15
- pg = pgClients.client, query, user,
16
- }, reply) {
17
- if (!query.id) {
18
- return reply.status(400).send({ error: 'not enough query params: id', code: 400 });
19
- }
20
- if (!query.to) {
21
- return reply.status(400).send({ error: 'not enough query params: to', code: 400 });
22
- }
23
-
24
- const { id, collection = 'pages', from = 'uk', to, nocache } = query;
25
- const { id: collectionId, table, columns = [] } = await getCollectionId(collection, pg);
26
-
27
- if (!collectionId) {
28
- return reply.status(404).send({ error: 'collection not found', code: 404 });
29
- }
30
-
31
- const schemaKeys = columns.filter(el => el.name && el.localization).map(el => el.name);
32
-
33
- if (!schemaKeys.length) {
34
- return reply.status(400).send({ error: 'collection does not contain any fields with localization enabled', code: 400 });
35
- }
36
-
37
- const result = await insertContentLocalization({ table, id, from, to, nocache, schemaKeys, user }, pg);
38
-
39
- if (result?.error) {
40
- return reply.status(result.code).send(result);
41
- }
42
-
43
- return result;
44
- }
45
-
46
- export async function translateCollection({
47
- pg = pgClients.client, params, query, user,
48
- }, reply) {
49
- if (!query.to) {
50
- return reply.status(400).send({ error: 'not enough query params: to', code: 400 });
51
- }
52
-
53
- const { id: collectionId, table, columns = [] } = await getCollectionId(params.id, pg);
54
-
55
- if (!collectionId) {
56
- return reply.status(404).send({ error: 'collection not found', code: 404 });
57
- }
58
-
59
- const schemaKeys = columns.filter(el => el.name && el.localization).map(el => el.name);
60
-
61
- if (!schemaKeys.length) {
62
- return reply.status(400).send({ error: 'collection does not contain any fields with localization enabled', code: 400 });
63
- }
64
-
65
- const { from = 'uk', to, nocache, debug } = query;
66
-
67
- const send = eventStream(reply);
68
-
69
- send(`target localization: ${to}`);
70
- send(`skip existing: ${!nocache}`);
71
- send('localization enabled for schema keys: ' + schemaKeys.join(','));
72
-
73
- try {
74
- const ids = !table
75
- ? await pg.query(`select array_agg(content_id) from site.contents where content_type_id=$1`, [collectionId]).then(el => el.rows?.[0]?.array_agg || [])
76
- : await pg.query(`select array_agg(id) from data."${table}"`).then(el => el.rows?.[0]?.array_agg || []);
77
-
78
- const skip = await pg.query('select array_agg(object_id) from site.localization where object_id=any($1::text[]) and split_part(field_key,\':\',2)=$2', [ids, to]).then(el => el.rows?.[0]?.array_agg || []);
79
- const filteredIds = nocache ? ids : ids.filter(id => !skip.includes(id));
80
-
81
- send(`target localization already exists for ${ids.length - filteredIds.length}/${ids.length} rows`);
82
-
83
- await sequence(debug ? filteredIds.slice(0, 1) : filteredIds, { send, pg, from, to, table, nocache: true, schemaKeys, user }, insertContentLocalization);
84
- return send('translation complete', true);
85
- } catch (err) {
86
- return send(err.toString(), true);
87
- }
88
- }
89
-
1
+ import { pgClients, eventStream } from "@opengis/fastify-table/utils.js";
2
+
3
+ import insertContentLocalization from "../utils/insertContentLocalization.js";
4
+
5
+ const getCollectionId = async (id, pg) => pg.query(`select content_type_id as id, table_name as table, columns FROM site.content_types
6
+ where $1 in (content_type_id, name)`, [id]).then(el => el.rows?.[0] || {});
7
+
8
+ function sequence(arr, data, fn) {
9
+ return arr.reduce((promise, id) => promise.then(() => fn({
10
+ ...data, id,
11
+ })), Promise.resolve());
12
+ }
13
+
14
+ export async function translateContent({
15
+ pg = pgClients.client, query, user,
16
+ }, reply) {
17
+ if (!query.id) {
18
+ return reply.status(400).send({ error: 'not enough query params: id', code: 400 });
19
+ }
20
+ if (!query.to) {
21
+ return reply.status(400).send({ error: 'not enough query params: to', code: 400 });
22
+ }
23
+
24
+ const { id, collection = 'pages', from = 'uk', to, nocache } = query;
25
+ const { id: collectionId, table, columns = [] } = await getCollectionId(collection, pg);
26
+
27
+ if (!collectionId) {
28
+ return reply.status(404).send({ error: 'collection not found', code: 404 });
29
+ }
30
+
31
+ const schemaKeys = columns.filter(el => el.name && el.localization).map(el => el.name);
32
+
33
+ if (!schemaKeys.length) {
34
+ return reply.status(400).send({ error: 'collection does not contain any fields with localization enabled', code: 400 });
35
+ }
36
+
37
+ const result = await insertContentLocalization({ table, id, from, to, nocache, schemaKeys, user }, pg);
38
+
39
+ if (result?.error) {
40
+ return reply.status(result.code).send(result);
41
+ }
42
+
43
+ return result;
44
+ }
45
+
46
+ export async function translateCollection({
47
+ pg = pgClients.client, params, query, user,
48
+ }, reply) {
49
+ if (!query.to) {
50
+ return reply.status(400).send({ error: 'not enough query params: to', code: 400 });
51
+ }
52
+
53
+ const { id: collectionId, table, columns = [] } = await getCollectionId(params.id, pg);
54
+
55
+ if (!collectionId) {
56
+ return reply.status(404).send({ error: 'collection not found', code: 404 });
57
+ }
58
+
59
+ const schemaKeys = columns.filter(el => el.name && el.localization).map(el => el.name);
60
+
61
+ if (!schemaKeys.length) {
62
+ return reply.status(400).send({ error: 'collection does not contain any fields with localization enabled', code: 400 });
63
+ }
64
+
65
+ const { from = 'uk', to, nocache, debug } = query;
66
+
67
+ const send = eventStream(reply);
68
+
69
+ send(`target localization: ${to}`);
70
+ send(`skip existing: ${!nocache}`);
71
+ send('localization enabled for schema keys: ' + schemaKeys.join(','));
72
+
73
+ try {
74
+ const ids = !table
75
+ ? await pg.query(`select array_agg(content_id) from site.contents where content_type_id=$1`, [collectionId]).then(el => el.rows?.[0]?.array_agg || [])
76
+ : await pg.query(`select array_agg(id) from data."${table}"`).then(el => el.rows?.[0]?.array_agg || []);
77
+
78
+ const skip = await pg.query('select array_agg(object_id) from site.localization where object_id=any($1::text[]) and split_part(field_key,\':\',2)=$2', [ids, to]).then(el => el.rows?.[0]?.array_agg || []);
79
+ const filteredIds = nocache ? ids : ids.filter(id => !skip.includes(id));
80
+
81
+ send(`target localization already exists for ${ids.length - filteredIds.length}/${ids.length} rows`);
82
+
83
+ await sequence(debug ? filteredIds.slice(0, 1) : filteredIds, { send, pg, from, to, table, nocache: true, schemaKeys, user }, insertContentLocalization);
84
+ return send('translation complete', true);
85
+ } catch (err) {
86
+ return send(err.toString(), true);
87
+ }
88
+ }
89
+
90
90
  export default null;