@tiledesk/tiledesk-server 2.27.0 → 2.27.2

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.
@@ -19,8 +19,22 @@ const { TdCache } = require('../utils/TdCache');
19
19
  const { QuoteManager } = require('./QuoteManager');
20
20
  var configGlobal = require('../config/global');
21
21
  const projectService = require('./projectService');
22
+ const chat21SyncTracer = require('./chat21SyncTracer');
22
23
  const axios = require("axios").default;
23
24
 
25
+ function traceEventEmitted(event, traceRef, request, data) {
26
+ chat21SyncTracer.trace(event, traceRef, 'service.event_emitted', 'info', {
27
+ ...data,
28
+ ...chat21SyncTracer.describeRequest(request),
29
+ chat21_enabled: process.env.CHAT21_ENABLED,
30
+ event_listeners: requestEvent.listenerCount(event)
31
+ });
32
+ }
33
+
34
+ function traceServiceError(event, traceRef, stage, err) {
35
+ chat21SyncTracer.trace(event, traceRef, 'service.error', 'error', { stage: stage, error: chat21SyncTracer.describeError(err) });
36
+ }
37
+
24
38
  let port = process.env.PORT || '3000';
25
39
  let TILEBOT_ENDPOINT = process.env.API_ENDPOINT ? process.env.API_ENDPOINT + "/modules/tilebot/ext/" : ("http://localhost:" + port + "/modules/tilebot/ext/");
26
40
 
@@ -1362,6 +1376,9 @@ class RequestService {
1362
1376
  // winston.info("force is: " + force);
1363
1377
  // }
1364
1378
 
1379
+ const traceRef = { request_id: request_id, id_project: id_project };
1380
+ chat21SyncTracer.trace('request.close', traceRef, 'service.called', 'info', { closed_by: closed_by, force: force, skipStatsUpdate: skipStatsUpdate });
1381
+
1365
1382
  return Request
1366
1383
  .findOne({ request_id: request_id, id_project: id_project })
1367
1384
 
@@ -1375,15 +1392,18 @@ class RequestService {
1375
1392
 
1376
1393
  if (err) {
1377
1394
  winston.error("Error getting closing request with request_id: " + request_id, err);
1395
+ traceServiceError('request.close', traceRef, 'find', err);
1378
1396
  return reject(err);
1379
1397
  }
1380
1398
  if (!request) {
1381
1399
  winston.error("Request not found for request_id " + request_id + " and id_project " + id_project);
1400
+ chat21SyncTracer.trace('request.close', traceRef, 'service.not_found', 'error');
1382
1401
  return reject({ "success": false, msg: "Request not found for request_id " + request_id + " and id_project " + id_project });
1383
1402
  }
1384
1403
  if (force == false && request.status == RequestConstants.CLOSED) {
1385
1404
  // qui1000
1386
1405
  // if (request.statusObj.closed) {
1406
+ traceEventEmitted('request.close', traceRef, request, { already_closed: true });
1387
1407
  requestEvent.emit('request.close', request);
1388
1408
  winston.debug("Request already closed for request_id " + request_id + " and id_project " + id_project);
1389
1409
  return resolve(request);
@@ -1395,6 +1415,7 @@ class RequestService {
1395
1415
 
1396
1416
 
1397
1417
  return that.changeStatusByRequestId(request_id, id_project, 1000).then(function (updatedRequest) {
1418
+ chat21SyncTracer.trace('request.close', traceRef, 'service.status_changed', 'info', chat21SyncTracer.describeRequest(updatedRequest));
1398
1419
  // qui1000
1399
1420
  // return that.changeStatusByRequestId(request_id, id_project, {closed:true}).then(function(updatedRequest) {
1400
1421
 
@@ -1410,6 +1431,7 @@ class RequestService {
1410
1431
  winston.verbose("Request closed with skipStatsUpdate and with id: " + updatedRequest.id);
1411
1432
  winston.debug("Request closed ", updatedRequest);
1412
1433
  //TODO ?? requestEvent.emit('request.update', updatedRequest);
1434
+ traceEventEmitted('request.close', traceRef, updatedRequest, { skipStatsUpdate: true });
1413
1435
  requestEvent.emit('request.close', updatedRequest);
1414
1436
  requestEvent.emit('request.close.extended', { request: updatedRequest, notify: notify });
1415
1437
  return resolve(updatedRequest);
@@ -1421,6 +1443,7 @@ class RequestService {
1421
1443
  winston.verbose("Request closed with id: " + updatedRequest.id);
1422
1444
  winston.debug("Request closed ", updatedRequest);
1423
1445
  //TODO ?? requestEvent.emit('request.update', updatedRequest);
1446
+ traceEventEmitted('request.close', traceRef, updatedRequest, {});
1424
1447
  requestEvent.emit('request.close', updatedRequest);
1425
1448
  requestEvent.emit('request.close.extended', { request: updatedRequest, notify: notify });
1426
1449
 
@@ -1433,6 +1456,7 @@ class RequestService {
1433
1456
  });
1434
1457
  }).catch(function (err) {
1435
1458
  winston.error(err);
1459
+ traceServiceError('request.close', traceRef, 'close_chain', err);
1436
1460
  return reject(err);
1437
1461
  });
1438
1462
  });
@@ -1557,10 +1581,15 @@ class RequestService {
1557
1581
  var that = this;
1558
1582
  return new Promise(function (resolve, reject) {
1559
1583
 
1584
+ const traceEvent = 'request.participants.update';
1585
+ const traceRef = { request_id: request_id, id_project: id_project };
1586
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.called', 'info', { participants: newparticipants, assignmentType: options?.assignmentType, source: options?.source });
1587
+
1560
1588
  var isArray = Array.isArray(newparticipants);
1561
1589
 
1562
1590
  if (isArray == false) {
1563
1591
  winston.error('setParticipantsByRequestId error newparticipants is not an array for request_id ' + request_id + ' and id_project ' + id_project);
1592
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.rejected', 'error', { reason: 'participants is not an array' });
1564
1593
  return reject('setParticipantsByRequestId error newparticipants is not an array for request_id ' + request_id + ' and id_project ' + id_project);
1565
1594
  }
1566
1595
 
@@ -1571,10 +1600,12 @@ class RequestService {
1571
1600
  .exec(function (err, request) {
1572
1601
  if (err) {
1573
1602
  winston.error("Error setParticipantsByRequestId", err);
1603
+ traceServiceError(traceEvent, traceRef, 'find', err);
1574
1604
  return reject(err);
1575
1605
  }
1576
1606
  if (!request) {
1577
1607
  winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1608
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.not_found', 'error');
1578
1609
  return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1579
1610
  }
1580
1611
 
@@ -1582,12 +1613,14 @@ class RequestService {
1582
1613
  if (Array.isArray(request.participantsAgents)) {
1583
1614
  if (request.participantsAgents.length === 1) {
1584
1615
  winston.error('Cannot add participants: participantsAgents already has one element for request_id ' + request_id + ' and id_project ' + id_project);
1616
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.rejected', 'error', { reason: 'only one participant allowed', channel: request.channel.name });
1585
1617
  return reject('Cannot add participants: only one participant allowed for this request');
1586
1618
  } else if (request.participantsAgents.length === 0) {
1587
1619
  if (Array.isArray(newparticipants) && newparticipants.length === 1) {
1588
1620
  // ok, allow to add one participant
1589
1621
  } else {
1590
1622
  winston.error('Can only add one participant for request_id ' + request_id + ' and id_project ' + id_project);
1623
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.rejected', 'error', { reason: 'can only add one participant', channel: request.channel.name });
1591
1624
  return reject('Can only add one participant for this request');
1592
1625
  }
1593
1626
  }
@@ -1601,6 +1634,7 @@ class RequestService {
1601
1634
  if (requestUtil.arraysEqual(oldParticipants, newparticipants)) {
1602
1635
  //if (oldParticipants === newparticipants) {
1603
1636
  winston.verbose('Request members ' + oldParticipants + ' already equal to ' + newparticipants + ' for request_id ' + request_id + ' and id_project ' + id_project);
1637
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.unchanged', 'skipped', chat21SyncTracer.describeRequest(request));
1604
1638
  return request
1605
1639
  .populate('lead')
1606
1640
  .populate('department')
@@ -1660,6 +1694,7 @@ class RequestService {
1660
1694
  // dopo save non aggiorna participating
1661
1695
  if (err) {
1662
1696
  winston.error("Error setParticipantsByRequestId", err);
1697
+ traceServiceError(traceEvent, traceRef, 'save', err);
1663
1698
  return reject(err);
1664
1699
  }
1665
1700
 
@@ -1674,6 +1709,7 @@ class RequestService {
1674
1709
 
1675
1710
  if (err) {
1676
1711
  winston.error("Error getting setParticipantsByRequestId", err);
1712
+ traceServiceError(traceEvent, traceRef, 'populate', err);
1677
1713
  return reject(err);
1678
1714
  }
1679
1715
 
@@ -1693,6 +1729,7 @@ class RequestService {
1693
1729
  requestEvent.emit("request.update.comment", { comment: "PARTICIPANTS_SET", request: requestComplete });//Deprecated
1694
1730
  requestEvent.emit("request.updated", { comment: "PARTICIPANTS_SET", request: requestComplete, patch: { removedParticipants: removedParticipants, addedParticipants: addedParticipants } });
1695
1731
 
1732
+ traceEventEmitted(traceEvent, traceRef, requestComplete, { removedParticipants: removedParticipants, addedParticipants: addedParticipants });
1696
1733
  requestEvent.emit('request.participants.update', {
1697
1734
  beforeRequest: request,
1698
1735
  removedParticipants: removedParticipants,
@@ -1730,9 +1767,14 @@ class RequestService {
1730
1767
  // validate member is string
1731
1768
  return new Promise(function (resolve, reject) {
1732
1769
 
1770
+ const traceEvent = 'request.participants.join';
1771
+ const traceRef = { request_id: request_id, id_project: id_project };
1772
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.called', 'info', { member: member, assignmentType: options?.assignmentType, source: options?.source });
1773
+
1733
1774
  if (member == undefined) {
1734
1775
  var err = "addParticipantByRequestId error, member field is null";
1735
1776
  winston.error(err);
1777
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.rejected', 'error', { reason: 'member is null' });
1736
1778
  return reject(err);
1737
1779
  }
1738
1780
 
@@ -1742,10 +1784,12 @@ class RequestService {
1742
1784
  .exec(function (err, request) {
1743
1785
  if (err) {
1744
1786
  winston.error("Error adding participant ", err);
1787
+ traceServiceError(traceEvent, traceRef, 'find', err);
1745
1788
  return reject(err);
1746
1789
  }
1747
1790
  if (!request) {
1748
1791
  winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1792
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.not_found', 'error');
1749
1793
  return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1750
1794
  }
1751
1795
 
@@ -1781,6 +1825,7 @@ class RequestService {
1781
1825
  request.save(function (err, savedRequest) {
1782
1826
  if (err) {
1783
1827
  winston.error(err);
1828
+ traceServiceError(traceEvent, traceRef, 'save', err);
1784
1829
  return reject(err);
1785
1830
  }
1786
1831
 
@@ -1796,6 +1841,7 @@ class RequestService {
1796
1841
 
1797
1842
  if (err) {
1798
1843
  winston.error("Error getting addParticipantByRequestId", err);
1844
+ traceServiceError(traceEvent, traceRef, 'populate', err);
1799
1845
  return reject(err);
1800
1846
  }
1801
1847
 
@@ -1805,6 +1851,7 @@ class RequestService {
1805
1851
  requestEvent.emit('request.update', requestComplete);
1806
1852
  requestEvent.emit("request.update.comment", { comment: "PARTICIPANT_ADD", request: requestComplete });//Deprecated
1807
1853
  requestEvent.emit("request.updated", { comment: "PARTICIPANT_ADD", request: requestComplete, patch: { member: member } });
1854
+ traceEventEmitted(traceEvent, traceRef, requestComplete, { member: member });
1808
1855
  requestEvent.emit('request.participants.join', { member: member, request: requestComplete });
1809
1856
 
1810
1857
  that.emitAssignedEvent(requestComplete, {
@@ -1821,6 +1868,7 @@ class RequestService {
1821
1868
  // qui assignetat
1822
1869
  } else {
1823
1870
  winston.debug('Request member ' + member + ' already added for request_id ' + request_id + ' and id_project ' + id_project);
1871
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.unchanged', 'skipped', { member: member, ...chat21SyncTracer.describeRequest(request) });
1824
1872
  return request
1825
1873
  .populate('lead')
1826
1874
  .populate('department')
@@ -1843,11 +1891,14 @@ class RequestService {
1843
1891
 
1844
1892
  return new Promise(function (resolve, reject) {
1845
1893
 
1846
-
1894
+ const traceEvent = 'request.participants.leave';
1895
+ const traceRef = { request_id: request_id, id_project: id_project };
1896
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.called', 'info', { member: member, source: options?.source });
1847
1897
 
1848
1898
  if (member == undefined) {
1849
1899
  var err = "removeParticipantByRequestId error, member field is null";
1850
1900
  winston.error(err);
1901
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.rejected', 'error', { reason: 'member is null' });
1851
1902
  return reject(err);
1852
1903
  }
1853
1904
 
@@ -1860,11 +1911,13 @@ class RequestService {
1860
1911
 
1861
1912
  if (err) {
1862
1913
  winston.error("Error removing participant ", err);
1914
+ traceServiceError(traceEvent, traceRef, 'find', err);
1863
1915
  return reject(err);
1864
1916
  }
1865
1917
 
1866
1918
  if (!request) {
1867
1919
  winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1920
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.not_found', 'error');
1868
1921
  return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1869
1922
  }
1870
1923
 
@@ -1942,6 +1995,7 @@ class RequestService {
1942
1995
  return request.save(function (err, savedRequest) {
1943
1996
  if (err) {
1944
1997
  //winston.error("Error saving removed participant ", err);
1998
+ traceServiceError(traceEvent, traceRef, 'save', err);
1945
1999
  return reject(err);
1946
2000
  }
1947
2001
 
@@ -1955,6 +2009,7 @@ class RequestService {
1955
2009
 
1956
2010
  if (err) {
1957
2011
  winston.error("Error getting removed participant ", err);
2012
+ traceServiceError(traceEvent, traceRef, 'populate', err);
1958
2013
  return reject(err);
1959
2014
  }
1960
2015
 
@@ -1962,6 +2017,7 @@ class RequestService {
1962
2017
  requestEvent.emit('request.update', requestComplete);
1963
2018
  requestEvent.emit("request.update.comment", { comment: "PARTICIPANT_REMOVE", request: requestComplete });//Deprecated
1964
2019
  requestEvent.emit("request.updated", { comment: "PARTICIPANT_REMOVE", request: requestComplete, patch: { member: member } });
2020
+ traceEventEmitted(traceEvent, traceRef, requestComplete, { member: member });
1965
2021
  requestEvent.emit('request.participants.leave', {
1966
2022
  member: member,
1967
2023
  request: requestComplete,
@@ -1979,6 +2035,7 @@ class RequestService {
1979
2035
 
1980
2036
  } else {
1981
2037
  winston.verbose('Request member ' + member + ' already not found for request_id ' + request_id + ' and id_project ' + id_project);
2038
+ chat21SyncTracer.trace(traceEvent, traceRef, 'service.unchanged', 'skipped', { member: member, ...chat21SyncTracer.describeRequest(request) });
1982
2039
 
1983
2040
  return request
1984
2041
  .populate('lead')