@tiledesk/tiledesk-server 2.9.31 → 2.9.33
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +13 -0
- package/config/labels/widget.json +15 -0
- package/deploy.sh +1 -1
- package/models/kb_setting.js +53 -1
- package/package.json +3 -3
- package/routes/kb.js +205 -35
- package/routes/project.js +108 -1
- package/routes/request.js +85 -94
- package/services/openaiService.js +6 -2
- package/test/kbRoute.js +319 -37
- package/test/projectRoute.js +41 -0
package/routes/request.js
CHANGED
@@ -884,7 +884,10 @@ router.get('/', function (req, res, next) {
|
|
884
884
|
|
885
885
|
var page = 0;
|
886
886
|
var limit = DEFAULT_LIMIT; // Number of request per page
|
887
|
+
var page = 0;
|
888
|
+
var skip = 0;
|
887
889
|
let statusArray = [];
|
890
|
+
var projectuser = req.projectuser;
|
888
891
|
|
889
892
|
if (req.query.limit) {
|
890
893
|
limit = parseInt(req.query.limit);
|
@@ -897,19 +900,16 @@ router.get('/', function (req, res, next) {
|
|
897
900
|
page = req.query.page;
|
898
901
|
}
|
899
902
|
|
900
|
-
|
901
|
-
winston.debug('REQUEST ROUTE - SKIP PAGE ', skip);
|
903
|
+
skip = page * limit;
|
902
904
|
|
903
|
-
// Default
|
905
|
+
// Default query
|
904
906
|
var query = { "id_project": req.projectid, "status": { $lt: 1000, $ne: 150 }, preflight: false };
|
905
907
|
|
906
|
-
var projectuser = req.projectuser;
|
907
|
-
|
908
908
|
if (req.user instanceof Subscription) {
|
909
|
-
//
|
909
|
+
// All request
|
910
910
|
} else if (projectuser && (projectuser.role == "owner" || projectuser.role == "admin")) {
|
911
|
-
//
|
912
|
-
//
|
911
|
+
// All request
|
912
|
+
// Per uni mostrare solo quelle proprie quindi solo participants
|
913
913
|
if (req.query.mine) {
|
914
914
|
query["$or"] = [{ "snapshot.agents.id_user": req.user.id }, { "participants": req.user.id }];
|
915
915
|
}
|
@@ -919,7 +919,6 @@ router.get('/', function (req, res, next) {
|
|
919
919
|
|
920
920
|
if (req.query.dept_id) {
|
921
921
|
query.department = req.query.dept_id;
|
922
|
-
winston.debug('REQUEST ROUTE - QUERY DEPT ID', query.department);
|
923
922
|
}
|
924
923
|
|
925
924
|
if (req.query.requester_email) {
|
@@ -927,27 +926,13 @@ router.get('/', function (req, res, next) {
|
|
927
926
|
}
|
928
927
|
|
929
928
|
if (req.query.full_text) {
|
930
|
-
winston.debug('req.query.fulltext', req.query.full_text);
|
931
929
|
query.$text = { "$search": req.query.full_text };
|
932
930
|
}
|
933
931
|
|
934
|
-
var history_search = false;
|
935
|
-
|
936
|
-
// if (req.query.status) {
|
937
|
-
// winston.debug('req.query.status', req.query.status);
|
938
|
-
// query.status = req.query.status;
|
939
|
-
|
940
|
-
// if (req.query.status == 1000 || req.query.status == "1000") {
|
941
|
-
// history_search = true;
|
942
|
-
// }
|
943
|
-
// if (req.query.status === "all") {
|
944
|
-
// history_search = true;
|
945
|
-
// delete query.status;
|
946
|
-
// }
|
947
|
-
// }
|
932
|
+
var history_search = false;
|
948
933
|
|
934
|
+
// Multiple status management
|
949
935
|
if (req.query.status) {
|
950
|
-
|
951
936
|
if (req.query.status === 'all') {
|
952
937
|
delete query.status;
|
953
938
|
} else {
|
@@ -961,40 +946,27 @@ router.get('/', function (req, res, next) {
|
|
961
946
|
delete query.status;
|
962
947
|
}
|
963
948
|
}
|
964
|
-
|
965
949
|
if (statusArray.length > 0) {
|
966
950
|
query.status = {
|
967
951
|
$in: statusArray
|
968
952
|
}
|
969
953
|
}
|
970
|
-
|
971
954
|
}
|
972
955
|
|
973
956
|
if (req.query.lead) {
|
974
|
-
winston.debug('req.query.lead', req.query.lead);
|
975
957
|
query.lead = req.query.lead;
|
976
958
|
}
|
977
959
|
|
978
960
|
// USERS & BOTS
|
979
961
|
if (req.query.participant) {
|
980
|
-
winston.debug('req.query.participant', req.query.participant);
|
981
962
|
query.participants = req.query.participant;
|
982
963
|
}
|
983
964
|
|
984
|
-
winston.debug('req.query.hasbot', req.query.hasbot);
|
985
965
|
if (req.query.hasbot != undefined) {
|
986
|
-
winston.debug('req.query.hasbot', req.query.hasbot);
|
987
966
|
query.hasBot = req.query.hasbot;
|
988
967
|
}
|
989
968
|
|
990
|
-
// if (req.query.waiting_time_exists) { //non basta aggiungi anche che nn è null
|
991
|
-
// query.waiting_time = {"$exists": req.query.waiting_time_exists} //{$ne:null}
|
992
|
-
// winston.debug('REQUEST ROUTE - QUERY waiting_time_exists', query.waiting_time_exists);
|
993
|
-
// }
|
994
|
-
|
995
|
-
|
996
969
|
if (req.query.tags) {
|
997
|
-
winston.debug('req.query.tags', req.query.tags);
|
998
970
|
query["tags.tag"] = req.query.tags;
|
999
971
|
}
|
1000
972
|
|
@@ -1026,9 +998,7 @@ router.get('/', function (req, res, next) {
|
|
1026
998
|
//fixato. secondo me qui manca un parentesi tonda per gli or
|
1027
999
|
if (history_search === true && req.project && req.project.profile && ((req.project.profile.type === 'free' && req.project.trialExpired === true) || (req.project.profile.type === 'payment' && req.project.isActiveSubscription === false))) {
|
1028
1000
|
|
1029
|
-
|
1030
1001
|
var startdate = moment().subtract(14, "days").format("YYYY-MM-DD");
|
1031
|
-
|
1032
1002
|
var enddate = moment().format("YYYY-MM-DD");
|
1033
1003
|
|
1034
1004
|
winston.debug('»»» REQUEST ROUTE - startdate ', startdate);
|
@@ -1037,11 +1007,8 @@ router.get('/', function (req, res, next) {
|
|
1037
1007
|
var enddatePlusOneDay = moment(new Date()).add(1, 'days').toDate()
|
1038
1008
|
winston.debug('»»» REQUEST ROUTE - enddate + 1 days: ', enddatePlusOneDay);
|
1039
1009
|
|
1040
|
-
// var enddatePlusOneDay = "2019-09-17T00:00:00.000Z"
|
1041
|
-
|
1042
1010
|
query.createdAt = { $gte: new Date(Date.parse(startdate)).toISOString(), $lte: new Date(enddatePlusOneDay).toISOString() }
|
1043
1011
|
winston.debug('REQUEST ROUTE - QUERY CREATED AT ', query.createdAt);
|
1044
|
-
|
1045
1012
|
}
|
1046
1013
|
|
1047
1014
|
/**
|
@@ -1072,9 +1039,6 @@ router.get('/', function (req, res, next) {
|
|
1072
1039
|
var newdate = new Date(date);
|
1073
1040
|
var endDate_plusOneDay = newdate.setDate(newdate.getDate() + 1);
|
1074
1041
|
winston.debug('REQUEST ROUTE - REQ QUERY FORMATTED END DATE + 1 DAY ', endDate_plusOneDay);
|
1075
|
-
// var endDate_plusOneDay = moment('2018-09-03').add(1, 'd')
|
1076
|
-
// var endDate_plusOneDay = endDate.add(1).day();
|
1077
|
-
// var toDate = new Date(Date.parse(endDate_plusOneDay)).toISOString()
|
1078
1042
|
|
1079
1043
|
query.createdAt = { $gte: new Date(Date.parse(startDate)).toISOString(), $lte: new Date(endDate_plusOneDay).toISOString() }
|
1080
1044
|
winston.debug('REQUEST ROUTE - QUERY CREATED AT ', query.createdAt);
|
@@ -1092,44 +1056,34 @@ router.get('/', function (req, res, next) {
|
|
1092
1056
|
|
1093
1057
|
winston.debug('REQUEST ROUTE - QUERY CREATED AT (only for start date)', query.createdAt);
|
1094
1058
|
}
|
1095
|
-
// }
|
1096
|
-
|
1097
|
-
|
1098
1059
|
|
1099
1060
|
if (req.query.snap_department_routing) {
|
1100
1061
|
query["snapshot.department.routing"] = req.query.snap_department_routing;
|
1101
|
-
winston.debug('REQUEST ROUTE - QUERY snap_department_routing', query.snap_department_routing);
|
1102
1062
|
}
|
1103
1063
|
|
1104
1064
|
if (req.query.snap_department_default) {
|
1105
1065
|
query["snapshot.department.default"] = req.query.snap_department_default;
|
1106
|
-
winston.debug('REQUEST ROUTE - QUERY snap_department_default', query.snap_department_default);
|
1107
1066
|
}
|
1108
1067
|
|
1109
1068
|
|
1110
1069
|
if (req.query.snap_department_id_bot) {
|
1111
1070
|
query["snapshot.department.id_bot"] = req.query.snap_department_id_bot;
|
1112
|
-
winston.debug('REQUEST ROUTE - QUERY snap_department_id_bot', query.snap_department_id_bot);
|
1113
1071
|
}
|
1114
1072
|
|
1115
1073
|
if (req.query.snap_department_id_bot_exists) {
|
1116
1074
|
query["snapshot.department.id_bot"] = { "$exists": req.query.snap_department_id_bot_exists }
|
1117
|
-
winston.debug('REQUEST ROUTE - QUERY snap_department_id_bot_exists', query.snap_department_id_bot_exists);
|
1118
1075
|
}
|
1119
1076
|
|
1120
1077
|
if (req.query.snap_lead_lead_id) {
|
1121
1078
|
query["snapshot.lead.lead_id"] = req.query.snap_lead_lead_id;
|
1122
|
-
winston.debug('REQUEST ROUTE - QUERY snap_lead_lead_id', query.snap_lead_lead_id);
|
1123
1079
|
}
|
1124
1080
|
|
1125
1081
|
if (req.query.snap_lead_email) {
|
1126
1082
|
query["snapshot.lead.email"] = req.query.snap_lead_email;
|
1127
|
-
winston.debug('REQUEST ROUTE - QUERY snap_lead_email', query.snap_lead_email);
|
1128
1083
|
}
|
1129
1084
|
|
1130
1085
|
if (req.query.smartAssignment) {
|
1131
1086
|
query.smartAssignment = req.query.smartAssignment;
|
1132
|
-
winston.debug('REQUEST ROUTE - QUERY smartAssignment', query.smartAssignment);
|
1133
1087
|
}
|
1134
1088
|
|
1135
1089
|
if (req.query.channel) {
|
@@ -1140,8 +1094,6 @@ router.get('/', function (req, res, next) {
|
|
1140
1094
|
} else {
|
1141
1095
|
query["channel.name"] = req.query.channel
|
1142
1096
|
}
|
1143
|
-
|
1144
|
-
winston.debug('REQUEST ROUTE - QUERY channel', query.channel);
|
1145
1097
|
}
|
1146
1098
|
|
1147
1099
|
if (req.query.priority) {
|
@@ -1153,17 +1105,37 @@ router.get('/', function (req, res, next) {
|
|
1153
1105
|
if (req.query.direction) {
|
1154
1106
|
direction = req.query.direction;
|
1155
1107
|
}
|
1156
|
-
winston.debug("direction", direction);
|
1157
1108
|
|
1158
1109
|
var sortField = "createdAt";
|
1159
1110
|
if (req.query.sort) {
|
1160
1111
|
sortField = req.query.sort;
|
1161
1112
|
}
|
1162
|
-
winston.debug("sortField", sortField);
|
1163
1113
|
|
1164
1114
|
var sortQuery = {};
|
1165
1115
|
sortQuery[sortField] = direction;
|
1166
|
-
|
1116
|
+
|
1117
|
+
// VOICE FILTERS - Start
|
1118
|
+
if (req.query.caller) {
|
1119
|
+
query["attributes.caller_phone"] = req.query.caller;
|
1120
|
+
}
|
1121
|
+
if (req.query.called) {
|
1122
|
+
query["attributes.called_phone"] = req.query.called;
|
1123
|
+
}
|
1124
|
+
if (req.query.call_id) {
|
1125
|
+
query["attributes.call_id"] = req.query.call_id;
|
1126
|
+
}
|
1127
|
+
// VOICE FILTERS - End
|
1128
|
+
|
1129
|
+
if (req.query.duration && req.query.duration_op) {
|
1130
|
+
let duration = Number(req.query.duration) * 60 * 1000;
|
1131
|
+
if (req.query.duration_op === 'gt') {
|
1132
|
+
query.duration = { $gte: duration }
|
1133
|
+
} else if (req.query.duration_op === 'lt') {
|
1134
|
+
query.duration = { $lte: duration }
|
1135
|
+
} else {
|
1136
|
+
winston.verbose("Duration operator can be 'gt' or 'lt'. Skip duration_op " + req.query.duration_op)
|
1137
|
+
}
|
1138
|
+
}
|
1167
1139
|
|
1168
1140
|
if (req.query.abandonded && (req.query.abandoned === true || req.query.abandoned === 'true')) {
|
1169
1141
|
query["attributes.fully_abandoned"] = true
|
@@ -1173,26 +1145,21 @@ router.get('/', function (req, res, next) {
|
|
1173
1145
|
query.draft = { $in: [false, null] }
|
1174
1146
|
}
|
1175
1147
|
|
1176
|
-
winston.debug('REQUEST ROUTE - REQUEST FIND ', query);
|
1177
|
-
|
1178
1148
|
var projection = undefined;
|
1179
1149
|
|
1180
1150
|
if (req.query.full_text) {
|
1181
|
-
|
1182
1151
|
if (req.query.no_textscore != "true" && req.query.no_textscore != true) {
|
1183
|
-
winston.
|
1152
|
+
winston.verbose('fulltext projection on');
|
1184
1153
|
projection = { score: { $meta: "textScore" } };
|
1185
1154
|
}
|
1186
|
-
|
1187
1155
|
}
|
1188
|
-
|
1156
|
+
|
1157
|
+
winston.verbose('REQUEST ROUTE - REQUEST FIND QUERY ', query);
|
1158
|
+
|
1189
1159
|
var q1 = Request.find(query, projection).
|
1190
1160
|
skip(skip).limit(limit);
|
1191
1161
|
|
1192
|
-
winston.debug('REQUEST ROUTE no_populate:' + req.query.no_populate);
|
1193
|
-
|
1194
1162
|
if (req.query.no_populate != "true" && req.query.no_populate != true) {
|
1195
|
-
winston.verbose('REQUEST ROUTE - no_polutate false ', req.headers);
|
1196
1163
|
q1.populate('department').
|
1197
1164
|
populate('participatingBots'). //nico già nn gli usa
|
1198
1165
|
populate('participatingAgents'). //nico già nn gli usa
|
@@ -1200,17 +1167,7 @@ router.get('/', function (req, res, next) {
|
|
1200
1167
|
populate({ path: 'requester', populate: { path: 'id_user' } }); //toglilo perche nico lo prende già da snapshot
|
1201
1168
|
}
|
1202
1169
|
|
1203
|
-
// cache(cacheUtil.defaultTTL, "requests-"+projectId).
|
1204
|
-
|
1205
|
-
|
1206
|
-
// if (req.query.select_snapshot) {
|
1207
|
-
// winston.info('select_snapshot');
|
1208
|
-
// q1.select("+snapshot");
|
1209
|
-
// // q1.select({ "snapshot": 1});
|
1210
|
-
// }
|
1211
|
-
|
1212
1170
|
if (req.query.full_text) {
|
1213
|
-
winston.debug('fulltext sort');
|
1214
1171
|
if (req.query.no_textscore != "true" && req.query.no_textscore != true) {
|
1215
1172
|
q1.sort({ score: { $meta: "textScore" } }) //https://docs.mongodb.com/manual/reference/operator/query/text/#sort-by-text-search-score
|
1216
1173
|
}
|
@@ -1218,10 +1175,6 @@ router.get('/', function (req, res, next) {
|
|
1218
1175
|
q1.sort(sortQuery);
|
1219
1176
|
}
|
1220
1177
|
|
1221
|
-
|
1222
|
-
// winston.info('q1',q1);
|
1223
|
-
|
1224
|
-
|
1225
1178
|
q1.exec();
|
1226
1179
|
|
1227
1180
|
// TODO if ?onlycount=true do not perform find query but only
|
@@ -1234,10 +1187,7 @@ router.get('/', function (req, res, next) {
|
|
1234
1187
|
q2 = 0;
|
1235
1188
|
}
|
1236
1189
|
|
1237
|
-
var promises = [
|
1238
|
-
q1,
|
1239
|
-
q2
|
1240
|
-
];
|
1190
|
+
var promises = [ q1, q2 ];
|
1241
1191
|
|
1242
1192
|
Promise.all(promises).then(function (results) {
|
1243
1193
|
var objectToReturn = {
|
@@ -1664,11 +1614,6 @@ router.get('/csv', function (req, res, next) {
|
|
1664
1614
|
query.$text = { "$search": req.query.full_text };
|
1665
1615
|
}
|
1666
1616
|
|
1667
|
-
// if (req.query.status) {
|
1668
|
-
// winston.debug('req.query.status', req.query.status);
|
1669
|
-
// query.status = req.query.status;
|
1670
|
-
// }
|
1671
|
-
|
1672
1617
|
if (req.query.status) {
|
1673
1618
|
|
1674
1619
|
if (req.query.status === 'all') {
|
@@ -1776,6 +1721,27 @@ router.get('/csv', function (req, res, next) {
|
|
1776
1721
|
|
1777
1722
|
winston.debug("sort query", sortQuery);
|
1778
1723
|
|
1724
|
+
if (req.query.channel) {
|
1725
|
+
if (req.query.channel === "offline") {
|
1726
|
+
query["channel.name"] = { "$in": ["email", "form"] }
|
1727
|
+
} else if (req.query.channel === "online") {
|
1728
|
+
query["channel.name"] = { "$nin": ["email", "form"] }
|
1729
|
+
} else {
|
1730
|
+
query["channel.name"] = req.query.channel
|
1731
|
+
}
|
1732
|
+
}
|
1733
|
+
|
1734
|
+
// VOICE FILTERS - Start
|
1735
|
+
if (req.query.caller) {
|
1736
|
+
query["attributes.caller_phone"] = req.query.caller;
|
1737
|
+
}
|
1738
|
+
if (req.query.called) {
|
1739
|
+
query["attributes.called_phone"] = req.query.called;
|
1740
|
+
}
|
1741
|
+
if (req.query.call_id) {
|
1742
|
+
query["attributes.call_id"] = req.query.call_id;
|
1743
|
+
}
|
1744
|
+
// VOICE FILTERS - End
|
1779
1745
|
|
1780
1746
|
// TODO ORDER BY SCORE
|
1781
1747
|
// return Faq.find(query, {score: { $meta: "textScore" } })
|
@@ -1783,6 +1749,21 @@ router.get('/csv', function (req, res, next) {
|
|
1783
1749
|
|
1784
1750
|
// aggiungi filtro per data marco
|
1785
1751
|
|
1752
|
+
if (req.query.duration && req.query.duration_op) {
|
1753
|
+
let duration = Number(req.query.duration) * 60 * 1000;
|
1754
|
+
if (req.query.duration_op === 'gt') {
|
1755
|
+
query.duration = { $gte: duration }
|
1756
|
+
} else if (req.query.duration_op === 'lt') {
|
1757
|
+
query.duration = { $lte: duration }
|
1758
|
+
} else {
|
1759
|
+
winston.verbose("Duration operator can be 'gt' or 'lt'. Skip duration_op " + req.query.duration_op)
|
1760
|
+
}
|
1761
|
+
}
|
1762
|
+
|
1763
|
+
if (req.query.draft && (req.query.draft === 'false' || req.query.draft === false)) {
|
1764
|
+
query.draft = { $in: [false, null] }
|
1765
|
+
}
|
1766
|
+
|
1786
1767
|
winston.debug('REQUEST ROUTE - REQUEST FIND ', query)
|
1787
1768
|
return Request.find(query, '-transcript -status -__v').
|
1788
1769
|
skip(skip).limit(limit).
|
@@ -1860,7 +1841,17 @@ router.get('/csv', function (req, res, next) {
|
|
1860
1841
|
// // da terminare e testare. potrebbe essere troppo lenta la query per tanti record
|
1861
1842
|
// element.participatingAgents = participatingAgents;
|
1862
1843
|
|
1863
|
-
|
1844
|
+
if (element.attributes) {
|
1845
|
+
if (element.attributes.caller_phone) {
|
1846
|
+
element.caller_phone = element.attributes.caller_phone;
|
1847
|
+
}
|
1848
|
+
if (element.attributes.called_phone) {
|
1849
|
+
element.called_phone = element.attributes.called_phone;
|
1850
|
+
}
|
1851
|
+
if (element.attributes.caller_phone) {
|
1852
|
+
element.call_id = element.attributes.call_id;
|
1853
|
+
}
|
1854
|
+
}
|
1864
1855
|
|
1865
1856
|
delete element.lead;
|
1866
1857
|
|
@@ -1,11 +1,13 @@
|
|
1
1
|
var winston = require('../config/winston');
|
2
2
|
const axios = require("axios").default;
|
3
3
|
require('dotenv').config();
|
4
|
+
const jwt = require("jsonwebtoken")
|
4
5
|
|
5
6
|
let openai_endpoint = process.env.OPENAI_ENDPOINT;
|
6
7
|
let kb_endpoint = process.env.KB_ENDPOINT;
|
7
8
|
let kb_endpoint_train = process.env.KB_ENDPOINT_TRAIN;
|
8
9
|
let kb_endpoint_qa = process.env.KB_ENDPOINT_QA;
|
10
|
+
let secret = process.env.JWT_SECRET_KEY;
|
9
11
|
|
10
12
|
class OpenaiService {
|
11
13
|
|
@@ -162,13 +164,15 @@ class OpenaiService {
|
|
162
164
|
})
|
163
165
|
}
|
164
166
|
|
165
|
-
getContentChunks(namespace_id, content_id) {
|
167
|
+
getContentChunks(namespace_id, content_id, engine) {
|
166
168
|
winston.info("[OPENAI SERVICE] kb endpoint: " + kb_endpoint_train);
|
167
169
|
winston.info(kb_endpoint_train + "/id/" + content_id + "/namespace/" + namespace_id)
|
168
170
|
return new Promise((resolve, reject) => {
|
169
171
|
|
172
|
+
let payload = { engine: engine };
|
173
|
+
let token = jwt.sign(payload, secret);
|
170
174
|
axios({
|
171
|
-
url: kb_endpoint_train + "/id/" + content_id + "/namespace/" + namespace_id,
|
175
|
+
url: kb_endpoint_train + "/id/" + content_id + "/namespace/" + namespace_id + "/" + token,
|
172
176
|
headers: {
|
173
177
|
'Content-Type': 'application/json'
|
174
178
|
},
|