@opengis/fastify-table 1.0.31 → 1.0.32
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/.eslintrc.cjs +42 -42
- package/Changelog.md +101 -101
- package/README.md +26 -26
- package/config.js +11 -11
- package/crud/controllers/deleteCrud.js +14 -14
- package/crud/controllers/insert.js +29 -29
- package/crud/controllers/update.js +31 -31
- package/crud/controllers/utils/checkXSS.js +45 -45
- package/crud/controllers/utils/xssInjection.js +72 -72
- package/crud/funcs/dataDelete.js +15 -15
- package/crud/funcs/dataInsert.js +24 -24
- package/crud/funcs/dataUpdate.js +24 -24
- package/crud/funcs/getOpt.js +10 -10
- package/crud/funcs/getToken.js +27 -27
- package/crud/funcs/isFileExists.js +13 -13
- package/crud/funcs/setOpt.js +16 -16
- package/crud/funcs/setToken.js +53 -53
- package/crud/index.js +29 -29
- package/helper.js +28 -28
- package/index.js +39 -41
- package/notification/controllers/userNotifications.js +19 -19
- package/notification/funcs/addNotification.js +8 -8
- package/notification/index.js +19 -19
- package/package.json +22 -22
- package/pg/funcs/getPG.js +29 -29
- package/pg/index.js +1 -1
- package/pg/pgClients.js +20 -20
- package/policy/funcs/checkPolicy.js +74 -74
- package/policy/funcs/sqlInjection.js +33 -33
- package/policy/index.js +14 -14
- package/redis/client.js +8 -8
- package/redis/funcs/getRedis.js +23 -23
- package/redis/funcs/redisClients.js +2 -2
- package/redis/index.js +19 -19
- package/server/migrations/crm.sql +95 -79
- package/server/migrations/log.sql +41 -41
- package/server/templates/form/test.dataset.form.json +411 -411
- package/server/templates/select/test.storage.data.json +2 -2
- package/server/templates/table/test.dataset.table.json +24 -24
- package/server.js +14 -14
- package/table/controllers/data.js +57 -57
- package/table/controllers/filter.js +32 -32
- package/table/controllers/form.js +10 -10
- package/table/controllers/search.js +41 -41
- package/table/controllers/suggest.js +60 -60
- package/table/controllers/utils/getSelect.js +20 -20
- package/table/controllers/utils/getSelectMeta.js +66 -66
- package/table/controllers/utils/getTemplate.js +28 -28
- package/table/funcs/getFilterSQL/index.js +75 -75
- package/table/funcs/getFilterSQL/util/formatValue.js +142 -142
- package/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/table/funcs/getFilterSQL/util/getFilterQuery.js +73 -73
- package/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
- package/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/table/funcs/metaFormat/getSelectVal.js +20 -20
- package/table/funcs/metaFormat/index.js +26 -26
- package/table/index.js +42 -42
- package/test/api/crud.test.js +56 -56
- package/test/api/crud.xss.test.js +71 -71
- package/test/api/notification.test.js +37 -37
- package/test/api/table.test.js +57 -57
- package/test/api/widget.test.js +114 -114
- package/test/config.example +18 -18
- package/test/funcs/crud.test.js +76 -76
- package/test/funcs/notification.test.js +31 -31
- package/test/funcs/pg.test.js +34 -34
- package/test/funcs/redis.test.js +19 -19
- package/test/templates/cls/test.json +9 -9
- package/test/templates/form/cp_building.form.json +32 -32
- package/test/templates/select/account_id.json +3 -3
- package/test/templates/select/storage.data.json +2 -2
- package/test/templates/table/gis.dataset.table.json +20 -20
- package/widget/controllers/utils/historyFormat.js +76 -76
- package/widget/controllers/utils/obj2db.js +13 -13
- package/widget/controllers/widget.del.js +41 -41
- package/widget/controllers/widget.get.js +77 -77
- package/widget/controllers/widget.set.js +64 -64
- package/widget/index.js +29 -29
- package/dblist/controllers/createItem.js +0 -32
- package/dblist/controllers/deleteItem.js +0 -14
- package/dblist/controllers/readItems.js +0 -10
- package/dblist/controllers/setItem.js +0 -21
- package/dblist/controllers/updateItem.js +0 -25
- package/dblist/controllers/utils/checkItem.js +0 -24
- package/dblist/controllers/utils/formatData.js +0 -7
- package/dblist/index.js +0 -49
- package/test/api/dblist.test.js +0 -59
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Дістає CRM дані для vue хешує ідентифікатори, підтягує селекти
|
|
3
|
-
*
|
|
4
|
-
* @method DELETE
|
|
5
|
-
* @summary CRM дані для обраного віджета.
|
|
6
|
-
* @priority 2
|
|
7
|
-
* @tag table
|
|
8
|
-
* @type api
|
|
9
|
-
* @requires setTokenById
|
|
10
|
-
* @requires getSelect
|
|
11
|
-
* @param {String} id Ідентифікатор для хешування
|
|
12
|
-
* @param {Any} sql Використовується для повернення sql запиту
|
|
13
|
-
* @param {String} type Тип для хешування даних
|
|
14
|
-
* @errors 400, 500
|
|
15
|
-
* @returns {Number} status Номер помилки
|
|
16
|
-
* @returns {String|Object} error Опис помилки
|
|
17
|
-
* @returns {String|Object} message Повідомлення про успішне виконання або об'єкт з параметрами
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
export default async function widgetDel({
|
|
21
|
-
pg, params = {}, session = {},
|
|
22
|
-
}) {
|
|
23
|
-
const { user = {} } = session.passport || {};
|
|
24
|
-
if (!user.uid) return { error: 'access restricted', status: 403 };
|
|
25
|
-
const { type, objectid, id } = params;
|
|
26
|
-
if (!['comment', 'checklist', 'file'].includes(type)) return { error: 'type not valid', status: 401 };
|
|
27
|
-
if (!objectid) return { error: 'id required', status: 400 };
|
|
28
|
-
|
|
29
|
-
const sql = {
|
|
30
|
-
comment: 'delete from crm.communications where entity_id=$1 and uid=$2 and communication_id=$3',
|
|
31
|
-
checklist: 'delete from crm.checklists where entity_id=$1 and uid=$2 and checklist_id=$3',
|
|
32
|
-
file: 'update crm.files set file_status=3 where entity_id=$1 and uid=$2 and file_id=$3',
|
|
33
|
-
};
|
|
34
|
-
try {
|
|
35
|
-
await pg.query(sql[type], [objectid, user.uid, id]);
|
|
36
|
-
return { data: { id }, user: { uid: user.uid, name: user.user_name } };
|
|
37
|
-
}
|
|
38
|
-
catch (err) {
|
|
39
|
-
return { message: err.toString(), status: 500 };
|
|
40
|
-
}
|
|
41
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Дістає CRM дані для vue хешує ідентифікатори, підтягує селекти
|
|
3
|
+
*
|
|
4
|
+
* @method DELETE
|
|
5
|
+
* @summary CRM дані для обраного віджета.
|
|
6
|
+
* @priority 2
|
|
7
|
+
* @tag table
|
|
8
|
+
* @type api
|
|
9
|
+
* @requires setTokenById
|
|
10
|
+
* @requires getSelect
|
|
11
|
+
* @param {String} id Ідентифікатор для хешування
|
|
12
|
+
* @param {Any} sql Використовується для повернення sql запиту
|
|
13
|
+
* @param {String} type Тип для хешування даних
|
|
14
|
+
* @errors 400, 500
|
|
15
|
+
* @returns {Number} status Номер помилки
|
|
16
|
+
* @returns {String|Object} error Опис помилки
|
|
17
|
+
* @returns {String|Object} message Повідомлення про успішне виконання або об'єкт з параметрами
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export default async function widgetDel({
|
|
21
|
+
pg, params = {}, session = {},
|
|
22
|
+
}) {
|
|
23
|
+
const { user = {} } = session.passport || {};
|
|
24
|
+
if (!user.uid) return { error: 'access restricted', status: 403 };
|
|
25
|
+
const { type, objectid, id } = params;
|
|
26
|
+
if (!['comment', 'checklist', 'file'].includes(type)) return { error: 'type not valid', status: 401 };
|
|
27
|
+
if (!objectid) return { error: 'id required', status: 400 };
|
|
28
|
+
|
|
29
|
+
const sql = {
|
|
30
|
+
comment: 'delete from crm.communications where entity_id=$1 and uid=$2 and communication_id=$3',
|
|
31
|
+
checklist: 'delete from crm.checklists where entity_id=$1 and uid=$2 and checklist_id=$3',
|
|
32
|
+
file: 'update crm.files set file_status=3 where entity_id=$1 and uid=$2 and file_id=$3',
|
|
33
|
+
};
|
|
34
|
+
try {
|
|
35
|
+
await pg.query(sql[type], [objectid, user.uid, id]);
|
|
36
|
+
return { data: { id }, user: { uid: user.uid, name: user.user_name } };
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
return { message: err.toString(), status: 500 };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
import getToken from '../../crud/funcs/getToken.js';
|
|
2
|
-
import getMeta from '../../pg/funcs/getMeta.js';
|
|
3
|
-
import historyFormat from './utils/historyFormat.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Дістає CRM для widget
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export default async function widgetGet({
|
|
11
|
-
pg, session = {}, params = {}, query = {},
|
|
12
|
-
}) {
|
|
13
|
-
const { user = {} } = session.passport || {};
|
|
14
|
-
console.log('test');
|
|
15
|
-
|
|
16
|
-
const param = user?.uid ? await getToken({
|
|
17
|
-
token: params.objectid, mode: 'w', uid: user.uid,
|
|
18
|
-
}) : null;
|
|
19
|
-
|
|
20
|
-
const objectid = param ? JSON.parse(param)?.id : params.objectid;
|
|
21
|
-
|
|
22
|
-
if (!['comment', 'history', 'file', 'checklist'].includes(params.type)) return { error: 'param type not valid', status: 400 };
|
|
23
|
-
if (!objectid) return { error: 'id required', status: 400 };
|
|
24
|
-
|
|
25
|
-
const sql = {
|
|
26
|
-
comment: `select communication_id, entity_id, body, subject, c.cdate, c.uid,
|
|
27
|
-
coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, avatar
|
|
28
|
-
from crm.communications c left join admin.users u on u.uid=c.uid where entity_id=$1 order by cdate desc`,
|
|
29
|
-
|
|
30
|
-
history: `SELECT table_change_id, entity_id, entity_type, change_key, change_date, json_old, json_new, date_old,
|
|
31
|
-
date_new, number_old, number_new, bool_old, bool_new, text_old,
|
|
32
|
-
text_new, uid, cdate FROM log.table_changes where entity_id=$1 order by cdate desc, change_key limit 100`,
|
|
33
|
-
|
|
34
|
-
checklist: `SELECT checklist_id, entity_id, subject, is_done, done_date, c.uid, c.cdate, coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username,
|
|
35
|
-
avatar FROM crm.checklists c left join admin.users u on u.uid=c.uid where entity_id=$1 order by cdate desc`,
|
|
36
|
-
|
|
37
|
-
file: `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
|
|
38
|
-
coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, isverified,
|
|
39
|
-
avatar, c.uid as author, file_status FROM crm.files c left join admin.users u on u.uid=c.uid
|
|
40
|
-
where entity_id=$1 and file_status<>3 order by cdate desc`,
|
|
41
|
-
|
|
42
|
-
};
|
|
43
|
-
try {
|
|
44
|
-
/* data */
|
|
45
|
-
const time = [Date.now()];
|
|
46
|
-
const { rows } = await pg.query(sql[params.type], [objectid]);
|
|
47
|
-
time.push(Date.now());
|
|
48
|
-
|
|
49
|
-
/* Object info */
|
|
50
|
-
const { tableName } = await pg.one('select entity_type as "tableName" from log.table_changes where entity_id=$1 limit 1', [objectid]);
|
|
51
|
-
const { pk } = await getMeta({ table: tableName });
|
|
52
|
-
|
|
53
|
-
const q = `select coalesce(b.user_name,'')||coalesce(' '||b.sur_name,'') as author, a.cdate, a.editor_date from ${tableName} a
|
|
54
|
-
left join admin.users b on a.uid=b.uid where a.${pk}=$1 limit 1`;
|
|
55
|
-
const data = pk ? await pg.one(q, [objectid]) : {};
|
|
56
|
-
|
|
57
|
-
if (query.debug && user?.user_type === 'admin') {
|
|
58
|
-
return {
|
|
59
|
-
sql, type: params.type, q, id: objectid, data,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
time.push(Date.now());
|
|
64
|
-
return {
|
|
65
|
-
time: { data: time[1] - time[0], format: time[2] - time[1] },
|
|
66
|
-
rows: params.type === 'history' ? await historyFormat(rows, tableName, pg) : rows,
|
|
67
|
-
user: { uid: user?.uid, name: user?.user_name },
|
|
68
|
-
data: { author: data?.author, cdate: data?.cdate, edate: data?.editor_date },
|
|
69
|
-
objectid: params.objectid,
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
catch (err) {
|
|
73
|
-
// 'history', 'file', 'checklist'
|
|
74
|
-
/* 'document', 'image' - Макс на клиенте */
|
|
75
|
-
return { message: err.toString(), status: 500 };
|
|
76
|
-
}
|
|
77
|
-
}
|
|
1
|
+
import getToken from '../../crud/funcs/getToken.js';
|
|
2
|
+
import getMeta from '../../pg/funcs/getMeta.js';
|
|
3
|
+
import historyFormat from './utils/historyFormat.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Дістає CRM для widget
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export default async function widgetGet({
|
|
11
|
+
pg, session = {}, params = {}, query = {},
|
|
12
|
+
}) {
|
|
13
|
+
const { user = {} } = session.passport || {};
|
|
14
|
+
console.log('test');
|
|
15
|
+
|
|
16
|
+
const param = user?.uid ? await getToken({
|
|
17
|
+
token: params.objectid, mode: 'w', uid: user.uid,
|
|
18
|
+
}) : null;
|
|
19
|
+
|
|
20
|
+
const objectid = param ? JSON.parse(param)?.id : params.objectid;
|
|
21
|
+
|
|
22
|
+
if (!['comment', 'history', 'file', 'checklist'].includes(params.type)) return { error: 'param type not valid', status: 400 };
|
|
23
|
+
if (!objectid) return { error: 'id required', status: 400 };
|
|
24
|
+
|
|
25
|
+
const sql = {
|
|
26
|
+
comment: `select communication_id, entity_id, body, subject, c.cdate, c.uid,
|
|
27
|
+
coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, avatar
|
|
28
|
+
from crm.communications c left join admin.users u on u.uid=c.uid where entity_id=$1 order by cdate desc`,
|
|
29
|
+
|
|
30
|
+
history: `SELECT table_change_id, entity_id, entity_type, change_key, change_date, json_old, json_new, date_old,
|
|
31
|
+
date_new, number_old, number_new, bool_old, bool_new, text_old,
|
|
32
|
+
text_new, uid, cdate FROM log.table_changes where entity_id=$1 order by cdate desc, change_key limit 100`,
|
|
33
|
+
|
|
34
|
+
checklist: `SELECT checklist_id, entity_id, subject, is_done, done_date, c.uid, c.cdate, coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username,
|
|
35
|
+
avatar FROM crm.checklists c left join admin.users u on u.uid=c.uid where entity_id=$1 order by cdate desc`,
|
|
36
|
+
|
|
37
|
+
file: `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
|
|
38
|
+
coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, isverified,
|
|
39
|
+
avatar, c.uid as author, file_status FROM crm.files c left join admin.users u on u.uid=c.uid
|
|
40
|
+
where entity_id=$1 and file_status<>3 order by cdate desc`,
|
|
41
|
+
|
|
42
|
+
};
|
|
43
|
+
try {
|
|
44
|
+
/* data */
|
|
45
|
+
const time = [Date.now()];
|
|
46
|
+
const { rows } = await pg.query(sql[params.type], [objectid]);
|
|
47
|
+
time.push(Date.now());
|
|
48
|
+
|
|
49
|
+
/* Object info */
|
|
50
|
+
const { tableName } = await pg.one('select entity_type as "tableName" from log.table_changes where entity_id=$1 limit 1', [objectid]);
|
|
51
|
+
const { pk } = await getMeta({ table: tableName });
|
|
52
|
+
|
|
53
|
+
const q = `select coalesce(b.user_name,'')||coalesce(' '||b.sur_name,'') as author, a.cdate, a.editor_date from ${tableName} a
|
|
54
|
+
left join admin.users b on a.uid=b.uid where a.${pk}=$1 limit 1`;
|
|
55
|
+
const data = pk ? await pg.one(q, [objectid]) : {};
|
|
56
|
+
|
|
57
|
+
if (query.debug && user?.user_type === 'admin') {
|
|
58
|
+
return {
|
|
59
|
+
sql, type: params.type, q, id: objectid, data,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
time.push(Date.now());
|
|
64
|
+
return {
|
|
65
|
+
time: { data: time[1] - time[0], format: time[2] - time[1] },
|
|
66
|
+
rows: params.type === 'history' ? await historyFormat(rows, tableName, pg) : rows,
|
|
67
|
+
user: { uid: user?.uid, name: user?.user_name },
|
|
68
|
+
data: { author: data?.author, cdate: data?.cdate, edate: data?.editor_date },
|
|
69
|
+
objectid: params.objectid,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
// 'history', 'file', 'checklist'
|
|
74
|
+
/* 'document', 'image' - Макс на клиенте */
|
|
75
|
+
return { message: err.toString(), status: 500 };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
|
|
3
|
-
import getMeta from '../../pg/funcs/getMeta.js';
|
|
4
|
-
import dataInsert from '../../crud/funcs/dataInsert.js';
|
|
5
|
-
import dataUpdate from '../../crud/funcs/dataUpdate.js';
|
|
6
|
-
|
|
7
|
-
const tableList = {
|
|
8
|
-
comment: 'crm.communications',
|
|
9
|
-
checklist: 'crm.checklists',
|
|
10
|
-
};
|
|
11
|
-
const pkList = {
|
|
12
|
-
comment: 'communication_id',
|
|
13
|
-
checklist: 'checklist_id',
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export default async function widgetSet(req) {
|
|
17
|
-
const {
|
|
18
|
-
pg, params = {}, session = {}, body = {}, funcs, log,
|
|
19
|
-
} = req;
|
|
20
|
-
const { user = {} } = session.passport || {};
|
|
21
|
-
const { type, id, objectid } = params;
|
|
22
|
-
if (!['comment', 'checklist', 'file'].includes(type)) return { error: 'param type not valid', status: 400 };
|
|
23
|
-
if (!objectid) return { error: 'id required', status: 400 };
|
|
24
|
-
|
|
25
|
-
const table = tableList[type];
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
if (type === 'file') {
|
|
29
|
-
const file = await funcs.uploadMultiPart(req);
|
|
30
|
-
const extName = path.extname(file.filepath).slice(1).toLowerCase();
|
|
31
|
-
|
|
32
|
-
const data = {
|
|
33
|
-
uploaded_name: file?.originalFilename?.toLocaleLowerCase()?.replace(/'/g, '\'\''),
|
|
34
|
-
file_path: file?.relativeFilepath?.replace(/\\/g, '/'),
|
|
35
|
-
ext: extName,
|
|
36
|
-
size: file?.size,
|
|
37
|
-
file_status: 1,
|
|
38
|
-
uid: user?.uid || 1,
|
|
39
|
-
entity_id: objectid,
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const { rows = [] } = await dataInsert({ table: 'crm.files', data });
|
|
43
|
-
return {
|
|
44
|
-
rowCount: 1, data: 'ok', command: 'UPLOAD', id: rows[0]?.file_id, entity_id: rows[0]?.entity_id,
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
const { pk } = await getMeta({ pg, table });
|
|
48
|
-
if (!pk) return { message: 'table not found', status: 404 };
|
|
49
|
-
|
|
50
|
-
const data = { ...body, uid: user?.uid, entity_id: objectid };
|
|
51
|
-
|
|
52
|
-
const result = id
|
|
53
|
-
? await dataUpdate({ table, data, id })
|
|
54
|
-
: await dataInsert({ table, data });
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
rowCount: result.rowCount, data: 'ok', command: result.command, id: result.rows?.[0]?.[pkList[type]] || result?.[pkList[type]],
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
catch (err) {
|
|
61
|
-
log.error('widget/upload', { error: err.toString(), params });
|
|
62
|
-
return { error: err.toString(), status: 500 };
|
|
63
|
-
}
|
|
64
|
-
}
|
|
1
|
+
import path from 'path';
|
|
2
|
+
|
|
3
|
+
import getMeta from '../../pg/funcs/getMeta.js';
|
|
4
|
+
import dataInsert from '../../crud/funcs/dataInsert.js';
|
|
5
|
+
import dataUpdate from '../../crud/funcs/dataUpdate.js';
|
|
6
|
+
|
|
7
|
+
const tableList = {
|
|
8
|
+
comment: 'crm.communications',
|
|
9
|
+
checklist: 'crm.checklists',
|
|
10
|
+
};
|
|
11
|
+
const pkList = {
|
|
12
|
+
comment: 'communication_id',
|
|
13
|
+
checklist: 'checklist_id',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default async function widgetSet(req) {
|
|
17
|
+
const {
|
|
18
|
+
pg, params = {}, session = {}, body = {}, funcs, log,
|
|
19
|
+
} = req;
|
|
20
|
+
const { user = {} } = session.passport || {};
|
|
21
|
+
const { type, id, objectid } = params;
|
|
22
|
+
if (!['comment', 'checklist', 'file'].includes(type)) return { error: 'param type not valid', status: 400 };
|
|
23
|
+
if (!objectid) return { error: 'id required', status: 400 };
|
|
24
|
+
|
|
25
|
+
const table = tableList[type];
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
if (type === 'file') {
|
|
29
|
+
const file = await funcs.uploadMultiPart(req);
|
|
30
|
+
const extName = path.extname(file.filepath).slice(1).toLowerCase();
|
|
31
|
+
|
|
32
|
+
const data = {
|
|
33
|
+
uploaded_name: file?.originalFilename?.toLocaleLowerCase()?.replace(/'/g, '\'\''),
|
|
34
|
+
file_path: file?.relativeFilepath?.replace(/\\/g, '/'),
|
|
35
|
+
ext: extName,
|
|
36
|
+
size: file?.size,
|
|
37
|
+
file_status: 1,
|
|
38
|
+
uid: user?.uid || 1,
|
|
39
|
+
entity_id: objectid,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const { rows = [] } = await dataInsert({ table: 'crm.files', data });
|
|
43
|
+
return {
|
|
44
|
+
rowCount: 1, data: 'ok', command: 'UPLOAD', id: rows[0]?.file_id, entity_id: rows[0]?.entity_id,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const { pk } = await getMeta({ pg, table });
|
|
48
|
+
if (!pk) return { message: 'table not found', status: 404 };
|
|
49
|
+
|
|
50
|
+
const data = { ...body, uid: user?.uid, entity_id: objectid };
|
|
51
|
+
|
|
52
|
+
const result = id
|
|
53
|
+
? await dataUpdate({ table, data, id })
|
|
54
|
+
: await dataInsert({ table, data });
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
rowCount: result.rowCount, data: 'ok', command: result.command, id: result.rows?.[0]?.[pkList[type]] || result?.[pkList[type]],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
log.error('widget/upload', { error: err.toString(), params });
|
|
62
|
+
return { error: err.toString(), status: 500 };
|
|
63
|
+
}
|
|
64
|
+
}
|
package/widget/index.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import widgetDel from './controllers/widget.del.js';
|
|
2
|
-
import widgetSet from './controllers/widget.set.js';
|
|
3
|
-
import widgetGet from './controllers/widget.get.js';
|
|
4
|
-
|
|
5
|
-
async function route(fastify, opt) {
|
|
6
|
-
const prefix = opt.prefix || '/api';
|
|
7
|
-
fastify.route({
|
|
8
|
-
method: 'DELETE',
|
|
9
|
-
url: `${prefix}/widget/:type/:objectid/:id`,
|
|
10
|
-
schema: {},
|
|
11
|
-
handler: widgetDel,
|
|
12
|
-
});
|
|
13
|
-
fastify.route({
|
|
14
|
-
method: 'POST',
|
|
15
|
-
path: `${prefix}/widget/:type/:objectid/:id?`,
|
|
16
|
-
schema: {},
|
|
17
|
-
handler: widgetSet,
|
|
18
|
-
});
|
|
19
|
-
fastify.route({
|
|
20
|
-
method: 'GET',
|
|
21
|
-
path: `${prefix}/widget/:type/:objectid`,
|
|
22
|
-
config: {
|
|
23
|
-
policy: ['public'],
|
|
24
|
-
},
|
|
25
|
-
schema: {},
|
|
26
|
-
handler: widgetGet,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
export default route;
|
|
1
|
+
import widgetDel from './controllers/widget.del.js';
|
|
2
|
+
import widgetSet from './controllers/widget.set.js';
|
|
3
|
+
import widgetGet from './controllers/widget.get.js';
|
|
4
|
+
|
|
5
|
+
async function route(fastify, opt) {
|
|
6
|
+
const prefix = opt.prefix || '/api';
|
|
7
|
+
fastify.route({
|
|
8
|
+
method: 'DELETE',
|
|
9
|
+
url: `${prefix}/widget/:type/:objectid/:id`,
|
|
10
|
+
schema: {},
|
|
11
|
+
handler: widgetDel,
|
|
12
|
+
});
|
|
13
|
+
fastify.route({
|
|
14
|
+
method: 'POST',
|
|
15
|
+
path: `${prefix}/widget/:type/:objectid/:id?`,
|
|
16
|
+
schema: {},
|
|
17
|
+
handler: widgetSet,
|
|
18
|
+
});
|
|
19
|
+
fastify.route({
|
|
20
|
+
method: 'GET',
|
|
21
|
+
path: `${prefix}/widget/:type/:objectid`,
|
|
22
|
+
config: {
|
|
23
|
+
policy: ['public'],
|
|
24
|
+
},
|
|
25
|
+
schema: {},
|
|
26
|
+
handler: widgetGet,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export default route;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from 'crypto';
|
|
2
|
-
import { existsSync } from 'fs';
|
|
3
|
-
import { readFile, writeFile } from 'fs/promises';
|
|
4
|
-
|
|
5
|
-
import checkItem from './utils/checkItem.js';
|
|
6
|
-
import formatData from './utils/formatData.js';
|
|
7
|
-
|
|
8
|
-
export default async function insertItem({ body = {} }) {
|
|
9
|
-
const check = checkItem(body);
|
|
10
|
-
if (check?.error) return check;
|
|
11
|
-
|
|
12
|
-
const data = existsSync('dblist.json') ? JSON.parse(await readFile('dblist.json') || '[]') : [];
|
|
13
|
-
const {
|
|
14
|
-
key, name, database, host, port = 5432,
|
|
15
|
-
} = body;
|
|
16
|
-
|
|
17
|
-
const keys = data.map((el) => el.key);
|
|
18
|
-
if (keys.includes(key)) {
|
|
19
|
-
return { error: 'key duplication not allowed', status: 400 };
|
|
20
|
-
}
|
|
21
|
-
data.push({
|
|
22
|
-
id: randomUUID(),
|
|
23
|
-
key,
|
|
24
|
-
name,
|
|
25
|
-
database,
|
|
26
|
-
host,
|
|
27
|
-
port,
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
await writeFile('dblist.json', JSON.stringify(data));
|
|
31
|
-
return { data: formatData(data) };
|
|
32
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'fs';
|
|
2
|
-
import { readFile, writeFile } from 'fs/promises';
|
|
3
|
-
|
|
4
|
-
import formatData from './utils/formatData.js';
|
|
5
|
-
|
|
6
|
-
export default async function deleteItem({ params = {} }) {
|
|
7
|
-
const { id } = params;
|
|
8
|
-
if (!id) return { error: 'not enough params', status: 400 };
|
|
9
|
-
if (!existsSync('dblist.json')) return { data: [] };
|
|
10
|
-
const data = JSON.parse(await readFile('dblist.json') || '[]');
|
|
11
|
-
|
|
12
|
-
await writeFile('dblist.json', JSON.stringify(data.filter((el) => el.key !== id && el.id !== id)));
|
|
13
|
-
return { data: formatData(data.filter((el) => el.key !== id && el.id !== id)) };
|
|
14
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'fs';
|
|
2
|
-
import { readFile } from 'fs/promises';
|
|
3
|
-
|
|
4
|
-
import formatData from './utils/formatData.js';
|
|
5
|
-
|
|
6
|
-
export default async function readItemList(req) {
|
|
7
|
-
const filedata = existsSync('dblist.json') ? JSON.parse(await readFile('dblist.json') || '[]') : [];
|
|
8
|
-
const data = formatData(filedata);
|
|
9
|
-
return { current: req.session?.currentDB || data[0]?.id, data };
|
|
10
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'fs';
|
|
2
|
-
import { readFile } from 'fs/promises';
|
|
3
|
-
|
|
4
|
-
export default async function setItem({
|
|
5
|
-
params = {}, session = {},
|
|
6
|
-
}) {
|
|
7
|
-
const { id } = params;
|
|
8
|
-
if (!id) return { error: 'not enough params', status: 400 };
|
|
9
|
-
|
|
10
|
-
if (!existsSync('dblist.json')) {
|
|
11
|
-
return { error: 'nothing to update: 1', status: 400 };
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const data = JSON.parse(await readFile('dblist.json') || '[]');
|
|
15
|
-
const current = data.find((el) => [el.id, el.key].includes(id));
|
|
16
|
-
if (!current?.database) {
|
|
17
|
-
return { error: 'invalid param id', status: 400 };
|
|
18
|
-
}
|
|
19
|
-
Object.assign(session, { currentDB: id });
|
|
20
|
-
return { current: id };
|
|
21
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'fs';
|
|
2
|
-
import { readFile, writeFile } from 'fs/promises';
|
|
3
|
-
|
|
4
|
-
import checkItem from './utils/checkItem.js';
|
|
5
|
-
import formatData from './utils/formatData.js';
|
|
6
|
-
|
|
7
|
-
export default async function updateItem({ body = {}, params = {} }) {
|
|
8
|
-
const { id } = params;
|
|
9
|
-
if (!id) return { error: 'not enough params', status: 400 };
|
|
10
|
-
|
|
11
|
-
if (!existsSync('dblist.json')) {
|
|
12
|
-
return { error: 'nothing to update: 1', status: 400 };
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const check = checkItem(body);
|
|
16
|
-
if (check?.error) return check;
|
|
17
|
-
|
|
18
|
-
const data = JSON.parse(await readFile('dblist.json') || '[]');
|
|
19
|
-
data.filter((el) => [el.id, el.key].includes(id))?.forEach((el) => {
|
|
20
|
-
['key', 'name', 'database', 'host', 'port'].forEach((key) => Object.assign(el, { [key]: body[key] }));
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
await writeFile('dblist.json', JSON.stringify(data));
|
|
24
|
-
return { data: formatData(data) };
|
|
25
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export default function validateItem(item) {
|
|
2
|
-
if (!item || typeof item !== 'object' || Array.isArray(item)) {
|
|
3
|
-
return { error: 'param data is invalid: 1', status: 400 };
|
|
4
|
-
}
|
|
5
|
-
const {
|
|
6
|
-
key, name, database, host, port,
|
|
7
|
-
} = item;
|
|
8
|
-
if (!key) {
|
|
9
|
-
return { error: 'param body.key is required', status: 400 };
|
|
10
|
-
}
|
|
11
|
-
if (!name) {
|
|
12
|
-
return { error: 'param body.name is required', status: 400 };
|
|
13
|
-
}
|
|
14
|
-
if (!database) {
|
|
15
|
-
return { error: 'param body.database is required', status: 400 };
|
|
16
|
-
}
|
|
17
|
-
if (!host) {
|
|
18
|
-
return { error: 'param body.host is required', status: 400 };
|
|
19
|
-
}
|
|
20
|
-
if (!port) {
|
|
21
|
-
return { error: 'param body.port is required', status: 400 };
|
|
22
|
-
}
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
const showKeys = ['id', 'key', 'name'];
|
|
2
|
-
|
|
3
|
-
export default function formatData(data = []) {
|
|
4
|
-
return data?.length
|
|
5
|
-
? data.map((el) => Object.keys(el).filter((key) => showKeys.includes(key)).reduce((acc, curr) => Object.assign(acc, { [curr]: el[curr] }), {}))
|
|
6
|
-
: [];
|
|
7
|
-
}
|
package/dblist/index.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import createItem from './controllers/createItem.js';
|
|
2
|
-
import readItemList from './controllers/readItems.js';
|
|
3
|
-
import updateItem from './controllers/updateItem.js';
|
|
4
|
-
import deleteItem from './controllers/deleteItem.js';
|
|
5
|
-
import setItem from './controllers/setItem.js';
|
|
6
|
-
|
|
7
|
-
export default async function plugin(fastify, config = {}) {
|
|
8
|
-
const prefix = config.prefix || '/api';
|
|
9
|
-
fastify.route({
|
|
10
|
-
method: 'POST',
|
|
11
|
-
url: `${prefix}/list`,
|
|
12
|
-
config: {
|
|
13
|
-
policy: [],
|
|
14
|
-
},
|
|
15
|
-
handler: createItem,
|
|
16
|
-
});
|
|
17
|
-
fastify.route({
|
|
18
|
-
method: 'GET',
|
|
19
|
-
url: `${prefix}/list`,
|
|
20
|
-
config: {
|
|
21
|
-
policy: [],
|
|
22
|
-
},
|
|
23
|
-
handler: readItemList,
|
|
24
|
-
});
|
|
25
|
-
fastify.route({
|
|
26
|
-
method: 'PUT',
|
|
27
|
-
url: `${prefix}/list/:id`,
|
|
28
|
-
config: {
|
|
29
|
-
policy: [],
|
|
30
|
-
},
|
|
31
|
-
handler: updateItem,
|
|
32
|
-
});
|
|
33
|
-
fastify.route({
|
|
34
|
-
method: 'DELETE',
|
|
35
|
-
url: `${prefix}/list/:id`,
|
|
36
|
-
config: {
|
|
37
|
-
policy: [],
|
|
38
|
-
},
|
|
39
|
-
handler: deleteItem,
|
|
40
|
-
});
|
|
41
|
-
fastify.route({
|
|
42
|
-
method: 'GET',
|
|
43
|
-
url: `${prefix}/list/:id`,
|
|
44
|
-
config: {
|
|
45
|
-
policy: [],
|
|
46
|
-
},
|
|
47
|
-
handler: setItem,
|
|
48
|
-
});
|
|
49
|
-
}
|