@opengis/fastify-table 1.0.42 → 1.0.43
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/Changelog.md +4 -0
- package/index.js +6 -0
- package/package.json +1 -1
- package/policy/funcs/checkPolicy.js +8 -0
- package/server/migrations/0.sql +13 -0
- package/server/migrations/crm.sql +7 -34
- package/server/migrations/log.sql +3 -3
- package/server/migrations/setting.sql +27 -0
- package/settings/controllers/settings.get.js +24 -0
- package/settings/controllers/settings.post.js +14 -0
- package/settings/funcs/getSettings.js +12 -0
- package/settings/funcs/setSettings.js +29 -0
- package/settings/index.js +22 -0
package/Changelog.md
CHANGED
package/index.js
CHANGED
|
@@ -13,6 +13,7 @@ import notificationPlugin from './notification/index.js';
|
|
|
13
13
|
import widgetPlugin from './widget/index.js';
|
|
14
14
|
import crudPlugin from './crud/index.js';
|
|
15
15
|
import policyPlugin from './policy/index.js';
|
|
16
|
+
import settingsPlugin from './settings/index.js';
|
|
16
17
|
|
|
17
18
|
import pgClients from './pg/pgClients.js';
|
|
18
19
|
|
|
@@ -59,6 +60,10 @@ async function plugin(fastify, opt) {
|
|
|
59
60
|
if (!fastify.funcs) {
|
|
60
61
|
fastify.addHook('onRequest', async (req) => {
|
|
61
62
|
req.funcs = fastify;
|
|
63
|
+
if (!req.user && req.session?.passport?.user) {
|
|
64
|
+
const { user } = req.session?.passport || {};
|
|
65
|
+
req.user = user;
|
|
66
|
+
}
|
|
62
67
|
});
|
|
63
68
|
// fastify.decorateRequest('funcs', fastify);
|
|
64
69
|
}
|
|
@@ -70,6 +75,7 @@ async function plugin(fastify, opt) {
|
|
|
70
75
|
crudPlugin(fastify, opt);
|
|
71
76
|
notificationPlugin(fastify, opt);
|
|
72
77
|
widgetPlugin(fastify, opt);
|
|
78
|
+
settingsPlugin(fastify, opt);
|
|
73
79
|
}
|
|
74
80
|
export default fp(plugin);
|
|
75
81
|
// export { rclient };
|
package/package.json
CHANGED
|
@@ -22,6 +22,14 @@ export default function checkPolicy(req) {
|
|
|
22
22
|
const isServer = process.argv[2];
|
|
23
23
|
const { policy = [] } = req.routeOptions?.config || {};
|
|
24
24
|
|
|
25
|
+
/*= == 0.Check superadmin access === */
|
|
26
|
+
if (policy.includes('superadmin') && user?.user_type !== 'superadmin') {
|
|
27
|
+
log.warn({
|
|
28
|
+
name: 'api/superadmin', params, query, body: JSON.stringify(req?.body || {}).substring(30), message: 'access restricted: 0',
|
|
29
|
+
});
|
|
30
|
+
return { message: 'access restricted: 0', status: 403 };
|
|
31
|
+
}
|
|
32
|
+
|
|
25
33
|
/*= == 1.File injection === */
|
|
26
34
|
if (JSON.stringify(params || {})?.includes('../') || JSON.stringify(query || {})?.includes('../') || path?.includes('../')) {
|
|
27
35
|
log.warn({
|
|
@@ -1,37 +1,8 @@
|
|
|
1
|
-
-- next_id()
|
|
2
|
-
CREATE SEQUENCE crm.cls_clsid_seq
|
|
3
|
-
INCREMENT 1
|
|
4
|
-
MINVALUE 1
|
|
5
|
-
MAXVALUE 9223372036854775807
|
|
6
|
-
START 128142470
|
|
7
|
-
CACHE 1;
|
|
8
|
-
CREATE OR REPLACE FUNCTION crm.next_id()
|
|
9
|
-
RETURNS bigint AS
|
|
10
|
-
$BODY$
|
|
11
|
-
DECLARE
|
|
12
|
-
our_epoch bigint := 1314220021721;
|
|
13
|
-
seq_id bigint;
|
|
14
|
-
now_millis bigint;
|
|
15
|
-
shard_id int := 1;
|
|
16
|
-
result bigint;
|
|
17
|
-
BEGIN
|
|
18
|
-
SELECT nextval('crm.cls_clsid_seq') % 1024 INTO seq_id;
|
|
19
|
-
|
|
20
|
-
SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
|
|
21
|
-
result := (now_millis - our_epoch) << 23;
|
|
22
|
-
result := result | (shard_id << 10);
|
|
23
|
-
result := result | (seq_id);
|
|
24
|
-
return result;
|
|
25
|
-
END;
|
|
26
|
-
$BODY$
|
|
27
|
-
LANGUAGE plpgsql VOLATILE
|
|
28
|
-
COST 100;
|
|
29
|
-
|
|
30
1
|
-- crm.notifications
|
|
31
2
|
-- DROP TABLE IF EXISTS crm.notifications;
|
|
32
3
|
CREATE TABLE IF NOT EXISTS crm.notifications();
|
|
33
4
|
ALTER TABLE crm.notifications DROP CONSTRAINT IF EXISTS crm_notifications_pkey;
|
|
34
|
-
ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS notification_id text NOT NULL DEFAULT
|
|
5
|
+
ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS notification_id text NOT NULL DEFAULT next_id();
|
|
35
6
|
|
|
36
7
|
ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS notification_user_id text;
|
|
37
8
|
ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS notification_type text DEFAULT 'notify'::text;
|
|
@@ -51,7 +22,7 @@ ALTER TABLE crm.notifications ADD CONSTRAINT crm_notifications_pkey PRIMARY KEY
|
|
|
51
22
|
-- DROP TABLE IF EXISTS crm.files;
|
|
52
23
|
CREATE TABLE IF NOT EXISTS crm.files();
|
|
53
24
|
ALTER TABLE crm.files DROP CONSTRAINT IF EXISTS crm_files_pkey;
|
|
54
|
-
ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS file_id text NOT NULL DEFAULT
|
|
25
|
+
ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS file_id text NOT NULL DEFAULT next_id();
|
|
55
26
|
|
|
56
27
|
ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS uploaded_name text;
|
|
57
28
|
ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS entity_id text; -- object_id
|
|
@@ -77,7 +48,7 @@ ALTER TABLE crm.files ADD CONSTRAINT crm_files_pkey PRIMARY KEY (file_id);
|
|
|
77
48
|
-- DROP TABLE IF EXISTS crm.communications;
|
|
78
49
|
CREATE TABLE IF NOT EXISTS crm.communications();
|
|
79
50
|
ALTER TABLE crm.communications DROP CONSTRAINT IF EXISTS crm_communications_pkey;
|
|
80
|
-
ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS communication_id text NOT NULL DEFAULT
|
|
51
|
+
ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS communication_id text NOT NULL DEFAULT next_id();
|
|
81
52
|
|
|
82
53
|
ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS entity_id text; -- object_id
|
|
83
54
|
ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS entity_type text; -- table_name
|
|
@@ -104,7 +75,7 @@ ALTER TABLE crm.communications ADD CONSTRAINT crm_communications_pkey PRIMARY KE
|
|
|
104
75
|
-- DROP TABLE IF EXISTS crm.checklists;
|
|
105
76
|
CREATE TABLE IF NOT EXISTS crm.checklists();
|
|
106
77
|
ALTER TABLE crm.checklists DROP CONSTRAINT IF EXISTS crm_checklists_pkey;
|
|
107
|
-
ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS checklist_id text NOT NULL DEFAULT
|
|
78
|
+
ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS checklist_id text NOT NULL DEFAULT next_id();
|
|
108
79
|
|
|
109
80
|
ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS entity_id text; -- object_id
|
|
110
81
|
ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS entity_type text; -- table_name
|
|
@@ -126,7 +97,9 @@ ALTER TABLE crm.checklists ADD CONSTRAINT crm_checklists_pkey PRIMARY KEY (check
|
|
|
126
97
|
-- crm.cls
|
|
127
98
|
-- DROP TABLE IF EXISTS crm.cls;
|
|
128
99
|
CREATE TABLE IF NOT EXISTS crm.cls();
|
|
129
|
-
ALTER TABLE crm.cls
|
|
100
|
+
ALTER TABLE crm.cls DROP CONSTRAINT IF EXISTS crm_cls_pkey;
|
|
101
|
+
ALTER TABLE crm.cls DROP CONSTRAINT IF EXISTS crm_cls_unique;
|
|
102
|
+
ALTER TABLE crm.cls ADD COLUMN IF NOT EXISTS cls_id text NOT NULL DEFAULT next_id();
|
|
130
103
|
|
|
131
104
|
ALTER TABLE crm.cls ADD COLUMN IF NOT EXISTS name text;
|
|
132
105
|
ALTER TABLE crm.cls ADD COLUMN IF NOT EXISTS data text;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
CREATE TABLE IF NOT EXISTS log.table_changes();
|
|
2
2
|
ALTER TABLE log.table_changes DROP CONSTRAINT IF EXISTS log_table_changes_pkey;
|
|
3
|
-
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS table_change_id text NOT NULL DEFAULT
|
|
3
|
+
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS table_change_id text NOT NULL DEFAULT next_id();
|
|
4
4
|
|
|
5
5
|
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS entity_type text;
|
|
6
6
|
ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS entity_id text; -- object_id
|
|
@@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS log.user_auth();
|
|
|
28
28
|
ALTER TABLE log.user_auth DROP CONSTRAINT IF EXISTS log_user_auth_pkey;
|
|
29
29
|
ALTER TABLE log.user_auth DROP CONSTRAINT IF EXISTS log_user_auth_user_id_fkey;
|
|
30
30
|
|
|
31
|
-
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS user_auth_id text NOT NULL DEFAULT
|
|
31
|
+
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS user_auth_id text NOT NULL DEFAULT next_id();
|
|
32
32
|
|
|
33
33
|
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS user_id text;
|
|
34
34
|
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS user_auth_date timestamp without time zone;
|
|
@@ -39,4 +39,4 @@ ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS cdate timestamp without time
|
|
|
39
39
|
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS editor_id text;
|
|
40
40
|
ALTER TABLE log.user_auth ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
|
|
41
41
|
ALTER TABLE log.user_auth ADD CONSTRAINT log_user_auth_pkey PRIMARY KEY (user_auth_id);
|
|
42
|
-
ALTER TABLE log.user_auth ADD CONSTRAINT log_user_auth_user_id_fkey FOREIGN KEY (user_id) REFERENCES admin.users (uid) MATCH SIMPLE;
|
|
42
|
+
-- ALTER TABLE log.user_auth ADD CONSTRAINT log_user_auth_user_id_fkey FOREIGN KEY (user_id) REFERENCES admin.users (uid) MATCH SIMPLE;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
-- DROP TABLE setting.property;
|
|
2
|
+
CREATE TABLE IF NOT EXISTS setting.property();
|
|
3
|
+
ALTER TABLE setting.property DROP CONSTRAINT IF EXISTS setting_property_pkey;
|
|
4
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS property_id text NOT NULL DEFAULT next_id();
|
|
5
|
+
|
|
6
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS property_entity text;
|
|
7
|
+
COMMENT ON COLUMN setting.property.property_entity IS 'Сутність';
|
|
8
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS property_key text;
|
|
9
|
+
COMMENT ON COLUMN setting.property.property_key IS 'Ключ';
|
|
10
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS property_text text;
|
|
11
|
+
COMMENT ON COLUMN setting.property.property_text IS 'Текстове значення налаштування';
|
|
12
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS property_int integer;
|
|
13
|
+
COMMENT ON COLUMN setting.property.property_int IS 'Цілочислове значения';
|
|
14
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS property_json json;
|
|
15
|
+
COMMENT ON COLUMN setting.property.property_json IS 'Значення налаштування';
|
|
16
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS level text;
|
|
17
|
+
COMMENT ON COLUMN setting.property.level IS 'Рівень (user/system)';
|
|
18
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS object_id text;
|
|
19
|
+
COMMENT ON COLUMN setting.property.object_id IS 'ID Об''єкту';
|
|
20
|
+
|
|
21
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS uid text NOT NULL DEFAULT '1'::text;
|
|
22
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS editor_id text;
|
|
23
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
|
|
24
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT now();
|
|
25
|
+
ALTER TABLE setting.property ADD COLUMN IF NOT EXISTS files json;
|
|
26
|
+
|
|
27
|
+
ALTER TABLE setting.property ADD CONSTRAINT setting_property_pkey PRIMARY KEY(property_id);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export default async function getSettingsAPI({
|
|
2
|
+
pg, funcs, params = {},
|
|
3
|
+
}) {
|
|
4
|
+
const { key } = params;
|
|
5
|
+
|
|
6
|
+
if (!pg?.pk?.['setting.property']) {
|
|
7
|
+
return { message: 'table not found', status: 404 };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const res = await funcs.getSettings({ pg, key });
|
|
12
|
+
if (key) {
|
|
13
|
+
if (!res) {
|
|
14
|
+
return { message: `settings not found: ${key}`, status: 404 };
|
|
15
|
+
}
|
|
16
|
+
return { message: res, status: 200 };
|
|
17
|
+
}
|
|
18
|
+
const { rows } = res || { };
|
|
19
|
+
return { message: { total: rows.length || 0, rows }, status: 200 };
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
return { error: err.toString(), status: 500 };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default async function postSettingsAPI({
|
|
2
|
+
pg, funcs, body = {},
|
|
3
|
+
}) {
|
|
4
|
+
const { key, val } = body;
|
|
5
|
+
if (!key || !val) {
|
|
6
|
+
return { message: 'not enough params', status: 400 };
|
|
7
|
+
}
|
|
8
|
+
const res = await funcs.setSettings({
|
|
9
|
+
pg, funcs, key, val,
|
|
10
|
+
});
|
|
11
|
+
if (res?.error) return res;
|
|
12
|
+
|
|
13
|
+
return { message: res, status: 200 };
|
|
14
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export default async function getSettings({ pg, key }) {
|
|
2
|
+
const sql = `select property_id as id, property_entity, property_key, property_text,
|
|
3
|
+
property_int, property_json, level, object_id, uid, cdate, editor_id, editor_date from setting.property
|
|
4
|
+
where ${key ? 'property_key=$1' : '1=1'}`;
|
|
5
|
+
|
|
6
|
+
if (!key) {
|
|
7
|
+
const { rows } = await pg.query(sql);
|
|
8
|
+
return { rows };
|
|
9
|
+
}
|
|
10
|
+
const data = await pg.one(sql, [key]);
|
|
11
|
+
return data;
|
|
12
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const table = 'setting.property';
|
|
2
|
+
|
|
3
|
+
function checkValueType(val) {
|
|
4
|
+
if (val) {
|
|
5
|
+
if (typeof val === 'object') {
|
|
6
|
+
return 'property_json';
|
|
7
|
+
}
|
|
8
|
+
if (typeof val === 'number' || (!/\D/.test(val.toString()) && val.length <= 10)) {
|
|
9
|
+
return 'property_int';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return 'property_text';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default async function setSettings({
|
|
16
|
+
pg, funcs, key, val,
|
|
17
|
+
}) {
|
|
18
|
+
try {
|
|
19
|
+
const columnType = checkValueType(val);
|
|
20
|
+
const data = { property_key: key, [columnType]: val };
|
|
21
|
+
|
|
22
|
+
await pg.query('delete from setting.property where property_key=$1', [key]);
|
|
23
|
+
const { rows } = await funcs.dataInsert({ pg, table, data });
|
|
24
|
+
return { key, val, data: rows };
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
return { error: err.toString(), status: 500 };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import getSettingsAPI from './controllers/settings.get.js';
|
|
2
|
+
import postSettingsAPI from './controllers/settings.post.js';
|
|
3
|
+
import getSettingsFunc from './funcs/getSettings.js';
|
|
4
|
+
import setSettingsFunc from './funcs/setSettings.js';
|
|
5
|
+
|
|
6
|
+
async function plugin(fastify, config = { }) {
|
|
7
|
+
const prefix = config.prefix || '/api';
|
|
8
|
+
fastify.decorate('getSettings', getSettingsFunc);
|
|
9
|
+
fastify.decorate('setSettings', setSettingsFunc);
|
|
10
|
+
fastify.get(`${prefix}/settings/:key?`, {}, getSettingsAPI);
|
|
11
|
+
|
|
12
|
+
fastify.route({
|
|
13
|
+
method: 'POST',
|
|
14
|
+
path: `${prefix}/settings`,
|
|
15
|
+
config: {
|
|
16
|
+
policy: ['superadmin'],
|
|
17
|
+
},
|
|
18
|
+
handler: postSettingsAPI,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default plugin;
|