@opengis/fastify-table 1.0.30 → 1.0.31

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 CHANGED
@@ -1,5 +1,9 @@
1
1
  # fastify-table
2
2
 
3
+ ## 1.0.31 - 20.05.2024
4
+
5
+ - widget db structure refactor
6
+
3
7
  ## 1.0.30 - 17.05.2024
4
8
 
5
9
  - code optimization
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.30",
3
+ "version": "1.0.31",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -1,57 +1,79 @@
1
- -- crm.notification
2
- CREATE TABLE IF NOT EXISTS crm.notification();
3
- ALTER TABLE crm.notification DROP CONSTRAINT IF EXISTS notification_key;
4
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS notification_id text NOT NULL DEFAULT admin.next_id();
5
-
6
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS notification_user_id text;
7
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS notification_type text DEFAULT 'notify'::text;
8
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS notification_status text DEFAULT 'not sent'::text;
9
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS title text;
10
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS body text;
11
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS link text;
12
-
13
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS uid text;
14
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
15
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS editor_id text;
16
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
17
- ALTER TABLE crm.notification ADD CONSTRAINT notification_key PRIMARY KEY (notification_id);
18
-
19
- -- crm.file
20
- CREATE TABLE IF NOT EXISTS crm.file();
21
- ALTER TABLE crm.file DROP CONSTRAINT IF EXISTS crm_file_pkey;
22
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS file_id text NOT NULL DEFAULT admin.next_id();
23
-
24
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS upload_name text;
25
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS object_id text;
26
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS file_type text;
27
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS file_status numeric;
28
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS doc_type text;
29
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS title text;
30
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS size numeric;
31
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS ext text;
32
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS tags text[];
33
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS file_path text;
34
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS ismain boolean default false;
35
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS isverified boolean default false;
36
-
37
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS uid text;
38
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
39
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS editor_id text;
40
- ALTER TABLE crm.file ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
41
- ALTER TABLE crm.file ADD CONSTRAINT crm_file_pkey PRIMARY KEY (file_id);
42
-
43
- -- crm.comment
44
- CREATE TABLE IF NOT EXISTS crm.comment();
45
- ALTER TABLE crm.comment DROP CONSTRAINT IF EXISTS comment_key;
46
- ALTER TABLE crm.comment ADD COLUMN IF NOT EXISTS comment_id text NOT NULL DEFAULT admin.next_id();
47
-
48
- ALTER TABLE crm.comment ADD COLUMN IF NOT EXISTS object_id text;
49
- ALTER TABLE crm.comment ADD COLUMN IF NOT EXISTS title text;
50
- ALTER TABLE crm.comment ADD COLUMN IF NOT EXISTS body text;
51
- ALTER TABLE crm.comment ADD COLUMN IF NOT EXISTS mark numeric;
52
-
53
- ALTER TABLE crm.comment ADD COLUMN IF NOT EXISTS uid text;
54
- ALTER TABLE crm.comment ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
55
- ALTER TABLE crm.comment ADD COLUMN IF NOT EXISTS editor_id text;
56
- ALTER TABLE crm.comment ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
57
- ALTER TABLE crm.comment ADD CONSTRAINT comment_key PRIMARY KEY (comment_id);
1
+ -- crm.notifications
2
+ CREATE TABLE IF NOT EXISTS crm.notifications();
3
+ ALTER TABLE crm.notifications DROP CONSTRAINT IF EXISTS notification_key;
4
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS notification_id text NOT NULL DEFAULT admin.next_id();
5
+
6
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS notification_user_id text;
7
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS notification_type text DEFAULT 'notify'::text;
8
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS notification_status text DEFAULT 'not sent'::text;
9
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS title text;
10
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS body text;
11
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS link text;
12
+
13
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS uid text;
14
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
15
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS editor_id text;
16
+ ALTER TABLE crm.notifications ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
17
+ ALTER TABLE crm.notifications ADD CONSTRAINT notifications_key PRIMARY KEY (notification_id);
18
+
19
+ -- crm.files
20
+ CREATE TABLE IF NOT EXISTS crm.files();
21
+ ALTER TABLE crm.files DROP CONSTRAINT IF EXISTS crm_file_pkey;
22
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS file_id text NOT NULL DEFAULT admin.next_id();
23
+
24
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS uploaded_name text;
25
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS entity_id text; -- object_id
26
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS entity_type text; -- table_name
27
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS file_type text;
28
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS file_status numeric;
29
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS doc_type text;
30
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS title text;
31
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS size numeric;
32
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS ext text;
33
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS tags text[];
34
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS file_path text;
35
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS ismain boolean default false;
36
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS isverified boolean default false;
37
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS uid text;
38
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
39
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS editor_id text;
40
+ ALTER TABLE crm.files ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
41
+ ALTER TABLE crm.files ADD CONSTRAINT crm_files_pkey PRIMARY KEY (file_id);
42
+
43
+ -- crm.communications
44
+ CREATE TABLE IF NOT EXISTS crm.communications();
45
+ ALTER TABLE crm.communications DROP CONSTRAINT IF EXISTS communication_key;
46
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS communication_id text NOT NULL DEFAULT admin.next_id();
47
+
48
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS entity_id text; -- object_id
49
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS entity_type text;
50
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS sender text;
51
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS channel text;
52
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS participant text;
53
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS communication_date date;
54
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS expiration_date date;
55
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS announcement text;
56
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS subject text; -- title
57
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS body text;
58
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS mark numeric;
59
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS uid text;
60
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
61
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS editor_id text;
62
+ ALTER TABLE crm.communications ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
63
+ ALTER TABLE crm.communications ADD CONSTRAINT communications_key PRIMARY KEY (communication_id);
64
+
65
+ CREATE TABLE IF NOT EXISTS crm.checklists();
66
+ ALTER TABLE crm.checklists DROP CONSTRAINT IF EXISTS checklists_pkey;
67
+ ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS checklist_id text NOT NULL DEFAULT admin.next_id();
68
+
69
+ ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS entity_id text; -- object_id
70
+ ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS subject text; -- title
71
+
72
+ alter table crm.checklists add column is_done boolean default false;
73
+ alter table crm.checklists add column done_date date;
74
+
75
+ ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS uid text;
76
+ ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
77
+ ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS editor_id text;
78
+ ALTER TABLE crm.checklists ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
79
+ ALTER TABLE crm.checklists ADD CONSTRAINT checklists_pkey PRIMARY KEY (checklist_id);
@@ -1,28 +1,28 @@
1
- CREATE TABLE IF NOT EXISTS log.table_change();
2
- ALTER TABLE log.table_change DROP CONSTRAINT IF EXISTS log_table_change_pkey;
3
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS table_change_id text NOT NULL DEFAULT admin.next_id();
1
+ CREATE TABLE IF NOT EXISTS log.table_changes();
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 admin.next_id();
4
4
 
5
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS table_name text;
6
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS object_id text;
7
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS change_type text;
8
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS change_key text;
9
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS change_date date;
10
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS json_old json;
11
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS json_new json;
12
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS date_old timestamp without time zone;
13
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS date_new timestamp without time zone;
14
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS number_old numeric;
15
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS number_new numeric;
16
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS bool_old boolean;
17
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS bool_new boolean;
18
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS text_old text;
19
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS text_new text;
5
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS entity_type text;
6
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS entity_id text; -- object_id
7
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS change_type text;
8
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS change_key text;
9
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS change_date date;
10
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS json_old json;
11
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS json_new json;
12
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS date_old timestamp without time zone;
13
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS date_new timestamp without time zone;
14
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS number_old numeric;
15
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS number_new numeric;
16
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS bool_old boolean;
17
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS bool_new boolean;
18
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS text_old text;
19
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS text_new text;
20
20
 
21
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS uid text;
22
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
23
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS editor_id text;
24
- ALTER TABLE log.table_change ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
25
- ALTER TABLE log.table_change ADD CONSTRAINT log_table_change_pkey PRIMARY KEY (table_change_id);
21
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS uid text;
22
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
23
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS editor_id text;
24
+ ALTER TABLE log.table_changes ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
25
+ ALTER TABLE log.table_changes ADD CONSTRAINT log_table_changes_pkey PRIMARY KEY (table_change_id);
26
26
 
27
27
  CREATE TABLE IF NOT EXISTS log.user_auth();
28
28
  ALTER TABLE log.user_auth DROP CONSTRAINT IF EXISTS log_user_auth_pkey;
@@ -15,25 +15,100 @@ import pgClients from '../../pg/pgClients.js';
15
15
  test('widget api', async (t) => {
16
16
  await build(t);
17
17
  const pg = pgClients.client;
18
- await t.test('POST /widget/:type/:objectid', async () => {
18
+
19
+ // comment
20
+ let commentId;
21
+ await t.test('POST /widget/comment/:objectid', async () => {
19
22
  const body = {};
20
- const rep = await widgetSet({
23
+ const resp = await widgetSet({
21
24
  pg, params: { type: 'comment', objectid: '1' }, session, body,
22
25
  });
23
- assert.ok(rep.data);
26
+ commentId = resp.id;
27
+ assert.ok(commentId, 'comment widget insert fail');
24
28
  });
25
-
26
- let resp;
27
- await t.test('GET /widget/:type/:objectid', async () => {
28
- resp = await widgetGet({
29
+ await t.test('GET /widget/comment/:objectid', async () => {
30
+ const resp = await widgetGet({
29
31
  pg, session, params: { type: 'comment', objectid: '1' },
30
32
  });
31
- assert.ok(resp.rows.length > 0, 'widget data get fail');
33
+ assert.ok(resp.rows?.length, 'comment widget get fail');
34
+ });
35
+ await t.test('DELETE /widget/comment/:objectid', async () => {
36
+ const resp = await widgetDel({
37
+ pg, session, params: { type: 'comment', objectid: '1', id: commentId },
38
+ });
39
+ assert.ok(resp?.data?.id === commentId, 'comment widget delete fail');
40
+ });
41
+
42
+ // checklist
43
+ let checklistId;
44
+ await t.test('POST /widget/checklist/:objectid', async () => {
45
+ const body = {};
46
+ const resp = await widgetSet({
47
+ pg, params: { type: 'checklist', objectid: '1' }, session, body,
48
+ });
49
+ checklistId = resp.id;
50
+ assert.ok(checklistId, 'checklist widget insert fail');
51
+ });
52
+ await t.test('GET /widget/checklist/:objectid', async () => {
53
+ const resp = await widgetGet({
54
+ pg, session, params: { type: 'checklist', objectid: '1' },
55
+ });
56
+ assert.ok(resp.rows?.length, 'checklist widget data get fail');
57
+ });
58
+ await t.test('DELETE /widget/checklist/:objectid', async () => {
59
+ const resp = await widgetDel({
60
+ pg, session, params: { type: 'checklist', objectid: '1', id: checklistId },
61
+ });
62
+ assert.ok(resp?.data?.id === checklistId, 'checklist widget delete fail');
32
63
  });
33
- await t.test('DELETE /widget/:type/:objectid', async () => {
34
- resp = await widgetDel({
35
- pg, session, params: { type: 'comment', objectid: '1', id: resp.rows?.find((row) => row)?.comment_id },
64
+
65
+ // before GET log request
66
+ await t.test('FAKE POST /widget/history/:objectid', async() => {
67
+ const res = await pg.query(`insert into log.table_changes(uid, entity_id, entity_type)
68
+ select $1, '1', 'admin.users'`, [config.testUser?.uid || '1']);
69
+ // console.log(res.rowCount);
70
+ });
71
+
72
+ // history
73
+ await t.test('GET /widget/history/:objectid', async () => {
74
+ const resp = await widgetGet({
75
+ pg, session, params: { type: 'history', objectid: '1' },
76
+ });
77
+ assert.ok(resp.rows?.length > 0, 'history widget data get fail');
78
+ });
79
+
80
+ // file
81
+ let fileId;
82
+ /* @opengis/fastify-file dependency - funcs */
83
+ /* await t.test('POST /widget/file/:objectid', async () => {
84
+ const body = {};
85
+ const resp = await widgetSet({
86
+ pg, params: { type: 'file', objectid: '1' }, session, body,
87
+ });
88
+ fileId = resp.id;
89
+ assert.ok(fileId, 'file widget insert fail');
90
+ }); */
91
+ await t.test('GET /widget/file/:objectid', async () => {
92
+ const { rows } = await pg.query(`insert into crm.files(entity_id, entity_type, uid, file_status)
93
+ select '1', 'admin.users', $1, 1 returning *`, [config.testUser?.uid || '1']);
94
+ fileId = rows?.[0]?.file_id; // substitude for POST request
95
+ const resp = await widgetGet({
96
+ pg, session, params: { type: 'file', objectid: '1' },
97
+ });
98
+ assert.ok(resp.rows?.length, 'file widget data get fail');
99
+ });
100
+ await t.test('DELETE /widget/file/:objectid', async () => {
101
+ const resp = await widgetDel({
102
+ pg, session, params: { type: 'file', objectid: '1', id: fileId },
36
103
  });
37
- assert.ok(resp.data.rowCount === 1, 'widget data delete fail');
104
+ assert.ok(resp?.data?.id === fileId, 'file widget delete fail');
105
+ });
106
+
107
+ await t.test('clean after test', async() => {
108
+ const res1 = await pg.query(`delete from crm.communications where entity_id=$1`, ['1']);
109
+ const res2 = await pg.query(`delete from crm.checklists where entity_id=$1`, ['1']);
110
+ const res3 = await pg.query(`delete from crm.files where entity_id=$1`, ['1']);
111
+ const res4 = await pg.query(`delete from log.table_changes where entity_id=$1`, ['1']);
112
+ pg.end();
38
113
  });
39
114
  });
@@ -23,16 +23,17 @@ export default async function widgetDel({
23
23
  const { user = {} } = session.passport || {};
24
24
  if (!user.uid) return { error: 'access restricted', status: 403 };
25
25
  const { type, objectid, id } = params;
26
- if (!['comment', 'checklist', 'link', 'time'].includes(type)) return { error: 'type not valid required', status: 401 };
26
+ if (!['comment', 'checklist', 'file'].includes(type)) return { error: 'type not valid', status: 401 };
27
27
  if (!objectid) return { error: 'id required', status: 400 };
28
28
 
29
29
  const sql = {
30
- comment: 'delete from crm.comment where object_id=$1 and uid=$2 and comment_id=$3',
31
- checklist: 'delete from crm.checklist where object_id=$1 and uid=$2 and checklist_id=$3 ',
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',
32
33
  };
33
34
  try {
34
- const { rows, rowCount } = await pg.query(sql[type], [objectid, user.uid, id]);
35
- return { data: { rows, rowCount }, user: { uid: user.uid, name: user.user_name } };
35
+ await pg.query(sql[type], [objectid, user.uid, id]);
36
+ return { data: { id }, user: { uid: user.uid, name: user.user_name } };
36
37
  }
37
38
  catch (err) {
38
39
  return { message: err.toString(), status: 500 };
@@ -11,6 +11,7 @@ export default async function widgetGet({
11
11
  pg, session = {}, params = {}, query = {},
12
12
  }) {
13
13
  const { user = {} } = session.passport || {};
14
+ console.log('test');
14
15
 
15
16
  const param = user?.uid ? await getToken({
16
17
  token: params.objectid, mode: 'w', uid: user.uid,
@@ -22,20 +23,21 @@ export default async function widgetGet({
22
23
  if (!objectid) return { error: 'id required', status: 400 };
23
24
 
24
25
  const sql = {
25
- comment: `select body,c.cdate, comment_id,object_id,c.uid, coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username,avatar
26
- from crm.comment c left join admin.users u on u.uid=c.uid where object_id=$1 order by cdate desc`,
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`,
27
29
 
28
- history: `SELECT object_id, table_name, change_key, change_date, json_old, json_new, date_old,
30
+ history: `SELECT table_change_id, entity_id, entity_type, change_key, change_date, json_old, json_new, date_old,
29
31
  date_new, number_old, number_new, bool_old, bool_new, text_old,
30
- text_new, uid, cdate FROM log.table_change where object_id=$1 order by cdate desc, change_key limit 100`,
32
+ text_new, uid, cdate FROM log.table_changes where entity_id=$1 order by cdate desc, change_key limit 100`,
31
33
 
32
- checklist: `SELECT checklist_id, title, is_done, done_date, c.uid, c.cdate, coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username,
33
- avatar FROM crm.checklist c left join admin.users u on u.uid=c.uid where object_id=$1 order by cdate desc`,
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`,
34
36
 
35
- file: `SELECT file_id, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
37
+ file: `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
36
38
  coalesce(user_name,' ')||' '||coalesce(sur_name,'') as username, isverified,
37
- avatar, c.uid as author, file_status FROM crm.file c left join admin.users u on u.uid=c.uid
38
- where object_id=$1 and file_status<>3 order by cdate desc`,
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`,
39
41
 
40
42
  };
41
43
  try {
@@ -45,10 +47,11 @@ export default async function widgetGet({
45
47
  time.push(Date.now());
46
48
 
47
49
  /* Object info */
48
- const { tableName } = await pg.one('select table_name as "tableName" from log.table_change where object_id=$1 limit 1', [objectid]);
50
+ const { tableName } = await pg.one('select entity_type as "tableName" from log.table_changes where entity_id=$1 limit 1', [objectid]);
49
51
  const { pk } = await getMeta({ table: tableName });
50
52
 
51
- const q = `select coalesce(b.user_name,'')||coalesce(' '||b.sur_name,'') as author, a.cdate, a.editor_date from ${tableName} a left join admin.users b on a.uid=b.uid where a.${pk}=$1 limit 1`;
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`;
52
55
  const data = pk ? await pg.one(q, [objectid]) : {};
53
56
 
54
57
  if (query.debug && user?.user_type === 'admin') {
@@ -1,13 +1,15 @@
1
1
  import path from 'path';
2
2
 
3
3
  import getMeta from '../../pg/funcs/getMeta.js';
4
+ import dataInsert from '../../crud/funcs/dataInsert.js';
5
+ import dataUpdate from '../../crud/funcs/dataUpdate.js';
4
6
 
5
7
  const tableList = {
6
- comment: 'crm.comment',
7
- checklist: 'crm.checklist',
8
+ comment: 'crm.communications',
9
+ checklist: 'crm.checklists',
8
10
  };
9
11
  const pkList = {
10
- comment: 'comment_id',
12
+ comment: 'communication_id',
11
13
  checklist: 'checklist_id',
12
14
  };
13
15
 
@@ -33,29 +35,30 @@ export default async function widgetSet(req) {
33
35
  ext: extName,
34
36
  size: file?.size,
35
37
  file_status: 1,
36
- uid: req.session?.passport?.user?.uid || 1,
37
- object_id: objectid,
38
+ uid: user?.uid || 1,
39
+ entity_id: objectid,
38
40
  };
39
41
 
40
- const { rows = [] } = await funcs.dataInsert({ table: 'crm.file', data });
42
+ const { rows = [] } = await dataInsert({ table: 'crm.files', data });
41
43
  return {
42
- rowCount: 1, data: 'ok', command: 'UPLOAD', id: rows[0]?.file_id, objectid: rows[0]?.object_id,
44
+ rowCount: 1, data: 'ok', command: 'UPLOAD', id: rows[0]?.file_id, entity_id: rows[0]?.entity_id,
43
45
  };
44
46
  }
45
47
  const { pk } = await getMeta({ pg, table });
46
48
  if (!pk) return { message: 'table not found', status: 404 };
47
49
 
48
- const data = { ...body, uid: user?.uid, object_id: objectid };
50
+ const data = { ...body, uid: user?.uid, entity_id: objectid };
49
51
 
50
52
  const result = id
51
- ? await funcs.dataUpdate({ table, data, id })
52
- : await funcs.dataInsert({ table, data });
53
+ ? await dataUpdate({ table, data, id })
54
+ : await dataInsert({ table, data });
53
55
 
54
56
  return {
55
57
  rowCount: result.rowCount, data: 'ok', command: result.command, id: result.rows?.[0]?.[pkList[type]] || result?.[pkList[type]],
56
58
  };
57
59
  }
58
60
  catch (err) {
61
+ log.error('widget/upload', { error: err.toString(), params });
59
62
  return { error: err.toString(), status: 500 };
60
63
  }
61
64
  }
@@ -1,14 +0,0 @@
1
- CREATE TABLE IF NOT EXISTS crm.notification();
2
- ALTER TABLE crm.notification DROP CONSTRAINT IF EXISTS notification_key;
3
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS notification_id text NOT NULL DEFAULT admin.next_id();
4
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS notification_user_id text;
5
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS notification_type text DEFAULT 'notify'::text;
6
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS notification_status text DEFAULT 'not sent'::text;
7
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS title text;
8
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS body text;
9
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS link text;
10
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS uid text;
11
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT (now())::timestamp without time zone;
12
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS editor_id text;
13
- ALTER TABLE crm.notification ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
14
- ALTER TABLE crm.notification ADD CONSTRAINT notification_key PRIMARY KEY (notification_id);
@@ -1,39 +0,0 @@
1
- import { test } from 'node:test';
2
- import assert from 'node:assert';
3
-
4
- import build from '../helper.js';
5
- import config from '../config.js';
6
-
7
- const session = { passport: { user: { uid: config.testUser?.uid || '1' } } };
8
-
9
- import widgetGet from '../widget/controllers/widget.get.js';
10
- import widgetSet from '../widget/controllers/widget.set.js';
11
- import widgetDel from '../widget/controllers/widget.del.js';
12
-
13
- import pgClients from '../pg/pgClients.js';
14
-
15
- test('api && funcs notification', async (t) => {
16
- const app = await build(t);
17
- const pg = pgClients.client;
18
- await t.test('POST /widget/:type/:objectid', async () => {
19
- const body = {};
20
- const rep = await widgetSet({
21
- pg, params: { type: 'comment', objectid: '1' }, session, body,
22
- });
23
- assert.ok(rep.data);
24
- });
25
-
26
- let resp;
27
- await t.test('GET /widget/:type/:objectid', async () => {
28
- resp = await widgetGet({
29
- pg, session, params: { type: 'comment', objectid: '1' },
30
- });
31
- assert.ok(resp.rows.length > 0, 'widget data get fail');
32
- });
33
- await t.test('DELETE /widget/:type/:objectid', async () => {
34
- resp = await widgetDel({
35
- pg, session, params: { type: 'comment', objectid: '1', id: resp.rows?.find((row) => row)?.comment_id },
36
- });
37
- assert.ok(resp.data.rowCount === 1, 'widget data delete fail');
38
- });
39
- });