-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-plugin.ts
More file actions
89 lines (77 loc) · 2.62 KB
/
basic-plugin.ts
File metadata and controls
89 lines (77 loc) · 2.62 KB
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
/**
* Example: Basic Justice OS Plugin
*
* Demonstrates how to create a plugin that listens for case lifecycle
* events and performs custom actions. This example logs case activity
* to an external webhook for integration with third-party systems.
*/
import type { JusticePlugin, PluginContext, Case, TimelineEvent } from '../src/types';
/**
* Activity Logger Plugin
*
* Subscribes to case lifecycle events and forwards them to an
* external webhook URL. Useful for Slack notifications, audit
* systems, or CRM integrations.
*/
const activityLoggerPlugin: JusticePlugin = {
name: 'activity-logger',
version: '1.0.0',
description: 'Forwards case lifecycle events to an external webhook',
async onLoad(ctx: PluginContext) {
const webhookUrl = process.env.WEBHOOK_URL;
if (!webhookUrl) {
console.warn('[activity-logger] WEBHOOK_URL not set — skipping webhook registration');
return;
}
// Listen for new cases
ctx.caseManager.on('caseCreated', async (caseData: Case) => {
await postToWebhook(webhookUrl, {
event: 'case.created',
caseId: caseData.id,
caseNumber: caseData.caseNumber,
title: caseData.title,
timestamp: new Date().toISOString(),
});
});
// Listen for status changes
ctx.caseManager.on('caseUpdated', async (caseData: Case) => {
await postToWebhook(webhookUrl, {
event: 'case.updated',
caseId: caseData.id,
status: caseData.status,
timestamp: new Date().toISOString(),
});
});
// Listen for new timeline events (hearings, filings, etc.)
ctx.caseManager.on('timelineEventAdded', async (event: TimelineEvent) => {
await postToWebhook(webhookUrl, {
event: 'timeline.event_added',
caseId: event.caseId,
eventType: event.type,
label: event.label,
date: event.date.toISOString(),
timestamp: new Date().toISOString(),
});
});
console.log('[activity-logger] Registered webhook listeners');
},
async onUnload() {
console.log('[activity-logger] Plugin unloaded');
},
};
/** Helper: POST JSON to a webhook endpoint */
async function postToWebhook(url: string, payload: Record<string, unknown>): Promise<void> {
try {
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
if (!response.ok) {
console.error(`[activity-logger] Webhook returned ${response.status}`);
}
} catch (error) {
console.error('[activity-logger] Webhook delivery failed:', error);
}
}
export default activityLoggerPlugin;