@tiledesk/tiledesk-server 2.9.12 → 2.9.14

Sign up to get free protection for your applications and to get access to all the features.
package/routes/project.js CHANGED
@@ -3,10 +3,13 @@ var router = express.Router();
3
3
  var Project = require("../models/project");
4
4
  var projectEvent = require("../event/projectEvent");
5
5
  var projectService = require("../services/projectService");
6
+ var projectUserService = require("../services/projectUserService");
6
7
 
7
8
  var Project_user = require("../models/project_user");
8
9
 
9
10
  var operatingHoursService = require("../services/operatingHoursService");
11
+ var Department = require('../models/department');
12
+ var Group = require('../models/group');
10
13
 
11
14
  var winston = require('../config/winston');
12
15
  var roleChecker = require('../middleware/has-role');
@@ -20,7 +23,7 @@ var RoleConstants = require("../models/roleConstants");
20
23
  var cacheUtil = require('../utils/cacheUtil');
21
24
  var orgUtil = require("../utils/orgUtil");
22
25
  var cacheEnabler = require("../services/cacheEnabler");
23
-
26
+ var mongoose = require('mongoose');
24
27
 
25
28
  router.post('/', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken], async (req, res) => {
26
29
 
@@ -881,83 +884,187 @@ router.get('/:projectid/isopen', function (req, res) {
881
884
  }
882
885
  });
883
886
 
884
- //togli questo route da qui e mettilo in altra route
885
- // NEW - RETURN THE USER NAME AND THE USER ID OF THE AVAILABLE PROJECT-USER FOR THE PROJECT ID PASSED
886
- router.get('/:projectid/users/availables', function (req, res) {
887
- //winston.debug("PROJECT ROUTES FINDS AVAILABLES project_users: projectid", req.params.projectid);
888
-
889
- if (req.query.raw && (req.query.raw === true || req.query.raw === 'true')) {
890
- Project_user.find({ id_project: req.params.projectid, user_available: true, role: { $in : [RoleConstants.OWNER, RoleConstants.ADMIN, RoleConstants.SUPERVISOR, RoleConstants.AGENT]}}).
891
- populate('id_user').
892
- exec(function (err, project_users) {
893
- if (err) {
894
- winston.debug('PROJECT ROUTES - FINDS AVAILABLES project_users - ERROR: ', err);
895
- return res.status(500).send({ success: false, msg: 'Error getting object.' });
896
- }
897
- if (project_users) {
898
-
899
- user_available_array = [];
900
- project_users.forEach(project_user => {
901
- if (project_user.id_user) {
902
- // winston.debug('PROJECT ROUTES - AVAILABLES PROJECT-USER: ', project_user)
903
- user_available_array.push({ "id": project_user.id_user._id, "firstname": project_user.id_user.firstname });
904
- } else {
905
- // winston.debug('PROJECT ROUTES - AVAILABLES PROJECT-USER (else): ', project_user)
906
- }
907
- });
887
+
888
+ router.get('/:projectid/users/availables', async (req, res) => {
889
+
890
+ let projectid = req.params.projectid;
891
+ let raw_option = req.query.raw;
892
+ let dep_id = req.query.department;
893
+ let isOpen = true;
894
+
895
+ winston.debug("(Users Availables) raw_option: " + raw_option);
896
+ winston.debug("(Users Availables) dep_id: " + dep_id);
897
+
898
+ let available_agents_array = [];
899
+
900
+ if (!raw_option || raw_option === false) {
901
+ try {
902
+ isOpen = await new Promise((resolve, reject) => {
903
+ operatingHoursService.projectIsOpenNow(projectid, (isOpen, err) => {
904
+ if (err) reject(err);
905
+ else resolve(isOpen);
906
+ });
907
+ });
908
+ } catch (err) {
909
+ wisnton.error("(Users Availables) check operating hours error: ", err);
910
+ return res.status(500).send({ success: false, msg: err });
911
+ }
912
+ }
913
+
914
+ if (isOpen === false) {
915
+ return res.json(available_agents_array);
916
+ }
917
+
918
+ let query = { id_project: projectid, user_available: true, role: { $in : [RoleConstants.OWNER, RoleConstants.ADMIN, RoleConstants.SUPERVISOR, RoleConstants.AGENT]} };
919
+
920
+ if (dep_id) {
921
+ let department = await Department.findById(dep_id).catch((err) => {
922
+ winston.error("(Users Availables) find department error: ", err)
923
+ return res.status(500).send({ success: false, error: err })
924
+ })
925
+
926
+ if (!department) {
927
+ winston.error("(Users Availables) department not found")
928
+ return res.status(404).send({ success: false, error: "Department " + dep_id + " not found" })
929
+ }
930
+
931
+ let group_id = department.id_group;
932
+ if (group_id) {
933
+ let group = await Group.findById(group_id).catch((err) => {
934
+ winston.error("(Users Availables) find group error: ", err)
935
+ return res.status(500).send({ success: false, error: err })
936
+ })
937
+
938
+ if (!group) {
939
+ winston.error("(Users Availables) group not found")
940
+ return res.status(404).send({ success: false, error: "Group " + group_id + " not found" })
941
+ }
942
+
943
+ query.id_user = { $in: group.members.map(id => mongoose.Types.ObjectId(id) )}
944
+ }
945
+ }
946
+
947
+ winston.debug("(Users Availables) project_users query ", query)
948
+ Project_user.find(query)
949
+ .populate('id_user')
950
+ .exec( async (err, project_users) => {
951
+ if (err) {
952
+ winston.debug('PROJECT ROUTES - FINDS AVAILABLES project_users - ERROR: ', err);
953
+ return res.status(500).send({ success: false, msg: 'Error getting object.' + err});
954
+ }
955
+
956
+ let project = await Project.findById(projectid).catch((err) => {
957
+ winston.error("find project error: ", err)
958
+ res.status(500).send({ success: false, error: err })
959
+ })
960
+
961
+ // check on SMART ASSIGNMENT
962
+ let available_agents = projectUserService.checkAgentsAvailablesWithSmartAssignment(project, project_users);
963
+ winston.verbose("(Users Availables) available agents after smart assignment check", available_agents);
964
+
965
+ if (available_agents) {
908
966
 
909
- //winston.debug('ARRAY OF THE AVAILABLE USER ', user_available_array);
910
- res.json(user_available_array);
967
+ available_agents_array = [];
968
+ available_agents.forEach(agent => {
969
+ //console.log("agent: ", agent);
970
+ if (agent.id_user) {
971
+ available_agents_array.push({
972
+ "id": agent.id_user._id,
973
+ "pu_id": agent._id,
974
+ "fullname": agent.id_user.firstname + " " + agent.id_user.lastname,
975
+ "email": agent.id_user.email,
976
+ "assigned_request": agent.number_assigned_requests
977
+ });
978
+ } else {
979
+ winston.warn("(Users Availables) agent.id_user is undefined");
911
980
  }
912
981
  });
913
- } else {
914
- operatingHoursService.projectIsOpenNow(req.params.projectid, function (isOpen, err) {
915
- //winston.debug('P ---> [ OHS ] -> [ PROJECT ROUTES ] -> IS OPEN THE PROJECT: ', isOpen);
982
+
983
+ winston.debug("(Users Availables) return following available_agents_array", available_agents_array);
984
+ res.json(available_agents_array);
985
+ }
986
+ })
916
987
 
917
- if (err) {
918
- winston.debug('P ---> [ OHS ] -> [ PROJECT ROUTES ] -> IS OPEN THE PROJECT - EROR: ', err)
919
- // sendError(err, res);
920
- return res.status(500).send({ success: false, msg: err });
921
- } else if (isOpen) {
922
-
923
- Project_user.find({ id_project: req.params.projectid, user_available: true, role: { $in : [RoleConstants.OWNER, RoleConstants.ADMIN, RoleConstants.SUPERVISOR, RoleConstants.AGENT]}}).
924
- populate('id_user').
925
- exec(function (err, project_users) {
926
- if (err) {
927
- winston.debug('PROJECT ROUTES - FINDS AVAILABLES project_users - ERROR: ', err);
928
- return res.status(500).send({ success: false, msg: 'Error getting object.' });
929
- }
930
- if (project_users) {
931
-
932
- user_available_array = [];
933
- project_users.forEach(project_user => {
934
- if (project_user.id_user) {
935
- // winston.debug('PROJECT ROUTES - AVAILABLES PROJECT-USER: ', project_user)
936
- user_available_array.push({ "id": project_user.id_user._id, "firstname": project_user.id_user.firstname });
937
- } else {
938
- // winston.debug('PROJECT ROUTES - AVAILABLES PROJECT-USER (else): ', project_user)
939
- }
940
- });
988
+ })
989
+
990
+ // OLD ENDPOINT for /users/availables
991
+ //togli questo route da qui e mettilo in altra route
992
+ // NEW - RETURN THE USER NAME AND THE USER ID OF THE AVAILABLE PROJECT-USER FOR THE PROJECT ID PASSED
993
+ // router.get('/:projectid/users/availables', function (req, res) {
994
+ // //winston.debug("PROJECT ROUTES FINDS AVAILABLES project_users: projectid", req.params.projectid);
995
+
996
+ // if (req.query.raw && (req.query.raw === true || req.query.raw === 'true')) {
997
+ // Project_user.find({ id_project: req.params.projectid, user_available: true, role: { $in : [RoleConstants.OWNER, RoleConstants.ADMIN, RoleConstants.SUPERVISOR, RoleConstants.AGENT]}}).
998
+ // populate('id_user').
999
+ // exec(function (err, project_users) {
1000
+ // if (err) {
1001
+ // winston.debug('PROJECT ROUTES - FINDS AVAILABLES project_users - ERROR: ', err);
1002
+ // return res.status(500).send({ success: false, msg: 'Error getting object.' });
1003
+ // }
1004
+ // if (project_users) {
941
1005
 
942
- //winston.debug('ARRAY OF THE AVAILABLE USER ', user_available_array);
1006
+ // user_available_array = [];
1007
+ // project_users.forEach(project_user => {
1008
+ // if (project_user.id_user) {
1009
+ // // winston.debug('PROJECT ROUTES - AVAILABLES PROJECT-USER: ', project_user)
1010
+ // user_available_array.push({ "id": project_user.id_user._id, "firstname": project_user.id_user.firstname });
1011
+ // } else {
1012
+ // // winston.debug('PROJECT ROUTES - AVAILABLES PROJECT-USER (else): ', project_user)
1013
+ // }
1014
+ // });
943
1015
 
944
- res.json(user_available_array);
945
- }
946
- });
1016
+ // //winston.debug('ARRAY OF THE AVAILABLE USER ', user_available_array);
1017
+ // res.json(user_available_array);
1018
+ // }
1019
+ // });
1020
+ // } else {
1021
+ // operatingHoursService.projectIsOpenNow(req.params.projectid, function (isOpen, err) {
1022
+ // //winston.debug('P ---> [ OHS ] -> [ PROJECT ROUTES ] -> IS OPEN THE PROJECT: ', isOpen);
947
1023
 
1024
+ // if (err) {
1025
+ // winston.debug('P ---> [ OHS ] -> [ PROJECT ROUTES ] -> IS OPEN THE PROJECT - EROR: ', err)
1026
+ // // sendError(err, res);
1027
+ // return res.status(500).send({ success: false, msg: err });
1028
+ // } else if (isOpen) {
948
1029
 
949
- } else {
950
- // winston.debug('P ---> [ OHS ] -> [ PROJECT ROUTES ] -> IS OPEN THE PRJCT: ', isOpen, ' -> AVAILABLE EMPTY');
951
- // closed
952
- user_available_array = [];
953
- res.json(user_available_array);
954
- }
955
- });
956
- }
1030
+ // Project_user.find({ id_project: req.params.projectid, user_available: true, role: { $in : [RoleConstants.OWNER, RoleConstants.ADMIN, RoleConstants.SUPERVISOR, RoleConstants.AGENT]}}).
1031
+ // populate('id_user').
1032
+ // exec(function (err, project_users) {
1033
+ // if (err) {
1034
+ // winston.debug('PROJECT ROUTES - FINDS AVAILABLES project_users - ERROR: ', err);
1035
+ // return res.status(500).send({ success: false, msg: 'Error getting object.' });
1036
+ // }
1037
+ // if (project_users) {
1038
+
1039
+ // user_available_array = [];
1040
+ // project_users.forEach(project_user => {
1041
+ // if (project_user.id_user) {
1042
+ // // winston.debug('PROJECT ROUTES - AVAILABLES PROJECT-USER: ', project_user)
1043
+ // user_available_array.push({ "id": project_user.id_user._id, "firstname": project_user.id_user.firstname });
1044
+ // } else {
1045
+ // // winston.debug('PROJECT ROUTES - AVAILABLES PROJECT-USER (else): ', project_user)
1046
+ // }
1047
+ // });
1048
+
1049
+ // //winston.debug('ARRAY OF THE AVAILABLE USER ', user_available_array);
1050
+
1051
+ // res.json(user_available_array);
1052
+ // }
1053
+ // });
1054
+
1055
+
1056
+ // } else {
1057
+ // // winston.debug('P ---> [ OHS ] -> [ PROJECT ROUTES ] -> IS OPEN THE PRJCT: ', isOpen, ' -> AVAILABLE EMPTY');
1058
+ // // closed
1059
+ // user_available_array = [];
1060
+ // res.json(user_available_array);
1061
+ // }
1062
+ // });
1063
+ // }
957
1064
 
958
1065
 
959
1066
 
960
- });
1067
+ // });
961
1068
 
962
1069
 
963
1070
 
@@ -9,6 +9,38 @@ var pendinginvitation = require("../services/pendingInvitationService");
9
9
 
10
10
  class ProjectUserService {
11
11
 
12
+ checkAgentsAvailablesWithSmartAssignment(project, available_agents) {
13
+
14
+ let max_assigned_chat;
15
+ let available_agents_request = [];
16
+
17
+ if (project && project.settings && project.settings.chat_limit_on && project.settings.max_agent_assigned_chat) {
18
+ max_assigned_chat = project.settings.max_agent_assigned_chat;
19
+ winston.verbose('[ProjectUserService] max_agent_assigned_chat: ' + max_assigned_chat);
20
+ } else {
21
+ winston.verbose('[ProjectUserService] chat_limit_on or max_agent_assigned_chat is undefined');
22
+ return available_agents
23
+ }
24
+
25
+ if (available_agents.length == 0) {
26
+ return available_agents_request;
27
+ }
28
+
29
+ for (const aa of available_agents) {
30
+ let max_assigned_chat_specific_user = max_assigned_chat;
31
+ if (aa.max_assigned_chat && aa.max_assigned_chat != -1) {
32
+ max_assigned_chat_specific_user = aa.max_assigned_chat;
33
+ }
34
+ winston.verbose("[ProjectUserService] max_assigned_chat_specific_user " + max_assigned_chat_specific_user);
35
+
36
+ if (aa.number_assigned_requests < max_assigned_chat_specific_user) {
37
+ available_agents_request.push(aa);
38
+ }
39
+ }
40
+
41
+ return available_agents_request;
42
+
43
+ }
12
44
 
13
45
  }
14
46
  var projectUserService = new ProjectUserService();