@tiledesk/tiledesk-server 3.3.5 → 3.5.1

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.
Files changed (75) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/app.js +7 -0
  3. package/deploynew.sh +7 -1
  4. package/docs/activities-client.md +27 -0
  5. package/docs/mcp-oauth2-it.md +279 -0
  6. package/docs/mcp-oauth2.md +279 -0
  7. package/docs/numeric-input-validation.md +91 -0
  8. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server-it.md +155 -0
  9. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server.md +155 -0
  10. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot-it.md +63 -0
  11. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot.md +63 -0
  12. package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design-it.md +207 -0
  13. package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design.md +207 -0
  14. package/jobs.js +15 -0
  15. package/models/mcpOAuthConnection.js +131 -0
  16. package/models/request.js +8 -0
  17. package/package.json +1 -1
  18. package/pubmodules/activities/activityMessageUtil.js +137 -0
  19. package/pubmodules/activities/models/activity.js +2 -0
  20. package/pubmodules/activities/routes/activity.js +39 -11
  21. package/pubmodules/activities/test/execution-filter.test.js +133 -0
  22. package/pubmodules/activities/test/execution-messages.test.js +158 -0
  23. package/pubmodules/canned/cannedResponseRoute.js +6 -9
  24. package/pubmodules/events/eventRoute.js +6 -9
  25. package/pubmodules/executions-publisher/index.js +457 -0
  26. package/pubmodules/executions-publisher/test/audit-consumer.test.js +384 -0
  27. package/pubmodules/executions-publisher/test/executions-publisher.test.js +204 -0
  28. package/pubmodules/pubModulesManager.js +12 -0
  29. package/routes/answered.js +7 -3
  30. package/routes/campaigns.js +7 -3
  31. package/routes/department.js +14 -3
  32. package/routes/executions-refire.js +148 -0
  33. package/routes/faq.js +6 -14
  34. package/routes/filesp.js +9 -4
  35. package/routes/kb.js +40 -15
  36. package/routes/lead.js +23 -28
  37. package/routes/logs.js +8 -1
  38. package/routes/mcp.js +11 -8
  39. package/routes/mcpOAuth.js +133 -0
  40. package/routes/mcpOAuthCallback.js +75 -0
  41. package/routes/message.js +3 -1
  42. package/routes/messagesRoot.js +5 -18
  43. package/routes/project.js +57 -12
  44. package/routes/project_user.js +21 -4
  45. package/routes/property.js +18 -20
  46. package/routes/request.js +36 -45
  47. package/routes/roles.js +8 -18
  48. package/routes/segment.js +8 -18
  49. package/routes/subscription.js +4 -5
  50. package/routes/tag.js +6 -9
  51. package/routes/unanswered.js +7 -3
  52. package/routes/user-request.js +29 -34
  53. package/routes/webhook.js +120 -54
  54. package/services/mcpOAuthDiscovery.js +248 -0
  55. package/services/mcpOAuthGoogleScopes.js +55 -0
  56. package/services/mcpOAuthService.js +732 -0
  57. package/services/mcpService.js +111 -15
  58. package/services/requestService.js +4 -2
  59. package/services/webhookService.js +4 -2
  60. package/test/executions-refire.test.js +362 -0
  61. package/test/kbRoute.js +4 -7
  62. package/test/mcpOAuthDiscovery.test.js +331 -0
  63. package/test/mcpOAuthGoogleScopes.test.js +25 -0
  64. package/test/mcpOAuthRoute.test.js +455 -0
  65. package/test/mcpOAuthService.test.js +693 -0
  66. package/test/mcpOAuthUtils.test.js +348 -0
  67. package/test/mcpServiceOAuth.test.js +267 -0
  68. package/test/numericInput.js +137 -0
  69. package/test/requestRoute.js +57 -0
  70. package/test/webhookConversationRoute.js +51 -0
  71. package/utils/logRedactor.js +4 -2
  72. package/utils/mcpOAuthCrypto.js +69 -0
  73. package/utils/mcpOAuthPkce.js +20 -0
  74. package/utils/numericInput.js +200 -0
  75. package/utils/outboundUrlGuard.js +131 -0
@@ -5,6 +5,7 @@ const activityMessageUtil = require('../activityMessageUtil');
5
5
  const winston = require('../../../config/winston');
6
6
  const moment = require('moment');
7
7
  const ObjectId = require('mongoose').Types.ObjectId;
8
+ const numericInput = require('../../../utils/numericInput');
8
9
 
9
10
  csv = require('csv-express');
10
11
  csv.separator = ';';
@@ -50,12 +51,30 @@ function resolveActivityQueryLimit(query) {
50
51
  };
51
52
  }
52
53
 
54
+ const EXECUTION_AUDIT_PERMISSION = 'executions_audit_read';
55
+
56
+ // EXECUTION_* rows are visible to the project owner and to custom roles that carry
57
+ // the executions_audit_read permission (hasRole sets req.projectuser, with
58
+ // _doc.rolePermissions for custom roles).
59
+ function canReadExecutionAudit(projectuser) {
60
+ if (!projectuser) return false;
61
+ if (projectuser.role === 'owner') return true;
62
+ const perms = projectuser._doc && projectuser._doc.rolePermissions;
63
+ return Array.isArray(perms) && perms.includes(EXECUTION_AUDIT_PERMISSION);
64
+ }
65
+
66
+ function hideExecutionVerbs(query) {
67
+ query.verb = Object.assign({}, query.verb, { $not: /^EXECUTION_/ });
68
+ }
69
+
53
70
  router.get('/', async (req, res) => {
54
71
 
55
72
  let id_project = req.projectid;
56
73
  const limitConfig = resolveActivityQueryLimit(req.query);
57
74
  let limit = limitConfig.limit;
58
- let page = parseInt(req.query.page) || 0;
75
+ let page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
76
+ if (page === null) return;
77
+ if (page === undefined) page = 0;
59
78
  let skip = page * limit;
60
79
 
61
80
 
@@ -101,6 +120,10 @@ router.get('/', async (req, res) => {
101
120
  };
102
121
  }
103
122
 
123
+ if (!canReadExecutionAudit(req.projectuser)) {
124
+ hideExecutionVerbs(query);
125
+ }
126
+
104
127
  let direction = parseInt(req.query.direction) || -1;
105
128
  let sortField = req.query.sort || "createdAt";
106
129
 
@@ -147,11 +170,9 @@ router.get('/', async (req, res) => {
147
170
  // DOWNLOAD ACTIVITIES AS CSV
148
171
  router.get('/csv', function (req, res) {
149
172
  var limit = 100000; // Number of activities per page
150
- var page = 0;
151
-
152
- if (req.query.page) {
153
- page = req.query.page;
154
- }
173
+ var page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
174
+ if (page === null) return;
175
+ if (page === undefined) page = 0;
155
176
 
156
177
  var skip = page * limit;
157
178
  winston.debug('Activity ROUTE - SKIP PAGE ', skip);
@@ -215,22 +236,24 @@ router.get('/csv', function (req, res) {
215
236
  let verbs = req.query.activities.split(",")
216
237
 
217
238
  winston.debug('verbs: ', verbs);
218
- query.verb = verbs;
239
+ query.verb = { $in: verbs };
219
240
 
220
241
  // to test
221
242
  // query.verb = ['PROJECT_USER_DELETE','PROJECT_USER_INVITE']
222
243
  }
223
244
 
245
+ if (!canReadExecutionAudit(req.projectuser)) {
246
+ hideExecutionVerbs(query);
247
+ }
248
+
224
249
  if (req.query.lang) {
225
250
  winston.debug('req.query.lang:', req.query.lang);
226
251
  var lang = req.query.lang;
227
252
  }
228
253
 
229
254
 
230
- var direction = -1; //-1 descending , 1 ascending
231
- if (req.query.direction) {
232
- direction = req.query.direction;
233
- }
255
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
256
+ if (direction === null) return;
234
257
  winston.debug("direction", direction);
235
258
 
236
259
  var sortField = "createdAt";
@@ -579,6 +602,10 @@ router.get('/csv', function (req, res) {
579
602
  message = buildMsg_REQUEST_CREATE(lang, activity)
580
603
  }
581
604
 
605
+ if (activityMessageUtil.buildExecutionMessage(activity, lang)) {
606
+ message = activityMessageUtil.buildExecutionMessage(activity, lang);
607
+ }
608
+
582
609
  if (!message) {
583
610
  message = activityMessageUtil.buildDefaultActivityMessage(activity) || '';
584
611
  }
@@ -678,3 +705,4 @@ router.get('/csv', function (req, res) {
678
705
 
679
706
 
680
707
  module.exports = router;
708
+ module.exports.canReadExecutionAudit = canReadExecutionAudit;
@@ -0,0 +1,133 @@
1
+ process.env.NODE_ENV = 'test';
2
+
3
+ // Mounts the activities router with a stubbed req.projectuser (what hasRole sets in production),
4
+ // so the tests do not need the full app/auth harness. Mongo: a dedicated test database.
5
+ const { expect } = require('chai');
6
+ const express = require('express');
7
+ const http = require('http');
8
+ const mongoose = require('mongoose');
9
+
10
+ const Activity = require('../models/activity');
11
+ const router = require('../routes/activity');
12
+ const { canReadExecutionAudit } = router;
13
+
14
+ const MONGO = process.env.ACTIVITIES_TEST_MONGO || 'mongodb://127.0.0.1:27017/tiledesk-activities-filter-test';
15
+ const PROJECT = new mongoose.Types.ObjectId().toString();
16
+
17
+ function pu(role, rolePermissions) {
18
+ const doc = { role: role };
19
+ if (rolePermissions) doc.rolePermissions = rolePermissions;
20
+ return { role: role, _doc: doc };
21
+ }
22
+
23
+ function get(projectuser, path) {
24
+ const app = express();
25
+ app.use((req, res, next) => { req.projectid = PROJECT; req.projectuser = projectuser; next(); });
26
+ app.use('/', router);
27
+ return new Promise((resolve, reject) => {
28
+ const srv = app.listen(0, () => {
29
+ http.get({ port: srv.address().port, path: path }, (res) => {
30
+ let body = '';
31
+ res.on('data', (c) => body += c);
32
+ res.on('end', () => { srv.close(); resolve({ status: res.statusCode, body: body }); });
33
+ }).on('error', (e) => { srv.close(); reject(e); });
34
+ });
35
+ });
36
+ }
37
+
38
+ describe('activities: EXECUTION_* rows are owner-only', function () {
39
+ this.timeout(20000);
40
+
41
+ before(async function () {
42
+ // run together with other suites: reuse their connection (rows are scoped by PROJECT)
43
+ if (mongoose.connection.readyState === 0) await mongoose.connect(MONGO);
44
+ await Activity.deleteMany({ id_project: PROJECT });
45
+ const mk = (verb) => ({ id_project: PROJECT, verb: verb, actor: { type: 'user', id: 'u1', name: 'Mario' },
46
+ target: { type: 'execution', id: 'abcdef123456', object: { agent_name: 'Bot' } }, actionObj: {} });
47
+ await Activity.create([mk('EXECUTION_DESTROY'), mk('EXECUTION_STOP_SOFT'), mk('REQUEST_CLOSE')]);
48
+ });
49
+
50
+ after(async function () {
51
+ await Activity.deleteMany({ id_project: PROJECT });
52
+ await mongoose.disconnect();
53
+ });
54
+
55
+ const verbsOf = (r) => JSON.parse(r.body).activities.map((a) => a.verb).sort();
56
+
57
+ it('canReadExecutionAudit', function () {
58
+ expect(canReadExecutionAudit(pu('owner'))).to.equal(true);
59
+ expect(canReadExecutionAudit(pu('admin'))).to.equal(false);
60
+ expect(canReadExecutionAudit(pu('custom', ['executions_audit_read']))).to.equal(true);
61
+ expect(canReadExecutionAudit(pu('custom', ['other']))).to.equal(false);
62
+ expect(canReadExecutionAudit(undefined)).to.equal(false);
63
+ });
64
+
65
+ it('owner sees EXECUTION_ rows', async function () {
66
+ const r = await get(pu('owner'), '/');
67
+ expect(r.status).to.equal(200);
68
+ expect(verbsOf(r)).to.deep.equal(['EXECUTION_DESTROY', 'EXECUTION_STOP_SOFT', 'REQUEST_CLOSE']);
69
+ });
70
+
71
+ it('data access and retention rows (retention has a project target) are owner-only too', async function () {
72
+ const base = { id_project: PROJECT, actor: { type: 'user', id: 'u1', name: 'Mario' }, actionObj: {} };
73
+ const rows = await Activity.create([
74
+ Object.assign({ verb: 'EXECUTION_DATA_ACCESS', target: { type: 'execution', id: 'abcdef123456', object: {} } }, base),
75
+ Object.assign({ verb: 'EXECUTION_DATA_ACCESS_DENIED', target: { type: 'execution', id: 'abcdef123456', object: {} } }, base),
76
+ Object.assign({ verb: 'EXECUTION_RETENTION_UPDATE', target: { type: 'project', id: PROJECT, object: {} } }, base)
77
+ ]);
78
+ try {
79
+ const owner = verbsOf(await get(pu('owner'), '/'));
80
+ expect(owner).to.include.members(['EXECUTION_DATA_ACCESS', 'EXECUTION_DATA_ACCESS_DENIED', 'EXECUTION_RETENTION_UPDATE']);
81
+ expect(verbsOf(await get(pu('admin'), '/'))).to.deep.equal(['REQUEST_CLOSE']);
82
+ } finally {
83
+ await Activity.deleteMany({ _id: { $in: rows.map((r) => r._id) } });
84
+ }
85
+ });
86
+
87
+ it('admin does not (list)', async function () {
88
+ const r = await get(pu('admin'), '/');
89
+ expect(verbsOf(r)).to.deep.equal(['REQUEST_CLOSE']);
90
+ expect(JSON.parse(r.body).count).to.equal(1);
91
+ });
92
+
93
+ it('csv for admin excludes EXECUTION_ verbs', async function () {
94
+ const r = await get(pu('admin'), '/csv');
95
+ expect(r.status).to.equal(200);
96
+ expect(r.body).to.not.contain('EXECUTION_');
97
+ expect(r.body).to.contain('REQUEST_CLOSE');
98
+ });
99
+
100
+ it('csv for owner includes EXECUTION_ verbs, also when filtered', async function () {
101
+ const r = await get(pu('owner'), '/csv?activities=EXECUTION_DESTROY,REQUEST_CLOSE');
102
+ expect(r.body).to.contain('EXECUTION_DESTROY');
103
+ expect(r.body).to.contain('REQUEST_CLOSE');
104
+ expect(r.body).to.not.contain('EXECUTION_STOP_SOFT');
105
+ });
106
+
107
+ it('csv for admin with a verb filter keeps non-execution verbs only', async function () {
108
+ const r = await get(pu('admin'), '/csv?activities=EXECUTION_DESTROY,REQUEST_CLOSE');
109
+ expect(r.body).to.not.contain('EXECUTION_');
110
+ expect(r.body).to.contain('REQUEST_CLOSE');
111
+ });
112
+
113
+ it('custom role with executions_audit_read sees them', async function () {
114
+ const r = await get(pu('auditor', ['executions_audit_read']), '/');
115
+ expect(verbsOf(r)).to.include('EXECUTION_DESTROY');
116
+ });
117
+
118
+ it('custom role without the permission does not', async function () {
119
+ const r = await get(pu('other', ['something']), '/');
120
+ expect(verbsOf(r)).to.deep.equal(['REQUEST_CLOSE']);
121
+ });
122
+
123
+ it('requested activities=EXECUTION_DESTROY for admin returns none', async function () {
124
+ const r = await get(pu('admin'), '/?activities=EXECUTION_DESTROY');
125
+ expect(JSON.parse(r.body).activities).to.have.length(0);
126
+ expect(JSON.parse(r.body).count).to.equal(0);
127
+ });
128
+
129
+ it('requested activities=EXECUTION_DESTROY for owner returns it', async function () {
130
+ const r = await get(pu('owner'), '/?activities=EXECUTION_DESTROY');
131
+ expect(verbsOf(r)).to.deep.equal(['EXECUTION_DESTROY']);
132
+ });
133
+ });
@@ -0,0 +1,158 @@
1
+ process.env.NODE_ENV = 'test';
2
+
3
+ const { expect } = require('chai');
4
+ const util = require('../activityMessageUtil');
5
+
6
+ function act(verb, reason) {
7
+ return {
8
+ verb: verb,
9
+ actor: { type: 'user', id: 'u1', name: 'Mario' },
10
+ target: { type: 'execution', id: 'abcdef123456', object: { agent_name: 'Support Bot' } },
11
+ actionObj: reason ? { reason: reason } : {}
12
+ };
13
+ }
14
+
15
+ describe('execution activity messages', function () {
16
+ it('sentences en/it for each verb', function () {
17
+ expect(util.buildDefaultActivityMessage(act('EXECUTION_STOP_SOFT')))
18
+ .to.equal('Mario paused execution 123456 of Support Bot after the current action');
19
+ expect(util.buildDefaultActivityMessage(act('EXECUTION_STOP_HARD')))
20
+ .to.equal('Mario paused execution 123456 of Support Bot now');
21
+ expect(util.buildDefaultActivityMessage(act('EXECUTION_DESTROY')))
22
+ .to.equal('Mario destroyed execution 123456 of Support Bot');
23
+ expect(util.buildExecutionMessage(act('EXECUTION_STOP_SOFT'), 'it'))
24
+ .to.equal('Mario ha messo in pausa l\'esecuzione 123456 di Support Bot dopo l\'azione corrente');
25
+ expect(util.buildExecutionMessage(act('EXECUTION_STOP_HARD'), 'it'))
26
+ .to.equal('Mario ha messo in pausa l\'esecuzione 123456 di Support Bot subito');
27
+ expect(util.buildExecutionMessage(act('EXECUTION_DESTROY'), 'it'))
28
+ .to.equal('Mario ha eliminato l\'esecuzione 123456 di Support Bot');
29
+ expect(util.buildExecutionMessage(act('EXECUTION_DESTROY'), 'en'))
30
+ .to.equal('Mario destroyed execution 123456 of Support Bot');
31
+ });
32
+
33
+ it('the short id is the random tail of a uuid v7 (last 6 hex chars without dashes), not its timestamp prefix', function () {
34
+ const a = act('EXECUTION_DESTROY');
35
+ a.target.id = '01a1c193-4e44-73c1-91d5-373a5c5d8888';
36
+ expect(util.buildDefaultActivityMessage(a)).to.equal('Mario destroyed execution 5d8888 of Support Bot');
37
+ });
38
+
39
+ it('appends the reason when present', function () {
40
+ expect(util.buildDefaultActivityMessage(act('EXECUTION_DESTROY', 'cleanup')))
41
+ .to.equal('Mario destroyed execution 123456 of Support Bot · "cleanup"');
42
+ expect(util.buildExecutionMessage(act('EXECUTION_STOP_SOFT', 'x'), 'it'))
43
+ .to.equal('Mario ha messo in pausa l\'esecuzione 123456 di Support Bot dopo l\'azione corrente · "x"');
44
+ });
45
+
46
+ it('a rejected control reads as an attempt, with the reason it was refused (en/it)', function () {
47
+ const rej = (verb, result, reason) => Object.assign(act(verb, reason), { actionObj: Object.assign({ result: result }, reason ? { reason: reason } : {}) });
48
+ expect(util.buildDefaultActivityMessage(rej('EXECUTION_STOP_HARD', 'rejected:stopped')))
49
+ .to.equal('Mario tried to pause execution 123456 of Support Bot now (rejected: paused)');
50
+ expect(util.buildDefaultActivityMessage(rej('EXECUTION_STOP_SOFT', 'rejected:completed')))
51
+ .to.equal('Mario tried to pause execution 123456 of Support Bot after the current action (rejected: completed)');
52
+ expect(util.buildDefaultActivityMessage(rej('EXECUTION_DESTROY', 'rejected:destroyed', 'again')))
53
+ .to.equal('Mario tried to destroy execution 123456 of Support Bot (rejected: destroyed) · "again"');
54
+ expect(util.buildDefaultActivityMessage(rej('EXECUTION_DESTROY', 'rejected')))
55
+ .to.equal('Mario tried to destroy execution 123456 of Support Bot (rejected)');
56
+ expect(util.buildExecutionMessage(rej('EXECUTION_STOP_HARD', 'rejected:stopped'), 'it'))
57
+ .to.equal('Mario ha provato a mettere in pausa l\'esecuzione 123456 di Support Bot subito (rifiutato: paused)');
58
+ expect(util.buildExecutionMessage(rej('EXECUTION_STOP_SOFT', 'rejected:completed'), 'it'))
59
+ .to.equal('Mario ha provato a mettere in pausa l\'esecuzione 123456 di Support Bot dopo l\'azione corrente (rifiutato: completed)');
60
+ expect(util.buildExecutionMessage(rej('EXECUTION_DESTROY', 'rejected:destroyed'), 'it'))
61
+ .to.equal('Mario ha provato a eliminare l\'esecuzione 123456 di Support Bot (rifiutato: destroyed)');
62
+ expect(util.buildDefaultActivityMessage(rej('EXECUTION_DESTROY', 'rejected:stopping')))
63
+ .to.equal('Mario tried to destroy execution 123456 of Support Bot (rejected: pausing)');
64
+ // accepted (or no result, as older rows): the plain sentence
65
+ expect(util.buildDefaultActivityMessage(rej('EXECUTION_STOP_HARD', 'accepted')))
66
+ .to.equal('Mario paused execution 123456 of Support Bot now');
67
+ });
68
+
69
+ it('audit resume/start_new map to the new verbs and sentences en/it', function () {
70
+ expect(util.buildDefaultActivityMessage(act('EXECUTION_RESUME')))
71
+ .to.equal('Mario resumed execution 123456 of Support Bot');
72
+ expect(util.buildDefaultActivityMessage(act('EXECUTION_START_NEW')))
73
+ .to.equal('Mario started a new run of execution 123456 of Support Bot');
74
+ expect(util.buildExecutionMessage(act('EXECUTION_RESUME'), 'it'))
75
+ .to.equal('Mario ha ripreso l\'esecuzione 123456 di Support Bot');
76
+ expect(util.buildExecutionMessage(act('EXECUTION_START_NEW'), 'it'))
77
+ .to.equal('Mario ha avviato una nuova esecuzione da 123456 di Support Bot');
78
+ expect(util.buildExecutionMessage(act('EXECUTION_START_NEW', 'retry'), 'en'))
79
+ .to.equal('Mario started a new run of execution 123456 of Support Bot · "retry"');
80
+
81
+ const rej = (verb, result) => Object.assign(act(verb), { actionObj: { result: result } });
82
+ expect(util.buildDefaultActivityMessage(rej('EXECUTION_RESUME', 'rejected:not_resumable')))
83
+ .to.equal('Mario tried to resume execution 123456 of Support Bot (rejected: not_resumable)');
84
+ expect(util.buildDefaultActivityMessage(rej('EXECUTION_START_NEW', 'rejected:already_started')))
85
+ .to.equal('Mario tried to start a new run of execution 123456 of Support Bot (rejected: already_started)');
86
+ expect(util.buildExecutionMessage(rej('EXECUTION_RESUME', 'rejected:not_resumable'), 'it'))
87
+ .to.equal('Mario ha provato a riprendere l\'esecuzione 123456 di Support Bot (rifiutato: not_resumable)');
88
+ expect(util.buildExecutionMessage(rej('EXECUTION_START_NEW', 'rejected:already_started'), 'it'))
89
+ .to.equal('Mario ha provato ad avviare una nuova esecuzione da 123456 di Support Bot (rifiutato: already_started)');
90
+
91
+ // an unconfirmed refire may have started the run: it reads as started, flagged
92
+ expect(util.buildDefaultActivityMessage(rej('EXECUTION_START_NEW', 'rejected:refire_unconfirmed')))
93
+ .to.equal('Mario started a new run of execution 123456 of Support Bot (unconfirmed)');
94
+ expect(util.buildExecutionMessage(rej('EXECUTION_START_NEW', 'rejected:refire_unconfirmed'), 'it'))
95
+ .to.equal('Mario ha avviato una nuova esecuzione da 123456 di Support Bot (non confermata)');
96
+ // other verbs keep the tried phrasing
97
+ expect(util.buildExecutionMessage(rej('EXECUTION_RESUME', 'rejected:refire_unconfirmed'), 'en'))
98
+ .to.equal('Mario tried to resume execution 123456 of Support Bot (rejected: refire_unconfirmed)');
99
+ });
100
+
101
+ describe('data access and retention', function () {
102
+ const da = (verb, actionObj) => Object.assign(act(verb), { actionObj: actionObj });
103
+ const ret = (before, after) => ({
104
+ verb: 'EXECUTION_RETENTION_UPDATE',
105
+ actor: { type: 'user', id: 'u1', name: 'Mario' },
106
+ target: { type: 'project', id: 'p1', object: {} },
107
+ actionObj: { result: 'accepted', retention: { before: before, after: after } }
108
+ });
109
+
110
+ it('EXECUTION_DATA_ACCESS: reason + one block or whole execution (en/it)', function () {
111
+ const step = da('EXECUTION_DATA_ACCESS', { result: 'accepted', reason: 'debug', scope: { type: 'step', step_seq: 3 } });
112
+ const whole = da('EXECUTION_DATA_ACCESS', { result: 'accepted', reason: 'debug', scope: { type: 'execution' } });
113
+ expect(util.buildDefaultActivityMessage(step))
114
+ .to.equal('Mario viewed the data of execution 123456 of Support Bot · "debug" · 1 block');
115
+ expect(util.buildDefaultActivityMessage(whole))
116
+ .to.equal('Mario viewed the data of execution 123456 of Support Bot · "debug" · whole execution');
117
+ expect(util.buildExecutionMessage(step, 'it'))
118
+ .to.equal('Mario ha visualizzato i dati dell\'esecuzione 123456 di Support Bot · "debug" · 1 blocco');
119
+ expect(util.buildExecutionMessage(whole, 'it'))
120
+ .to.equal('Mario ha visualizzato i dati dell\'esecuzione 123456 di Support Bot · "debug" · intera esecuzione');
121
+ });
122
+
123
+ it('EXECUTION_DATA_ACCESS_DENIED carries the role (en/it)', function () {
124
+ const a = da('EXECUTION_DATA_ACCESS_DENIED', { result: 'rejected:forbidden', role: 'agent' });
125
+ expect(util.buildDefaultActivityMessage(a))
126
+ .to.equal('Mario was denied access to the data of execution 123456 of Support Bot (role agent)');
127
+ expect(util.buildExecutionMessage(a, 'it'))
128
+ .to.equal('Mario non ha ottenuto l\'accesso ai dati dell\'esecuzione 123456 di Support Bot (ruolo agent)');
129
+ });
130
+
131
+ it('EXECUTION_STOP_NOTICE_UPDATE states the setting after the change (en/it)', function () {
132
+ const sn = (after) => ({ verb: 'EXECUTION_STOP_NOTICE_UPDATE', actor: { type: 'user', id: 'u1', name: 'Mario' },
133
+ target: { type: 'project', id: 'p1', object: {} }, actionObj: { result: 'accepted', stop_notice: { before: { enabled: true, text: null }, after } } });
134
+ expect(util.buildDefaultActivityMessage(sn({ enabled: false, text: null })))
135
+ .to.equal('Mario changed the executions pause notice: off');
136
+ expect(util.buildDefaultActivityMessage(sn({ enabled: true, text: 'Back soon' })))
137
+ .to.equal('Mario changed the executions pause notice: on, custom text');
138
+ expect(util.buildExecutionMessage(sn({ enabled: true, text: null }), 'it'))
139
+ .to.equal("Mario ha modificato l'avviso di esecuzione in pausa: attivo, testo predefinito");
140
+ expect(util.buildDefaultActivityMessage(Object.assign(sn({}), { actionObj: {} })))
141
+ .to.equal('Mario changed the executions pause notice');
142
+ });
143
+
144
+ it('EXECUTION_RETENTION_UPDATE shows only the changed parts (en/it)', function () {
145
+ const both = ret({ trace_days: 90, data_days: 30 }, { trace_days: 30, data_days: 7 });
146
+ expect(util.buildDefaultActivityMessage(both))
147
+ .to.equal('Mario changed executions retention: trace 90 → 30 days, data 30 → 7 days');
148
+ expect(util.buildExecutionMessage(both, 'it'))
149
+ .to.equal('Mario ha modificato la conservazione delle esecuzioni: tracce 90 → 30 giorni, dati 30 → 7 giorni');
150
+ const onlyTrace = ret({ trace_days: 90, data_days: 30 }, { trace_days: 60, data_days: 30 });
151
+ expect(util.buildDefaultActivityMessage(onlyTrace))
152
+ .to.equal('Mario changed executions retention: trace 90 → 60 days');
153
+ const onlyData = ret({ trace_days: 90, data_days: 30 }, { trace_days: 90, data_days: 0 });
154
+ expect(util.buildExecutionMessage(onlyData, 'it'))
155
+ .to.equal('Mario ha modificato la conservazione delle esecuzioni: dati 30 → 0 giorni');
156
+ });
157
+ });
158
+ });
@@ -4,6 +4,7 @@ var CannedResponse = require("./cannedResponse");
4
4
  var winston = require('../../config/winston');
5
5
  const RoleConstants = require('../../models/roleConstants');
6
6
  const roleConstants = require('../../models/roleConstants');
7
+ var numericInput = require('../../utils/numericInput');
7
8
  // const CannedResponseEvent = require('../event/CannedResponseEvent');
8
9
 
9
10
 
@@ -244,11 +245,9 @@ router.get('/:cannedResponseid', function (req, res) {
244
245
 
245
246
  router.get('/', function (req, res) {
246
247
  var limit = 1000; // Number of CannedResponses per page
247
- var page = 0;
248
-
249
- if (req.query.page) {
250
- page = req.query.page;
251
- }
248
+ var page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
249
+ if (page === null) return;
250
+ if (page === undefined) page = 0;
252
251
 
253
252
  var skip = page * limit;
254
253
  winston.debug('CannedResponse ROUTE - SKIP PAGE ', skip);
@@ -261,10 +260,8 @@ router.get('/', function (req, res) {
261
260
  query.$text = { "$search": req.query.full_text };
262
261
  }
263
262
 
264
- var direction = -1; //-1 descending , 1 ascending
265
- if (req.query.direction) {
266
- direction = req.query.direction;
267
- }
263
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
264
+ if (direction === null) return;
268
265
 
269
266
  var sortField = "createdAt";
270
267
  if (req.query.sort) {
@@ -8,6 +8,7 @@ const { check, validationResult } = require('express-validator');
8
8
  var passport = require('passport');
9
9
  require('../../middleware/passport')(passport);
10
10
  var roleChecker = require('../../middleware/has-role');
11
+ var numericInput = require('../../utils/numericInput');
11
12
 
12
13
  const messageEvent = require('../../event/messageEvent');
13
14
 
@@ -78,11 +79,9 @@ router.get('/', [passport.authenticate(['basic', 'jwt'],
78
79
  roleChecker.hasRole('agent')],
79
80
  function (req, res) {
80
81
  var limit = 40; // Number of leads per page
81
- var page = 0;
82
-
83
- if (req.query.page) {
84
- page = req.query.page;
85
- }
82
+ var page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
83
+ if (page === null) return;
84
+ if (page === undefined) page = 0;
86
85
 
87
86
  var skip = page * limit;
88
87
  winston.debug('Event ROUTE - SKIP PAGE ', skip);
@@ -95,10 +94,8 @@ router.get('/', [passport.authenticate(['basic', 'jwt'],
95
94
  query.project_user = project_user;
96
95
  }
97
96
 
98
- var direction = -1; //-1 descending , 1 ascending
99
- if (req.query.direction) {
100
- direction = req.query.direction;
101
- }
97
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
98
+ if (direction === null) return;
102
99
  //console.log("direction", direction);
103
100
 
104
101
  var sortField = "createdAt";