@opengis/cms 0.0.58 → 0.0.60

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 (129) hide show
  1. package/README.md +131 -131
  2. package/dist/{ArticlesPage-CFjE_cw_.js → ArticlesPage-BjYzvTWM.js} +3 -3
  3. package/dist/{CollectionsBreadcrumb-BCxeRikP.js → CollectionsBreadcrumb-HePNJb-d.js} +1 -1
  4. package/dist/CollectionsBreadcrumb.vue_vue_type_script_setup_true_lang-BJh-tjam.js +53 -0
  5. package/dist/{Dashboard-C1eGscNd.js → Dashboard-CXkg_pk8.js} +132 -132
  6. package/dist/EditCollectionPage-BycuD920.js +188 -0
  7. package/dist/{MenuAddPage-D-p3gFgm.js → MenuAddPage-QTnwCoGh.js} +1 -1
  8. package/dist/{MenuBody-rN5j4YBu.js → MenuBody-Bi0ONVZf.js} +2 -2
  9. package/dist/{MenuItemPage-BoJw885D.js → MenuItemPage-B7Y9KFyb.js} +3 -3
  10. package/dist/{MenuList-DFEBS0NB.js → MenuList-BLIpeqSd.js} +53 -53
  11. package/dist/{MenuPage-BCZB_S8j.js → MenuPage-3W6jZ15H.js} +1 -1
  12. package/dist/{MenuWrapper-AZ_8s-zd.js → MenuWrapper-OrOv6sOb.js} +1 -1
  13. package/dist/{MonacoEditor-Db-3Jc3E.js → MonacoEditor-ByPT8pnv.js} +1 -1
  14. package/dist/MonacoEditor.vue_vue_type_script_setup_true_lang-C8cip9Ci.js +84 -0
  15. package/dist/{UniversalTable-CzqPG-tY.js → UniversalTable-GBd_pStq.js} +80 -80
  16. package/dist/{UniversalTablePagination-4gL47A7I.js → UniversalTablePagination-Dw2hc0nc.js} +46 -46
  17. package/dist/{contentForm-CtMhQTG0.js → contentForm-yMn63kza.js} +48 -48
  18. package/dist/index.js +5 -5
  19. package/dist/{vs-builder-monaco-B3Jj0V31.js → vs-builder-monaco-Cw-f19gc.js} +1 -1
  20. package/input-types.json +9 -9
  21. package/locales/en.json +815 -815
  22. package/locales/uk.json +813 -813
  23. package/module/cms/cls/content.status.json +17 -17
  24. package/module/cms/cls/user_type.json +9 -9
  25. package/module/cms/form/admin.users.form.json +77 -77
  26. package/module/cms/select/cms.page_type.sql +1 -1
  27. package/module/cms/select/news_tag_id.sql +11 -11
  28. package/module/cms/table/admin.users.table.json +53 -53
  29. package/module/cms/table/collection.default.table.json +96 -96
  30. package/module/cms/table/single.default.table.json +116 -116
  31. package/package.json +2 -1
  32. package/plugin.js +43 -43
  33. package/server/app.js +35 -35
  34. package/server/config.js +4 -4
  35. package/server/functions/getContent.js +45 -45
  36. package/server/functions/getDraftKey.js +22 -22
  37. package/server/functions/getSearchData.js +31 -31
  38. package/server/functions/getTags.js +30 -30
  39. package/server/functions/getUser.js +27 -27
  40. package/server/functions/utils/mock.reply.js +55 -55
  41. package/server/index.js +22 -22
  42. package/server/migrations/fixes.sql +129 -129
  43. package/server/migrations/site.sql +595 -595
  44. package/server/plugins/adminHook.js +78 -78
  45. package/server/plugins/hook.js +59 -59
  46. package/server/plugins/vite.js +75 -75
  47. package/server/routes/category/controllers/cms.category.delete.js +21 -21
  48. package/server/routes/category/controllers/cms.category.get.js +17 -17
  49. package/server/routes/category/controllers/cms.category.list.js +16 -16
  50. package/server/routes/category/controllers/cms.category.post.js +21 -21
  51. package/server/routes/category/controllers/cms.category.put.js +23 -23
  52. package/server/routes/category/index.mjs +22 -22
  53. package/server/routes/cms/controllers/cmsStat.js +55 -55
  54. package/server/routes/cms/controllers/cmsSuggest.js +57 -57
  55. package/server/routes/cms/controllers/deleteContent.js +113 -113
  56. package/server/routes/cms/controllers/deleteMedia.js +76 -76
  57. package/server/routes/cms/controllers/downloadMedia.js +84 -84
  58. package/server/routes/cms/controllers/getContent.js +113 -113
  59. package/server/routes/cms/controllers/getContentBySlug.js +93 -93
  60. package/server/routes/cms/controllers/getPermissions.js +15 -15
  61. package/server/routes/cms/controllers/insertContent.js +217 -217
  62. package/server/routes/cms/controllers/listMedia.js +155 -155
  63. package/server/routes/cms/controllers/metadataMedia.js +39 -39
  64. package/server/routes/cms/controllers/properties.get.js +18 -18
  65. package/server/routes/cms/controllers/properties.post.js +99 -99
  66. package/server/routes/cms/controllers/searchContent.js +214 -214
  67. package/server/routes/cms/controllers/setPermissions.js +49 -49
  68. package/server/routes/cms/controllers/translate.js +89 -89
  69. package/server/routes/cms/controllers/updateContent.js +266 -266
  70. package/server/routes/cms/controllers/uploadMedia.js +79 -79
  71. package/server/routes/cms/functions/getSettings.js +48 -48
  72. package/server/routes/cms/index.mjs +112 -112
  73. package/server/routes/cms/utils/additionalData.js +35 -35
  74. package/server/routes/cms/utils/getCollection.js +89 -89
  75. package/server/routes/cms/utils/getSingle.js +188 -188
  76. package/server/routes/cms/utils/inputTypes.js +5 -5
  77. package/server/routes/cms/utils/insertContentLocalization.js +104 -104
  78. package/server/routes/cms/utils/requestTranslation.js +113 -85
  79. package/server/routes/cms/utils/updateLocalization.js +47 -47
  80. package/server/routes/cmsSpace/controllers/deleteSpace.js +25 -25
  81. package/server/routes/cmsSpace/controllers/getSpaces.js +27 -27
  82. package/server/routes/cmsSpace/controllers/insertSpace.js +21 -21
  83. package/server/routes/cmsSpace/controllers/updateSpace.js +23 -23
  84. package/server/routes/cmsSpace/index.mjs +20 -20
  85. package/server/routes/contentType/controllers/addContentType.js +160 -160
  86. package/server/routes/contentType/controllers/contentTypeList.js +54 -54
  87. package/server/routes/contentType/controllers/delContentType.js +75 -75
  88. package/server/routes/contentType/controllers/editContentType.js +88 -88
  89. package/server/routes/contentType/controllers/getContentType.js +65 -65
  90. package/server/routes/contentType/index.mjs +35 -35
  91. package/server/routes/contentType/utils/updateContents.js +44 -44
  92. package/server/routes/contentType/utils/updateCustomContentTable.js +53 -53
  93. package/server/routes/feedback/controllers/email.list.js +24 -24
  94. package/server/routes/feedback/controllers/feedback.js +48 -48
  95. package/server/routes/feedback/controllers/feedback.list.js +37 -37
  96. package/server/routes/feedback/controllers/news.subscriptions.js +44 -44
  97. package/server/routes/feedback/index.mjs +71 -71
  98. package/server/routes/logs/controllers/export.user.logs.js +77 -77
  99. package/server/routes/logs/controllers/user.logs.js +44 -44
  100. package/server/routes/logs/index.mjs +9 -9
  101. package/server/routes/menu/controllers/addMenu.js +37 -37
  102. package/server/routes/menu/controllers/delMenu.js +31 -31
  103. package/server/routes/menu/controllers/editMenu.js +41 -41
  104. package/server/routes/menu/controllers/getMenu.js +24 -24
  105. package/server/routes/menu/functions/getMenu.js +50 -50
  106. package/server/routes/menu/index.mjs +13 -13
  107. package/server/routes/migration/controllers/collectionToCustom.js +137 -137
  108. package/server/routes/migration/index.mjs +8 -8
  109. package/server/routes/root.mjs +8 -8
  110. package/server/routes/tags/controllers/add.tags.js +24 -24
  111. package/server/routes/tags/controllers/del.tags.js +19 -19
  112. package/server/routes/tags/controllers/edit.tags.js +25 -25
  113. package/server/routes/tags/controllers/get.tags.js +15 -15
  114. package/server/routes/tags/index.mjs +14 -14
  115. package/server/templates/cls/cms.category_type.json +9 -9
  116. package/server/templates/cls/cms.content_review_status.json +9 -9
  117. package/server/templates/cls/cms.content_status.json +9 -9
  118. package/server/templates/cls/cms.content_type.json +9 -9
  119. package/server/templates/cls/cms.lang.json +9 -9
  120. package/server/templates/page/login.html +126 -126
  121. package/server/templates/select/core.user_mentioned.sql +1 -1
  122. package/utils.d.ts +52 -52
  123. package/utils.js +8 -8
  124. package/dist/CollectionsBreadcrumb.vue_vue_type_script_setup_true_lang-umRzB5mY.js +0 -53
  125. package/dist/EditCollectionPage-DIr1tdtn.js +0 -187
  126. package/dist/MonacoEditor.vue_vue_type_script_setup_true_lang-B1DrxmQX.js +0 -84
  127. package/dist/images/logo.png +0 -0
  128. package/dist/index.html +0 -29
  129. package/dist/vite.svg +0 -1
@@ -1,90 +1,90 @@
1
- import { pgClients, getData, getMeta } from "@opengis/fastify-table/utils.js";
2
-
3
- import additionalData from "./additionalData.js";
4
-
5
- export default async function getCollection({
6
- table, id, limit, maxLimit, order, search, tags, filter, state, page, desc, sql, locale, contextQuery, statusQuery, columns, preview, user, defaultColumns, fields, defaultFields = [], defaultFilters = [], called = false,
7
- }, reply, pg = pgClients.client) {
8
- const meta = table ? await getMeta({ pg, table: 'data.' + table }) : {};
9
-
10
- if (!table || !meta?.pk) {
11
- return { message: 'content table not found', status: 404 };
12
- }
13
-
14
- const isPinExists = await pg.query(`
15
- SELECT 1
16
- FROM pg_catalog.pg_attribute a
17
- JOIN pg_catalog.pg_class c ON a.attrelid = c.oid
18
- JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
19
- WHERE n.nspname = 'data'
20
- AND c.relname = $1
21
- AND a.attname = 'is_pin'
22
- AND a.attnum > 0
23
- AND NOT a.attisdropped;
24
- `, [table]).then(el => el.rowCount);
25
-
26
- const defaultOrder = `case when status='archived' then true else false end ${isPinExists ? ', is_pin desc' : ''}, published_at desc nulls last`;
27
-
28
- const localeQuery = locale && locale !== 'uk' ? `id in ( select object_id from site.localization where REVERSE(split_part(REVERSE(field_key), ':', 1)) = '${locale.replace(/'/g, "''")}' )` : undefined;
29
- const tagQuery = 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('{ ${tags.replace(/'/g, "''")} }'::text[]) or ts.tag_id = any('{ ${tags.replace(/'/g, "''")} }'::text[]))` : null;
30
- const cQuery = [id ? `'${id}' in (id,slug)` : contextQuery, localeQuery, ((columns || []).concat(defaultColumns || [])).find(col => col.name === 'status') ? statusQuery : null, tagQuery].filter(Boolean).join(' and ');
31
-
32
- const columnList = await getMeta({
33
- pg,
34
- table: 'data.' + `"${table}"`
35
- }).then(el => el.columns?.map?.(col => col.name) || []);
36
- const existingFields = fields && !id ? defaultFields.concat((fields || '').split(',')).filter(colName => columnList.includes(colName)).join(',') : null;
37
-
38
- const filterList = defaultFilters.concat(columns.map(col => ({ name: col.name, type: col.type && ['date', 'datetime'].includes(col.type) ? 'Date' : 'Text', label: col.label, sql: col.sql })));
39
-
40
- const res = await getData({
41
- pg,
42
- table: 'data.' + `"${table}"`,
43
- columns: existingFields,
44
- filterList,
45
- query: {
46
- filter,
47
- state,
48
- limit: Math.min(limit, maxLimit),
49
- page,
50
- search,
51
- order: order ? order : defaultOrder,
52
- desc,
53
- sql
54
- },
55
- user,
56
- contextQuery: cQuery,
57
- actions: ['view']
58
- }, reply, true);
59
-
60
- const locales = await pg.query(`select array_agg(distinct REVERSE(split_part(REVERSE(field_key), ':', 1))) from site.localization where object_id in (select id from ${'data.' + `"${table}"`})`).then(el => el.rows?.[0]?.array_agg || []);
61
-
62
- // Apply localization and tags etc.
63
- if (res?.rows?.length) {
64
- await additionalData(pg, res.rows, locale, id ? null : existingFields);
65
- }
66
-
67
- const columns1 = !id
68
- ? columns?.map(col => Object.assign(col, { type: col.name === 'published_at' ? 'date' : col.type }))?.filter?.(el => !el.hidden)
69
- : columns?.filter?.(el => !el.hidden);
70
-
71
- const finalColumns = (defaultColumns || []).concat(columns1.filter(col => defaultColumns.findIndex(el => el.name === col.name) === -1));
72
-
73
- finalColumns.filter(el => el.default).forEach(col => {
74
- const { name, localization } = columns1.find(item => item.name === col.name) || {};
75
- if (name) {
76
- Object.assign(col, { localization });
77
- }
78
- });
79
-
80
- if (res?.columns) {
81
- Object.assign(res, { type: 'collection', locales, preview_path: preview, columns: finalColumns });
82
- }
83
-
84
- // add table name for cache via getContent function
85
- if (called) {
86
- Object.assign(res, { table: 'data.' + `"${table}"` });
87
- }
88
-
89
- return res;
1
+ import { pgClients, getData, getMeta } from "@opengis/fastify-table/utils.js";
2
+
3
+ import additionalData from "./additionalData.js";
4
+
5
+ export default async function getCollection({
6
+ table, id, limit, maxLimit, order, search, tags, filter, state, page, desc, sql, locale, contextQuery, statusQuery, columns, preview, user, defaultColumns, fields, defaultFields = [], defaultFilters = [], called = false,
7
+ }, reply, pg = pgClients.client) {
8
+ const meta = table ? await getMeta({ pg, table: 'data.' + table }) : {};
9
+
10
+ if (!table || !meta?.pk) {
11
+ return { message: 'content table not found', status: 404 };
12
+ }
13
+
14
+ const isPinExists = await pg.query(`
15
+ SELECT 1
16
+ FROM pg_catalog.pg_attribute a
17
+ JOIN pg_catalog.pg_class c ON a.attrelid = c.oid
18
+ JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
19
+ WHERE n.nspname = 'data'
20
+ AND c.relname = $1
21
+ AND a.attname = 'is_pin'
22
+ AND a.attnum > 0
23
+ AND NOT a.attisdropped;
24
+ `, [table]).then(el => el.rowCount);
25
+
26
+ const defaultOrder = `case when status='archived' then true else false end ${isPinExists ? ', is_pin desc' : ''}, published_at desc nulls last`;
27
+
28
+ const localeQuery = locale && locale !== 'uk' ? `id in ( select object_id from site.localization where REVERSE(split_part(REVERSE(field_key), ':', 1)) = '${locale.replace(/'/g, "''")}' )` : undefined;
29
+ const tagQuery = 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('{ ${tags.replace(/'/g, "''")} }'::text[]) or ts.tag_id = any('{ ${tags.replace(/'/g, "''")} }'::text[]))` : null;
30
+ const cQuery = [id ? `'${id}' in (id,slug)` : contextQuery, localeQuery, ((columns || []).concat(defaultColumns || [])).find(col => col.name === 'status') ? statusQuery : null, tagQuery].filter(Boolean).join(' and ');
31
+
32
+ const columnList = await getMeta({
33
+ pg,
34
+ table: 'data.' + `"${table}"`
35
+ }).then(el => el.columns?.map?.(col => col.name) || []);
36
+ const existingFields = fields && !id ? defaultFields.concat((fields || '').split(',')).filter(colName => columnList.includes(colName)).join(',') : null;
37
+
38
+ const filterList = defaultFilters.concat(columns.map(col => ({ name: col.name, type: col.type && ['date', 'datetime'].includes(col.type) ? 'Date' : 'Text', label: col.label, sql: col.sql })));
39
+
40
+ const res = await getData({
41
+ pg,
42
+ table: 'data.' + `"${table}"`,
43
+ columns: existingFields,
44
+ filterList,
45
+ query: {
46
+ filter,
47
+ state,
48
+ limit: Math.min(limit, maxLimit),
49
+ page,
50
+ search,
51
+ order: order ? order : defaultOrder,
52
+ desc,
53
+ sql
54
+ },
55
+ user,
56
+ contextQuery: cQuery,
57
+ actions: ['view']
58
+ }, reply, true);
59
+
60
+ const locales = await pg.query(`select array_agg(distinct REVERSE(split_part(REVERSE(field_key), ':', 1))) from site.localization where object_id in (select id from ${'data.' + `"${table}"`})`).then(el => el.rows?.[0]?.array_agg || []);
61
+
62
+ // Apply localization and tags etc.
63
+ if (res?.rows?.length) {
64
+ await additionalData(pg, res.rows, locale, id ? null : existingFields);
65
+ }
66
+
67
+ const columns1 = !id
68
+ ? columns?.map(col => Object.assign(col, { type: col.name === 'published_at' ? 'date' : col.type }))?.filter?.(el => !el.hidden)
69
+ : columns?.filter?.(el => !el.hidden);
70
+
71
+ const finalColumns = (defaultColumns || []).concat(columns1.filter(col => defaultColumns.findIndex(el => el.name === col.name) === -1));
72
+
73
+ finalColumns.filter(el => el.default).forEach(col => {
74
+ const { name, localization } = columns1.find(item => item.name === col.name) || {};
75
+ if (name) {
76
+ Object.assign(col, { localization });
77
+ }
78
+ });
79
+
80
+ if (res?.columns) {
81
+ Object.assign(res, { type: 'collection', locales, preview_path: preview, columns: finalColumns });
82
+ }
83
+
84
+ // add table name for cache via getContent function
85
+ if (called) {
86
+ Object.assign(res, { table: 'data.' + `"${table}"` });
87
+ }
88
+
89
+ return res;
90
90
  }
@@ -1,189 +1,189 @@
1
- import path from 'node:path';
2
-
3
- import { pgClients, metaFormat, getFilterSQL, getFilter, getMeta } from '@opengis/fastify-table/utils.js';
4
-
5
- import inputTypes from '../utils/inputTypes.js';
6
- import additionalData from "./additionalData.js";
7
-
8
- export default async function getSingle({
9
- contentId: contentId1, contentTypeId, id: id1, limit: queryLimit, maxLimit, search, filter, locale, statusQuery, user, sql, page, defaultColumns = [], fields, defaultFields = [], defaultFilters = [],
10
- }, pg = pgClients.client) {
11
- const id = id1 || (contentId1 === 'pages' ? undefined : contentId1) || '';
12
- const contentId = id1 ? contentId1 : "pages";
13
-
14
- // new ? 1 : 2
15
- const customColumns = contentTypeId === (id || contentId)
16
- ? await pg.query(
17
- `SELECT columns FROM site.content_types WHERE content_type_id=$1`, [id || contentId]
18
- ).then(el => el.rows?.[0]?.columns || [])
19
- : await pg.query(`
20
- SELECT columns FROM site.content_types WHERE content_type_id in (select content_type_id from site.contents where content_id=$1) limit 1
21
- `, [id || contentId]).then(el => el.rows?.[0]?.columns || []);
22
-
23
- const contentQuery = contentId === 'pages' || !id
24
- ? `(type = 'single' or content_type_id = 'pages')`
25
- : 'content_type_id=(select content_type_id from site.contents where content_id=$2)';
26
-
27
- const columns = defaultColumns.concat(customColumns.filter(col => defaultColumns.findIndex(el => el.name === col.name) === -1));
28
-
29
- columns.filter(el => el.default).forEach(col => {
30
- const { name, localization } = customColumns.find(item => item.name === col.name) || {};
31
- if (name) {
32
- Object.assign(col, { localization });
33
- }
34
- });
35
-
36
- const limit = Math.min(maxLimit, +(queryLimit || 20));
37
- const offset = page && page > 0 && !id ? (page - 1) * limit : 0;
38
-
39
- const fData = filter
40
- ? await getFilterSQL({
41
- pg,
42
- table: 'site.contents',
43
- filter,
44
- search,
45
- filterList: defaultFilters,
46
- query: `
47
- content_type_id IN (
48
- SELECT content_type_id
49
- FROM site.content_types
50
- WHERE ${contentQuery || 'true'}
51
- )
52
- `,
53
- })
54
- : {
55
- optimizedSQL: 'SELECT * FROM site.contents WHERE true',
56
- };
57
-
58
- const localeQuery = locale && locale !== 'uk' && pg.pk?.['site.contents'] ? `${pg.pk?.['site.contents']} in ( select object_id from site.localization where REVERSE( split_part( REVERSE(field_key), ':', 1) ) = '${locale.replace(/'/g, "''")}' )` : undefined;
59
-
60
- const columnList = await getMeta({
61
- pg,
62
- table: 'site.contents',
63
- }).then(el => el.columns?.map?.(col => col.name) || []);
64
- const existingFields = fields && contentId === 'pages' || !id ? defaultFields.concat((fields || '').split(',')).filter(colName => columnList.includes(colName)).concat(['content_id AS id']).join(',') : '';
65
- const extraKeys = fields ? fields.split(',').filter(key => !defaultFields.includes(key)) : [];
66
- const cols = existingFields.length ? existingFields.concat(extraKeys?.length ? `,"extraKeys"` : []) : `content_id AS id,
67
- content_id,
68
- created_by,
69
- title,
70
- slug,
71
- meta,
72
- published_by,
73
- published_at,
74
- status,
75
- content_type_id,
76
- /* content_type_id AS type, */
77
- main_image
78
- ${contentId === 'pages' || !id ? `,"extraKeys"` : ''}`;
79
-
80
- const q = `
81
- SELECT ${cols} FROM (${fData.optimizedSQL}) a
82
- ${extraKeys || id ? `LEFT JOIN LATERAL (
83
- SELECT json_agg(row_to_json(q)) AS "extraKeys"
84
- FROM (
85
- SELECT field_key, field_type, field_value, field_value_object
86
- FROM site.content_data
87
- WHERE content_id = a.content_id
88
- ) q
89
- ) b ON true` : ''}
90
-
91
- LEFT JOIN LATERAL (
92
- SELECT type FROM site.content_types WHERE content_type_id=a.content_type_id LIMIT 1
93
- )c ON true
94
-
95
- WHERE $1=$1 and $2=$2
96
- and ${statusQuery || 'true'}
97
- and ${localeQuery || '1=1'}
98
- and ${contentQuery || 'true'}
99
- and ${id ? `$2 in (slug, content_id)` : 'true'}
100
- and content_id<>'pages'
101
- order by case when status='archived' then true else false end, published_at desc nulls last
102
- LIMIT ${limit} OFFSET ${id ? 0 : offset}
103
- `;
104
-
105
- if (sql && user?.user_type?.includes?.('admin')) {
106
- return q; // Expose raw SQL for debugging
107
- }
108
-
109
- const { total = 0, filtered = 0 } = await pg.query(`
110
- SELECT COUNT(*)::int as total,
111
- count(*) FILTER(WHERE ${fData.q || "true"})::int as filtered
112
- FROM site.contents
113
- WHERE content_type_id IN (
114
- SELECT content_type_id
115
- FROM site.content_types
116
- WHERE ${localeQuery || '1=1'}
117
- and $1=$1 and $2=$2 and ${contentQuery || 'true'}
118
- )
119
- and ${statusQuery || 'true'}
120
- and content_id<>'pages'
121
- `, [contentId, id]).then(el => el.rows?.[0] || {});
122
-
123
- const { rows = [] } = await pg.query(q, [contentId, id || '']);
124
-
125
- const locales = await pg.query(`select array_agg(distinct REVERSE(split_part(REVERSE(field_key), ':', 1))) from site.localization
126
- where object_id in (select content_id from site.contents
127
- where content_type_id IN (
128
- SELECT content_type_id FROM site.content_types where $1=$1 and $2=$2 and ${contentQuery || 'true'}
129
- )
130
- and ${statusQuery || 'true'}
131
- )`, [contentId, id]
132
- ).then(el => el.rows?.[0]?.array_agg || []);
133
-
134
- // Flatten extra fields into main object
135
- const rows1 = rows.map(row => ({
136
- ...row,
137
- ...Object.fromEntries(
138
- row.extraKeys?.filter(k => extraKeys?.length ? extraKeys.includes(k.field_key) : true)?.map(k =>
139
- [k.field_key, inputTypes[k.field_type] === 'json' || k.field_key === 'meta'
140
- ? k.field_value_object
141
- : k.field_value]
142
- ) || []
143
- ),
144
- extraKeys: undefined,
145
- }));
146
-
147
- // Apply localization and tags etc.
148
- await additionalData(pg, rows1, locale, contentId === 'pages' || !id ? [existingFields, extraKeys].filter(Boolean).join(',') : null);
149
-
150
- // Apply metadata formatting
151
- await metaFormat({
152
- rows: rows1,
153
- cls: {
154
- created_by: 'core.user_mentioned',
155
- // content_type_id: 'cms.type_id',
156
- // type: 'cms.type_id',
157
- },
158
- sufix: false,
159
- }, pg);
160
-
161
- const { list: filters = [] } = await getFilter({
162
- pg, table: 'single.default.table', user, filter: `content_type_id in (select content_type_id from site.content_types where type='single' or content_type_id='pages')`
163
- }) || {};
164
-
165
- const finalColumns = !id
166
- ? columns?.map?.(col => Object.assign(col, { type: col.name === 'published_at' ? 'date' : col.type }))?.filter?.(el => !el.hidden)
167
- : columns?.filter?.(el => !el.hidden);
168
-
169
- rows1.filter(row => row.single_sections?.length).forEach(row => {
170
- row.single_sections.filter(section => section.documents?.length).forEach(section => {
171
- section.documents.filter(doc => doc.file).forEach(doc => Object.assign(doc, { file_name: path.basename(doc.file) }));
172
- });
173
- });
174
-
175
- // temporary solution, before front makes changes to call by id, not type
176
- rows1.forEach(row => Object.assign(row, { content_type_id: !row.content_type_id || row.content_type_id === 'pages' ? row.content_id : row.content_type_id }));
177
-
178
- return {
179
- locales,
180
- type: 'single',
181
- total,
182
- filtered,
183
- count: rows1.length,
184
- pk: 'id',
185
- rows: rows1,
186
- columns: finalColumns,
187
- filters,
188
- };
1
+ import path from 'node:path';
2
+
3
+ import { pgClients, metaFormat, getFilterSQL, getFilter, getMeta } from '@opengis/fastify-table/utils.js';
4
+
5
+ import inputTypes from '../utils/inputTypes.js';
6
+ import additionalData from "./additionalData.js";
7
+
8
+ export default async function getSingle({
9
+ contentId: contentId1, contentTypeId, id: id1, limit: queryLimit, maxLimit, search, filter, locale, statusQuery, user, sql, page, defaultColumns = [], fields, defaultFields = [], defaultFilters = [],
10
+ }, pg = pgClients.client) {
11
+ const id = id1 || (contentId1 === 'pages' ? undefined : contentId1) || '';
12
+ const contentId = id1 ? contentId1 : "pages";
13
+
14
+ // new ? 1 : 2
15
+ const customColumns = contentTypeId === (id || contentId)
16
+ ? await pg.query(
17
+ `SELECT columns FROM site.content_types WHERE content_type_id=$1`, [id || contentId]
18
+ ).then(el => el.rows?.[0]?.columns || [])
19
+ : await pg.query(`
20
+ SELECT columns FROM site.content_types WHERE content_type_id in (select content_type_id from site.contents where content_id=$1) limit 1
21
+ `, [id || contentId]).then(el => el.rows?.[0]?.columns || []);
22
+
23
+ const contentQuery = contentId === 'pages' || !id
24
+ ? `(type = 'single' or content_type_id = 'pages')`
25
+ : 'content_type_id=(select content_type_id from site.contents where content_id=$2)';
26
+
27
+ const columns = defaultColumns.concat(customColumns.filter(col => defaultColumns.findIndex(el => el.name === col.name) === -1));
28
+
29
+ columns.filter(el => el.default).forEach(col => {
30
+ const { name, localization } = customColumns.find(item => item.name === col.name) || {};
31
+ if (name) {
32
+ Object.assign(col, { localization });
33
+ }
34
+ });
35
+
36
+ const limit = Math.min(maxLimit, +(queryLimit || 20));
37
+ const offset = page && page > 0 && !id ? (page - 1) * limit : 0;
38
+
39
+ const fData = filter
40
+ ? await getFilterSQL({
41
+ pg,
42
+ table: 'site.contents',
43
+ filter,
44
+ search,
45
+ filterList: defaultFilters,
46
+ query: `
47
+ content_type_id IN (
48
+ SELECT content_type_id
49
+ FROM site.content_types
50
+ WHERE ${contentQuery || 'true'}
51
+ )
52
+ `,
53
+ })
54
+ : {
55
+ optimizedSQL: 'SELECT * FROM site.contents WHERE true',
56
+ };
57
+
58
+ const localeQuery = locale && locale !== 'uk' && pg.pk?.['site.contents'] ? `${pg.pk?.['site.contents']} in ( select object_id from site.localization where REVERSE( split_part( REVERSE(field_key), ':', 1) ) = '${locale.replace(/'/g, "''")}' )` : undefined;
59
+
60
+ const columnList = await getMeta({
61
+ pg,
62
+ table: 'site.contents',
63
+ }).then(el => el.columns?.map?.(col => col.name) || []);
64
+ const existingFields = fields && contentId === 'pages' || !id ? defaultFields.concat((fields || '').split(',')).filter(colName => columnList.includes(colName)).concat(['content_id AS id']).join(',') : '';
65
+ const extraKeys = fields ? fields.split(',').filter(key => !defaultFields.includes(key)) : [];
66
+ const cols = existingFields.length ? existingFields.concat(extraKeys?.length ? `,"extraKeys"` : []) : `content_id AS id,
67
+ content_id,
68
+ created_by,
69
+ title,
70
+ slug,
71
+ meta,
72
+ published_by,
73
+ published_at,
74
+ status,
75
+ content_type_id,
76
+ /* content_type_id AS type, */
77
+ main_image
78
+ ${contentId === 'pages' || !id ? `,"extraKeys"` : ''}`;
79
+
80
+ const q = `
81
+ SELECT ${cols} FROM (${fData.optimizedSQL}) a
82
+ ${extraKeys || id ? `LEFT JOIN LATERAL (
83
+ SELECT json_agg(row_to_json(q)) AS "extraKeys"
84
+ FROM (
85
+ SELECT field_key, field_type, field_value, field_value_object
86
+ FROM site.content_data
87
+ WHERE content_id = a.content_id
88
+ ) q
89
+ ) b ON true` : ''}
90
+
91
+ LEFT JOIN LATERAL (
92
+ SELECT type FROM site.content_types WHERE content_type_id=a.content_type_id LIMIT 1
93
+ )c ON true
94
+
95
+ WHERE $1=$1 and $2=$2
96
+ and ${statusQuery || 'true'}
97
+ and ${localeQuery || '1=1'}
98
+ and ${contentQuery || 'true'}
99
+ and ${id ? `$2 in (slug, content_id)` : 'true'}
100
+ and content_id<>'pages'
101
+ order by case when status='archived' then true else false end, published_at desc nulls last
102
+ LIMIT ${limit} OFFSET ${id ? 0 : offset}
103
+ `;
104
+
105
+ if (sql && user?.user_type?.includes?.('admin')) {
106
+ return q; // Expose raw SQL for debugging
107
+ }
108
+
109
+ const { total = 0, filtered = 0 } = await pg.query(`
110
+ SELECT COUNT(*)::int as total,
111
+ count(*) FILTER(WHERE ${fData.q || "true"})::int as filtered
112
+ FROM site.contents
113
+ WHERE content_type_id IN (
114
+ SELECT content_type_id
115
+ FROM site.content_types
116
+ WHERE ${localeQuery || '1=1'}
117
+ and $1=$1 and $2=$2 and ${contentQuery || 'true'}
118
+ )
119
+ and ${statusQuery || 'true'}
120
+ and content_id<>'pages'
121
+ `, [contentId, id]).then(el => el.rows?.[0] || {});
122
+
123
+ const { rows = [] } = await pg.query(q, [contentId, id || '']);
124
+
125
+ const locales = await pg.query(`select array_agg(distinct REVERSE(split_part(REVERSE(field_key), ':', 1))) from site.localization
126
+ where object_id in (select content_id from site.contents
127
+ where content_type_id IN (
128
+ SELECT content_type_id FROM site.content_types where $1=$1 and $2=$2 and ${contentQuery || 'true'}
129
+ )
130
+ and ${statusQuery || 'true'}
131
+ )`, [contentId, id]
132
+ ).then(el => el.rows?.[0]?.array_agg || []);
133
+
134
+ // Flatten extra fields into main object
135
+ const rows1 = rows.map(row => ({
136
+ ...row,
137
+ ...Object.fromEntries(
138
+ row.extraKeys?.filter(k => extraKeys?.length ? extraKeys.includes(k.field_key) : true)?.map(k =>
139
+ [k.field_key, inputTypes[k.field_type] === 'json' || k.field_key === 'meta'
140
+ ? k.field_value_object
141
+ : k.field_value]
142
+ ) || []
143
+ ),
144
+ extraKeys: undefined,
145
+ }));
146
+
147
+ // Apply localization and tags etc.
148
+ await additionalData(pg, rows1, locale, contentId === 'pages' || !id ? [existingFields, extraKeys].filter(Boolean).join(',') : null);
149
+
150
+ // Apply metadata formatting
151
+ await metaFormat({
152
+ rows: rows1,
153
+ cls: {
154
+ created_by: 'core.user_mentioned',
155
+ // content_type_id: 'cms.type_id',
156
+ // type: 'cms.type_id',
157
+ },
158
+ sufix: false,
159
+ }, pg);
160
+
161
+ const { list: filters = [] } = await getFilter({
162
+ pg, table: 'single.default.table', user, filter: `content_type_id in (select content_type_id from site.content_types where type='single' or content_type_id='pages')`
163
+ }) || {};
164
+
165
+ const finalColumns = !id
166
+ ? columns?.map?.(col => Object.assign(col, { type: col.name === 'published_at' ? 'date' : col.type }))?.filter?.(el => !el.hidden)
167
+ : columns?.filter?.(el => !el.hidden);
168
+
169
+ rows1.filter(row => row.single_sections?.length).forEach(row => {
170
+ row.single_sections.filter(section => section.documents?.length).forEach(section => {
171
+ section.documents.filter(doc => doc.file).forEach(doc => Object.assign(doc, { file_name: path.basename(doc.file) }));
172
+ });
173
+ });
174
+
175
+ // temporary solution, before front makes changes to call by id, not type
176
+ rows1.forEach(row => Object.assign(row, { content_type_id: !row.content_type_id || row.content_type_id === 'pages' ? row.content_id : row.content_type_id }));
177
+
178
+ return {
179
+ locales,
180
+ type: 'single',
181
+ total,
182
+ filtered,
183
+ count: rows1.length,
184
+ pk: 'id',
185
+ rows: rows1,
186
+ columns: finalColumns,
187
+ filters,
188
+ };
189
189
  }
@@ -1,6 +1,6 @@
1
- import path from 'node:path';
2
- import { existsSync, readFileSync } from 'node:fs';
3
-
4
- const inputTypes = ['input-types.json', path.join('node_modules/@opengis/cms', 'input-types.json')].filter(filepath => existsSync(filepath)).reduce((acc, curr) => ({ ...acc, ...JSON.parse(readFileSync(curr) || '{}') }), {});
5
-
1
+ import path from 'node:path';
2
+ import { existsSync, readFileSync } from 'node:fs';
3
+
4
+ const inputTypes = ['input-types.json', path.join('node_modules/@opengis/cms', 'input-types.json')].filter(filepath => existsSync(filepath)).reduce((acc, curr) => ({ ...acc, ...JSON.parse(readFileSync(curr) || '{}') }), {});
5
+
6
6
  export default inputTypes