File size: 17,263 Bytes
2274b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
{
  "name": "DefectRAG Corrective Actions — INACTIVE REVIEW TEMPLATE",
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveDataSuccessExecution": "none",
    "saveDataErrorExecution": "none",
    "saveManualExecutions": false
  },
  "nodes": [
    {
      "id": "webhook",
      "name": "Authenticated case event",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "defectrag-corrective-actions",
        "authentication": "headerAuth",
        "responseMode": "responseNode",
        "options": {}
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "CONFIGURE_IN_N8N",
          "name": "DefectRAG CA inbound key"
        }
      }
    },
    {
      "id": "validate",
      "name": "Validate case event",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ],
      "parameters": {
        "jsCode": "const b=$input.first().json.body;const u=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;if(!b||!u.test(b.event_id)||!u.test(b.lease_token)||!u.test(b.case_id)||!['created','updated','reminder','overdue'].includes(b.kind)||!Number.isInteger(b.version)||b.version<1)throw Error('Invalid case event');return [{json:{event_id:b.event_id,lease_token:b.lease_token}}];"
      }
    },
    {
      "id": "begin",
      "name": "Reserve notification effect",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "https://PROJECT_REF.supabase.co/functions/v1/corrective-actions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-DefectRAG-Operation",
              "value": "effect"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ {operation:'begin',event_id:$json.event_id,lease_token:$json.lease_token} }}",
        "options": {
          "timeout": 10000
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "CONFIGURE_IN_N8N",
          "name": "DefectRAG CA callback bearer"
        }
      }
    },
    {
      "id": "decision",
      "name": "Effect requires execution",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        660,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "leftValue": "={{ $json.execute }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "email",
      "name": "Notify verified supervisor",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        880,
        -100
      ],
      "parameters": {
        "fromEmail": "CONFIGURE_VERIFIED_SENDER@example.invalid",
        "toEmail": "={{ $json.recipient_email }}",
        "subject": "={{ ({created:'DefectRAG: New corrective action for you · due '+DateTime.fromISO($json.due_at).setZone('Asia/Kuala_Lumpur').toFormat('ccc d LLL, h:mm a'),updated:'DefectRAG: Case update · now '+$json.state,reminder:'DefectRAG: Friendly reminder · due '+DateTime.fromISO($json.due_at).setZone('Asia/Kuala_Lumpur').toFormat('ccc d LLL, h:mm a'),overdue:'DefectRAG: Quick check-in · was due '+DateTime.fromISO($json.due_at).setZone('Asia/Kuala_Lumpur').toFormat('ccc d LLL, h:mm a'),done:'DefectRAG: Nice work · case '+$json.state.toLowerCase(),cancelled:'DefectRAG: Case cancelled · no action needed'})[($json.kind==='updated'&&['Resolved','Closed'].includes($json.state)?'done':$json.kind==='updated'&&$json.state==='Cancelled'?'cancelled':$json.kind)] }}",
        "emailFormat": "both",
        "text": "=A corrective action case needs attention.\n\nWhat happened: {{ ({created:'New case created',updated:'Case updated',reminder:'Due within 24 hours',overdue:'Overdue'})[$json.kind] || $json.kind }}\nCase: {{ $json.case_id.slice(0,8) }} · Priority: {{ $json.priority }} · Status: {{ $json.state }}\nDue: {{ DateTime.fromISO($json.due_at).setZone('Asia/Kuala_Lumpur').toFormat('cccc d LLL yyyy, h:mm a') }} (Malaysia time)\n\nSent to the case supervisor{{ $json.cc_email ? ', with the person responsible copied' : '' }}.\nOpen the case (sign in with your security code): https://wizzas-defectrag.hf.space/#cases\nPhotos and notes stay private inside DefectRAG.",
        "options": {
          "appendAttribution": false,
          "ccEmail": "={{ $json.cc_email || '' }}"
        },
        "html": "=<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n<meta name=\"x-apple-disable-message-reformatting\"><meta name=\"color-scheme\" content=\"light dark\"><meta name=\"supported-color-schemes\" content=\"light dark\">\n<title>DefectRAG corrective action</title>\n<!--[if mso]><style>td, a, span, b { font-family: Arial, sans-serif !important; }\n</style><![endif]-->\n<style>\n@media (prefers-color-scheme: dark) {\n .bg { background-color: #111318 !important; }\n .tx { color: #f3f4f6 !important; }\n .tx2 { color: #d1d5db !important; }\n .mu { color: #9ca3af !important; }\n .hr { border-color: #2d3039 !important; }\n}\n@media only screen and (max-width: 600px) {\n .wrap { padding: 24px 16px !important; }\n .btn a { display: block !important; text-align: center !important; }\n}\n</style></head><body class=\"bg\" style=\"margin:0;padding:0;background:#ffffff\"><span style=\"display:none !important;visibility:hidden;mso-hide:all;font-size:1px;line-height:1px;max-height:0;max-width:0;opacity:0;overflow:hidden\">{{ ({created:'New case',updated:'Quick update',reminder:'Friendly reminder',overdue:'Needs a little attention',done:'All done',cancelled:'Cancelled'})[($json.kind==='updated'&&['Resolved','Closed'].includes($json.state)?'done':$json.kind==='updated'&&$json.state==='Cancelled'?'cancelled':$json.kind)] }} · due {{ DateTime.fromISO($json.due_at).setZone('Asia/Kuala_Lumpur').toFormat(\"cccc, d LLL 'at' h:mm a\") }} · priority {{ $json.priority }}&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;&#8203;&nbsp;</span>\n<table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" class=\"bg\" style=\"background:#ffffff;font-family:-apple-system,'Segoe UI',Helvetica,Arial,sans-serif\"><tr><td class=\"wrap\" style=\"padding:32px 20px\">\n<table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"max-width:560px\">\n<tr><td class=\"tx hr\" style=\"padding-bottom:20px;border-bottom:1px solid #e5e7eb;color:#111827;font-size:15px;font-weight:600\">DefectRAG <span class=\"mu\" style=\"color:#9ca3af;font-weight:400\">/ Corrective actions</span></td></tr>\n<tr><td style=\"padding-top:24px\"><img src=\"https://wizzas-defectrag.hf.space/assets/email/robot-hello.gif\" width=\"96\" height=\"96\" alt=\"DefectRAG robot waving hello\" style=\"display:block;border:0;outline:none;border-radius:20px;width:96px;height:96px\"></td></tr>\n<tr><td style=\"padding-top:16px;color:{{ ({created:'#5b4fd6',updated:'#5b4fd6',reminder:'#b54708',overdue:'#9a4a0e',done:'#1b7f4b',cancelled:'#6b7280'})[($json.kind==='updated'&&['Resolved','Closed'].includes($json.state)?'done':$json.kind==='updated'&&$json.state==='Cancelled'?'cancelled':$json.kind)] }};font-size:13px;font-weight:600\">{{ ({created:'New case',updated:'Quick update',reminder:'Friendly reminder',overdue:'Needs a little attention',done:'All done',cancelled:'Cancelled'})[($json.kind==='updated'&&['Resolved','Closed'].includes($json.state)?'done':$json.kind==='updated'&&$json.state==='Cancelled'?'cancelled':$json.kind)] }}</td></tr>\n<tr><td class=\"tx\" style=\"padding-top:4px;color:#111827;font-size:20px;line-height:1.35;font-weight:600\">{{ ({created:'Hi! A new corrective action is ready for you',updated:'Hi! This case is now '+$json.state,reminder:'Hi! Just a gentle heads-up',overdue:'Hi! This case could use a quick check-in',done:'Nice work! This case is '+$json.state.toLowerCase(),cancelled:'Hi! This case was cancelled'})[($json.kind==='updated'&&['Resolved','Closed'].includes($json.state)?'done':$json.kind==='updated'&&$json.state==='Cancelled'?'cancelled':$json.kind)] }}</td></tr>\n<tr><td class=\"tx2\" style=\"padding-top:12px;color:#374151;font-size:15px;line-height:1.6\">{{ ({created:'A Staff review spotted a defect that needs fixing. No need to worry, this is a normal follow-up: take a look when you can and agree the next step together. It’s due on ',updated:'There’s a small update on this corrective action. Open it whenever you’re ready to see what changed. It’s due on ',reminder:'This case is due on ',overdue:'This case was due on ',done:'Thanks for following it through! It was due on ',cancelled:'No further work is needed. It was due on '})[($json.kind==='updated'&&['Resolved','Closed'].includes($json.state)?'done':$json.kind==='updated'&&$json.state==='Cancelled'?'cancelled':$json.kind)] }}<b class=\"tx\" style=\"color:#111827\">{{ DateTime.fromISO($json.due_at).setZone('Asia/Kuala_Lumpur').toFormat(\"cccc, d LLL 'at' h:mm a\") }}</b>{{ ({created:'.',updated:'.',reminder:' and is still open. A quick look at progress is all it needs.',overdue:' and is still open. No stress, could you check in with the person responsible today and update the case?',done:'. Nothing else is needed unless the case is reopened.',cancelled:'.'})[($json.kind==='updated'&&['Resolved','Closed'].includes($json.state)?'done':$json.kind==='updated'&&$json.state==='Cancelled'?'cancelled':$json.kind)] }}</td></tr>\n<tr><td style=\"padding-top:20px\"><table role=\"presentation\" cellpadding=\"0\" cellspacing=\"0\">\n<tr><td class=\"mu\" style=\"padding:3px 16px 3px 0;color:#6b7280;font-size:14px;white-space:nowrap;vertical-align:top\">Case</td><td class=\"tx\" style=\"padding:3px 0;color:#111827;font-size:14px\">{{ $json.case_id.slice(0,8) }}</td></tr>\n<tr><td class=\"mu\" style=\"padding:3px 16px 3px 0;color:#6b7280;font-size:14px;white-space:nowrap;vertical-align:top\">Status</td><td class=\"tx\" style=\"padding:3px 0;color:#111827;font-size:14px\">{{ $json.state }}</td></tr>\n<tr><td class=\"mu\" style=\"padding:3px 16px 3px 0;color:#6b7280;font-size:14px;white-space:nowrap;vertical-align:top\">Priority</td><td class=\"tx\" style=\"padding:3px 0;color:#111827;font-size:14px\"><span style=\"color:{{ ({Critical:'#c0362c',High:'#b54708'})[$json.priority] || '#111827' }};font-weight:{{ ['Critical','High'].includes($json.priority) ? '600' : '400' }}\">{{ $json.priority }}</span></td></tr>\n<tr><td class=\"mu\" style=\"padding:3px 16px 3px 0;color:#6b7280;font-size:14px;white-space:nowrap;vertical-align:top\">Due</td><td class=\"tx\" style=\"padding:3px 0;color:#111827;font-size:14px\">{{ DateTime.fromISO($json.due_at).setZone('Asia/Kuala_Lumpur').toFormat('d LLL yyyy, h:mm a') }} <span class=\"mu\" style=\"color:#9ca3af\">(Malaysia time)</span></td></tr>\n</table></td></tr>\n<tr><td style=\"padding-top:24px\" class=\"btn\"><a href=\"https://wizzas-defectrag.hf.space/#cases\" style=\"display:inline-block;background-color:#5b4fd6;color:#ffffff;font-size:14px;font-weight:600;line-height:20px;text-decoration:none;padding:11px 20px;border-radius:6px;mso-padding-alt:0\">View case</a></td></tr>\n<tr><td class=\"mu\" style=\"padding-top:32px;color:#9ca3af;font-size:12px;line-height:1.6\">You are receiving this as the supervisor{{ $json.cc_email ? ' or the person responsible' : '' }} on this case. Photos and review notes stay inside DefectRAG, so you will need to sign in with your security code to see them.</td></tr>\n</table></td></tr></table></body></html>"
      },
      "credentials": {
        "smtp": {
          "id": "CONFIGURE_IN_N8N",
          "name": "DefectRAG CA isolated TEST SMTP"
        }
      }
    },
    {
      "id": "accept",
      "name": "Verify supervisor acceptance",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        -100
      ],
      "parameters": {
        "jsCode": "const r=$input.first().json;const reserved=$('Reserve notification effect').first().json;const accepted=Array.isArray(r.accepted)?r.accepted.map(e=>String(e).toLowerCase()):[];const required=[reserved.recipient_email,reserved.cc_email].filter(Boolean).map(e=>String(e).toLowerCase());if(!required.length||!required.every(e=>accepted.includes(e))||(r.rejected||[]).length||typeof r.messageId!=='string'||!r.messageId||r.messageId.length>200)throw Error('SMTP acceptance not verified');const e=$('Validate case event').first().json;return [{json:{operation:'finish',event_id:e.event_id,lease_token:e.lease_token,receipt:r.messageId}}];"
      }
    },
    {
      "id": "finish",
      "name": "Record notification receipt",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        -100
      ],
      "parameters": {
        "method": "POST",
        "url": "https://PROJECT_REF.supabase.co/functions/v1/corrective-actions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-DefectRAG-Operation",
              "value": "effect"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json }}",
        "options": {
          "timeout": 10000
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "CONFIGURE_IN_N8N",
          "name": "DefectRAG CA callback bearer"
        }
      }
    },
    {
      "id": "done",
      "name": "Require durable completion",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1540,
        0
      ],
      "parameters": {
        "jsCode": "if($input.first().json.done!==true)throw Error('Ambiguous notification requires manual reconciliation');return [{json:{event_id:$('Validate case event').first().json.event_id,completed:true}}];"
      }
    },
    {
      "id": "response",
      "name": "Workflow completed",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1760,
        0
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {
          "responseCode": 200
        }
      }
    }
  ],
  "connections": {
    "Authenticated case event": {
      "main": [
        [
          {
            "node": "Validate case event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate case event": {
      "main": [
        [
          {
            "node": "Reserve notification effect",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reserve notification effect": {
      "main": [
        [
          {
            "node": "Effect requires execution",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Effect requires execution": {
      "main": [
        [
          {
            "node": "Notify verified supervisor",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Require durable completion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Notify verified supervisor": {
      "main": [
        [
          {
            "node": "Verify supervisor acceptance",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify supervisor acceptance": {
      "main": [
        [
          {
            "node": "Record notification receipt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Record notification receipt": {
      "main": [
        [
          {
            "node": "Require durable completion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Require durable completion": {
      "main": [
        [
          {
            "node": "Workflow completed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}