@opengis/cms 0.0.58 → 0.0.59

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 (126) 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-DIr1tdtn.js → EditCollectionPage-CqYHpEON.js} +2 -2
  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-Buku-lel.js} +1 -1
  18. package/dist/index.html +29 -29
  19. package/dist/index.js +5 -5
  20. package/dist/{vs-builder-monaco-B3Jj0V31.js → vs-builder-monaco-Cw-f19gc.js} +1 -1
  21. package/input-types.json +9 -9
  22. package/locales/en.json +815 -815
  23. package/locales/uk.json +813 -813
  24. package/module/cms/cls/content.status.json +17 -17
  25. package/module/cms/cls/user_type.json +9 -9
  26. package/module/cms/form/admin.users.form.json +77 -77
  27. package/module/cms/select/cms.page_type.sql +1 -1
  28. package/module/cms/select/news_tag_id.sql +11 -11
  29. package/module/cms/table/admin.users.table.json +53 -53
  30. package/module/cms/table/collection.default.table.json +96 -96
  31. package/module/cms/table/single.default.table.json +116 -116
  32. package/package.json +2 -1
  33. package/plugin.js +43 -43
  34. package/server/app.js +35 -35
  35. package/server/config.js +4 -4
  36. package/server/functions/getContent.js +45 -45
  37. package/server/functions/getDraftKey.js +22 -22
  38. package/server/functions/getSearchData.js +31 -31
  39. package/server/functions/getTags.js +30 -30
  40. package/server/functions/getUser.js +27 -27
  41. package/server/functions/utils/mock.reply.js +55 -55
  42. package/server/index.js +22 -22
  43. package/server/migrations/fixes.sql +129 -129
  44. package/server/migrations/site.sql +595 -595
  45. package/server/plugins/adminHook.js +78 -78
  46. package/server/plugins/hook.js +59 -59
  47. package/server/plugins/vite.js +75 -75
  48. package/server/routes/category/controllers/cms.category.delete.js +21 -21
  49. package/server/routes/category/controllers/cms.category.get.js +17 -17
  50. package/server/routes/category/controllers/cms.category.list.js +16 -16
  51. package/server/routes/category/controllers/cms.category.post.js +21 -21
  52. package/server/routes/category/controllers/cms.category.put.js +23 -23
  53. package/server/routes/category/index.mjs +22 -22
  54. package/server/routes/cms/controllers/cmsStat.js +55 -55
  55. package/server/routes/cms/controllers/cmsSuggest.js +57 -57
  56. package/server/routes/cms/controllers/deleteContent.js +113 -113
  57. package/server/routes/cms/controllers/deleteMedia.js +76 -76
  58. package/server/routes/cms/controllers/downloadMedia.js +84 -84
  59. package/server/routes/cms/controllers/getContent.js +113 -113
  60. package/server/routes/cms/controllers/getContentBySlug.js +93 -93
  61. package/server/routes/cms/controllers/getPermissions.js +15 -15
  62. package/server/routes/cms/controllers/insertContent.js +217 -217
  63. package/server/routes/cms/controllers/listMedia.js +155 -155
  64. package/server/routes/cms/controllers/metadataMedia.js +39 -39
  65. package/server/routes/cms/controllers/properties.get.js +18 -18
  66. package/server/routes/cms/controllers/properties.post.js +99 -99
  67. package/server/routes/cms/controllers/searchContent.js +214 -214
  68. package/server/routes/cms/controllers/setPermissions.js +49 -49
  69. package/server/routes/cms/controllers/translate.js +89 -89
  70. package/server/routes/cms/controllers/updateContent.js +266 -266
  71. package/server/routes/cms/controllers/uploadMedia.js +79 -79
  72. package/server/routes/cms/functions/getSettings.js +48 -48
  73. package/server/routes/cms/index.mjs +112 -112
  74. package/server/routes/cms/utils/additionalData.js +35 -35
  75. package/server/routes/cms/utils/getCollection.js +89 -89
  76. package/server/routes/cms/utils/getSingle.js +188 -188
  77. package/server/routes/cms/utils/inputTypes.js +5 -5
  78. package/server/routes/cms/utils/insertContentLocalization.js +104 -104
  79. package/server/routes/cms/utils/requestTranslation.js +85 -85
  80. package/server/routes/cms/utils/updateLocalization.js +47 -47
  81. package/server/routes/cmsSpace/controllers/deleteSpace.js +25 -25
  82. package/server/routes/cmsSpace/controllers/getSpaces.js +27 -27
  83. package/server/routes/cmsSpace/controllers/insertSpace.js +21 -21
  84. package/server/routes/cmsSpace/controllers/updateSpace.js +23 -23
  85. package/server/routes/cmsSpace/index.mjs +20 -20
  86. package/server/routes/contentType/controllers/addContentType.js +160 -160
  87. package/server/routes/contentType/controllers/contentTypeList.js +54 -54
  88. package/server/routes/contentType/controllers/delContentType.js +75 -75
  89. package/server/routes/contentType/controllers/editContentType.js +88 -88
  90. package/server/routes/contentType/controllers/getContentType.js +65 -65
  91. package/server/routes/contentType/index.mjs +35 -35
  92. package/server/routes/contentType/utils/updateContents.js +44 -44
  93. package/server/routes/contentType/utils/updateCustomContentTable.js +53 -53
  94. package/server/routes/feedback/controllers/email.list.js +24 -24
  95. package/server/routes/feedback/controllers/feedback.js +48 -48
  96. package/server/routes/feedback/controllers/feedback.list.js +37 -37
  97. package/server/routes/feedback/controllers/news.subscriptions.js +44 -44
  98. package/server/routes/feedback/index.mjs +71 -71
  99. package/server/routes/logs/controllers/export.user.logs.js +77 -77
  100. package/server/routes/logs/controllers/user.logs.js +44 -44
  101. package/server/routes/logs/index.mjs +9 -9
  102. package/server/routes/menu/controllers/addMenu.js +37 -37
  103. package/server/routes/menu/controllers/delMenu.js +31 -31
  104. package/server/routes/menu/controllers/editMenu.js +41 -41
  105. package/server/routes/menu/controllers/getMenu.js +24 -24
  106. package/server/routes/menu/functions/getMenu.js +50 -50
  107. package/server/routes/menu/index.mjs +13 -13
  108. package/server/routes/migration/controllers/collectionToCustom.js +137 -137
  109. package/server/routes/migration/index.mjs +8 -8
  110. package/server/routes/root.mjs +8 -8
  111. package/server/routes/tags/controllers/add.tags.js +24 -24
  112. package/server/routes/tags/controllers/del.tags.js +19 -19
  113. package/server/routes/tags/controllers/edit.tags.js +25 -25
  114. package/server/routes/tags/controllers/get.tags.js +15 -15
  115. package/server/routes/tags/index.mjs +14 -14
  116. package/server/templates/cls/cms.category_type.json +9 -9
  117. package/server/templates/cls/cms.content_review_status.json +9 -9
  118. package/server/templates/cls/cms.content_status.json +9 -9
  119. package/server/templates/cls/cms.content_type.json +9 -9
  120. package/server/templates/cls/cms.lang.json +9 -9
  121. package/server/templates/page/login.html +126 -126
  122. package/server/templates/select/core.user_mentioned.sql +1 -1
  123. package/utils.d.ts +52 -52
  124. package/utils.js +8 -8
  125. package/dist/CollectionsBreadcrumb.vue_vue_type_script_setup_true_lang-umRzB5mY.js +0 -53
  126. package/dist/MonacoEditor.vue_vue_type_script_setup_true_lang-B1DrxmQX.js +0 -84
@@ -1,22 +1,22 @@
1
- import cmsCategoryGet from './controllers/cms.category.get.js';
2
- import cmsCategoryList from './controllers/cms.category.list.js';
3
- import cmsCategoryPut from './controllers/cms.category.put.js';
4
- import cmsCategoryPost from './controllers/cms.category.post.js';
5
- import cmsCategoryDelete from './controllers/cms.category.delete.js'
6
-
7
- export default async function route(app) {
8
- // category add
9
- app.post('/cms-category', {}, cmsCategoryPost);
10
-
11
- // category edit
12
- app.put('/cms-category/:id', {}, cmsCategoryPut);
13
-
14
- // category list
15
- app.get('/cms-category', { config: { policy: ['site'] }}, cmsCategoryList);
16
-
17
- // category get
18
- app.get('/cms-category/:id', {}, cmsCategoryGet);
19
-
20
- // category delete
21
- app.delete('/cms-category/:id', { config: { policy: ['site'] }}, cmsCategoryDelete);
22
- }
1
+ import cmsCategoryGet from './controllers/cms.category.get.js';
2
+ import cmsCategoryList from './controllers/cms.category.list.js';
3
+ import cmsCategoryPut from './controllers/cms.category.put.js';
4
+ import cmsCategoryPost from './controllers/cms.category.post.js';
5
+ import cmsCategoryDelete from './controllers/cms.category.delete.js'
6
+
7
+ export default async function route(app) {
8
+ // category add
9
+ app.post('/cms-category', {}, cmsCategoryPost);
10
+
11
+ // category edit
12
+ app.put('/cms-category/:id', {}, cmsCategoryPut);
13
+
14
+ // category list
15
+ app.get('/cms-category', { config: { policy: ['site'] }}, cmsCategoryList);
16
+
17
+ // category get
18
+ app.get('/cms-category/:id', {}, cmsCategoryGet);
19
+
20
+ // category delete
21
+ app.delete('/cms-category/:id', { config: { policy: ['site'] }}, cmsCategoryDelete);
22
+ }
@@ -1,56 +1,56 @@
1
- import { metaFormat, pgClients } from '@opengis/fastify-table/utils.js';
2
-
3
- const q = `select
4
- count(distinct object_id) as cdtotal,
5
- count(distinct object_id) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as cdnewcount from site.content_data`;
6
-
7
- const q1 = `select
8
- count(*) as usercount,
9
- count(*) filter( where date_trunc('month', cdate) = date_trunc('month', CURRENT_DATE) ) as usernewcount from admin.users`;
10
-
11
- const q2 = `select
12
- count(*) as mediacount,
13
- count(*) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as medianewcount from site.media`;
14
-
15
- const q3 = `select type, title, updated_at, content_type_id as object_id, slug as content_id from site.contents a
16
- left join lateral (select type from site.content_types where content_type_id=a.content_type_id limit 1)b on 1=1
17
- where 1=1 order by updated_at desc limit 5`;
18
-
19
- export default async function cmsStat({ pg = pgClients.client }, reply) {
20
- if (!pg) {
21
- return reply.status(500).send('empty pg');
22
- }
23
-
24
- const { cdtotal = 0, cdnewcount = 0 } = await pg.query(q).then(el => el.rows?.[0] || {});
25
-
26
- const customTables = 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 || []);
27
-
28
- const custom = await Promise.all(customTables.filter(table => pg?.pk?.[`data.${table}`]).map(async table => {
29
- // await pg.query(`alter table data.${table} add column if not exists created_at timestamp with time zone not null default now()`);
30
- const { total = 0, newcount = 0 } = await pg.queryCache(`select count(*) as total, count(*) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as newcount from data.${table}`, { table: `data.${table}` }).then(el => el.rows?.[0] || {});
31
- return { table, total, newcount };
32
- }));
33
-
34
- const totalContent = custom.reduce((acc, el) => acc + +el.total, +cdtotal);
35
- const newContent = custom.reduce((acc, el) => acc + +el.newcount, +cdnewcount);
36
-
37
- const { usercount = 0, usernewcount = 0 } = await pg.query(q1).then(el => el.rows?.[0] || {});
38
-
39
- const { mediacount = 0, medianewcount = 0 } = await pg.query(q2).then(el => el.rows?.[0] || {});
40
-
41
- const { rows = [] } = await pg.query(q3);
42
-
43
- await metaFormat({ rows, cls: { updated_by: 'core.user_mentioned' }, sufix: false }, pg);
44
-
45
- return {
46
- stat: {
47
- totalContent,
48
- newContent,
49
- totalUsers: +usercount,
50
- newUsers: +usernewcount,
51
- totalMediaFiles: +mediacount,
52
- newMediaFiles: +medianewcount,
53
- },
54
- recentContent: rows,
55
- };
1
+ import { metaFormat, pgClients } from '@opengis/fastify-table/utils.js';
2
+
3
+ const q = `select
4
+ count(distinct object_id) as cdtotal,
5
+ count(distinct object_id) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as cdnewcount from site.content_data`;
6
+
7
+ const q1 = `select
8
+ count(*) as usercount,
9
+ count(*) filter( where date_trunc('month', cdate) = date_trunc('month', CURRENT_DATE) ) as usernewcount from admin.users`;
10
+
11
+ const q2 = `select
12
+ count(*) as mediacount,
13
+ count(*) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as medianewcount from site.media`;
14
+
15
+ const q3 = `select type, title, updated_at, content_type_id as object_id, slug as content_id from site.contents a
16
+ left join lateral (select type from site.content_types where content_type_id=a.content_type_id limit 1)b on 1=1
17
+ where 1=1 order by updated_at desc limit 5`;
18
+
19
+ export default async function cmsStat({ pg = pgClients.client }, reply) {
20
+ if (!pg) {
21
+ return reply.status(500).send('empty pg');
22
+ }
23
+
24
+ const { cdtotal = 0, cdnewcount = 0 } = await pg.query(q).then(el => el.rows?.[0] || {});
25
+
26
+ const customTables = 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 || []);
27
+
28
+ const custom = await Promise.all(customTables.filter(table => pg?.pk?.[`data.${table}`]).map(async table => {
29
+ // await pg.query(`alter table data.${table} add column if not exists created_at timestamp with time zone not null default now()`);
30
+ const { total = 0, newcount = 0 } = await pg.queryCache(`select count(*) as total, count(*) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as newcount from data.${table}`, { table: `data.${table}` }).then(el => el.rows?.[0] || {});
31
+ return { table, total, newcount };
32
+ }));
33
+
34
+ const totalContent = custom.reduce((acc, el) => acc + +el.total, +cdtotal);
35
+ const newContent = custom.reduce((acc, el) => acc + +el.newcount, +cdnewcount);
36
+
37
+ const { usercount = 0, usernewcount = 0 } = await pg.query(q1).then(el => el.rows?.[0] || {});
38
+
39
+ const { mediacount = 0, medianewcount = 0 } = await pg.query(q2).then(el => el.rows?.[0] || {});
40
+
41
+ const { rows = [] } = await pg.query(q3);
42
+
43
+ await metaFormat({ rows, cls: { updated_by: 'core.user_mentioned' }, sufix: false }, pg);
44
+
45
+ return {
46
+ stat: {
47
+ totalContent,
48
+ newContent,
49
+ totalUsers: +usercount,
50
+ newUsers: +usernewcount,
51
+ totalMediaFiles: +mediacount,
52
+ newMediaFiles: +medianewcount,
53
+ },
54
+ recentContent: rows,
55
+ };
56
56
  }
@@ -1,58 +1,58 @@
1
- import { config, pgClients } from '@opengis/fastify-table/utils.js';
2
-
3
- const maxLimit = 50;
4
-
5
- export default async function suggest({ pg = pgClients.client, params = {}, query = {}, user = {} }, reply) {
6
- const t1 = Date.now();
7
-
8
- const { id } = params;
9
- const { key, val } = query;
10
-
11
- if (!pg?.pk) {
12
- return reply.status(500).send('empty pg');
13
- }
14
-
15
- if (!id) {
16
- return reply.status(400).send('not enough params');
17
- }
18
-
19
- const limit = Math.min(query.limit || 16, maxLimit);
20
- const offset = query.page && query.page > 0 ? (query.page - 1) * limit : 0;
21
-
22
- const { ctid, table } = await pg.query(
23
- 'select content_type_id as ctid, table_name as table, columns from site.content_types where $1 in (content_type_id, name)',
24
- [id],
25
- ).then(el => el.rows?.[0] || {});
26
-
27
- if (!ctid) {
28
- return reply.status(404).send('content type not found');
29
- }
30
-
31
- // site.content_data
32
- if (!table) {
33
- const q = `select content_id as id, title as text from site.contents where content_type_id=$1 and ${key ? `lower(title) ~ lower($2)` : (val ? 'content_id=$2' : '1=1')} limit ${limit}`;
34
-
35
- const { rows = [] } = await pg.query(q, [ctid, (key || val)].filter(Boolean));
36
-
37
- return {
38
- time: Date.now() - t1,
39
- limit,
40
- count: rows.length,
41
- mode: 'array',
42
- sql: (config.local || user?.user_type?.includes?.('admin')) ? q : undefined,
43
- data: rows,
44
- };
45
- }
46
-
47
- const q = `select id, title as text from data.${table} where ${key ? `lower(title) ~ lower($1)` : (val ? 'id=$1' : '1=1')} limit ${limit}`;
48
- const { rows = [] } = await pg.query(q, [key || val].filter(Boolean));
49
-
50
- return {
51
- time: Date.now() - t1,
52
- limit,
53
- count: rows.length,
54
- mode: 'array',
55
- sql: (config.local || user?.user_type?.includes?.('admin')) ? q : undefined,
56
- data: rows,
57
- };
1
+ import { config, pgClients } from '@opengis/fastify-table/utils.js';
2
+
3
+ const maxLimit = 50;
4
+
5
+ export default async function suggest({ pg = pgClients.client, params = {}, query = {}, user = {} }, reply) {
6
+ const t1 = Date.now();
7
+
8
+ const { id } = params;
9
+ const { key, val } = query;
10
+
11
+ if (!pg?.pk) {
12
+ return reply.status(500).send('empty pg');
13
+ }
14
+
15
+ if (!id) {
16
+ return reply.status(400).send('not enough params');
17
+ }
18
+
19
+ const limit = Math.min(query.limit || 16, maxLimit);
20
+ const offset = query.page && query.page > 0 ? (query.page - 1) * limit : 0;
21
+
22
+ const { ctid, table } = await pg.query(
23
+ 'select content_type_id as ctid, table_name as table, columns from site.content_types where $1 in (content_type_id, name)',
24
+ [id],
25
+ ).then(el => el.rows?.[0] || {});
26
+
27
+ if (!ctid) {
28
+ return reply.status(404).send('content type not found');
29
+ }
30
+
31
+ // site.content_data
32
+ if (!table) {
33
+ const q = `select content_id as id, title as text from site.contents where content_type_id=$1 and ${key ? `lower(title) ~ lower($2)` : (val ? 'content_id=$2' : '1=1')} limit ${limit}`;
34
+
35
+ const { rows = [] } = await pg.query(q, [ctid, (key || val)].filter(Boolean));
36
+
37
+ return {
38
+ time: Date.now() - t1,
39
+ limit,
40
+ count: rows.length,
41
+ mode: 'array',
42
+ sql: (config.local || user?.user_type?.includes?.('admin')) ? q : undefined,
43
+ data: rows,
44
+ };
45
+ }
46
+
47
+ const q = `select id, title as text from data.${table} where ${key ? `lower(title) ~ lower($1)` : (val ? 'id=$1' : '1=1')} limit ${limit}`;
48
+ const { rows = [] } = await pg.query(q, [key || val].filter(Boolean));
49
+
50
+ return {
51
+ time: Date.now() - t1,
52
+ limit,
53
+ count: rows.length,
54
+ mode: 'array',
55
+ sql: (config.local || user?.user_type?.includes?.('admin')) ? q : undefined,
56
+ data: rows,
57
+ };
58
58
  }
@@ -1,114 +1,114 @@
1
- import { config, pgClients, dataUpdate, dataDelete } from '@opengis/fastify-table/utils.js';
2
-
3
- export default async function deleteContent(req, reply) {
4
- const {
5
- pg = pgClients.client,
6
- params = {},
7
- user = {},
8
- headers = {},
9
- } = req;
10
-
11
- const { type, id } = params;
12
-
13
- if (!type) {
14
- return reply.status(400).send('not enough params: type');
15
- }
16
-
17
- if (!id) {
18
- return reply.status(400).send('not enough params: id');
19
- }
20
-
21
- if (id === 'pages') {
22
- return reply.status(403).send('access restricted: pages contents cannot be deleted');
23
- }
24
-
25
- const { ctid, dbtable } = await pg.query('select content_type_id as ctid, table_name as dbtable from site.content_types where $1 in (content_type_id, name)', [params.type])
26
- .then(el => el.rows?.[0] || {});
27
-
28
- const arr = config.pg ? await pg.query(`select array_agg(relname)::text[] from pg_class a
29
- left join pg_namespace b on a.relnamespace=b.oid
30
- where a.relam=2 and b.nspname='data'`).then(el => el.rows?.[0]?.array_agg || []) : [];
31
-
32
- if (!arr.length && type !== 'pages') {
33
- return reply.status(400).send('empty schema: data');
34
- }
35
-
36
- const table = arr.find(el => el === params.type);
37
-
38
- // singletone with extra at site.content_data
39
- if (!table && !dbtable && ctid) {
40
- const { cid, status } = ctid === 'pages' && id
41
- ? await pg.query(
42
- 'select content_id as cid, status from site.contents where content_id=$1 limit 1',
43
- [id]).then(el => el.rows?.[0] || {})
44
- : await pg.query(
45
- 'select content_id as cid, status from site.contents where content_type_id=$1 limit 1',
46
- [ctid],
47
- ).then(el => el.rows?.[0] || {});
48
-
49
- if (!cid) {
50
- return reply.status(404).send('contents not found');
51
- }
52
-
53
- if (cid === 'pages') {
54
- return reply.status(403).send('access restricted: pages contents cannot be deleted');
55
- }
56
-
57
- if (status === 'archived') {
58
- const row = await dataDelete({
59
- pg,
60
- id,
61
- table: 'site.contents',
62
- referer: headers?.referer,
63
- uid: user?.uid || 0,
64
- })
65
- return { id, ...row || {} };
66
- }
67
-
68
- // archived
69
- const row = await dataUpdate({
70
- pg,
71
- id,
72
- table: 'site.contents',
73
- data: {
74
- status: 'archived',
75
- },
76
- referer: headers?.referer,
77
- uid: user?.uid || 0,
78
- })
79
- return { id, ...row || {} };
80
- }
81
-
82
- // custom table
83
- if (!table && !dbtable) {
84
- return reply.status(400).send('invalid params: type');
85
- }
86
-
87
- const status = await pg.query(`select status from data."${(table || dbtable)}" where id =$1`, [id]).then(el => el.rows?.[0]?.status);
88
-
89
- if (status === 'archived') {
90
- const row = await dataDelete({
91
- pg,
92
- id,
93
- table: 'data.' + `"${(table || dbtable)}"`,
94
- referer: headers?.referer,
95
- uid: user?.uid || 0,
96
- })
97
- return { id, ...row || {} };
98
- }
99
-
100
- // archived
101
- const result = await dataUpdate({
102
- pg,
103
- id,
104
- table: 'data.' + `"${(table || dbtable)}"`,
105
- data: {
106
- status: 'archived',
107
- },
108
- referer: headers?.referer,
109
- uid: user?.uid || 0,
110
- });
111
-
112
- const pk = pg.pk?.['data.' + table || dbtable];
113
- return reply.status(200).send({ id: result?.[pk], ...result || {} });
1
+ import { config, pgClients, dataUpdate, dataDelete } from '@opengis/fastify-table/utils.js';
2
+
3
+ export default async function deleteContent(req, reply) {
4
+ const {
5
+ pg = pgClients.client,
6
+ params = {},
7
+ user = {},
8
+ headers = {},
9
+ } = req;
10
+
11
+ const { type, id } = params;
12
+
13
+ if (!type) {
14
+ return reply.status(400).send('not enough params: type');
15
+ }
16
+
17
+ if (!id) {
18
+ return reply.status(400).send('not enough params: id');
19
+ }
20
+
21
+ if (id === 'pages') {
22
+ return reply.status(403).send('access restricted: pages contents cannot be deleted');
23
+ }
24
+
25
+ const { ctid, dbtable } = await pg.query('select content_type_id as ctid, table_name as dbtable from site.content_types where $1 in (content_type_id, name)', [params.type])
26
+ .then(el => el.rows?.[0] || {});
27
+
28
+ const arr = config.pg ? await pg.query(`select array_agg(relname)::text[] from pg_class a
29
+ left join pg_namespace b on a.relnamespace=b.oid
30
+ where a.relam=2 and b.nspname='data'`).then(el => el.rows?.[0]?.array_agg || []) : [];
31
+
32
+ if (!arr.length && type !== 'pages') {
33
+ return reply.status(400).send('empty schema: data');
34
+ }
35
+
36
+ const table = arr.find(el => el === params.type);
37
+
38
+ // singletone with extra at site.content_data
39
+ if (!table && !dbtable && ctid) {
40
+ const { cid, status } = ctid === 'pages' && id
41
+ ? await pg.query(
42
+ 'select content_id as cid, status from site.contents where content_id=$1 limit 1',
43
+ [id]).then(el => el.rows?.[0] || {})
44
+ : await pg.query(
45
+ 'select content_id as cid, status from site.contents where content_type_id=$1 limit 1',
46
+ [ctid],
47
+ ).then(el => el.rows?.[0] || {});
48
+
49
+ if (!cid) {
50
+ return reply.status(404).send('contents not found');
51
+ }
52
+
53
+ if (cid === 'pages') {
54
+ return reply.status(403).send('access restricted: pages contents cannot be deleted');
55
+ }
56
+
57
+ if (status === 'archived') {
58
+ const row = await dataDelete({
59
+ pg,
60
+ id,
61
+ table: 'site.contents',
62
+ referer: headers?.referer,
63
+ uid: user?.uid || 0,
64
+ })
65
+ return { id, ...row || {} };
66
+ }
67
+
68
+ // archived
69
+ const row = await dataUpdate({
70
+ pg,
71
+ id,
72
+ table: 'site.contents',
73
+ data: {
74
+ status: 'archived',
75
+ },
76
+ referer: headers?.referer,
77
+ uid: user?.uid || 0,
78
+ })
79
+ return { id, ...row || {} };
80
+ }
81
+
82
+ // custom table
83
+ if (!table && !dbtable) {
84
+ return reply.status(400).send('invalid params: type');
85
+ }
86
+
87
+ const status = await pg.query(`select status from data."${(table || dbtable)}" where id =$1`, [id]).then(el => el.rows?.[0]?.status);
88
+
89
+ if (status === 'archived') {
90
+ const row = await dataDelete({
91
+ pg,
92
+ id,
93
+ table: 'data.' + `"${(table || dbtable)}"`,
94
+ referer: headers?.referer,
95
+ uid: user?.uid || 0,
96
+ })
97
+ return { id, ...row || {} };
98
+ }
99
+
100
+ // archived
101
+ const result = await dataUpdate({
102
+ pg,
103
+ id,
104
+ table: 'data.' + `"${(table || dbtable)}"`,
105
+ data: {
106
+ status: 'archived',
107
+ },
108
+ referer: headers?.referer,
109
+ uid: user?.uid || 0,
110
+ });
111
+
112
+ const pk = pg.pk?.['data.' + table || dbtable];
113
+ return reply.status(200).send({ id: result?.[pk], ...result || {} });
114
114
  }
@@ -1,77 +1,77 @@
1
- import path from 'node:path';
2
- import { existsSync } from 'node:fs';
3
- import { rm, stat, readdir } from 'node:fs/promises';
4
-
5
- import { config, dataDelete, getFolder, pgClients } from "@opengis/fastify-table/utils.js";
6
-
7
- // path.resolve() converts POSIX paths from getFolder to valid Windows paths (Bun/Node fs require this on Windows)
8
- const rootDir = path.resolve(getFolder(config, 'local'));
9
- const dir = '/files';
10
-
11
- export default async function deleteMedia({
12
- pg = pgClients.client, params = {}, query = {}, user = {}, method = 'DELETE',
13
- }, reply) {
14
- if (!config.debug && method !== 'DELETE') {
15
- return reply.status(403).send('access restricted');
16
- }
17
-
18
- if (!params.id && query.subdir) {
19
- const dirpath = path.join(rootDir, dir, query.subdir || '');
20
- const exists = existsSync(dirpath);
21
- const stats = await stat(dirpath);
22
-
23
- if (!stats.isDirectory()) {
24
- return reply.status(400).send('not a directory');
25
- }
26
-
27
- if (!exists) {
28
- return reply.status(404).send('subdir not found: ' + query.subdir);
29
- }
30
-
31
- const content = await readdir(dirpath, { recursive: true });
32
-
33
- if (query.subdir.startsWith('uploads') && !query.subdir.split('/')[1]) {
34
- return reply.status(403).send('access restricted: uploads directory');
35
- }
36
-
37
- // only admins are allowed to delete non-empty directories
38
- if (content?.length && !user?.user_type?.includes?.('admin')) {
39
- return reply.status(400).send('directory is not empty');
40
- }
41
-
42
- await rm(dirpath, { recursive: true });
43
- return reply.status(200).send('subdirectory successfully deleted');
44
- }
45
-
46
- if (!params?.id) {
47
- return reply.status(400).send('not enough params: id');
48
- }
49
-
50
- if (!pg.pk?.['site.media']) {
51
- return reply.status(404).send('table not found');
52
- }
53
-
54
- const { url: relpath, id } = await pg.query(
55
- 'select media_id as id, url from site.media where media_id = $1 and url is not null',
56
- [params.id],
57
- ).then(el => el.rows?.[0] || {});
58
-
59
- if (!id) {
60
- return reply.status(404).send('media not found: ' + params.id);
61
- }
62
-
63
- const res = await dataDelete({
64
- pg,
65
- id,
66
- table: 'site.media',
67
- uid: user?.uid || 0,
68
- });
69
-
70
- const filepath = path.join(rootDir, relpath);
71
-
72
- if (existsSync(filepath)) {
73
- await rm(filepath, { recursive: true });
74
- }
75
-
76
- return { id, ...res || {} };
1
+ import path from 'node:path';
2
+ import { existsSync } from 'node:fs';
3
+ import { rm, stat, readdir } from 'node:fs/promises';
4
+
5
+ import { config, dataDelete, getFolder, pgClients } from "@opengis/fastify-table/utils.js";
6
+
7
+ // path.resolve() converts POSIX paths from getFolder to valid Windows paths (Bun/Node fs require this on Windows)
8
+ const rootDir = path.resolve(getFolder(config, 'local'));
9
+ const dir = '/files';
10
+
11
+ export default async function deleteMedia({
12
+ pg = pgClients.client, params = {}, query = {}, user = {}, method = 'DELETE',
13
+ }, reply) {
14
+ if (!config.debug && method !== 'DELETE') {
15
+ return reply.status(403).send('access restricted');
16
+ }
17
+
18
+ if (!params.id && query.subdir) {
19
+ const dirpath = path.join(rootDir, dir, query.subdir || '');
20
+ const exists = existsSync(dirpath);
21
+ const stats = await stat(dirpath);
22
+
23
+ if (!stats.isDirectory()) {
24
+ return reply.status(400).send('not a directory');
25
+ }
26
+
27
+ if (!exists) {
28
+ return reply.status(404).send('subdir not found: ' + query.subdir);
29
+ }
30
+
31
+ const content = await readdir(dirpath, { recursive: true });
32
+
33
+ if (query.subdir.startsWith('uploads') && !query.subdir.split('/')[1]) {
34
+ return reply.status(403).send('access restricted: uploads directory');
35
+ }
36
+
37
+ // only admins are allowed to delete non-empty directories
38
+ if (content?.length && !user?.user_type?.includes?.('admin')) {
39
+ return reply.status(400).send('directory is not empty');
40
+ }
41
+
42
+ await rm(dirpath, { recursive: true });
43
+ return reply.status(200).send('subdirectory successfully deleted');
44
+ }
45
+
46
+ if (!params?.id) {
47
+ return reply.status(400).send('not enough params: id');
48
+ }
49
+
50
+ if (!pg.pk?.['site.media']) {
51
+ return reply.status(404).send('table not found');
52
+ }
53
+
54
+ const { url: relpath, id } = await pg.query(
55
+ 'select media_id as id, url from site.media where media_id = $1 and url is not null',
56
+ [params.id],
57
+ ).then(el => el.rows?.[0] || {});
58
+
59
+ if (!id) {
60
+ return reply.status(404).send('media not found: ' + params.id);
61
+ }
62
+
63
+ const res = await dataDelete({
64
+ pg,
65
+ id,
66
+ table: 'site.media',
67
+ uid: user?.uid || 0,
68
+ });
69
+
70
+ const filepath = path.join(rootDir, relpath);
71
+
72
+ if (existsSync(filepath)) {
73
+ await rm(filepath, { recursive: true });
74
+ }
75
+
76
+ return { id, ...res || {} };
77
77
  }