@tiledesk/tiledesk-server 3.0.1 → 3.1.0

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 (136) hide show
  1. package/.circleci/config.yml +1 -1
  2. package/CHANGELOG.md +34 -0
  3. package/Dockerfile +3 -2
  4. package/Dockerfile-en +3 -2
  5. package/Dockerfile-jobs +3 -2
  6. package/Dockerfile-profiler +3 -2
  7. package/README.md +15 -0
  8. package/app.js +12 -1
  9. package/bin/quota-reset +68 -0
  10. package/bin/quota-restore +86 -0
  11. package/channels/chat21/chat21Handler.js +50 -34
  12. package/channels/chat21/chat21JoinTracker.js +43 -0
  13. package/config/database.js +5 -5
  14. package/config/native_mcp/native_mcp_servers.json +1 -0
  15. package/controllers/chatbot.controller.js +80 -27
  16. package/controllers/intents.controller.js +42 -0
  17. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-chat21-ionic.md +468 -0
  18. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-design-studio.md +1349 -0
  19. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-server.md +1694 -0
  20. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-tybot.md +789 -0
  21. package/docs/superpowers/plans/2026-09-29-hitl-known-limits-server.md +83 -0
  22. package/docs/superpowers/plans/2026-09-29-hitl-known-limits-tybot.md +76 -0
  23. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-chat21-ionic.md +97 -0
  24. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-design-studio.md +72 -0
  25. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-server.md +223 -0
  26. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-tybot.md +121 -0
  27. package/docs/superpowers/plans/2026-09-29-hitl-pre-branch-ports.md +65 -0
  28. package/docs/superpowers/plans/2026-09-30-flow-start-points-design-studio.md +88 -0
  29. package/docs/superpowers/plans/2026-09-30-flow-start-points-server.md +103 -0
  30. package/docs/superpowers/plans/2026-09-30-flow-start-points-tybot.md +43 -0
  31. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-design-studio.md +80 -0
  32. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-runtime.md +39 -0
  33. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-server.md +106 -0
  34. package/docs/superpowers/specs/2026-09-15-webhook-chatbot-hitl-design.md +715 -0
  35. package/docs/superpowers/specs/2026-09-30-flow-start-points-design.md +156 -0
  36. package/docs/superpowers/specs/2026-10-01-scheduled-start-point-design.md +303 -0
  37. package/docs/superpowers/todo/2026-09-14-webhook-chatbot-shared-code-fixes.md +191 -0
  38. package/event/botEvent.js +12 -1
  39. package/event/emailEvent.js +3 -0
  40. package/event/messageEvent.js +44 -0
  41. package/jobs.js +10 -0
  42. package/jobsManager.js +19 -0
  43. package/middleware/errorHandler.js +6 -2
  44. package/models/analyticResult.js +1 -1
  45. package/models/channelConstants.js +3 -2
  46. package/models/quotaUsage.js +71 -0
  47. package/models/request.js +6 -0
  48. package/models/requestSnapshot.js +12 -0
  49. package/models/routerLogger.js +1 -1
  50. package/models/webhook.js +44 -0
  51. package/package.json +7 -5
  52. package/pubmodules/emailNotification/requestNotification.js +14 -22
  53. package/pubmodules/events/test/eventRoute.js +7 -6
  54. package/pubmodules/rules/conciergeBot.js +9 -4
  55. package/routes/auth.js +1 -12
  56. package/routes/campaigns.js +0 -13
  57. package/routes/dataTable.js +0 -3
  58. package/routes/faq_kb.js +12 -1
  59. package/routes/messagesRoot.js +0 -1
  60. package/routes/quotes.js +38 -1
  61. package/routes/request.js +81 -5
  62. package/routes/users.js +0 -1
  63. package/routes/webhook.js +160 -8
  64. package/routes/webhooks.js +276 -6
  65. package/routes/widgetLoader.js +6 -3
  66. package/services/QuoteManager.js +77 -16
  67. package/services/aiService.js +1 -1
  68. package/services/chatbotService.js +55 -1
  69. package/services/departmentService.js +60 -43
  70. package/services/dolphinSchedulerClient.js +116 -0
  71. package/services/emailService.js +6 -1
  72. package/services/hitlParticipantListener.js +260 -0
  73. package/services/participantInviteService.js +302 -0
  74. package/services/participantRemoveService.js +79 -0
  75. package/services/quotaSnapshotService.js +1167 -0
  76. package/services/requestService.js +102 -144
  77. package/services/scheduledStart/dolphinBackend.js +246 -0
  78. package/services/scheduledStart/schedulerBackend.js +101 -0
  79. package/services/scheduledStart/schedulerServiceClient.js +41 -0
  80. package/services/scheduledStartPublishListener.js +186 -0
  81. package/services/scheduledStartRules.js +173 -0
  82. package/services/scheduledStartService.js +130 -0
  83. package/services/startPointCleanupListener.js +30 -0
  84. package/services/startPointService.js +158 -0
  85. package/services/testWsService.js +1 -4
  86. package/services/updateRequestSnapshotQueued.js +8 -9
  87. package/services/webhookConversationService.js +185 -0
  88. package/services/webhookSettingsValidator.js +61 -0
  89. package/template/email/checkpointReachedEmail.html +5 -0
  90. package/test/agentPromptsRoute.test.js +3 -2
  91. package/test/agentTokensQuota.js +237 -0
  92. package/test/botEventHitlFallback.js +19 -0
  93. package/test/chat21HandlerJoinWiring.js +59 -0
  94. package/test/dataTablesRoute.test.js +10 -2
  95. package/test/dateUtils.test.js +0 -3
  96. package/test/departmentService.js +8 -12
  97. package/test/{fileRoute.js → deprecated/_deprecated_fileRoute} +2 -2
  98. package/test/{kbsettingsRoute.js → deprecated/_deprecated_kbsettingsRoute} +3 -3
  99. package/test/faqRoute.js +6 -1
  100. package/test/faqkbRoute.js +14 -10
  101. package/test/faqkbV3Route.js +1 -1
  102. package/test/filepRoute.js +4 -50
  103. package/test/hitlParticipantListener.js +446 -0
  104. package/test/hitlQueueWiring.js +191 -0
  105. package/test/hitlWaitingReset.js +124 -0
  106. package/test/inviteParticipantsRoute.js +492 -0
  107. package/test/kbRoute.js +1 -1
  108. package/test/messageRoute.js +33 -21
  109. package/test/nativemcp.test.js +7 -1
  110. package/test/openaiRoute.js +2 -8
  111. package/test/publishFailLoud.test.js +414 -0
  112. package/test/quotaSnapshots.js +1044 -0
  113. package/test/removeParticipantsRoute.js +186 -0
  114. package/test/requestService.js +24 -16
  115. package/test/requestServiceRemoveParticipant.js +144 -0
  116. package/test/requestVisibility.test.js +186 -0
  117. package/test/scheduledStartFacade.js +184 -0
  118. package/test/scheduledStartHandover.js +95 -0
  119. package/test/scheduledStartPublish.js +320 -0
  120. package/test/scheduledStartRoute.js +163 -0
  121. package/test/scheduledStartRules.js +177 -0
  122. package/test/scheduledStartRun.js +299 -0
  123. package/test/scheduledStartSchedulerBackend.js +116 -0
  124. package/test/scheduledStartService.js +290 -0
  125. package/test/startPointsCleanup.js +69 -0
  126. package/test/startPointsRegression.js +169 -0
  127. package/test/startPointsRoute.js +158 -0
  128. package/test/webhookConversationRoute.js +347 -0
  129. package/test/webhookConversationService.js +261 -0
  130. package/test/webhookConversationTestUtils.js +55 -0
  131. package/utils/aiUtils.js +95 -1
  132. package/utils/intentFieldNames.js +192 -0
  133. package/utils/quotaCliUtil.js +43 -0
  134. package/utils/requestVisibility.js +355 -0
  135. package/websocket/webSocketServer.js +36 -31
  136. /package/test/{imageRoute.js → deprecated/_deprecated_imageRoute} +0 -0
@@ -0,0 +1,468 @@
1
+ # Webhook-started chatbot conversations + HITL — chat21-ionic Implementation Plan
2
+
3
+ > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
4
+
5
+ **Goal:** Make the agent chat usable for conversations started by a webhook flow: approval buttons stay clickable after info lines, the conversation shows a webhook channel icon, closing it asks for confirmation (it stops the flow), and lead-less loading races don't throw.
6
+
7
+ **Architecture:** Small, isolated changes. A pure helper change in `utils-message.ts` (`isLastMessage` ignores info messages) plus a new `isWebhookConversation` helper, both unit-tested; template additions for the icon; a confirmation alert in the conversations list page before the existing close/archive logic; null guards in the conversation detail page.
8
+
9
+ **Tech Stack:** Angular + Ionic, TypeScript, Jasmine/Karma (`ng test`), ngx-translate via `CustomTranslateService`.
10
+
11
+ **Spec:** `tiledesk-server/docs/superpowers/specs/2026-09-15-webhook-chatbot-hitl-design.md` (section 7, 9.4).
12
+
13
+ ## Global Constraints
14
+
15
+ - Repo `chat21-ionic`, branch `feature/webhook-chatbot-hitl` created from `origin/master` (`a7894025`, 3.4.35). Never commit to `master`.
16
+ - Webhook conversations are identified by `conversation.attributes.request_channel === 'webhook'` (set by tiledesk-server from `request.channel.name`).
17
+ - Do not change composer, canned responses, Copilot, push notifications (spec non-goals).
18
+ - Commit messages end with `Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>`.
19
+
20
+ ### How to run tests
21
+ ```bash
22
+ cd /Users/nicocardone/Desktop/DEV/TILEDESK/chat21-ionic
23
+ npx ng test --watch=false --browsers=ChromeHeadless --include=src/chat21-core/utils/utils-message.spec.ts
24
+ ```
25
+ (`karma.conf.js` defaults to `browsers: ['Chrome']`, `singleRun: false`; the flags above make it a single headless run.)
26
+
27
+ ---
28
+
29
+ ## File Structure
30
+
31
+ | File | Status | Responsibility |
32
+ |---|---|---|
33
+ | `src/chat21-core/utils/utils-message.ts` | Modify | `isLastMessage` ignores info messages; new `isWebhookConversation` |
34
+ | `src/chat21-core/utils/utils-message.spec.ts` | Create | unit tests for both helpers |
35
+ | `src/chat21-core/utils/constants.ts` | Modify | `CHANNEL_TYPE.WEBHOOK = 'webhook'` |
36
+ | `src/assets/img/channel_icons/webhook.svg` | Create | channel icon |
37
+ | `src/app/chatlib/list-conversations-component/ion-list-conversations/ion-list-conversations.component.html` | Modify | webhook icon in the list |
38
+ | `src/app/components/conversation-detail/header-conversation-detail/header-conversation-detail.component.html` | Modify | webhook icon in the header |
39
+ | `src/app/pages/conversations-list/conversations-list.page.ts` | Modify | confirmation before closing a webhook conversation |
40
+ | `src/assets/i18n/en.json`, `src/assets/i18n/it.json` | Modify | confirmation message |
41
+ | `src/app/pages/conversation-detail/conversation-detail.page.ts` | Modify | null guards in `getLeadDetail` / `updateLeadInfo` |
42
+
43
+ ---
44
+
45
+ ### Task 1: Create the branch and make buttons survive trailing info messages
46
+
47
+ **Files:**
48
+ - Modify: `src/chat21-core/utils/utils-message.ts:78-83`
49
+ - Create: `src/chat21-core/utils/utils-message.spec.ts`
50
+
51
+ **Interfaces:**
52
+ - Produces: `isLastMessage(messages: any[], idMessage: string): boolean` — true only if `idMessage` is the uid of the last message whose `attributes.subtype` is not `info`/`info/support`. Used unchanged by `ConversationContentComponent.isLastMessage` (`src/app/chatlib/conversation-detail/conversation-content/conversation-content.component.ts:215`), which `ion-conversation-detail.component.html:207` passes to the attachment/buttons component.
53
+
54
+ - [ ] **Step 1: Create the feature branch**
55
+
56
+ ```bash
57
+ cd /Users/nicocardone/Desktop/DEV/TILEDESK/chat21-ionic
58
+ git fetch origin
59
+ git switch -c feature/webhook-chatbot-hitl origin/master
60
+ git branch --unset-upstream
61
+ ```
62
+
63
+ - [ ] **Step 2: Write the failing tests**
64
+
65
+ Create `src/chat21-core/utils/utils-message.spec.ts`:
66
+
67
+ ```ts
68
+ import { isLastMessage } from './utils-message';
69
+
70
+ describe('utils-message isLastMessage', () => {
71
+
72
+ const text = (uid: string) => ({ uid: uid, attributes: {} });
73
+ const info = (uid: string) => ({ uid: uid, attributes: { subtype: 'info' } });
74
+ const infoSupport = (uid: string) => ({ uid: uid, attributes: { subtype: 'info/support' } });
75
+
76
+ it('returns true only for the last message', () => {
77
+ expect(isLastMessage([text('1'), text('2')], '2')).toBeTrue();
78
+ expect(isLastMessage([text('1'), text('2')], '1')).toBeFalse();
79
+ });
80
+
81
+ it('ignores trailing info messages (e.g. "agent added to the group")', () => {
82
+ expect(isLastMessage([text('1'), info('2'), infoSupport('3')], '1')).toBeTrue();
83
+ expect(isLastMessage([text('1'), info('2')], '2')).toBeFalse();
84
+ });
85
+
86
+ it('handles messages without attributes', () => {
87
+ expect(isLastMessage([{ uid: '1' }, info('2')], '1')).toBeTrue();
88
+ });
89
+
90
+ it('returns false for empty or missing lists', () => {
91
+ expect(isLastMessage([], '1')).toBeFalse();
92
+ expect(isLastMessage(undefined, '1')).toBeFalse();
93
+ expect(isLastMessage([info('1')], '1')).toBeFalse();
94
+ });
95
+ });
96
+ ```
97
+
98
+ - [ ] **Step 3: Run the tests to verify they fail**
99
+
100
+ Run: `npx ng test --watch=false --browsers=ChromeHeadless --include=src/chat21-core/utils/utils-message.spec.ts`
101
+ Expected: FAIL — `ignores trailing info messages` (`Expected false to be true`) and `returns false for empty or missing lists` (`TypeError: Cannot read properties of undefined`).
102
+
103
+ - [ ] **Step 4: Implement**
104
+
105
+ In `src/chat21-core/utils/utils-message.ts`, replace:
106
+
107
+ ```ts
108
+ export function isLastMessage(messages, idMessage):boolean {
109
+ if (idMessage === messages[messages.length - 1].uid) {
110
+ return true;
111
+ }
112
+ return false;
113
+ }
114
+ ```
115
+
116
+ with:
117
+
118
+ ```ts
119
+ export function isLastMessage(messages, idMessage):boolean {
120
+ if (!messages || messages.length === 0) {
121
+ return false;
122
+ }
123
+ // info lines (member added, conversation reopened, ...) must not hide the buttons
124
+ // of the last real message
125
+ for (let i = messages.length - 1; i >= 0; i--) {
126
+ if (!isInfo(messages[i])) {
127
+ return messages[i].uid === idMessage;
128
+ }
129
+ }
130
+ return false;
131
+ }
132
+ ```
133
+
134
+ `isInfo` is defined earlier in the same file and already guards `message.attributes`.
135
+
136
+ - [ ] **Step 5: Run the tests to verify they pass**
137
+
138
+ Run: `npx ng test --watch=false --browsers=ChromeHeadless --include=src/chat21-core/utils/utils-message.spec.ts`
139
+ Expected: PASS (4 specs, 0 failures).
140
+
141
+ - [ ] **Step 6: Commit**
142
+
143
+ ```bash
144
+ git add src/chat21-core/utils/utils-message.ts src/chat21-core/utils/utils-message.spec.ts
145
+ git commit -m "fix(chat): keep reply buttons visible when info messages follow them
146
+
147
+ Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
148
+ ```
149
+
150
+ ---
151
+
152
+ ### Task 2: Webhook channel constant, helper and icon
153
+
154
+ **Files:**
155
+ - Modify: `src/chat21-core/utils/constants.ts:44-55`
156
+ - Modify: `src/chat21-core/utils/utils-message.ts` (append helper)
157
+ - Modify: `src/chat21-core/utils/utils-message.spec.ts` (append specs)
158
+ - Create: `src/assets/img/channel_icons/webhook.svg`
159
+ - Modify: `src/app/chatlib/list-conversations-component/ion-list-conversations/ion-list-conversations.component.html:117`
160
+ - Modify: `src/app/components/conversation-detail/header-conversation-detail/header-conversation-detail.component.html:56`
161
+
162
+ **Interfaces:**
163
+ - Produces: `CHANNEL_TYPE.WEBHOOK === 'webhook'`; `isWebhookConversation(conversation: any): boolean`.
164
+
165
+ - [ ] **Step 1: Write the failing tests**
166
+
167
+ Append to `src/chat21-core/utils/utils-message.spec.ts`:
168
+
169
+ ```ts
170
+ import { isWebhookConversation } from './utils-message';
171
+
172
+ describe('utils-message isWebhookConversation', () => {
173
+
174
+ it('is true for conversations whose request channel is webhook', () => {
175
+ expect(isWebhookConversation({ attributes: { request_channel: 'webhook' } })).toBeTrue();
176
+ });
177
+
178
+ it('is false for other channels and missing data', () => {
179
+ expect(isWebhookConversation({ attributes: { request_channel: 'chat21' } })).toBeFalse();
180
+ expect(isWebhookConversation({ attributes: {} })).toBeFalse();
181
+ expect(isWebhookConversation({})).toBeFalse();
182
+ expect(isWebhookConversation(null)).toBeFalse();
183
+ });
184
+ });
185
+ ```
186
+
187
+ - [ ] **Step 2: Run the tests to verify they fail**
188
+
189
+ Run: `npx ng test --watch=false --browsers=ChromeHeadless --include=src/chat21-core/utils/utils-message.spec.ts`
190
+ Expected: FAIL — compilation error `Module '"./utils-message"' has no exported member 'isWebhookConversation'`.
191
+
192
+ - [ ] **Step 3: Add the enum value**
193
+
194
+ In `src/chat21-core/utils/constants.ts`, in `export enum CHANNEL_TYPE { ... }`, replace:
195
+
196
+ ```ts
197
+ VOICE = 'voice',
198
+ }
199
+ ```
200
+
201
+ with:
202
+
203
+ ```ts
204
+ VOICE = 'voice',
205
+ WEBHOOK = 'webhook',
206
+ }
207
+ ```
208
+
209
+ - [ ] **Step 4: Add the helper**
210
+
211
+ Append to `src/chat21-core/utils/utils-message.ts`:
212
+
213
+ ```ts
214
+ /** Conversations started by a webhook flow (tiledesk-server request.channel.name === 'webhook') */
215
+ export function isWebhookConversation(conversation: any): boolean {
216
+ return !!(conversation && conversation.attributes && conversation.attributes.request_channel === CHANNEL_TYPE.WEBHOOK);
217
+ }
218
+ ```
219
+
220
+ (`CHANNEL_TYPE` is already imported at the top of the file.)
221
+
222
+ - [ ] **Step 5: Run the tests to verify they pass**
223
+
224
+ Run: `npx ng test --watch=false --browsers=ChromeHeadless --include=src/chat21-core/utils/utils-message.spec.ts`
225
+ Expected: PASS (6 specs, 0 failures).
226
+
227
+ - [ ] **Step 6: Add the icon asset**
228
+
229
+ Create `src/assets/img/channel_icons/webhook.svg`:
230
+
231
+ ```svg
232
+ <svg xmlns="http://www.w3.org/2000/svg" height="24" width="24" viewBox="0 0 24 24" fill="#5f6368"><path d="M10 15h5.88c.27-.31.67-.5 1.12-.5.83 0 1.5.67 1.5 1.5s-.67 1.5-1.5 1.5c-.44 0-.84-.19-1.12-.5H11.9c-.46 2.28-2.48 4-4.9 4-2.76 0-5-2.24-5-5 0-2.42 1.72-4.44 4-4.9v2.07c-1.16.41-2 1.53-2 2.83 0 1.65 1.35 3 3 3s3-1.35 3-3v-1zm2.5-11c1.65 0 3 1.35 3 3h2c0-2.76-2.24-5-5-5s-5 2.24-5 5c0 1.43.6 2.71 1.55 3.62l-2.35 3.9c-.68.14-1.2.75-1.2 1.48 0 .83.67 1.5 1.5 1.5s1.5-.67 1.5-1.5c0-.16-.02-.31-.07-.45l3.38-5.63C10.49 9.61 9.5 8.42 9.5 7c0-1.65 1.35-3 3-3zm4.5 9c-.64 0-1.23.2-1.72.54l-3.05-5.07C11.53 8.35 11 7.74 11 7c0-.83.67-1.5 1.5-1.5S14 6.17 14 7c0 .15-.02.29-.06.43l2.19 3.65c.28-.05.57-.08.87-.08 2.76 0 5 2.24 5 5s-2.24 5-5 5c-1.85 0-3.47-1.01-4.33-2.5h2.67c.48.32 1.05.5 1.66.5 1.65 0 3-1.35 3-3s-1.35-3-3-3z"/></svg>
233
+ ```
234
+
235
+ - [ ] **Step 7: Show the icon in the conversation list**
236
+
237
+ In `ion-list-conversations.component.html`, after the line (117):
238
+
239
+ ```html
240
+ <img class="channel-icon" src="assets/img/channel_icons/voice.svg" *ngIf="conversation?.attributes && conversation?.attributes?.request_channel && conversation?.attributes?.request_channel.startsWith(CHANNEL_TYPE.VOICE)" >
241
+ ```
242
+
243
+ add:
244
+
245
+ ```html
246
+ <img class="channel-icon" src="assets/img/channel_icons/webhook.svg" *ngIf="conversation?.attributes && conversation?.attributes?.request_channel && conversation?.attributes?.request_channel === CHANNEL_TYPE.WEBHOOK" >
247
+ ```
248
+
249
+ - [ ] **Step 8: Show the icon in the conversation header**
250
+
251
+ In `header-conversation-detail.component.html`, after the line (56):
252
+
253
+ ```html
254
+ <img class="channel-icon" src="assets/img/channel_icons/voice.svg" *ngIf="conversationAvatar?.request_channel && conversationAvatar?.request_channel.startsWith(CHANNEL_TYPE.VOICE)" >
255
+ ```
256
+
257
+ add:
258
+
259
+ ```html
260
+ <img class="channel-icon" src="assets/img/channel_icons/webhook.svg" *ngIf="conversationAvatar?.request_channel && conversationAvatar?.request_channel === CHANNEL_TYPE.WEBHOOK" >
261
+ ```
262
+
263
+ - [ ] **Step 9: Compile check**
264
+
265
+ Run: `npx ng build`
266
+ Expected: build completes without errors.
267
+
268
+ - [ ] **Step 10: Commit**
269
+
270
+ ```bash
271
+ git add src/chat21-core/utils/constants.ts src/chat21-core/utils/utils-message.ts src/chat21-core/utils/utils-message.spec.ts src/assets/img/channel_icons/webhook.svg src/app/chatlib/list-conversations-component/ion-list-conversations/ion-list-conversations.component.html src/app/components/conversation-detail/header-conversation-detail/header-conversation-detail.component.html
272
+ git commit -m "feat(chat): show a webhook channel icon for flow-started conversations
273
+
274
+ Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
275
+ ```
276
+
277
+ ---
278
+
279
+ ### Task 3: Confirm before closing a webhook conversation
280
+
281
+ **Files:**
282
+ - Modify: `src/app/pages/conversations-list/conversations-list.page.ts` (imports `:4` and `:49-50`, constructor, `onCloseConversation` `:1060-1102`)
283
+ - Modify: `src/assets/i18n/en.json:55`, `src/assets/i18n/it.json` (same key)
284
+
285
+ **Interfaces:**
286
+ - Consumes: `isWebhookConversation` (Task 2).
287
+ - Produces: `onCloseConversation(conversation)` shows an Ionic alert for webhook conversations and runs the unchanged close logic (now `closeConversationConfirmed(conversation)`) only after confirmation. Both close entry points (list item button → `onCloseConversation`, and the `conversation:closed` event at `:1041-1049`) go through it.
288
+
289
+ - [ ] **Step 1: Add the translation**
290
+
291
+ In `src/assets/i18n/en.json`, after `"CLOSE_ALERT_MSG": "Close the conversation?",` add:
292
+
293
+ ```json
294
+ "CLOSE_FLOW_CONVERSATION_ALERT_MSG": "This conversation was started by an automated flow. Closing it stops the flow. Continue?",
295
+ ```
296
+
297
+ In `src/assets/i18n/it.json`, after the `"CLOSE_ALERT_MSG"` entry add:
298
+
299
+ ```json
300
+ "CLOSE_FLOW_CONVERSATION_ALERT_MSG": "Questa conversazione è stata avviata da un flusso automatico. Chiuderla interrompe il flusso. Continuare?",
301
+ ```
302
+
303
+ (If `it.json` has no `CLOSE_ALERT_MSG`, add the key after `"ALERT_TITLE"`.)
304
+
305
+ - [ ] **Step 2: Import AlertController and the helper**
306
+
307
+ In `conversations-list.page.ts` replace:
308
+
309
+ ```ts
310
+ import { IonContent, ModalController } from '@ionic/angular'
311
+ ```
312
+
313
+ with:
314
+
315
+ ```ts
316
+ import { AlertController, IonContent, ModalController } from '@ionic/angular'
317
+ ```
318
+
319
+ and replace:
320
+
321
+ ```ts
322
+ import { getProjectIdSelectedConversation } from 'src/chat21-core/utils/utils-message';
323
+ ```
324
+
325
+ with:
326
+
327
+ ```ts
328
+ import { getProjectIdSelectedConversation, isWebhookConversation } from 'src/chat21-core/utils/utils-message';
329
+ ```
330
+
331
+ Add `public alertController: AlertController,` as a new constructor parameter (next to the existing `ModalController` parameter).
332
+
333
+ - [ ] **Step 3: Split the close logic behind a confirmation**
334
+
335
+ Replace the method signature line and the lines before `this.loadingIsActive = false`:
336
+
337
+ ```ts
338
+ onCloseConversation(conversation: ConversationModel) {
339
+ this.logger.log('[CONVS-LIST-PAGE] onCloseConversation conversation', conversation)
340
+ ```
341
+
342
+ with:
343
+
344
+ ```ts
345
+ onCloseConversation(conversation: ConversationModel) {
346
+ this.logger.log('[CONVS-LIST-PAGE] onCloseConversation conversation', conversation)
347
+ if (isWebhookConversation(conversation)) {
348
+ this.presentAlertConfirmCloseFlowConversation(conversation)
349
+ return
350
+ }
351
+ this.closeConversationConfirmed(conversation)
352
+ }
353
+
354
+ async presentAlertConfirmCloseFlowConversation(conversation: ConversationModel) {
355
+ const keys = ['ALERT_TITLE', 'CLOSE_FLOW_CONVERSATION_ALERT_MSG', 'CLOSE_ALERT_CANCEL_LABEL', 'CLOSE_ALERT_CONFIRM_LABEL']
356
+ const translationMap = this.translateService.translateLanguage(keys)
357
+ const alert = await this.alertController.create({
358
+ cssClass: 'my-custom-class',
359
+ header: translationMap.get('ALERT_TITLE'),
360
+ message: translationMap.get('CLOSE_FLOW_CONVERSATION_ALERT_MSG'),
361
+ buttons: [
362
+ {
363
+ text: translationMap.get('CLOSE_ALERT_CANCEL_LABEL'),
364
+ role: 'cancel',
365
+ cssClass: 'secondary',
366
+ },
367
+ {
368
+ text: translationMap.get('CLOSE_ALERT_CONFIRM_LABEL'),
369
+ handler: () => {
370
+ this.closeConversationConfirmed(conversation)
371
+ },
372
+ },
373
+ ],
374
+ })
375
+ await alert.present()
376
+ }
377
+
378
+ closeConversationConfirmed(conversation: ConversationModel) {
379
+ ```
380
+
381
+ The rest of the original method body (from `this.loadingIsActive = false` to its closing `}`) stays unchanged and now belongs to `closeConversationConfirmed`.
382
+
383
+ - [ ] **Step 4: Compile check**
384
+
385
+ Run: `npx ng build`
386
+ Expected: build completes without errors.
387
+
388
+ - [ ] **Step 5: Manual verification**
389
+
390
+ With a local stack (or stage after deployment), open chat21-ionic as an agent participant of:
391
+ 1. a conversation with `request.channel.name = 'webhook'` → click the list item's archive icon → the alert appears; **Cancel** keeps the conversation; **OK** closes it (request status 1000 in the dashboard).
392
+ 2. a widget conversation → archive closes immediately, as before.
393
+
394
+ - [ ] **Step 6: Commit**
395
+
396
+ ```bash
397
+ git add src/app/pages/conversations-list/conversations-list.page.ts src/assets/i18n/en.json src/assets/i18n/it.json
398
+ git commit -m "feat(chat): confirm before closing a conversation started by a flow
399
+
400
+ Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
401
+ ```
402
+
403
+ ---
404
+
405
+ ### Task 4: Null guards in the conversation detail page
406
+
407
+ **Files:**
408
+ - Modify: `src/app/pages/conversation-detail/conversation-detail.page.ts` (`getLeadDetail` `:1004-1044`, `updateLeadInfo` `:1365-1395`)
409
+
410
+ **Interfaces:** none (defensive change).
411
+
412
+ - [ ] **Step 1: Guard `getLeadDetail`**
413
+
414
+ Replace:
415
+
416
+ ```ts
417
+ if(request && request.channel){
418
+ this.conversation.attributes['request_channel'] = request.channel.name
419
+ }
420
+ if (request.lead && request.lead.email) { //LEAD has an email
421
+ ```
422
+
423
+ with:
424
+
425
+ ```ts
426
+ if(request && request.channel && this.conversation && this.conversation.attributes){
427
+ this.conversation.attributes['request_channel'] = request.channel.name
428
+ }
429
+ if (!request || !request.lead) {
430
+ return
431
+ }
432
+ if (request.lead.email) { //LEAD has an email
433
+ ```
434
+
435
+ - [ ] **Step 2: Guard `updateLeadInfo`**
436
+
437
+ In `updateLeadInfo(msg)`, replace both occurrences of:
438
+
439
+ ```ts
440
+ this.conversation.attributes['projectId'],
441
+ this.conversation.attributes['project_name'],
442
+ this.conversation.attributes['request_channel']
443
+ ```
444
+
445
+ with:
446
+
447
+ ```ts
448
+ this.conversation?.attributes?.['projectId'],
449
+ this.conversation?.attributes?.['project_name'],
450
+ this.conversation?.attributes?.['request_channel']
451
+ ```
452
+
453
+ - [ ] **Step 3: Compile check and existing tests**
454
+
455
+ Run: `npx ng build`
456
+ Expected: build completes without errors.
457
+
458
+ Run: `npx ng test --watch=false --browsers=ChromeHeadless --include=src/chat21-core/utils/utils-message.spec.ts`
459
+ Expected: PASS.
460
+
461
+ - [ ] **Step 4: Commit**
462
+
463
+ ```bash
464
+ git add src/app/pages/conversation-detail/conversation-detail.page.ts
465
+ git commit -m "fix(chat): guard lead and conversation attributes while loading request details
466
+
467
+ Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
468
+ ```