@tiledesk/tiledesk-server 2.19.12 → 2.19.14

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.
@@ -10,6 +10,7 @@ const leadEvent = require('../event/leadEvent');
10
10
  var winston = require('../config/winston');
11
11
  var RequestConstants = require("../models/requestConstants");
12
12
  var requestUtil = require("../utils/requestUtil");
13
+ var assignmentContextUtil = require("../utils/assignmentContextUtil");
13
14
  var cacheUtil = require("../utils/cacheUtil");
14
15
  var arrayUtil = require("../utils/arrayUtil");
15
16
  var cacheEnabler = require("../services/cacheEnabler");
@@ -245,7 +246,7 @@ class RequestService {
245
246
 
246
247
  // TODO changePreflightByRequestId se un agente entra in request freflight true disabilitare add agente e reassing ma mettere un bottone removePreflight???
247
248
  // usalo no_populate
248
- async route(request_id, departmentid, id_project, nobot, no_populate) {
249
+ async route(request_id, departmentid, id_project, nobot, no_populate, options) {
249
250
 
250
251
  try {
251
252
  winston.debug("request_id:" + request_id);
@@ -255,13 +256,15 @@ class RequestService {
255
256
  //winston.info("main_flow_cache_3 route");
256
257
 
257
258
  // Find request
258
- let query = Request.findOne({ request_id, id_project });
259
- if (cacheEnabler.request) {
260
- query = query.cache(cacheUtil.defaultTTL, id_project + ":requests:request_id:" + request_id + ":simple");
261
- winston.debug('request cache enabled');
262
- }
263
-
264
- const request = await query.exec();
259
+ // Cache disabled: a stale cached document can have an outdated __v and cause
260
+ // Mongoose VersionError on save ("No matching document found for id ... version N").
261
+ // let query = Request.findOne({ request_id, id_project });
262
+ // if (cacheEnabler.request) {
263
+ // query = query.cache(cacheUtil.defaultTTL, id_project + ":requests:request_id:" + request_id + ":simple");
264
+ // winston.debug('request cache enabled');
265
+ // }
266
+ // const request = await query.exec();
267
+ const request = await Request.findOne({ request_id, id_project }).exec();
265
268
 
266
269
  if (!request) {
267
270
  throw new Error(`Request not found: ${request_id}`);
@@ -322,7 +325,13 @@ class RequestService {
322
325
  this.emitParticipantsEvents(
323
326
  request,
324
327
  requestComplete,
325
- beforeParticipants
328
+ beforeParticipants,
329
+ options,
330
+ beforeDepartmentId !== afterDepartmentId ? {
331
+ previousDepartmentId: beforeDepartmentId,
332
+ departmentId: afterDepartmentId,
333
+ departmentName: requestComplete.department && requestComplete.department.name
334
+ } : undefined
326
335
  );
327
336
 
328
337
  return requestComplete;
@@ -377,10 +386,18 @@ class RequestService {
377
386
  .execPopulate();
378
387
 
379
388
 
389
+ const departmentChange = beforeDepartmentId !== afterDepartmentId ? {
390
+ previousDepartmentId: beforeDepartmentId,
391
+ departmentId: afterDepartmentId,
392
+ departmentName: requestComplete.department && requestComplete.department.name
393
+ } : undefined;
394
+
380
395
  this.emitParticipantsEvents(
381
396
  request,
382
397
  requestComplete,
383
- beforeParticipants
398
+ beforeParticipants,
399
+ options,
400
+ departmentChange
384
401
  );
385
402
 
386
403
  requestEvent.emit("request.department.update", requestComplete);
@@ -394,22 +411,30 @@ class RequestService {
394
411
  }
395
412
 
396
413
 
397
- reroute(request_id, id_project, nobot) {
414
+ reroute(request_id, id_project, nobot, options) {
398
415
  var that = this;
399
416
  var startDate = new Date();
417
+ const assignmentOptions = options || {
418
+ actor: assignmentContextUtil.systemActor(),
419
+ source: 'queue',
420
+ assignmentType: 'auto'
421
+ };
400
422
  return new Promise(function (resolve, reject) {
401
423
  // winston.debug("request_id", request_id);
402
424
  // winston.debug("newstatus", newstatus);
403
425
 
404
- let q = Request
405
- .findOne({ request_id: request_id, id_project: id_project });
406
-
407
- if (cacheEnabler.request) {
408
- q.cache(cacheUtil.defaultTTL, id_project + ":requests:request_id:" + request_id + ":simple") //request_cache
409
- winston.debug('request cache enabled');
410
- }
411
-
412
- return q.exec(function (err, request) {
426
+ // Cache disabled: a stale cached document can have an outdated __v and cause
427
+ // Mongoose VersionError on save ("No matching document found for id ... version N").
428
+ // let q = Request
429
+ // .findOne({ request_id: request_id, id_project: id_project });
430
+ // if (cacheEnabler.request) {
431
+ // q.cache(cacheUtil.defaultTTL, id_project + ":requests:request_id:" + request_id + ":simple");
432
+ // winston.debug('request cache enabled');
433
+ // }
434
+ // return q.exec(function (err, request) {
435
+ return Request
436
+ .findOne({ request_id: request_id, id_project: id_project })
437
+ .exec(function (err, request) {
413
438
 
414
439
  if (err) {
415
440
  winston.error(err);
@@ -427,7 +452,7 @@ class RequestService {
427
452
  }
428
453
 
429
454
 
430
- return that.route(request_id, request.department.toString(), id_project, nobot).then(function (routedRequest) {
455
+ return that.route(request_id, request.department.toString(), id_project, nobot, undefined, assignmentOptions).then(function (routedRequest) {
431
456
 
432
457
  var endDate = new Date();
433
458
  winston.verbose("Performance Request reroute in millis: " + endDate - startDate);
@@ -1523,10 +1548,11 @@ class RequestService {
1523
1548
 
1524
1549
 
1525
1550
 
1526
- setParticipantsByRequestId(request_id, id_project, newparticipants) {
1551
+ setParticipantsByRequestId(request_id, id_project, newparticipants, options) {
1527
1552
 
1528
1553
  //TODO validate participants
1529
1554
  // validate if array of string newparticipants
1555
+ var that = this;
1530
1556
  return new Promise(function (resolve, reject) {
1531
1557
 
1532
1558
  var isArray = Array.isArray(newparticipants);
@@ -1672,6 +1698,14 @@ class RequestService {
1672
1698
  request: requestComplete
1673
1699
  });
1674
1700
 
1701
+ that.emitAssignedEvent(requestComplete, {
1702
+ assignmentType: options && options.assignmentType,
1703
+ actor: options && options.actor,
1704
+ source: options && options.source,
1705
+ addedParticipants: addedParticipants,
1706
+ removedParticipants: removedParticipants
1707
+ });
1708
+
1675
1709
  // TODO allora neanche qui participatingAgent è ok?
1676
1710
  return resolve(requestComplete);
1677
1711
  });
@@ -1683,12 +1717,12 @@ class RequestService {
1683
1717
  });
1684
1718
  }
1685
1719
 
1686
- addParticipantByRequestId(request_id, id_project, member) {
1720
+ addParticipantByRequestId(request_id, id_project, member, options) {
1687
1721
  winston.debug("request_id: " + request_id);
1688
1722
  winston.debug("id_project: " + id_project);
1689
1723
  winston.debug("addParticipantByRequestId member: " + member);
1690
1724
 
1691
-
1725
+ var that = this;
1692
1726
 
1693
1727
  //TODO control if member is a valid project_user of the project
1694
1728
  // validate member is string
@@ -1771,6 +1805,14 @@ class RequestService {
1771
1805
  requestEvent.emit("request.updated", { comment: "PARTICIPANT_ADD", request: requestComplete, patch: { member: member } });
1772
1806
  requestEvent.emit('request.participants.join', { member: member, request: requestComplete });
1773
1807
 
1808
+ that.emitAssignedEvent(requestComplete, {
1809
+ assignmentType: options && options.assignmentType,
1810
+ actor: options && options.actor,
1811
+ source: options && options.source,
1812
+ addedParticipants: [member],
1813
+ removedParticipants: []
1814
+ });
1815
+
1774
1816
  return resolve(requestComplete);
1775
1817
  });
1776
1818
  });
@@ -1792,7 +1834,7 @@ class RequestService {
1792
1834
  });
1793
1835
  }
1794
1836
 
1795
- removeParticipantByRequestId(request_id, id_project, member) {
1837
+ removeParticipantByRequestId(request_id, id_project, member, options) {
1796
1838
  winston.debug("request_id", request_id);
1797
1839
  winston.debug("id_project", id_project);
1798
1840
  winston.debug("member", member);
@@ -1918,7 +1960,13 @@ class RequestService {
1918
1960
  requestEvent.emit('request.update', requestComplete);
1919
1961
  requestEvent.emit("request.update.comment", { comment: "PARTICIPANT_REMOVE", request: requestComplete });//Deprecated
1920
1962
  requestEvent.emit("request.updated", { comment: "PARTICIPANT_REMOVE", request: requestComplete, patch: { member: member } });
1921
- requestEvent.emit('request.participants.leave', { member: member, request: requestComplete });
1963
+ requestEvent.emit('request.participants.leave', {
1964
+ member: member,
1965
+ request: requestComplete,
1966
+ actor: options && options.actor,
1967
+ source: options && options.source,
1968
+ trackLeave: options && options.trackLeave
1969
+ });
1922
1970
 
1923
1971
 
1924
1972
  return resolve(requestComplete);
@@ -2482,7 +2530,8 @@ class RequestService {
2482
2530
  })
2483
2531
  }
2484
2532
 
2485
- emitParticipantsEvents(beforeRequest, requestComplete, oldParticipants) {
2533
+ emitParticipantsEvents(beforeRequest, requestComplete, oldParticipants, options, departmentChange) {
2534
+ const that = this;
2486
2535
  const newParticipants = requestComplete.participants;
2487
2536
 
2488
2537
  const removedParticipants = oldParticipants.filter(
@@ -2505,6 +2554,110 @@ class RequestService {
2505
2554
  addedParticipants,
2506
2555
  request: requestComplete
2507
2556
  });
2557
+
2558
+ that.emitAssignedEvent(requestComplete, {
2559
+ assignmentType: options && options.assignmentType,
2560
+ actor: options && options.actor,
2561
+ source: options && options.source,
2562
+ addedParticipants,
2563
+ removedParticipants,
2564
+ departmentChange
2565
+ });
2566
+ }
2567
+
2568
+ emitAssignedEvent(requestComplete, context) {
2569
+ const {
2570
+ assignmentType,
2571
+ actor,
2572
+ source,
2573
+ addedParticipants = [],
2574
+ removedParticipants = [],
2575
+ departmentChange
2576
+ } = context || {};
2577
+
2578
+ const humanAdded = assignmentContextUtil.filterHumanParticipants(addedParticipants);
2579
+ const humanRemoved = assignmentContextUtil.filterHumanParticipants(removedParticipants);
2580
+ const botAdded = assignmentContextUtil.filterBotParticipants(addedParticipants);
2581
+ const resolvedActor = actor || assignmentContextUtil.systemActor();
2582
+ const resolvedSource = source || 'system';
2583
+
2584
+ if (
2585
+ assignmentType === 'manual_reassign_department' &&
2586
+ departmentChange &&
2587
+ departmentChange.departmentId &&
2588
+ String(departmentChange.departmentId) !== String(departmentChange.previousDepartmentId || '')
2589
+ ) {
2590
+ requestEvent.emit('request.assigned', {
2591
+ request: requestComplete,
2592
+ assigneeId: String(departmentChange.departmentId),
2593
+ assigneeName: departmentChange.departmentName || assignmentContextUtil.resolveDepartmentName(requestComplete),
2594
+ assigneeType: 'department',
2595
+ assignmentType: 'manual_reassign_department',
2596
+ actor: resolvedActor,
2597
+ source: resolvedSource,
2598
+ previousAssigneeId: humanRemoved.length > 0 ? String(humanRemoved[0]) : null,
2599
+ removedParticipants: humanRemoved
2600
+ });
2601
+ return;
2602
+ }
2603
+
2604
+ if (assignmentType === 'manual_reassign_bot' && botAdded.length > 0) {
2605
+ const botId = assignmentContextUtil.botIdFromParticipant(botAdded[0]);
2606
+ requestEvent.emit('request.assigned', {
2607
+ request: requestComplete,
2608
+ assigneeId: botId,
2609
+ assigneeName: assignmentContextUtil.resolveBotName(requestComplete, botId),
2610
+ assigneeType: 'bot',
2611
+ assignmentType: 'manual_reassign_bot',
2612
+ actor: resolvedActor,
2613
+ source: resolvedSource,
2614
+ previousAssigneeId: humanRemoved.length > 0 ? String(humanRemoved[0]) : null,
2615
+ removedParticipants: humanRemoved
2616
+ });
2617
+ return;
2618
+ }
2619
+
2620
+ if (assignmentType === 'unassign' || (humanAdded.length === 0 && humanRemoved.length > 0)) {
2621
+ humanRemoved.forEach((assigneeId) => {
2622
+ requestEvent.emit('request.assigned', {
2623
+ request: requestComplete,
2624
+ assigneeId: String(assigneeId),
2625
+ assignmentType: 'unassign',
2626
+ actor: resolvedActor,
2627
+ source: resolvedSource,
2628
+ removedParticipants: humanRemoved,
2629
+ previousAssigneeId: String(assigneeId)
2630
+ });
2631
+ });
2632
+ return;
2633
+ }
2634
+
2635
+ if (humanAdded.length === 0) {
2636
+ return;
2637
+ }
2638
+
2639
+ const resolvedType = assignmentType || assignmentContextUtil.deriveAssignmentType({
2640
+ actor: resolvedActor,
2641
+ assigneeIds: humanAdded,
2642
+ isUnassign: false
2643
+ });
2644
+
2645
+ if (!resolvedType) {
2646
+ return;
2647
+ }
2648
+
2649
+ humanAdded.forEach((assigneeId) => {
2650
+ requestEvent.emit('request.assigned', {
2651
+ request: requestComplete,
2652
+ assigneeId: String(assigneeId),
2653
+ assigneeType: 'user',
2654
+ assignmentType: resolvedType,
2655
+ actor: resolvedActor,
2656
+ source: resolvedSource,
2657
+ removedParticipants: humanRemoved,
2658
+ previousAssigneeId: humanRemoved.length > 0 ? String(humanRemoved[0]) : null
2659
+ });
2660
+ });
2508
2661
  }
2509
2662
 
2510
2663