@opengis/cms 0.0.57 → 0.0.58

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 (149) hide show
  1. package/README.md +131 -131
  2. package/dist/AddNewItemInTree-05PSSEFi.js +76 -0
  3. package/dist/ArticlesPage-CFjE_cw_.js +298 -0
  4. package/dist/CollectionsBreadcrumb-BCxeRikP.js +4 -0
  5. package/dist/CollectionsBreadcrumb.vue_vue_type_script_setup_true_lang-umRzB5mY.js +53 -0
  6. package/dist/CollectionsPage-DHfPNql6.js +124 -0
  7. package/dist/{CreateForm-BMOBeP4G.js → CreateForm-5FvT45vH.js} +1 -1
  8. package/dist/Dashboard-C1eGscNd.js +358 -0
  9. package/dist/EditCollectionPage-DIr1tdtn.js +187 -0
  10. package/dist/{EmptyData-DaZt_nAm.js → EmptyData-DxPrSXhV.js} +1 -1
  11. package/dist/{MenuAddPage-Bf48Z-ah.js → MenuAddPage-D-p3gFgm.js} +40 -35
  12. package/dist/MenuBody-rN5j4YBu.js +125 -0
  13. package/dist/MenuItemPage-BoJw885D.js +1027 -0
  14. package/dist/MenuList-DFEBS0NB.js +172 -0
  15. package/dist/MenuPage-BCZB_S8j.js +107 -0
  16. package/dist/MenuWrapper-AZ_8s-zd.js +12 -0
  17. package/dist/MonacoEditor-Db-3Jc3E.js +4 -0
  18. package/dist/{UniversalTable.vue_vue_type_script_setup_true_lang-CJGTsd1V.js → UniversalTable-CzqPG-tY.js} +12 -12
  19. package/dist/{UniversalTablePagination.vue_vue_type_script_setup_true_lang-GYZd_gkA.js → UniversalTablePagination-4gL47A7I.js} +1 -1
  20. package/dist/VsFormTags-CMjiu9sY.js +114 -0
  21. package/dist/VsPreview-DwETkOpb.js +63 -0
  22. package/dist/contentForm-CtMhQTG0.js +489 -0
  23. package/dist/getField-CpwVE28P.js +179 -0
  24. package/dist/index.d.ts +8 -0
  25. package/dist/index.html +29 -29
  26. package/dist/index.js +72 -71
  27. package/dist/style.css +1 -1
  28. package/dist/vs-builder-edit-D-q1o8tF.js +604 -0
  29. package/dist/vs-builder-monaco-B3Jj0V31.js +33 -0
  30. package/dist/vs-builder-preview-BH4VAM3a.js +44 -0
  31. package/dist/vs-form-custom-datatable-BDZo48w3.js +317 -0
  32. package/dist/vs-form-integer-BZ855R3g.js +61 -0
  33. package/dist/vs-form-media-select-NY27EaG1.js +837 -0
  34. package/dist/vs-form-reference-list-Dtv8fJJU.js +1536 -0
  35. package/dist/vs-form-reletion-link-BhzNQszm.js +34 -0
  36. package/dist/vs-form-tiptap-DDFQjRjY.js +4 -0
  37. package/dist/vs-form-tiptap.vue_vue_type_script_setup_true_lang-DGgsqXwg.js +11 -0
  38. package/dist/vs-richtext-md-C098v_6Q.js +4 -0
  39. package/dist/vs-richtext-md.vue_vue_type_script_setup_true_lang-Ct8uTV-J.js +14 -0
  40. package/input-types.json +9 -9
  41. package/locales/en.json +815 -814
  42. package/locales/uk.json +813 -812
  43. package/module/cms/cls/content.status.json +17 -17
  44. package/module/cms/cls/user_type.json +9 -9
  45. package/module/cms/form/admin.users.form.json +77 -77
  46. package/module/cms/select/cms.page_type.sql +1 -1
  47. package/module/cms/select/news_tag_id.sql +11 -11
  48. package/module/cms/table/admin.users.table.json +53 -53
  49. package/module/cms/table/collection.default.table.json +96 -96
  50. package/module/cms/table/single.default.table.json +116 -116
  51. package/package.json +68 -68
  52. package/plugin.js +43 -43
  53. package/server/app.js +35 -35
  54. package/server/config.js +4 -4
  55. package/server/functions/getContent.js +45 -45
  56. package/server/functions/getDraftKey.js +22 -22
  57. package/server/functions/getSearchData.js +31 -31
  58. package/server/functions/getTags.js +30 -30
  59. package/server/functions/getUser.js +27 -27
  60. package/server/functions/utils/mock.reply.js +55 -55
  61. package/server/index.js +22 -22
  62. package/server/migrations/fixes.sql +129 -129
  63. package/server/migrations/site.sql +595 -595
  64. package/server/plugins/adminHook.js +78 -78
  65. package/server/plugins/hook.js +59 -59
  66. package/server/plugins/vite.js +75 -75
  67. package/server/routes/category/controllers/cms.category.delete.js +21 -21
  68. package/server/routes/category/controllers/cms.category.get.js +17 -17
  69. package/server/routes/category/controllers/cms.category.list.js +16 -16
  70. package/server/routes/category/controllers/cms.category.post.js +21 -21
  71. package/server/routes/category/controllers/cms.category.put.js +23 -23
  72. package/server/routes/category/index.mjs +22 -22
  73. package/server/routes/cms/controllers/cmsStat.js +55 -55
  74. package/server/routes/cms/controllers/cmsSuggest.js +57 -57
  75. package/server/routes/cms/controllers/deleteContent.js +113 -113
  76. package/server/routes/cms/controllers/deleteMedia.js +76 -76
  77. package/server/routes/cms/controllers/downloadMedia.js +84 -84
  78. package/server/routes/cms/controllers/getContent.js +113 -113
  79. package/server/routes/cms/controllers/getContentBySlug.js +93 -93
  80. package/server/routes/cms/controllers/insertContent.js +217 -217
  81. package/server/routes/cms/controllers/listMedia.js +155 -155
  82. package/server/routes/cms/controllers/metadataMedia.js +39 -39
  83. package/server/routes/cms/controllers/properties.get.js +18 -18
  84. package/server/routes/cms/controllers/properties.post.js +99 -99
  85. package/server/routes/cms/controllers/searchContent.js +214 -214
  86. package/server/routes/cms/controllers/translate.js +89 -89
  87. package/server/routes/cms/controllers/updateContent.js +266 -266
  88. package/server/routes/cms/controllers/uploadMedia.js +79 -79
  89. package/server/routes/cms/functions/getSettings.js +48 -48
  90. package/server/routes/cms/index.mjs +112 -112
  91. package/server/routes/cms/utils/additionalData.js +35 -35
  92. package/server/routes/cms/utils/getCollection.js +89 -89
  93. package/server/routes/cms/utils/getSingle.js +188 -188
  94. package/server/routes/cms/utils/inputTypes.js +5 -5
  95. package/server/routes/cms/utils/insertContentLocalization.js +104 -104
  96. package/server/routes/cms/utils/requestTranslation.js +85 -85
  97. package/server/routes/cms/utils/updateLocalization.js +47 -47
  98. package/server/routes/cmsSpace/controllers/deleteSpace.js +25 -25
  99. package/server/routes/cmsSpace/controllers/getSpaces.js +27 -27
  100. package/server/routes/cmsSpace/controllers/insertSpace.js +21 -21
  101. package/server/routes/cmsSpace/controllers/updateSpace.js +23 -23
  102. package/server/routes/cmsSpace/index.mjs +20 -20
  103. package/server/routes/contentType/controllers/addContentType.js +160 -160
  104. package/server/routes/contentType/controllers/contentTypeList.js +54 -54
  105. package/server/routes/contentType/controllers/delContentType.js +75 -75
  106. package/server/routes/contentType/controllers/editContentType.js +88 -88
  107. package/server/routes/contentType/controllers/getContentType.js +65 -65
  108. package/server/routes/contentType/index.mjs +35 -35
  109. package/server/routes/contentType/utils/updateContents.js +44 -44
  110. package/server/routes/contentType/utils/updateCustomContentTable.js +53 -53
  111. package/server/routes/feedback/controllers/email.list.js +24 -24
  112. package/server/routes/feedback/controllers/feedback.js +48 -48
  113. package/server/routes/feedback/controllers/feedback.list.js +37 -37
  114. package/server/routes/feedback/controllers/news.subscriptions.js +44 -44
  115. package/server/routes/feedback/index.mjs +71 -71
  116. package/server/routes/logs/controllers/export.user.logs.js +77 -77
  117. package/server/routes/logs/controllers/user.logs.js +44 -44
  118. package/server/routes/logs/index.mjs +9 -9
  119. package/server/routes/menu/controllers/addMenu.js +37 -37
  120. package/server/routes/menu/controllers/delMenu.js +31 -31
  121. package/server/routes/menu/controllers/editMenu.js +41 -41
  122. package/server/routes/menu/controllers/getMenu.js +24 -24
  123. package/server/routes/menu/functions/getMenu.js +50 -50
  124. package/server/routes/menu/index.mjs +13 -13
  125. package/server/routes/migration/controllers/collectionToCustom.js +137 -137
  126. package/server/routes/migration/index.mjs +8 -8
  127. package/server/routes/root.mjs +8 -8
  128. package/server/routes/tags/controllers/add.tags.js +24 -24
  129. package/server/routes/tags/controllers/del.tags.js +19 -19
  130. package/server/routes/tags/controllers/edit.tags.js +25 -25
  131. package/server/routes/tags/controllers/get.tags.js +15 -15
  132. package/server/routes/tags/index.mjs +14 -14
  133. package/server/templates/cls/cms.category_type.json +9 -9
  134. package/server/templates/cls/cms.content_review_status.json +9 -9
  135. package/server/templates/cls/cms.content_status.json +9 -9
  136. package/server/templates/cls/cms.content_type.json +9 -9
  137. package/server/templates/cls/cms.lang.json +9 -9
  138. package/server/templates/page/login.html +126 -126
  139. package/utils.d.ts +52 -52
  140. package/utils.js +8 -8
  141. package/dist/ArticlesPage-BcR1hbds.js +0 -286
  142. package/dist/BuilderPage-CK_osM89.js +0 -386
  143. package/dist/CollectionsBreadcrumb.vue_vue_type_script_setup_true_lang-CnOe9ORD.js +0 -45
  144. package/dist/CollectionsPage-JfmrHNR_.js +0 -110
  145. package/dist/EditCollectionPage-Cw3GQYRe.js +0 -809
  146. package/dist/MenuItemPage-CXn5HC8j.js +0 -1366
  147. package/dist/MenuPage-tJZtK46W.js +0 -106
  148. package/dist/contentForm-B6gHgGkz.js +0 -586
  149. package/dist/getField-Y5WXnRR0.js +0 -2948
@@ -1,267 +1,267 @@
1
- import { config, pgClients, dataUpdate, dataInsert, getTemplate, checkSQL, logger } from '@opengis/fastify-table/utils.js';
2
-
3
- import inputTypes from '../utils/inputTypes.js';
4
-
5
- import updateLocalization from '../utils/updateLocalization.js';
6
-
7
- const defaultColumns = [
8
- 'content_id',
9
- 'space_id',
10
- 'content_type_id',
11
- 'created_at',
12
- 'updated_at',
13
- 'published_at',
14
- 'revision',
15
- 'locale',
16
- 'status',
17
- 'slug',
18
- 'title',
19
- 'created_by',
20
- 'published_by',
21
- 'updated_by',
22
- 'meta',
23
- ];
24
-
25
- export default async function updateContent(req, reply) {
26
- const {
27
- pg = pgClients.client,
28
- params = {},
29
- user = {},
30
- body = {},
31
- headers = {},
32
- } = req;
33
-
34
- const { type, id } = params;
35
-
36
- if (!type) {
37
- return reply.status(400).send('not enough params: type');
38
- }
39
-
40
- if (!id) {
41
- return reply.status(400).send('not enough params: id');
42
- }
43
-
44
- if (!Object.keys(body || {}).length) {
45
- return reply.status(400).send('empty body');
46
- }
47
-
48
- // order priority - custom columns -> default for pages
49
- const { ctid, ctname, dbtable, columns: contentColumns = [] } = await pg.query('select content_type_id as ctid, name as ctname, table_name as dbtable, columns from site.content_types where content_type_id in (select content_type_id from site.contents where content_id=$1) or content_type_id=$2 order by content_type_id = \'pages\'', [id, type]).then(el => el.rows?.[0] || {});
50
-
51
- const arr = config.pg ? await pg.query(`select array_agg(relname)::text[] from pg_class a
52
- left join pg_namespace b on a.relnamespace=b.oid
53
- where a.relam=2 and b.nspname='data'`).then(el => el.rows?.[0]?.array_agg || []) : [];
54
-
55
- if (!arr.length && type !== 'pages') {
56
- return reply.status(400).send('empty schema: data');
57
- }
58
-
59
- const table = arr.find(el => el === params.type);
60
-
61
- const loadTable = type === 'pages' ? await getTemplate('table', 'single.default.table') : {};
62
- const columns = type === 'pages'
63
- ? (loadTable?.columns || []).concat((contentColumns || []).filter(col => loadTable?.columns.findIndex(el => el.name === col.name) === -1))
64
- : contentColumns;
65
-
66
- // const xss = checkXSS({ body });
67
- const sql = checkSQL({ body });
68
-
69
- if (/*xss.error ||*/ sql.error && false) {
70
- logger.file(`injection/${/*xss.error ? 'xss' : */'sql'}/cms`, {
71
- table,
72
- ...params,
73
- uid: user?.uid,
74
- ...(/*xss.error ? xss : */sql),
75
- });
76
- return reply
77
- .status(409)
78
- .send(
79
- `Дані містять заборонені ${/*xss.error ? 'xss' : */'sql'} символи. Приберіть їх та спробуйте ще раз`
80
- );
81
- }
82
-
83
- // site.content_data, includes singletone
84
- if (((!table && !dbtable) || ['single', 'pages'].includes(type)) && ctid) {
85
- const cid = await pg.query(
86
- 'select content_id from site.contents where content_type_id=$1 limit 1',
87
- [ctid],
88
- ).then(el => el.rows?.[0]?.content_id);
89
-
90
- const ctid1 = body.content_type_id || ctid;
91
-
92
- if (!cid) {
93
- return reply.status(404).send('contents not found');
94
- }
95
-
96
- // ? deprecated, blocks if only default columns provided, therefore commented for now
97
- // const contentId = cid === 'pages'
98
- // ? await pg.query('select content_id from site.content_data where object_id=$1', [id]).then(el => el.rows?.[0]?.content_id)
99
- // : cid;
100
-
101
- // if (!contentId) {
102
- // return reply.status(404).send('contents not found: 2');
103
- // }
104
-
105
- const columnList = columns?.map?.(el => el.name) || [];
106
- const types = columns?.reduce?.((acc, curr) => ({ ...acc, [curr.name]: curr.type || 'text' }), {}) || {};
107
- const keys = Object.keys(body || {}).filter(key => columnList.includes(key) && !defaultColumns.includes(key));
108
-
109
-
110
- if (!Object.keys(body || {}).length) {
111
- return reply.status(400).send('invalid payload');
112
- }
113
-
114
- const blocks = await pg.query(`select json_object_agg(field_key,field_value) from site.content_data where content_id=$1 and field_type='reference'`, [id])
115
- .then(el => el.rows?.[0]?.json_object_agg || {});
116
-
117
- const emptyBlock = Object.keys(body).find(key => blocks[key] && (!body[key] || typeof body[key] !== 'object' || Object.keys(body[key] || {}) === 0));
118
-
119
- if (emptyBlock) {
120
- return reply.status(400).send('access restricted: empty/invalid block ' + emptyBlock);
121
- }
122
-
123
- const single = await pg.query(
124
- `select content_id, content_type_id, slug FROM site.contents
125
- where content_id=$1`, [params.id]
126
- ).then(el => el.rows?.[0]);
127
-
128
- const customCtExists = await pg.query(
129
- `select 1 FROM site.content_types
130
- where content_type_id=$1`, [params.id]
131
- ).then(el => el.rowCount || 0);
132
-
133
- const client = await pg.connect();
134
-
135
- try {
136
- await client.query('begin');
137
-
138
- const res = {};
139
-
140
- if (single && !customCtExists && (single?.content_type_id === 'pages' || single?.content_type_id === params.id)) {
141
- // const loadTable = await getTemplate('table', 'single.default.table');
142
- // const { columns: defaultColumns } = loadTable || {};
143
- const res1 = await dataInsert({
144
- pg: client,
145
- table: 'site.content_types',
146
- id: params.id,
147
- data: { ...body, type: 'single', name: body.name === 'pages' ? (single.slug || params.id) : (body.name || body.slug) /*, columns: defaultColumns*/ },
148
- uid: user?.uid,
149
- });
150
- Object.assign(res, res1);
151
- }
152
-
153
- const res1 = await dataUpdate({
154
- pg: client,
155
- table: 'site.contents',
156
- id,
157
- data: { ...body, content_type_id: ctid1 },
158
- uid: user?.uid,
159
- });
160
- Object.assign(res, res1);
161
-
162
- // if (contentId) {
163
- // await client.query(`delete from site.content_data where content_id=$1`, [contentId]);
164
- // }
165
- const objectId = (ctname === 'pages' || ['single', 'pages'].includes(type)) && id ? id : cid;
166
- await client.query(`delete from site.content_data where object_id=$1`, [objectId]);
167
- await Promise.all(keys.map(async key => dataInsert({
168
- pg: client,
169
- table: 'site.content_data',
170
- data: {
171
- field_key: key,
172
- content_id: objectId,
173
- object_id: objectId,
174
- field_type: types[key] || 'text',
175
- field_value: inputTypes[types[key] || ''] === 'json' || key === 'meta' ? undefined : body[key],
176
- field_value_object: inputTypes[types[key] || ''] === 'json' || key === 'meta' ? body[key] : undefined,
177
- },
178
- uid: user?.uid,
179
- })));
180
-
181
- // if pages allow localization, update localization
182
- const localeRes = await updateLocalization(client, id, body, type === 'pages' ? type : ctid1, user?.uid);
183
-
184
- if (Object.hasOwn(body, 'tag_list')) {
185
- await client.query('delete from site.tag_data where data_id=$1', [id]);
186
- if (body.tag_list?.length) {
187
- await Promise.all(body.tag_list.map(async tag => dataInsert({
188
- pg: client,
189
- table: 'site.tag_data',
190
- data: {
191
- tag_id: tag,
192
- data_id: id,
193
- },
194
- uid: user?.uid,
195
- })));
196
- Object.assign(res, { tag_list: body.tag_list });
197
- }
198
- }
199
-
200
- await client.query('commit');
201
-
202
- return {
203
- id, ...res || {}, ...(keys || []).reduce((acc, curr) => ({ ...acc, [curr]: body[curr] }), {}), ...(localeRes || {})
204
- };
205
- } catch (err) {
206
- await client.query('rollback');
207
- return reply.status(500).send(err.toString());
208
- } finally {
209
- client.release();
210
- }
211
- }
212
-
213
- if (!table && !dbtable) {
214
- return reply.status(400).send('invalid params: type');
215
- }
216
-
217
- const client = await pg.connect();
218
-
219
- try {
220
- await client.query('begin');
221
-
222
- const result = await dataUpdate({
223
- pg: client,
224
- id,
225
- table: 'data.' + `"${(table || dbtable)}"`,
226
- data: body,
227
- referer: headers?.referer,
228
- uid: user?.uid,
229
- }).catch(err => {
230
- if (err.message?.includes?.('unique constraint')) {
231
- throw new Error('Порушенні унікальності: ' + err.message?.match?.(/([^"]+)/g)?.[1]);
232
- }
233
- throw err;
234
- });
235
-
236
- await updateLocalization(client, result?.id, body, ctid, user?.uid);
237
-
238
- if (Object.hasOwn(body, 'tag_list')) {
239
- await client.query('delete from site.tag_data where data_id=$1', [id]);
240
- if (body.tag_list?.length) {
241
- await Promise.all(body.tag_list.map(async tag => dataInsert({
242
- pg: client,
243
- table: 'site.tag_data',
244
- data: {
245
- tag_id: tag?.id,
246
- data_id: id,
247
- },
248
- uid: user?.uid,
249
- })));
250
- Object.assign(result, { tag_list: body.tag_list });
251
- }
252
- }
253
-
254
- await client.query('commit');
255
-
256
- if (!result?.id) {
257
- return reply.status(404).send('content not found');
258
- }
259
-
260
- return reply.status(200).send(result);
261
- } catch (err) {
262
- await client.query('rollback');
263
- return reply.status(500).send(err.toString());
264
- } finally {
265
- client.release();
266
- }
1
+ import { config, pgClients, dataUpdate, dataInsert, getTemplate, checkSQL, logger } from '@opengis/fastify-table/utils.js';
2
+
3
+ import inputTypes from '../utils/inputTypes.js';
4
+
5
+ import updateLocalization from '../utils/updateLocalization.js';
6
+
7
+ const defaultColumns = [
8
+ 'content_id',
9
+ 'space_id',
10
+ 'content_type_id',
11
+ 'created_at',
12
+ 'updated_at',
13
+ 'published_at',
14
+ 'revision',
15
+ 'locale',
16
+ 'status',
17
+ 'slug',
18
+ 'title',
19
+ 'created_by',
20
+ 'published_by',
21
+ 'updated_by',
22
+ 'meta',
23
+ ];
24
+
25
+ export default async function updateContent(req, reply) {
26
+ const {
27
+ pg = pgClients.client,
28
+ params = {},
29
+ user = {},
30
+ body = {},
31
+ headers = {},
32
+ } = req;
33
+
34
+ const { type, id } = params;
35
+
36
+ if (!type) {
37
+ return reply.status(400).send('not enough params: type');
38
+ }
39
+
40
+ if (!id) {
41
+ return reply.status(400).send('not enough params: id');
42
+ }
43
+
44
+ if (!Object.keys(body || {}).length) {
45
+ return reply.status(400).send('empty body');
46
+ }
47
+
48
+ // order priority - custom columns -> default for pages
49
+ const { ctid, ctname, dbtable, columns: contentColumns = [] } = await pg.query('select content_type_id as ctid, name as ctname, table_name as dbtable, columns from site.content_types where content_type_id in (select content_type_id from site.contents where content_id=$1) or content_type_id=$2 order by content_type_id = \'pages\'', [id, type]).then(el => el.rows?.[0] || {});
50
+
51
+ const arr = config.pg ? await pg.query(`select array_agg(relname)::text[] from pg_class a
52
+ left join pg_namespace b on a.relnamespace=b.oid
53
+ where a.relam=2 and b.nspname='data'`).then(el => el.rows?.[0]?.array_agg || []) : [];
54
+
55
+ if (!arr.length && type !== 'pages') {
56
+ return reply.status(400).send('empty schema: data');
57
+ }
58
+
59
+ const table = arr.find(el => el === params.type);
60
+
61
+ const loadTable = type === 'pages' ? await getTemplate('table', 'single.default.table') : {};
62
+ const columns = type === 'pages'
63
+ ? (loadTable?.columns || []).concat((contentColumns || []).filter(col => loadTable?.columns.findIndex(el => el.name === col.name) === -1))
64
+ : contentColumns;
65
+
66
+ // const xss = checkXSS({ body });
67
+ const sql = checkSQL({ body });
68
+
69
+ if (/*xss.error ||*/ sql.error && false) {
70
+ logger.file(`injection/${/*xss.error ? 'xss' : */'sql'}/cms`, {
71
+ table,
72
+ ...params,
73
+ uid: user?.uid,
74
+ ...(/*xss.error ? xss : */sql),
75
+ });
76
+ return reply
77
+ .status(409)
78
+ .send(
79
+ `Дані містять заборонені ${/*xss.error ? 'xss' : */'sql'} символи. Приберіть їх та спробуйте ще раз`
80
+ );
81
+ }
82
+
83
+ // site.content_data, includes singletone
84
+ if (((!table && !dbtable) || ['single', 'pages'].includes(type)) && ctid) {
85
+ const cid = await pg.query(
86
+ 'select content_id from site.contents where content_type_id=$1 limit 1',
87
+ [ctid],
88
+ ).then(el => el.rows?.[0]?.content_id);
89
+
90
+ const ctid1 = body.content_type_id || ctid;
91
+
92
+ if (!cid) {
93
+ return reply.status(404).send('contents not found');
94
+ }
95
+
96
+ // ? deprecated, blocks if only default columns provided, therefore commented for now
97
+ // const contentId = cid === 'pages'
98
+ // ? await pg.query('select content_id from site.content_data where object_id=$1', [id]).then(el => el.rows?.[0]?.content_id)
99
+ // : cid;
100
+
101
+ // if (!contentId) {
102
+ // return reply.status(404).send('contents not found: 2');
103
+ // }
104
+
105
+ const columnList = columns?.map?.(el => el.name) || [];
106
+ const types = columns?.reduce?.((acc, curr) => ({ ...acc, [curr.name]: curr.type || 'text' }), {}) || {};
107
+ const keys = Object.keys(body || {}).filter(key => columnList.includes(key) && !defaultColumns.includes(key));
108
+
109
+
110
+ if (!Object.keys(body || {}).length) {
111
+ return reply.status(400).send('invalid payload');
112
+ }
113
+
114
+ const blocks = await pg.query(`select json_object_agg(field_key,field_value) from site.content_data where content_id=$1 and field_type='reference'`, [id])
115
+ .then(el => el.rows?.[0]?.json_object_agg || {});
116
+
117
+ const emptyBlock = Object.keys(body).find(key => blocks[key] && (!body[key] || typeof body[key] !== 'object' || Object.keys(body[key] || {}) === 0));
118
+
119
+ if (emptyBlock) {
120
+ return reply.status(400).send('access restricted: empty/invalid block ' + emptyBlock);
121
+ }
122
+
123
+ const single = await pg.query(
124
+ `select content_id, content_type_id, slug FROM site.contents
125
+ where content_id=$1`, [params.id]
126
+ ).then(el => el.rows?.[0]);
127
+
128
+ const customCtExists = await pg.query(
129
+ `select 1 FROM site.content_types
130
+ where content_type_id=$1`, [params.id]
131
+ ).then(el => el.rowCount || 0);
132
+
133
+ const client = await pg.connect();
134
+
135
+ try {
136
+ await client.query('begin');
137
+
138
+ const res = {};
139
+
140
+ if (single && !customCtExists && (single?.content_type_id === 'pages' || single?.content_type_id === params.id)) {
141
+ // const loadTable = await getTemplate('table', 'single.default.table');
142
+ // const { columns: defaultColumns } = loadTable || {};
143
+ const res1 = await dataInsert({
144
+ pg: client,
145
+ table: 'site.content_types',
146
+ id: params.id,
147
+ data: { ...body, type: 'single', name: body.name === 'pages' ? (single.slug || params.id) : (body.name || body.slug) /*, columns: defaultColumns*/ },
148
+ uid: user?.uid,
149
+ });
150
+ Object.assign(res, res1);
151
+ }
152
+
153
+ const res1 = await dataUpdate({
154
+ pg: client,
155
+ table: 'site.contents',
156
+ id,
157
+ data: { ...body, content_type_id: ctid1 },
158
+ uid: user?.uid,
159
+ });
160
+ Object.assign(res, res1);
161
+
162
+ // if (contentId) {
163
+ // await client.query(`delete from site.content_data where content_id=$1`, [contentId]);
164
+ // }
165
+ const objectId = (ctname === 'pages' || ['single', 'pages'].includes(type)) && id ? id : cid;
166
+ await client.query(`delete from site.content_data where object_id=$1`, [objectId]);
167
+ await Promise.all(keys.map(async key => dataInsert({
168
+ pg: client,
169
+ table: 'site.content_data',
170
+ data: {
171
+ field_key: key,
172
+ content_id: objectId,
173
+ object_id: objectId,
174
+ field_type: types[key] || 'text',
175
+ field_value: inputTypes[types[key] || ''] === 'json' || key === 'meta' ? undefined : body[key],
176
+ field_value_object: inputTypes[types[key] || ''] === 'json' || key === 'meta' ? body[key] : undefined,
177
+ },
178
+ uid: user?.uid,
179
+ })));
180
+
181
+ // if pages allow localization, update localization
182
+ const localeRes = await updateLocalization(client, id, body, type === 'pages' ? type : ctid1, user?.uid);
183
+
184
+ if (Object.hasOwn(body, 'tag_list')) {
185
+ await client.query('delete from site.tag_data where data_id=$1', [id]);
186
+ if (body.tag_list?.length) {
187
+ await Promise.all(body.tag_list.map(async tag => dataInsert({
188
+ pg: client,
189
+ table: 'site.tag_data',
190
+ data: {
191
+ tag_id: tag,
192
+ data_id: id,
193
+ },
194
+ uid: user?.uid,
195
+ })));
196
+ Object.assign(res, { tag_list: body.tag_list });
197
+ }
198
+ }
199
+
200
+ await client.query('commit');
201
+
202
+ return {
203
+ id, ...res || {}, ...(keys || []).reduce((acc, curr) => ({ ...acc, [curr]: body[curr] }), {}), ...(localeRes || {})
204
+ };
205
+ } catch (err) {
206
+ await client.query('rollback');
207
+ return reply.status(500).send(err.toString());
208
+ } finally {
209
+ client.release();
210
+ }
211
+ }
212
+
213
+ if (!table && !dbtable) {
214
+ return reply.status(400).send('invalid params: type');
215
+ }
216
+
217
+ const client = await pg.connect();
218
+
219
+ try {
220
+ await client.query('begin');
221
+
222
+ const result = await dataUpdate({
223
+ pg: client,
224
+ id,
225
+ table: 'data.' + `"${(table || dbtable)}"`,
226
+ data: body,
227
+ referer: headers?.referer,
228
+ uid: user?.uid,
229
+ }).catch(err => {
230
+ if (err.message?.includes?.('unique constraint')) {
231
+ throw new Error('Порушенні унікальності: ' + err.message?.match?.(/([^"]+)/g)?.[1]);
232
+ }
233
+ throw err;
234
+ });
235
+
236
+ await updateLocalization(client, result?.id, body, ctid, user?.uid);
237
+
238
+ if (Object.hasOwn(body, 'tag_list')) {
239
+ await client.query('delete from site.tag_data where data_id=$1', [id]);
240
+ if (body.tag_list?.length) {
241
+ await Promise.all(body.tag_list.map(async tag => dataInsert({
242
+ pg: client,
243
+ table: 'site.tag_data',
244
+ data: {
245
+ tag_id: tag?.id,
246
+ data_id: id,
247
+ },
248
+ uid: user?.uid,
249
+ })));
250
+ Object.assign(result, { tag_list: body.tag_list });
251
+ }
252
+ }
253
+
254
+ await client.query('commit');
255
+
256
+ if (!result?.id) {
257
+ return reply.status(404).send('content not found');
258
+ }
259
+
260
+ return reply.status(200).send(result);
261
+ } catch (err) {
262
+ await client.query('rollback');
263
+ return reply.status(500).send(err.toString());
264
+ } finally {
265
+ client.release();
266
+ }
267
267
  }