@opengis/cms 0.0.26 → 0.0.28

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 (90) hide show
  1. package/README.md +98 -98
  2. package/dist/index-W-qQIppj-DunG40EG.js +2452 -0
  3. package/dist/index.html +28 -28
  4. package/dist/index.js +12683 -6702
  5. package/dist/index.umd.cjs +87 -41
  6. package/dist/style.css +1 -0
  7. package/package.json +68 -68
  8. package/plugin.js +76 -76
  9. package/server/app.js +35 -35
  10. package/server/config.js +4 -4
  11. package/server/functions/getDraftKey.js +22 -22
  12. package/server/index.js +22 -22
  13. package/server/migrations/fixes.sql +123 -123
  14. package/server/migrations/site.sql +545 -545
  15. package/server/plugins/adminHook.js +78 -78
  16. package/server/plugins/hook.js +59 -59
  17. package/server/plugins/vite.js +84 -84
  18. package/server/routes/category/controllers/cms.category.delete.js +21 -21
  19. package/server/routes/category/controllers/cms.category.get.js +17 -17
  20. package/server/routes/category/controllers/cms.category.list.js +16 -16
  21. package/server/routes/category/controllers/cms.category.post.js +21 -21
  22. package/server/routes/category/controllers/cms.category.put.js +23 -23
  23. package/server/routes/category/index.mjs +22 -22
  24. package/server/routes/cms/controllers/cmsStat.js +55 -55
  25. package/server/routes/cms/controllers/cmsSuggest.js +57 -57
  26. package/server/routes/cms/controllers/deleteContent.js +114 -114
  27. package/server/routes/cms/controllers/deleteMedia.js +75 -75
  28. package/server/routes/cms/controllers/downloadMedia.js +48 -48
  29. package/server/routes/cms/controllers/getContent.js +110 -110
  30. package/server/routes/cms/controllers/getContentBySlug.js +95 -95
  31. package/server/routes/cms/controllers/getPermissions.js +15 -15
  32. package/server/routes/cms/controllers/insertContent.js +218 -218
  33. package/server/routes/cms/controllers/listMedia.js +93 -93
  34. package/server/routes/cms/controllers/metadataMedia.js +38 -38
  35. package/server/routes/cms/controllers/properties.get.js +53 -53
  36. package/server/routes/cms/controllers/properties.post.js +99 -99
  37. package/server/routes/cms/controllers/searchContent.js +205 -205
  38. package/server/routes/cms/controllers/setPermissions.js +49 -49
  39. package/server/routes/cms/controllers/translate.js +89 -89
  40. package/server/routes/cms/controllers/updateContent.js +238 -238
  41. package/server/routes/cms/controllers/uploadMedia.js +78 -78
  42. package/server/routes/cms/index.mjs +114 -114
  43. package/server/routes/cms/utils/additionalData.js +35 -35
  44. package/server/routes/cms/utils/getCollection.js +81 -81
  45. package/server/routes/cms/utils/getSingle.js +187 -187
  46. package/server/routes/cms/utils/insertContentLocalization.js +86 -86
  47. package/server/routes/cms/utils/requestTranslation.js +85 -85
  48. package/server/routes/cms/utils/updateLocalization.js +47 -47
  49. package/server/routes/cmsSpace/controllers/deleteSpace.js +25 -25
  50. package/server/routes/cmsSpace/controllers/getSpaces.js +27 -27
  51. package/server/routes/cmsSpace/controllers/insertSpace.js +21 -21
  52. package/server/routes/cmsSpace/controllers/updateSpace.js +23 -23
  53. package/server/routes/cmsSpace/index.mjs +20 -20
  54. package/server/routes/contentType/controllers/addContentType.js +162 -162
  55. package/server/routes/contentType/controllers/contentTypeList.js +54 -54
  56. package/server/routes/contentType/controllers/delContentType.js +75 -75
  57. package/server/routes/contentType/controllers/editContentType.js +61 -61
  58. package/server/routes/contentType/controllers/getContentType.js +37 -37
  59. package/server/routes/contentType/index.mjs +35 -35
  60. package/server/routes/contentType/utils/updateContents.js +28 -28
  61. package/server/routes/contentType/utils/updateCustomContentTable.js +55 -55
  62. package/server/routes/feedback/controllers/email.list.js +24 -24
  63. package/server/routes/feedback/controllers/feedback.js +48 -48
  64. package/server/routes/feedback/controllers/feedback.list.js +37 -37
  65. package/server/routes/feedback/controllers/news.subscriptions.js +44 -44
  66. package/server/routes/feedback/index.mjs +71 -71
  67. package/server/routes/logs/controllers/export.user.logs.js +77 -77
  68. package/server/routes/logs/controllers/user.logs.js +44 -44
  69. package/server/routes/logs/index.mjs +9 -9
  70. package/server/routes/menu/controllers/addMenu.js +37 -37
  71. package/server/routes/menu/controllers/delMenu.js +31 -31
  72. package/server/routes/menu/controllers/editMenu.js +41 -41
  73. package/server/routes/menu/controllers/getMenu.js +42 -42
  74. package/server/routes/menu/index.mjs +13 -13
  75. package/server/routes/migration/controllers/collectionToCustom.js +137 -137
  76. package/server/routes/migration/index.mjs +8 -8
  77. package/server/routes/root.mjs +8 -8
  78. package/server/routes/tags/controllers/add.tags.js +24 -24
  79. package/server/routes/tags/controllers/del.tags.js +19 -19
  80. package/server/routes/tags/controllers/edit.tags.js +25 -25
  81. package/server/routes/tags/controllers/get.tags.js +15 -15
  82. package/server/routes/tags/index.mjs +14 -14
  83. package/server/templates/cls/cms.category_type.json +9 -9
  84. package/server/templates/cls/cms.content_review_status.json +9 -9
  85. package/server/templates/cls/cms.content_status.json +9 -9
  86. package/server/templates/cls/cms.content_type.json +9 -9
  87. package/server/templates/cls/cms.lang.json +9 -9
  88. package/server/templates/page/login.html +126 -126
  89. package/server/templates/select/core.user_mentioned.sql +1 -1
  90. package/dist/index-W-qQIppj-DRzFSjU1.js +0 -2452
@@ -1,61 +1,61 @@
1
- import { config, dataUpdate, pgClients } from '@opengis/fastify-table/utils.js';
2
-
3
- import updateCustomContentTable from '../utils/updateCustomContentTable.js';
4
- import updateContents from '../utils/updateContents.js';
5
-
6
- export default async function builderPut(req, reply) {
7
- const { pg = pgClients.client, body = {}, params = {}, user = {} } = req;
8
- if (!params.id) {
9
- return reply.status(400).send('not enough params: id');
10
- }
11
-
12
- if (!pg?.pk?.['site.content_types']) {
13
- return reply.status(404).send('table not found');
14
- }
15
-
16
- const { cid, table } = await pg.query(
17
- `select content_type_id as cid, table_name as table, type from site.content_types where $1 in (content_type_id, name)`,
18
- [params.id],
19
- ).then(el => el.rows?.[0] || {});
20
-
21
- if (!cid) {
22
- return reply.status(404).send('content type not found');
23
- }
24
-
25
- const columns = (body.fields || body.columns)?.filter?.(el => el.name)?.map?.(el => Object.assign(el, { name: el.name.toLowerCase() })); // for rest crud
26
-
27
- const client = await pg.connect();
28
-
29
- client.options = pg.options;
30
- client.tlist = pg.tlist;
31
- client.pgType = pg.pgType;
32
- client.relkinds = pg.relkinds;
33
- client.pk = pg.pk;
34
-
35
- try {
36
- await client.query('begin');
37
-
38
- if (table) {
39
- await updateCustomContentTable({ pg: client, id: cid, columns });
40
- }
41
-
42
- await dataUpdate({
43
- pg: client,
44
- table: 'site.content_types',
45
- id: cid,
46
- data: body,
47
- uid: user?.uid,
48
- });
49
-
50
- const row = await updateContents(client, cid, { ...body, slug: body.slug || body.name }, user?.uid);
51
-
52
- await client.query('commit');
53
-
54
- return { id: row?.content_type_id, rows: [row].filter(Boolean) };
55
- } catch (err) {
56
- await client.query('rollback');
57
- throw err;
58
- } finally {
59
- client.release();
60
- }
61
- }
1
+ import { config, dataUpdate, pgClients } from '@opengis/fastify-table/utils.js';
2
+
3
+ import updateCustomContentTable from '../utils/updateCustomContentTable.js';
4
+ import updateContents from '../utils/updateContents.js';
5
+
6
+ export default async function builderPut(req, reply) {
7
+ const { pg = pgClients.client, body = {}, params = {}, user = {} } = req;
8
+ if (!params.id) {
9
+ return reply.status(400).send('not enough params: id');
10
+ }
11
+
12
+ if (!pg?.pk?.['site.content_types']) {
13
+ return reply.status(404).send('table not found');
14
+ }
15
+
16
+ const { cid, table } = await pg.query(
17
+ `select content_type_id as cid, table_name as table, type from site.content_types where $1 in (content_type_id, name)`,
18
+ [params.id],
19
+ ).then(el => el.rows?.[0] || {});
20
+
21
+ if (!cid) {
22
+ return reply.status(404).send('content type not found');
23
+ }
24
+
25
+ const columns = (body.fields || body.columns)?.filter?.(el => el.name)?.map?.(el => Object.assign(el, { name: el.name.toLowerCase() })); // for rest crud
26
+
27
+ const client = await pg.connect();
28
+
29
+ client.options = pg.options;
30
+ client.tlist = pg.tlist;
31
+ client.pgType = pg.pgType;
32
+ client.relkinds = pg.relkinds;
33
+ client.pk = pg.pk;
34
+
35
+ try {
36
+ await client.query('begin');
37
+
38
+ if (table) {
39
+ await updateCustomContentTable({ pg: client, id: cid, columns });
40
+ }
41
+
42
+ await dataUpdate({
43
+ pg: client,
44
+ table: 'site.content_types',
45
+ id: cid,
46
+ data: body,
47
+ uid: user?.uid,
48
+ });
49
+
50
+ const row = await updateContents(client, cid, { ...body, slug: body.slug || body.name }, user?.uid);
51
+
52
+ await client.query('commit');
53
+
54
+ return { id: row?.content_type_id, rows: [row].filter(Boolean) };
55
+ } catch (err) {
56
+ await client.query('rollback');
57
+ throw err;
58
+ } finally {
59
+ client.release();
60
+ }
61
+ }
@@ -1,37 +1,37 @@
1
- import { pgClients, getTemplate } from '@opengis/fastify-table/utils.js';
2
-
3
- export default async function builderGet({ pg = pgClients.client, params = {} }, reply) {
4
- if (!params.id) {
5
- return reply.status(400).send('not enough params: id');
6
- }
7
-
8
- if (!pg?.pk?.['site.content_types']) {
9
- return reply.status(404).send('table not found');
10
- }
11
-
12
- const row = await pg.query(
13
- `select content_type_id as id, * FROM site.content_types
14
- where $1 in (content_type_id, name)`, [params.id]
15
- ).then(el => el.rows?.[0]);
16
-
17
- if (!row) {
18
- return reply.status(404).send('content type not found');
19
- }
20
-
21
- const space = await pg.query(
22
- 'select name, default_locale, locales from site.spaces where space_id = $1', ['default'],
23
- ).then(el => el.rows?.[0] || {});
24
- Object.assign(row, { space });
25
-
26
- const loadTable = params.id === 'pages' ? await getTemplate('table', 'single.default.table') : {};
27
-
28
- const columns = params.id === 'pages' ? loadTable?.columns : row.columns;
29
- Object.assign(row, { columns });
30
-
31
- // filter system / hidden
32
- if (Array.isArray(row.columns)) {
33
- row.columns.filter(col => !col.hidden);
34
- }
35
-
36
- return reply.status(200).send(row);
37
- }
1
+ import { pgClients, getTemplate } from '@opengis/fastify-table/utils.js';
2
+
3
+ export default async function builderGet({ pg = pgClients.client, params = {} }, reply) {
4
+ if (!params.id) {
5
+ return reply.status(400).send('not enough params: id');
6
+ }
7
+
8
+ if (!pg?.pk?.['site.content_types']) {
9
+ return reply.status(404).send('table not found');
10
+ }
11
+
12
+ const row = await pg.query(
13
+ `select content_type_id as id, * FROM site.content_types
14
+ where $1 in (content_type_id, name)`, [params.id]
15
+ ).then(el => el.rows?.[0]);
16
+
17
+ if (!row) {
18
+ return reply.status(404).send('content type not found');
19
+ }
20
+
21
+ const space = await pg.query(
22
+ 'select name, default_locale, locales from site.spaces where space_id = $1', ['default'],
23
+ ).then(el => el.rows?.[0] || {});
24
+ Object.assign(row, { space });
25
+
26
+ const loadTable = params.id === 'pages' ? await getTemplate('table', 'single.default.table') : {};
27
+
28
+ const columns = params.id === 'pages' ? loadTable?.columns : row.columns;
29
+ Object.assign(row, { columns });
30
+
31
+ // filter system / hidden
32
+ if (Array.isArray(row.columns)) {
33
+ row.columns.filter(col => !col.hidden);
34
+ }
35
+
36
+ return reply.status(200).send(row);
37
+ }
@@ -1,35 +1,35 @@
1
- import getContentType from './controllers/getContentType.js';
2
- import contentTypeList from './controllers/contentTypeList.js';
3
- import editContentType from './controllers/editContentType.js';
4
- import addContentType from './controllers/addContentType.js';
5
- import delContentType from './controllers/delContentType.js';
6
-
7
- const params = { config: { policy: ['public'] } };
8
-
9
- export default async function route(app) {
10
- const schema = {
11
- params: {
12
- type: 'object',
13
- properties: {
14
- id: {
15
- type: 'string'
16
- },
17
- },
18
- },
19
- body: {
20
- type: 'object',
21
- required: ['name'],
22
- properties: {
23
- name: { type: 'string' }
24
- }
25
- }
26
- };
27
-
28
- app.get('/cms', params, contentTypeList);
29
- app.get('/cms-type', params, contentTypeList);
30
-
31
- app.post('/cms-type', { ...params, schema }, addContentType);
32
- app.put('/cms-type/:id', { ...params, schema }, editContentType);
33
- app.get('/cms-type/:id', params, getContentType);
34
- app.delete('/cms-type/:id', params, delContentType);
35
- }
1
+ import getContentType from './controllers/getContentType.js';
2
+ import contentTypeList from './controllers/contentTypeList.js';
3
+ import editContentType from './controllers/editContentType.js';
4
+ import addContentType from './controllers/addContentType.js';
5
+ import delContentType from './controllers/delContentType.js';
6
+
7
+ const params = { config: { policy: ['public'] } };
8
+
9
+ export default async function route(app) {
10
+ const schema = {
11
+ params: {
12
+ type: 'object',
13
+ properties: {
14
+ id: {
15
+ type: 'string'
16
+ },
17
+ },
18
+ },
19
+ body: {
20
+ type: 'object',
21
+ required: ['name'],
22
+ properties: {
23
+ name: { type: 'string' }
24
+ }
25
+ }
26
+ };
27
+
28
+ app.get('/cms', params, contentTypeList);
29
+ app.get('/cms-type', params, contentTypeList);
30
+
31
+ app.post('/cms-type', { ...params, schema }, addContentType);
32
+ app.put('/cms-type/:id', { ...params, schema }, editContentType);
33
+ app.get('/cms-type/:id', params, getContentType);
34
+ app.delete('/cms-type/:id', params, delContentType);
35
+ }
@@ -1,29 +1,29 @@
1
- import { dataInsert, dataUpdate } from "@opengis/fastify-table/utils.js";
2
-
3
- export default async function updateContents(pg, id, data, uid) {
4
- if (!pg?.pk || !id) return null;
5
-
6
- const contentId = await pg.query(
7
- `select content_id from site.contents where content_type_id = $1 order by content_id=content_type_id desc limit 1`, // update pages collection, avoid singles
8
- [id],
9
- ).then(el => el.rows?.[0]?.content_id);
10
-
11
- if (contentId) {
12
- const result = await dataUpdate({
13
- pg,
14
- id: contentId,
15
- table: 'site.contents',
16
- data,
17
- uid,
18
- });
19
- return result;
20
- }
21
-
22
- const result = await dataInsert({
23
- pg,
24
- table: 'site.contents',
25
- data: { ...data || {}, content_type_id: id },
26
- uid,
27
- }).then(el => el.rows?.[0]);
28
- return result;
1
+ import { dataInsert, dataUpdate } from "@opengis/fastify-table/utils.js";
2
+
3
+ export default async function updateContents(pg, id, data, uid) {
4
+ if (!pg?.pk || !id) return null;
5
+
6
+ const contentId = await pg.query(
7
+ `select content_id from site.contents where content_type_id = $1 order by content_id=content_type_id desc limit 1`, // update pages collection, avoid singles
8
+ [id],
9
+ ).then(el => el.rows?.[0]?.content_id);
10
+
11
+ if (contentId) {
12
+ const result = await dataUpdate({
13
+ pg,
14
+ id: contentId,
15
+ table: 'site.contents',
16
+ data,
17
+ uid,
18
+ });
19
+ return result;
20
+ }
21
+
22
+ const result = await dataInsert({
23
+ pg,
24
+ table: 'site.contents',
25
+ data: { ...data || {}, content_type_id: id },
26
+ uid,
27
+ }).then(el => el.rows?.[0]);
28
+ return result;
29
29
  }
@@ -1,56 +1,56 @@
1
- import { existsSync, readFileSync } from 'node:fs';
2
-
3
- import { getMeta } from '@opengis/fastify-table/utils.js';
4
-
5
- const inputTypes = existsSync('input-types.json') ? JSON.parse(readFileSync('input-types.json') || '{}') : {};
6
-
7
- const types = {
8
- number: 'numeric',
9
- datetime: 'timestamp without time zone',
10
- boolean: 'boolean',
11
- date: 'date',
12
- array: 'text[]',
13
- FileList: 'text[]',
14
- };
15
-
16
- export default async function updateCustomContentTable({ pg, id, columns = [] }) {
17
- if (!pg?.pk || !id) return null;
18
-
19
- const { table_name: tablename, dbcolumns } = await pg.query(
20
- `select table_name, columns as dbcolumns from site.content_types where content_type_id = $1`,
21
- [id],
22
- ).then(el => el.rows?.[0] || {});
23
-
24
- if (!tablename) return null;
25
-
26
- const droppedColumns = (dbcolumns || []).filter(el => columns.findIndex(col => col.name === el.name) === -1);
27
- // console.log(droppedColumns);
28
-
29
- const q = `create table if not exists data."${tablename}"
30
- ( id text not null default next_id() PRIMARY KEY );
31
- ${droppedColumns.map(col => `
32
- alter table data."${tablename}" add column if not exists "${col.name}" text;
33
- alter table data."${tablename}" alter column "${col.name}" drop not null;`).join('\n')}
34
- ${columns?.filter?.(col => col.name)?.map?.(col => `
35
- alter table data."${tablename}" add column if not exists "${col.name}" ${types[col.type] || inputTypes[col.type] || 'text'} ${col.required ? ' not null ' : ''};
36
- alter table data."${tablename}" alter column "${col.name}" ${col.required ? ' set ' : ' drop '} not null;
37
- alter table data."${tablename}" alter column "${col.name}" set default ${col.default ? col.default : 'null'};
38
- comment on column data."${tablename}"."${col.name}" is ${col.label ? `'${col.label.replace(/'/g, "''")}'` : 'null'} `
39
- ).join(';') || ''}`;
40
- await pg.query(q);
41
- await pg.query(`alter table data."${tablename}" add column if not exists created_at timestamp with time zone not null default now()`);
42
- await pg.query(`alter table data."${tablename}" add column if not exists updated_at timestamp with time zone not null default now()`);
43
- await pg.query(`alter table data."${tablename}" add column if not exists created_by text`);
44
- await pg.query(`alter table data."${tablename}" add column if not exists updated_by text`);
45
- await pg.query(`alter table data."${tablename}" drop constraint if exists c${id}_slug_unique;alter table data."${tablename}" add constraint c${id}_slug_unique UNIQUE (slug);`);
46
-
47
- // force meta columns refresh to avoid crud skips
48
- getMeta({ pg, table: `data.${tablename}` }, true);
49
-
50
- if (pg?.pk && pg.tlist && !pg.tlist.find(el => el === `data.${tablename}`)) {
51
- pg.pk[`data.${tablename}`] = 'id';
52
- pg.tlist?.push?.(`data.${tablename}`);
53
- }
54
-
55
- return null;
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+
3
+ import { getMeta } from '@opengis/fastify-table/utils.js';
4
+
5
+ const inputTypes = existsSync('input-types.json') ? JSON.parse(readFileSync('input-types.json') || '{}') : {};
6
+
7
+ const types = {
8
+ number: 'numeric',
9
+ datetime: 'timestamp without time zone',
10
+ boolean: 'boolean',
11
+ date: 'date',
12
+ array: 'text[]',
13
+ FileList: 'text[]',
14
+ };
15
+
16
+ export default async function updateCustomContentTable({ pg, id, columns = [] }) {
17
+ if (!pg?.pk || !id) return null;
18
+
19
+ const { table_name: tablename, dbcolumns } = await pg.query(
20
+ `select table_name, columns as dbcolumns from site.content_types where content_type_id = $1`,
21
+ [id],
22
+ ).then(el => el.rows?.[0] || {});
23
+
24
+ if (!tablename) return null;
25
+
26
+ const droppedColumns = (dbcolumns || []).filter(el => columns.findIndex(col => col.name === el.name) === -1);
27
+ // console.log(droppedColumns);
28
+
29
+ const q = `create table if not exists data."${tablename}"
30
+ ( id text not null default next_id() PRIMARY KEY );
31
+ ${droppedColumns.map(col => `
32
+ alter table data."${tablename}" add column if not exists "${col.name}" text;
33
+ alter table data."${tablename}" alter column "${col.name}" drop not null;`).join('\n')}
34
+ ${columns?.filter?.(col => col.name)?.map?.(col => `
35
+ alter table data."${tablename}" add column if not exists "${col.name}" ${types[col.type] || inputTypes[col.type] || 'text'} ${col.required ? ' not null ' : ''};
36
+ alter table data."${tablename}" alter column "${col.name}" ${col.required ? ' set ' : ' drop '} not null;
37
+ alter table data."${tablename}" alter column "${col.name}" set default ${col.default ? col.default : 'null'};
38
+ comment on column data."${tablename}"."${col.name}" is ${col.label ? `'${col.label.replace(/'/g, "''")}'` : 'null'} `
39
+ ).join(';') || ''}`;
40
+ await pg.query(q);
41
+ await pg.query(`alter table data."${tablename}" add column if not exists created_at timestamp with time zone not null default now()`);
42
+ await pg.query(`alter table data."${tablename}" add column if not exists updated_at timestamp with time zone not null default now()`);
43
+ await pg.query(`alter table data."${tablename}" add column if not exists created_by text`);
44
+ await pg.query(`alter table data."${tablename}" add column if not exists updated_by text`);
45
+ await pg.query(`alter table data."${tablename}" drop constraint if exists c${id}_slug_unique;alter table data."${tablename}" add constraint c${id}_slug_unique UNIQUE (slug);`);
46
+
47
+ // force meta columns refresh to avoid crud skips
48
+ getMeta({ pg, table: `data.${tablename}` }, true);
49
+
50
+ if (pg?.pk && pg.tlist && !pg.tlist.find(el => el === `data.${tablename}`)) {
51
+ pg.pk[`data.${tablename}`] = 'id';
52
+ pg.tlist?.push?.(`data.${tablename}`);
53
+ }
54
+
55
+ return null;
56
56
  }
@@ -1,25 +1,25 @@
1
- export default async function feedbackList(req, reply) {
2
- const { pg, user = {}, query } = req;
3
- const { uid } = user;
4
- if (!uid) return reply.code(401).send({ message: 'Not authorized', status: 401 });
5
-
6
- const { page = 1, limit = 16 } = query;
7
- const offset = limit * (page - 1) || 0;
8
-
9
- const { total, filtered } = await pg.one(`select count(*)::int as total from site.feedback`)
10
-
11
- const { rows = [] } = await pg.query(`
12
- select
13
- row_number() over(order by created_at desc) as num,
14
- subscription_id,
15
- email,
16
- created_at,
17
- enabled
18
- from site.subscription
19
- order by created_at desc
20
- limit ${limit} offset ${offset}
21
- `);
22
-
23
- const status = rows?.length ? 200 : 204;
24
- return reply.code(status).send({ message: { total, filtered, rows }, status })
1
+ export default async function feedbackList(req, reply) {
2
+ const { pg, user = {}, query } = req;
3
+ const { uid } = user;
4
+ if (!uid) return reply.code(401).send({ message: 'Not authorized', status: 401 });
5
+
6
+ const { page = 1, limit = 16 } = query;
7
+ const offset = limit * (page - 1) || 0;
8
+
9
+ const { total, filtered } = await pg.one(`select count(*)::int as total from site.feedback`)
10
+
11
+ const { rows = [] } = await pg.query(`
12
+ select
13
+ row_number() over(order by created_at desc) as num,
14
+ subscription_id,
15
+ email,
16
+ created_at,
17
+ enabled
18
+ from site.subscription
19
+ order by created_at desc
20
+ limit ${limit} offset ${offset}
21
+ `);
22
+
23
+ const status = rows?.length ? 200 : 204;
24
+ return reply.code(status).send({ message: { total, filtered, rows }, status })
25
25
  }
@@ -1,49 +1,49 @@
1
- import { pgClients, dataInsert, config } from '@opengis/fastify-table/utils.js';
2
-
3
- const refererAllow = config?.refererAllow || ['127.0.0.1', 'localhost', 'softpro.ua', 'opengis.info'];
4
-
5
- function checkReferer(headers) {
6
- if (config?.local) return true;
7
- if (!headers?.referer) return false;
8
- return refererAllow.some(allowed => headers.referer.includes(allowed));
9
- }
10
-
11
- export default async function feedback({ body = {}, pg = pgClients.client, headers, params }, reply) {
12
- reply
13
- .header('Access-Control-Allow-Origin', '*')
14
- .header('Access-Control-Allow-Methods', 'POST, OPTIONS')
15
- .header('Access-Control-Allow-Headers', 'Content-Type');
16
-
17
- if (!checkReferer(headers)) {
18
- return reply.status(403).send('access restricted');
19
- }
20
-
21
- if ((params?.type !== 'vacancy') && (!body?.message_text || !body?.email)) {
22
- return reply.status(400).send('message_text and email is required');
23
- }
24
-
25
- if ((params?.type === 'vacancy') && !body?.user_name) {
26
- return reply.status(400).send('user_name is required');
27
- }
28
-
29
- const allowedFields = ['title', 'message_text', 'email', 'phone', 'user_name', 'resume_link', 'linkedin_link', 'portfolio_link', 'telegram_link'];
30
- const data = Object.fromEntries(
31
- Object.entries(body).filter(([key]) => allowedFields.includes(key))
32
- );
33
- data["feedback_type"] = params?.type;
34
-
35
- const { rows } = await dataInsert({
36
- table: 'site.feedback',
37
- data: {
38
- ...data,
39
- headers
40
- },
41
- pg
42
- });
43
-
44
- if (!rows?.length) {
45
- return reply.status(500).send('insert error');
46
- }
47
-
48
- return reply.status(200).send('Звернення додано успішно');
1
+ import { pgClients, dataInsert, config } from '@opengis/fastify-table/utils.js';
2
+
3
+ const refererAllow = config?.refererAllow || ['127.0.0.1', 'localhost', 'softpro.ua', 'opengis.info'];
4
+
5
+ function checkReferer(headers) {
6
+ if (config?.local) return true;
7
+ if (!headers?.referer) return false;
8
+ return refererAllow.some(allowed => headers.referer.includes(allowed));
9
+ }
10
+
11
+ export default async function feedback({ body = {}, pg = pgClients.client, headers, params }, reply) {
12
+ reply
13
+ .header('Access-Control-Allow-Origin', '*')
14
+ .header('Access-Control-Allow-Methods', 'POST, OPTIONS')
15
+ .header('Access-Control-Allow-Headers', 'Content-Type');
16
+
17
+ if (!checkReferer(headers)) {
18
+ return reply.status(403).send('access restricted');
19
+ }
20
+
21
+ if ((params?.type !== 'vacancy') && (!body?.message_text || !body?.email)) {
22
+ return reply.status(400).send('message_text and email is required');
23
+ }
24
+
25
+ if ((params?.type === 'vacancy') && !body?.user_name) {
26
+ return reply.status(400).send('user_name is required');
27
+ }
28
+
29
+ const allowedFields = ['title', 'message_text', 'email', 'phone', 'user_name', 'resume_link', 'linkedin_link', 'portfolio_link', 'telegram_link'];
30
+ const data = Object.fromEntries(
31
+ Object.entries(body).filter(([key]) => allowedFields.includes(key))
32
+ );
33
+ data["feedback_type"] = params?.type;
34
+
35
+ const { rows } = await dataInsert({
36
+ table: 'site.feedback',
37
+ data: {
38
+ ...data,
39
+ headers
40
+ },
41
+ pg
42
+ });
43
+
44
+ if (!rows?.length) {
45
+ return reply.status(500).send('insert error');
46
+ }
47
+
48
+ return reply.status(200).send('Звернення додано успішно');
49
49
  }