@opengis/cms 0.0.61 → 0.0.62
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/README.md +131 -131
- package/dist/{EditCollectionPage-3Q57ptN3.js → EditCollectionPage-C4uNmBJf.js} +1 -1
- package/dist/{contentForm-CLStrfSg.js → contentForm-CcFbUeal.js} +144 -146
- package/dist/index.js +1 -1
- package/dist/{vs-builder-preview-BH4VAM3a.js → vs-builder-preview-DL3RYMp7.js} +11 -12
- package/dist/vs-form-custom-datatable-D880w8gx.js +493 -0
- package/input-types.json +9 -9
- package/locales/en.json +815 -815
- package/locales/uk.json +813 -813
- package/module/cms/cls/content.status.json +17 -17
- package/module/cms/cls/user_type.json +9 -9
- package/module/cms/form/admin.users.form.json +77 -77
- package/module/cms/select/cms.page_type.sql +1 -1
- package/module/cms/select/news_tag_id.sql +11 -11
- package/module/cms/table/admin.users.table.json +53 -53
- package/module/cms/table/collection.default.table.json +96 -96
- package/module/cms/table/single.default.table.json +116 -116
- package/package.json +69 -69
- package/plugin.js +43 -43
- package/server/app.js +35 -35
- package/server/config.js +4 -4
- package/server/functions/getContent.js +45 -45
- package/server/functions/getDraftKey.js +22 -22
- package/server/functions/getSearchData.js +31 -31
- package/server/functions/getTags.js +30 -30
- package/server/functions/getUser.js +27 -27
- package/server/functions/utils/mock.reply.js +55 -55
- package/server/index.js +22 -22
- package/server/migrations/fixes.sql +132 -132
- package/server/migrations/site.sql +596 -596
- package/server/plugins/adminHook.js +78 -78
- package/server/plugins/hook.js +59 -59
- package/server/plugins/vite.js +75 -75
- package/server/routes/category/controllers/cms.category.delete.js +21 -21
- package/server/routes/category/controllers/cms.category.get.js +17 -17
- package/server/routes/category/controllers/cms.category.list.js +16 -16
- package/server/routes/category/controllers/cms.category.post.js +21 -21
- package/server/routes/category/controllers/cms.category.put.js +23 -23
- package/server/routes/category/index.mjs +22 -22
- package/server/routes/cms/controllers/cmsStat.js +55 -55
- package/server/routes/cms/controllers/cmsSuggest.js +57 -57
- package/server/routes/cms/controllers/deleteContent.js +113 -113
- package/server/routes/cms/controllers/deleteMedia.js +76 -76
- package/server/routes/cms/controllers/downloadMedia.js +84 -84
- package/server/routes/cms/controllers/getContent.js +113 -113
- package/server/routes/cms/controllers/getContentBySlug.js +93 -93
- package/server/routes/cms/controllers/insertContent.js +226 -226
- package/server/routes/cms/controllers/listMedia.js +155 -155
- package/server/routes/cms/controllers/metadataMedia.js +39 -39
- package/server/routes/cms/controllers/properties.get.js +18 -18
- package/server/routes/cms/controllers/properties.post.js +99 -99
- package/server/routes/cms/controllers/searchContent.js +214 -214
- package/server/routes/cms/controllers/translate.js +89 -89
- package/server/routes/cms/controllers/updateContent.js +231 -231
- package/server/routes/cms/controllers/uploadMedia.js +79 -79
- package/server/routes/cms/functions/getSettings.js +48 -48
- package/server/routes/cms/index.mjs +112 -112
- package/server/routes/cms/utils/additionalData.js +35 -35
- package/server/routes/cms/utils/getCollection.js +89 -89
- package/server/routes/cms/utils/getSingle.js +188 -188
- package/server/routes/cms/utils/inputTypes.js +5 -5
- package/server/routes/cms/utils/insertContentLocalization.js +104 -104
- package/server/routes/cms/utils/requestTranslation.js +135 -135
- package/server/routes/cms/utils/updateLocalization.js +42 -42
- package/server/routes/cmsSpace/controllers/deleteSpace.js +26 -26
- package/server/routes/cmsSpace/controllers/getSpaces.js +28 -28
- package/server/routes/cmsSpace/controllers/insertSpace.js +22 -22
- package/server/routes/cmsSpace/controllers/updateSpace.js +24 -24
- package/server/routes/cmsSpace/index.mjs +20 -20
- package/server/routes/contentType/controllers/addContentType.js +160 -160
- package/server/routes/contentType/controllers/contentTypeList.js +47 -47
- package/server/routes/contentType/controllers/delContentType.js +75 -75
- package/server/routes/contentType/controllers/editContentType.js +70 -70
- package/server/routes/contentType/controllers/getContentType.js +57 -57
- package/server/routes/contentType/index.mjs +35 -35
- package/server/routes/contentType/utils/updateContents.js +44 -44
- package/server/routes/contentType/utils/updateCustomContentTable.js +53 -53
- package/server/routes/feedback/controllers/email.list.js +24 -24
- package/server/routes/feedback/controllers/feedback.js +48 -48
- package/server/routes/feedback/controllers/feedback.list.js +37 -37
- package/server/routes/feedback/controllers/news.subscriptions.js +44 -44
- package/server/routes/feedback/index.mjs +71 -71
- package/server/routes/logs/controllers/export.user.logs.js +77 -77
- package/server/routes/logs/controllers/user.logs.js +44 -44
- package/server/routes/logs/index.mjs +9 -9
- package/server/routes/menu/controllers/addMenu.js +37 -37
- package/server/routes/menu/controllers/delMenu.js +31 -31
- package/server/routes/menu/controllers/editMenu.js +41 -41
- package/server/routes/menu/controllers/getMenu.js +24 -24
- package/server/routes/menu/functions/getMenu.js +50 -50
- package/server/routes/menu/index.mjs +13 -13
- package/server/routes/migration/controllers/collectionToCustom.js +137 -137
- package/server/routes/migration/index.mjs +8 -8
- package/server/routes/root.mjs +8 -8
- package/server/routes/tags/controllers/add.tags.js +24 -24
- package/server/routes/tags/controllers/del.tags.js +19 -19
- package/server/routes/tags/controllers/edit.tags.js +25 -25
- package/server/routes/tags/controllers/get.tags.js +15 -15
- package/server/routes/tags/index.mjs +14 -14
- package/server/templates/cls/cms.category_type.json +9 -9
- package/server/templates/cls/cms.content_review_status.json +9 -9
- package/server/templates/cls/cms.content_status.json +9 -9
- package/server/templates/cls/cms.content_type.json +9 -9
- package/server/templates/cls/cms.lang.json +9 -9
- package/server/templates/page/login.html +126 -126
- package/utils.d.ts +52 -52
- package/utils.js +8 -8
- package/dist/getField-CpwVE28P.js +0 -179
- package/dist/vs-form-custom-datatable-BDZo48w3.js +0 -317
|
@@ -1,227 +1,227 @@
|
|
|
1
|
-
import { config, checkSQL, getTemplate, pgClients, dataInsert, 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 insertContent(req, reply) {
|
|
26
|
-
const {
|
|
27
|
-
pg = pgClients.client,
|
|
28
|
-
params = {},
|
|
29
|
-
user = {},
|
|
30
|
-
body = {},
|
|
31
|
-
headers = {},
|
|
32
|
-
} = req;
|
|
33
|
-
|
|
34
|
-
const { type, id = body?.id } = params;
|
|
35
|
-
|
|
36
|
-
if (!type) {
|
|
37
|
-
return reply.status(400).send({ error: 'not enough params: type', code: 400 });
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const arr = config.pg ? await pg.query(`select array_agg(relname)::text[] from pg_class a
|
|
41
|
-
left join pg_namespace b on a.relnamespace=b.oid
|
|
42
|
-
where a.relam=2 and b.nspname='data'`).then(el => el.rows?.[0]?.array_agg || []) : [];
|
|
43
|
-
|
|
44
|
-
const { content_id: contentId, type: contentType } = await pg.query(
|
|
45
|
-
'select content_id, (select type from site.content_types where content_type_id=t.content_type_id) from site.contents t where slug=$1',
|
|
46
|
-
[['single', 'pages'].includes(type) ? id : type],
|
|
47
|
-
).then(el => el.rows?.[0] || {});
|
|
48
|
-
|
|
49
|
-
const ctypeId = contentId && contentId !== 'pages' && contentType === 'single' ? await pg.query(
|
|
50
|
-
'select content_type_id from site.contents where content_id=$1',
|
|
51
|
-
[contentId],
|
|
52
|
-
).then(el => el.rows?.[0]?.content_type_id) : null;
|
|
53
|
-
|
|
54
|
-
if (!arr.length && (ctypeId || type) !== 'pages') {
|
|
55
|
-
return reply.status(400).send({ error: 'empty schema: data', code: 400 });
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const table = arr.find(el => el === params.type);
|
|
59
|
-
|
|
60
|
-
const { ctid, dbtable, columns: contentColumns } = await pg.query(
|
|
61
|
-
'select content_type_id as ctid, table_name as dbtable, columns from site.content_types where $1 in (content_type_id, name)',
|
|
62
|
-
[type === 'single' && id ? id : (ctypeId || type)],
|
|
63
|
-
).then(el => el.rows?.[0] || {});
|
|
64
|
-
|
|
65
|
-
const loadTable = (ctypeId || type) === 'pages' ? await getTemplate('table', 'single.default.table') : {};
|
|
66
|
-
|
|
67
|
-
const columns = (ctypeId || type) === 'pages'
|
|
68
|
-
? (loadTable?.columns || []).concat((contentColumns || []).filter(col => loadTable?.columns.findIndex(el => el.name === col.name) === -1))
|
|
69
|
-
: contentColumns;
|
|
70
|
-
|
|
71
|
-
// site.content_data, includes singletone
|
|
72
|
-
if (((!table && !dbtable) || (ctypeId || type) === 'pages')) {
|
|
73
|
-
const cid = await pg.query(
|
|
74
|
-
'select content_id from site.contents where content_type_id=$1 limit 1',
|
|
75
|
-
[ctid || 'pages'],
|
|
76
|
-
).then(el => el.rows?.[0]?.content_id);
|
|
77
|
-
|
|
78
|
-
const ctid1 = body.content_type_id || ctid || 'pages';
|
|
79
|
-
|
|
80
|
-
if (!cid) {
|
|
81
|
-
return reply.status(404).send({ error: 'contents not found', code: 404 });
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const columnList = columns?.map?.(el => el.name) || [];
|
|
85
|
-
const types = columns?.reduce?.((acc, curr) => ({ ...acc, [curr.name]: curr.type || 'text' }), {}) || {};
|
|
86
|
-
const keys = Object.keys(body || {}).filter(key => columnList.includes(key) && !defaultColumns.includes(key));
|
|
87
|
-
|
|
88
|
-
if (!Object.keys(body || {}).length) {
|
|
89
|
-
return reply.status(400).send('invalid payload');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// const xss = checkXSS({ body });
|
|
93
|
-
const sql = checkSQL({ body });
|
|
94
|
-
|
|
95
|
-
if (/*xss.error ||*/ sql.error && false) {
|
|
96
|
-
logger.file(`injection/${/*xss.error ? 'xss' : */'sql'}/cms`, {
|
|
97
|
-
table,
|
|
98
|
-
...params,
|
|
99
|
-
uid: user?.uid,
|
|
100
|
-
...(/*xss.error ? xss : */sql),
|
|
101
|
-
});
|
|
102
|
-
return reply
|
|
103
|
-
.status(409)
|
|
104
|
-
.send(
|
|
105
|
-
`Дані містять заборонені ${/*xss.error ? 'xss' : */'sql'} символи. Приберіть їх та спробуйте ще раз`
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const id1 = id || await pg.query('select next_id()').then(el => el.rows[0].next_id);
|
|
110
|
-
|
|
111
|
-
const client = await pg.connect();
|
|
112
|
-
|
|
113
|
-
try {
|
|
114
|
-
await client.query('begin');
|
|
115
|
-
await dataInsert({
|
|
116
|
-
pg: client,
|
|
117
|
-
table: 'site.content_types',
|
|
118
|
-
id: id1,
|
|
119
|
-
data: { ...body, type: 'single', name: body.slug },
|
|
120
|
-
uid: user?.uid,
|
|
121
|
-
});
|
|
122
|
-
const res = await dataInsert({
|
|
123
|
-
pg: client,
|
|
124
|
-
table: 'site.contents',
|
|
125
|
-
id: id1,
|
|
126
|
-
data: { ...body, content_type_id: id1 },
|
|
127
|
-
uid: user?.uid,
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
if (!res?.content_id) {
|
|
131
|
-
throw new Error('insert contents error');
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
await Promise.all(keys.map(async key => dataInsert({
|
|
135
|
-
pg: client,
|
|
136
|
-
table: 'site.content_data',
|
|
137
|
-
data: {
|
|
138
|
-
field_key: key,
|
|
139
|
-
content_id: res.content_id,
|
|
140
|
-
object_id: res.content_id,
|
|
141
|
-
field_type: types[key] || 'text',
|
|
142
|
-
field_value: inputTypes[types[key] || ''] === 'json' ? undefined : body[key],
|
|
143
|
-
field_value_object: inputTypes[types[key] || ''] === 'json' ? body[key] : undefined,
|
|
144
|
-
},
|
|
145
|
-
uid: user?.uid,
|
|
146
|
-
})));
|
|
147
|
-
|
|
148
|
-
await updateLocalization(client, res.content_id, body, ctid1, user?.uid);
|
|
149
|
-
|
|
150
|
-
if (body?.tag_list?.length) {
|
|
151
|
-
await Promise.all(body.tag_list.map(async tag => dataInsert({
|
|
152
|
-
pg: client,
|
|
153
|
-
table: 'site.tag_data',
|
|
154
|
-
data: {
|
|
155
|
-
tag_id: tag,
|
|
156
|
-
data_id: id,
|
|
157
|
-
},
|
|
158
|
-
uid: user?.uid,
|
|
159
|
-
})));
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
await client.query('commit');
|
|
163
|
-
|
|
164
|
-
return {
|
|
165
|
-
id: res.content_id, rows: [res].filter(Boolean)
|
|
166
|
-
};
|
|
167
|
-
} catch (err) {
|
|
168
|
-
await client.query('rollback');
|
|
169
|
-
return reply.status(500).send({ error: err.toString(), code: 500 });
|
|
170
|
-
} finally {
|
|
171
|
-
client.release();
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// custom table
|
|
176
|
-
if (!table && !dbtable) {
|
|
177
|
-
return reply.status(400).send({ error: 'invalid params: type', code: 400 });
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const client = await pg.connect();
|
|
181
|
-
|
|
182
|
-
try {
|
|
183
|
-
await client.query('begin');
|
|
184
|
-
|
|
185
|
-
// const types = columns?.reduce?.((acc, curr) => ({ ...acc, [curr.name]: inputTypes[curr.type] || 'text' }), {}) || {};
|
|
186
|
-
const row = await dataInsert({
|
|
187
|
-
pg: client,
|
|
188
|
-
id,
|
|
189
|
-
table: 'data.' + `"${(table || dbtable)}"`,
|
|
190
|
-
data: body,
|
|
191
|
-
referer: headers?.referer,
|
|
192
|
-
uid: user?.uid,
|
|
193
|
-
}).catch(err => {
|
|
194
|
-
if (err.message?.includes?.('unique constraint')) {
|
|
195
|
-
throw new Error('Порушенні унікальності: ' + err.message?.match?.(/([^']+)/g)?.[1] || err.message.split('unique constraint')[1]);
|
|
196
|
-
}
|
|
197
|
-
throw err;
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
if (!row?.id) {
|
|
201
|
-
throw new Error('content insert error');
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
await updateLocalization(client, row.id, body, ctid, user?.uid);
|
|
205
|
-
|
|
206
|
-
if (body?.tag_list?.length) {
|
|
207
|
-
await Promise.all(body.tag_list.map(async tag => dataInsert({
|
|
208
|
-
pg: client,
|
|
209
|
-
table: 'site.tag_data',
|
|
210
|
-
data: {
|
|
211
|
-
tag_id: tag,
|
|
212
|
-
data_id: id || row.id,
|
|
213
|
-
},
|
|
214
|
-
uid: user?.uid,
|
|
215
|
-
})));
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
await client.query('commit');
|
|
219
|
-
|
|
220
|
-
return reply.status(200).send({ id: row.id, rows: [row] });
|
|
221
|
-
} catch (err) {
|
|
222
|
-
await client.query('rollback');
|
|
223
|
-
return reply.status(500).send({ error: err.toString(), code: 500 });
|
|
224
|
-
} finally {
|
|
225
|
-
client.release();
|
|
226
|
-
}
|
|
1
|
+
import { config, checkSQL, getTemplate, pgClients, dataInsert, 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 insertContent(req, reply) {
|
|
26
|
+
const {
|
|
27
|
+
pg = pgClients.client,
|
|
28
|
+
params = {},
|
|
29
|
+
user = {},
|
|
30
|
+
body = {},
|
|
31
|
+
headers = {},
|
|
32
|
+
} = req;
|
|
33
|
+
|
|
34
|
+
const { type, id = body?.id } = params;
|
|
35
|
+
|
|
36
|
+
if (!type) {
|
|
37
|
+
return reply.status(400).send({ error: 'not enough params: type', code: 400 });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const arr = config.pg ? await pg.query(`select array_agg(relname)::text[] from pg_class a
|
|
41
|
+
left join pg_namespace b on a.relnamespace=b.oid
|
|
42
|
+
where a.relam=2 and b.nspname='data'`).then(el => el.rows?.[0]?.array_agg || []) : [];
|
|
43
|
+
|
|
44
|
+
const { content_id: contentId, type: contentType } = await pg.query(
|
|
45
|
+
'select content_id, (select type from site.content_types where content_type_id=t.content_type_id) from site.contents t where slug=$1',
|
|
46
|
+
[['single', 'pages'].includes(type) ? id : type],
|
|
47
|
+
).then(el => el.rows?.[0] || {});
|
|
48
|
+
|
|
49
|
+
const ctypeId = contentId && contentId !== 'pages' && contentType === 'single' ? await pg.query(
|
|
50
|
+
'select content_type_id from site.contents where content_id=$1',
|
|
51
|
+
[contentId],
|
|
52
|
+
).then(el => el.rows?.[0]?.content_type_id) : null;
|
|
53
|
+
|
|
54
|
+
if (!arr.length && (ctypeId || type) !== 'pages') {
|
|
55
|
+
return reply.status(400).send({ error: 'empty schema: data', code: 400 });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const table = arr.find(el => el === params.type);
|
|
59
|
+
|
|
60
|
+
const { ctid, dbtable, columns: contentColumns } = await pg.query(
|
|
61
|
+
'select content_type_id as ctid, table_name as dbtable, columns from site.content_types where $1 in (content_type_id, name)',
|
|
62
|
+
[type === 'single' && id ? id : (ctypeId || type)],
|
|
63
|
+
).then(el => el.rows?.[0] || {});
|
|
64
|
+
|
|
65
|
+
const loadTable = (ctypeId || type) === 'pages' ? await getTemplate('table', 'single.default.table') : {};
|
|
66
|
+
|
|
67
|
+
const columns = (ctypeId || type) === 'pages'
|
|
68
|
+
? (loadTable?.columns || []).concat((contentColumns || []).filter(col => loadTable?.columns.findIndex(el => el.name === col.name) === -1))
|
|
69
|
+
: contentColumns;
|
|
70
|
+
|
|
71
|
+
// site.content_data, includes singletone
|
|
72
|
+
if (((!table && !dbtable) || (ctypeId || type) === 'pages')) {
|
|
73
|
+
const cid = await pg.query(
|
|
74
|
+
'select content_id from site.contents where content_type_id=$1 limit 1',
|
|
75
|
+
[ctid || 'pages'],
|
|
76
|
+
).then(el => el.rows?.[0]?.content_id);
|
|
77
|
+
|
|
78
|
+
const ctid1 = body.content_type_id || ctid || 'pages';
|
|
79
|
+
|
|
80
|
+
if (!cid) {
|
|
81
|
+
return reply.status(404).send({ error: 'contents not found', code: 404 });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const columnList = columns?.map?.(el => el.name) || [];
|
|
85
|
+
const types = columns?.reduce?.((acc, curr) => ({ ...acc, [curr.name]: curr.type || 'text' }), {}) || {};
|
|
86
|
+
const keys = Object.keys(body || {}).filter(key => columnList.includes(key) && !defaultColumns.includes(key));
|
|
87
|
+
|
|
88
|
+
if (!Object.keys(body || {}).length) {
|
|
89
|
+
return reply.status(400).send('invalid payload');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// const xss = checkXSS({ body });
|
|
93
|
+
const sql = checkSQL({ body });
|
|
94
|
+
|
|
95
|
+
if (/*xss.error ||*/ sql.error && false) {
|
|
96
|
+
logger.file(`injection/${/*xss.error ? 'xss' : */'sql'}/cms`, {
|
|
97
|
+
table,
|
|
98
|
+
...params,
|
|
99
|
+
uid: user?.uid,
|
|
100
|
+
...(/*xss.error ? xss : */sql),
|
|
101
|
+
});
|
|
102
|
+
return reply
|
|
103
|
+
.status(409)
|
|
104
|
+
.send(
|
|
105
|
+
`Дані містять заборонені ${/*xss.error ? 'xss' : */'sql'} символи. Приберіть їх та спробуйте ще раз`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const id1 = id || await pg.query('select next_id()').then(el => el.rows[0].next_id);
|
|
110
|
+
|
|
111
|
+
const client = await pg.connect();
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
await client.query('begin');
|
|
115
|
+
await dataInsert({
|
|
116
|
+
pg: client,
|
|
117
|
+
table: 'site.content_types',
|
|
118
|
+
id: id1,
|
|
119
|
+
data: { ...body, type: 'single', name: body.slug },
|
|
120
|
+
uid: user?.uid,
|
|
121
|
+
});
|
|
122
|
+
const res = await dataInsert({
|
|
123
|
+
pg: client,
|
|
124
|
+
table: 'site.contents',
|
|
125
|
+
id: id1,
|
|
126
|
+
data: { ...body, content_type_id: id1 },
|
|
127
|
+
uid: user?.uid,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
if (!res?.content_id) {
|
|
131
|
+
throw new Error('insert contents error');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
await Promise.all(keys.map(async key => dataInsert({
|
|
135
|
+
pg: client,
|
|
136
|
+
table: 'site.content_data',
|
|
137
|
+
data: {
|
|
138
|
+
field_key: key,
|
|
139
|
+
content_id: res.content_id,
|
|
140
|
+
object_id: res.content_id,
|
|
141
|
+
field_type: types[key] || 'text',
|
|
142
|
+
field_value: inputTypes[types[key] || ''] === 'json' ? undefined : body[key],
|
|
143
|
+
field_value_object: inputTypes[types[key] || ''] === 'json' ? body[key] : undefined,
|
|
144
|
+
},
|
|
145
|
+
uid: user?.uid,
|
|
146
|
+
})));
|
|
147
|
+
|
|
148
|
+
await updateLocalization(client, res.content_id, body, ctid1, user?.uid);
|
|
149
|
+
|
|
150
|
+
if (body?.tag_list?.length) {
|
|
151
|
+
await Promise.all(body.tag_list.map(async tag => dataInsert({
|
|
152
|
+
pg: client,
|
|
153
|
+
table: 'site.tag_data',
|
|
154
|
+
data: {
|
|
155
|
+
tag_id: tag,
|
|
156
|
+
data_id: id,
|
|
157
|
+
},
|
|
158
|
+
uid: user?.uid,
|
|
159
|
+
})));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
await client.query('commit');
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
id: res.content_id, rows: [res].filter(Boolean)
|
|
166
|
+
};
|
|
167
|
+
} catch (err) {
|
|
168
|
+
await client.query('rollback');
|
|
169
|
+
return reply.status(500).send({ error: err.toString(), code: 500 });
|
|
170
|
+
} finally {
|
|
171
|
+
client.release();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// custom table
|
|
176
|
+
if (!table && !dbtable) {
|
|
177
|
+
return reply.status(400).send({ error: 'invalid params: type', code: 400 });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const client = await pg.connect();
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
await client.query('begin');
|
|
184
|
+
|
|
185
|
+
// const types = columns?.reduce?.((acc, curr) => ({ ...acc, [curr.name]: inputTypes[curr.type] || 'text' }), {}) || {};
|
|
186
|
+
const row = await dataInsert({
|
|
187
|
+
pg: client,
|
|
188
|
+
id,
|
|
189
|
+
table: 'data.' + `"${(table || dbtable)}"`,
|
|
190
|
+
data: body,
|
|
191
|
+
referer: headers?.referer,
|
|
192
|
+
uid: user?.uid,
|
|
193
|
+
}).catch(err => {
|
|
194
|
+
if (err.message?.includes?.('unique constraint')) {
|
|
195
|
+
throw new Error('Порушенні унікальності: ' + err.message?.match?.(/([^']+)/g)?.[1] || err.message.split('unique constraint')[1]);
|
|
196
|
+
}
|
|
197
|
+
throw err;
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
if (!row?.id) {
|
|
201
|
+
throw new Error('content insert error');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
await updateLocalization(client, row.id, body, ctid, user?.uid);
|
|
205
|
+
|
|
206
|
+
if (body?.tag_list?.length) {
|
|
207
|
+
await Promise.all(body.tag_list.map(async tag => dataInsert({
|
|
208
|
+
pg: client,
|
|
209
|
+
table: 'site.tag_data',
|
|
210
|
+
data: {
|
|
211
|
+
tag_id: tag,
|
|
212
|
+
data_id: id || row.id,
|
|
213
|
+
},
|
|
214
|
+
uid: user?.uid,
|
|
215
|
+
})));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
await client.query('commit');
|
|
219
|
+
|
|
220
|
+
return reply.status(200).send({ id: row.id, rows: [row] });
|
|
221
|
+
} catch (err) {
|
|
222
|
+
await client.query('rollback');
|
|
223
|
+
return reply.status(500).send({ error: err.toString(), code: 500 });
|
|
224
|
+
} finally {
|
|
225
|
+
client.release();
|
|
226
|
+
}
|
|
227
227
|
}
|