@bernierllc/email-webhook-events 1.0.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.
- package/.eslintrc.cjs +29 -0
- package/README.md +349 -0
- package/__tests__/EmailWebhookEventsService.test.ts +247 -0
- package/__tests__/analytics/AnalyticsEngine.test.ts +244 -0
- package/__tests__/normalizers/MailgunNormalizer.test.ts +149 -0
- package/__tests__/normalizers/PostmarkNormalizer.test.ts +112 -0
- package/__tests__/normalizers/SESNormalizer.test.ts +168 -0
- package/__tests__/normalizers/SMTP2GONormalizer.test.ts +83 -0
- package/__tests__/normalizers/SendGridNormalizer.test.ts +181 -0
- package/__tests__/persistence/PersistenceAdapter.test.ts +103 -0
- package/coverage/clover.xml +328 -0
- package/coverage/coverage-final.json +10 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +161 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov-report/src/EmailWebhookEventsService.ts.html +826 -0
- package/coverage/lcov-report/src/analytics/AnalyticsEngine.ts.html +775 -0
- package/coverage/lcov-report/src/analytics/index.html +116 -0
- package/coverage/lcov-report/src/index.html +131 -0
- package/coverage/lcov-report/src/normalizers/MailgunNormalizer.ts.html +301 -0
- package/coverage/lcov-report/src/normalizers/PostmarkNormalizer.ts.html +301 -0
- package/coverage/lcov-report/src/normalizers/SESNormalizer.ts.html +436 -0
- package/coverage/lcov-report/src/normalizers/SMTP2GONormalizer.ts.html +247 -0
- package/coverage/lcov-report/src/normalizers/SendGridNormalizer.ts.html +274 -0
- package/coverage/lcov-report/src/normalizers/index.html +176 -0
- package/coverage/lcov-report/src/persistence/InMemoryPersistenceAdapter.ts.html +289 -0
- package/coverage/lcov-report/src/persistence/index.html +116 -0
- package/coverage/lcov-report/src/types.ts.html +823 -0
- package/coverage/lcov.info +710 -0
- package/dist/EmailWebhookEventsService.d.ts +53 -0
- package/dist/EmailWebhookEventsService.d.ts.map +1 -0
- package/dist/EmailWebhookEventsService.js +198 -0
- package/dist/EmailWebhookEventsService.js.map +1 -0
- package/dist/analytics/AnalyticsEngine.d.ts +20 -0
- package/dist/analytics/AnalyticsEngine.d.ts.map +1 -0
- package/dist/analytics/AnalyticsEngine.js +160 -0
- package/dist/analytics/AnalyticsEngine.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/normalizers/EventNormalizer.d.ts +5 -0
- package/dist/normalizers/EventNormalizer.d.ts.map +1 -0
- package/dist/normalizers/EventNormalizer.js +10 -0
- package/dist/normalizers/EventNormalizer.js.map +1 -0
- package/dist/normalizers/MailgunNormalizer.d.ts +9 -0
- package/dist/normalizers/MailgunNormalizer.d.ts.map +1 -0
- package/dist/normalizers/MailgunNormalizer.js +73 -0
- package/dist/normalizers/MailgunNormalizer.js.map +1 -0
- package/dist/normalizers/PostmarkNormalizer.d.ts +10 -0
- package/dist/normalizers/PostmarkNormalizer.d.ts.map +1 -0
- package/dist/normalizers/PostmarkNormalizer.js +75 -0
- package/dist/normalizers/PostmarkNormalizer.js.map +1 -0
- package/dist/normalizers/SESNormalizer.d.ts +16 -0
- package/dist/normalizers/SESNormalizer.d.ts.map +1 -0
- package/dist/normalizers/SESNormalizer.js +107 -0
- package/dist/normalizers/SESNormalizer.js.map +1 -0
- package/dist/normalizers/SMTP2GONormalizer.d.ts +9 -0
- package/dist/normalizers/SMTP2GONormalizer.d.ts.map +1 -0
- package/dist/normalizers/SMTP2GONormalizer.js +55 -0
- package/dist/normalizers/SMTP2GONormalizer.js.map +1 -0
- package/dist/normalizers/SendGridNormalizer.d.ts +9 -0
- package/dist/normalizers/SendGridNormalizer.d.ts.map +1 -0
- package/dist/normalizers/SendGridNormalizer.js +64 -0
- package/dist/normalizers/SendGridNormalizer.js.map +1 -0
- package/dist/persistence/InMemoryPersistenceAdapter.d.ts +12 -0
- package/dist/persistence/InMemoryPersistenceAdapter.d.ts.map +1 -0
- package/dist/persistence/InMemoryPersistenceAdapter.js +58 -0
- package/dist/persistence/InMemoryPersistenceAdapter.js.map +1 -0
- package/dist/persistence/PersistenceAdapter.d.ts +7 -0
- package/dist/persistence/PersistenceAdapter.d.ts.map +1 -0
- package/dist/persistence/PersistenceAdapter.js +10 -0
- package/dist/persistence/PersistenceAdapter.js.map +1 -0
- package/dist/types.d.ts +178 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +41 -0
- package/dist/types.js.map +1 -0
- package/jest.config.cjs +29 -0
- package/package.json +51 -0
- package/src/EmailWebhookEventsService.ts +247 -0
- package/src/analytics/AnalyticsEngine.ts +230 -0
- package/src/index.ts +39 -0
- package/src/normalizers/EventNormalizer.ts +13 -0
- package/src/normalizers/MailgunNormalizer.ts +72 -0
- package/src/normalizers/PostmarkNormalizer.ts +72 -0
- package/src/normalizers/SESNormalizer.ts +117 -0
- package/src/normalizers/SMTP2GONormalizer.ts +54 -0
- package/src/normalizers/SendGridNormalizer.ts +63 -0
- package/src/persistence/InMemoryPersistenceAdapter.ts +68 -0
- package/src/persistence/PersistenceAdapter.ts +15 -0
- package/src/types.ts +246 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2025 Bernier LLC
|
|
3
|
+
|
|
4
|
+
This file is licensed to the client under a limited-use license.
|
|
5
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
EmailEvent,
|
|
11
|
+
EmailAnalytics,
|
|
12
|
+
EmailEventType,
|
|
13
|
+
EmailProvider,
|
|
14
|
+
ProviderAnalytics,
|
|
15
|
+
AnalyticsFilters
|
|
16
|
+
} from '../types';
|
|
17
|
+
|
|
18
|
+
export class AnalyticsEngine {
|
|
19
|
+
private events: EmailEvent[] = [];
|
|
20
|
+
private config?: {
|
|
21
|
+
realTimeEnabled: boolean;
|
|
22
|
+
aggregationInterval: number;
|
|
23
|
+
retentionDays: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
constructor(config?: { realTimeEnabled: boolean; aggregationInterval: number; retentionDays: number }) {
|
|
27
|
+
this.config = config;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async recordEvent(event: EmailEvent): Promise<void> {
|
|
31
|
+
this.events.push(event);
|
|
32
|
+
|
|
33
|
+
// Cleanup old events based on retention policy
|
|
34
|
+
if (this.config?.retentionDays) {
|
|
35
|
+
const cutoffDate = new Date();
|
|
36
|
+
cutoffDate.setDate(cutoffDate.getDate() - this.config.retentionDays);
|
|
37
|
+
this.events = this.events.filter(e => e.timestamp >= cutoffDate);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async getAnalytics(
|
|
42
|
+
startDate: Date,
|
|
43
|
+
endDate: Date,
|
|
44
|
+
filters?: AnalyticsFilters
|
|
45
|
+
): Promise<EmailAnalytics> {
|
|
46
|
+
// Filter events by date range and filters
|
|
47
|
+
let filteredEvents = this.events.filter(
|
|
48
|
+
e => e.timestamp >= startDate && e.timestamp <= endDate
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
if (filters?.provider) {
|
|
52
|
+
filteredEvents = filteredEvents.filter(e => e.provider === filters.provider);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (filters?.emailId) {
|
|
56
|
+
filteredEvents = filteredEvents.filter(e => e.emailId === filters.emailId);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (filters?.recipient) {
|
|
60
|
+
filteredEvents = filteredEvents.filter(e => e.recipient === filters.recipient);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (filters?.eventTypes && filters.eventTypes.length > 0) {
|
|
64
|
+
filteredEvents = filteredEvents.filter(e => filters.eventTypes!.includes(e.type));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Calculate counts
|
|
68
|
+
const counts = this.calculateCounts(filteredEvents);
|
|
69
|
+
|
|
70
|
+
// Calculate rates
|
|
71
|
+
const rates = this.calculateRates(counts);
|
|
72
|
+
|
|
73
|
+
// Calculate breakdowns
|
|
74
|
+
const byProvider = this.calculateProviderBreakdown(filteredEvents);
|
|
75
|
+
const byEventType = this.calculateEventTypeBreakdown(filteredEvents);
|
|
76
|
+
const byBounceType = this.calculateBounceTypeBreakdown(filteredEvents);
|
|
77
|
+
const topLinks = this.calculateTopLinks(filteredEvents);
|
|
78
|
+
const byDevice = this.calculateDeviceBreakdown(filteredEvents);
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
startDate,
|
|
82
|
+
endDate,
|
|
83
|
+
...counts,
|
|
84
|
+
...rates,
|
|
85
|
+
byProvider,
|
|
86
|
+
byEventType,
|
|
87
|
+
byBounceType,
|
|
88
|
+
topLinks,
|
|
89
|
+
byDevice
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private calculateCounts(events: EmailEvent[]): {
|
|
94
|
+
totalSent: number;
|
|
95
|
+
totalDelivered: number;
|
|
96
|
+
totalOpened: number;
|
|
97
|
+
totalClicked: number;
|
|
98
|
+
totalBounced: number;
|
|
99
|
+
totalComplained: number;
|
|
100
|
+
totalUnsubscribed: number;
|
|
101
|
+
totalFailed: number;
|
|
102
|
+
} {
|
|
103
|
+
return {
|
|
104
|
+
totalSent: events.length, // Simplified - in real implementation, would track sent separately
|
|
105
|
+
totalDelivered: events.filter(e => e.type === EmailEventType.DELIVERED).length,
|
|
106
|
+
totalOpened: events.filter(e => e.type === EmailEventType.OPENED).length,
|
|
107
|
+
totalClicked: events.filter(e => e.type === EmailEventType.CLICKED).length,
|
|
108
|
+
totalBounced: events.filter(e => e.type === EmailEventType.BOUNCED).length,
|
|
109
|
+
totalComplained: events.filter(e => e.type === EmailEventType.COMPLAINED).length,
|
|
110
|
+
totalUnsubscribed: events.filter(e => e.type === EmailEventType.UNSUBSCRIBED).length,
|
|
111
|
+
totalFailed: events.filter(e => e.type === EmailEventType.FAILED).length
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private calculateRates(counts: {
|
|
116
|
+
totalSent: number;
|
|
117
|
+
totalDelivered: number;
|
|
118
|
+
totalOpened: number;
|
|
119
|
+
totalClicked: number;
|
|
120
|
+
totalBounced: number;
|
|
121
|
+
totalComplained: number;
|
|
122
|
+
totalUnsubscribed: number;
|
|
123
|
+
}): {
|
|
124
|
+
deliveryRate: number;
|
|
125
|
+
openRate: number;
|
|
126
|
+
clickRate: number;
|
|
127
|
+
clickToOpenRate: number;
|
|
128
|
+
bounceRate: number;
|
|
129
|
+
complaintRate: number;
|
|
130
|
+
unsubscribeRate: number;
|
|
131
|
+
} {
|
|
132
|
+
const safeDiv = (num: number, denom: number): number => (denom === 0 ? 0 : num / denom);
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
deliveryRate: safeDiv(counts.totalDelivered, counts.totalSent),
|
|
136
|
+
openRate: safeDiv(counts.totalOpened, counts.totalDelivered),
|
|
137
|
+
clickRate: safeDiv(counts.totalClicked, counts.totalDelivered),
|
|
138
|
+
clickToOpenRate: safeDiv(counts.totalClicked, counts.totalOpened),
|
|
139
|
+
bounceRate: safeDiv(counts.totalBounced, counts.totalSent),
|
|
140
|
+
complaintRate: safeDiv(counts.totalComplained, counts.totalSent),
|
|
141
|
+
unsubscribeRate: safeDiv(counts.totalUnsubscribed, counts.totalDelivered)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private calculateProviderBreakdown(events: EmailEvent[]): Record<EmailProvider, ProviderAnalytics> {
|
|
146
|
+
const providers = Object.values(EmailProvider);
|
|
147
|
+
const breakdown = {} as Record<EmailProvider, ProviderAnalytics>;
|
|
148
|
+
|
|
149
|
+
for (const provider of providers) {
|
|
150
|
+
const providerEvents = events.filter(e => e.provider === provider);
|
|
151
|
+
const counts = this.calculateCounts(providerEvents);
|
|
152
|
+
const rates = this.calculateRates(counts);
|
|
153
|
+
|
|
154
|
+
breakdown[provider] = {
|
|
155
|
+
sent: counts.totalSent,
|
|
156
|
+
delivered: counts.totalDelivered,
|
|
157
|
+
opened: counts.totalOpened,
|
|
158
|
+
clicked: counts.totalClicked,
|
|
159
|
+
bounced: counts.totalBounced,
|
|
160
|
+
complained: counts.totalComplained,
|
|
161
|
+
failed: counts.totalFailed,
|
|
162
|
+
deliveryRate: rates.deliveryRate,
|
|
163
|
+
openRate: rates.openRate,
|
|
164
|
+
clickRate: rates.clickRate
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return breakdown;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private calculateEventTypeBreakdown(events: EmailEvent[]): Record<EmailEventType, number> {
|
|
172
|
+
const eventTypes = Object.values(EmailEventType);
|
|
173
|
+
const breakdown = {} as Record<EmailEventType, number>;
|
|
174
|
+
|
|
175
|
+
for (const eventType of eventTypes) {
|
|
176
|
+
breakdown[eventType] = events.filter(e => e.type === eventType).length;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return breakdown;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private calculateBounceTypeBreakdown(events: EmailEvent[]): { hard: number; soft: number } | undefined {
|
|
183
|
+
const bounceEvents = events.filter(e => e.type === EmailEventType.BOUNCED);
|
|
184
|
+
if (bounceEvents.length === 0) return undefined;
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
hard: bounceEvents.filter(e => e.metadata.bounceType === 'hard').length,
|
|
188
|
+
soft: bounceEvents.filter(e => e.metadata.bounceType === 'soft').length
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private calculateTopLinks(events: EmailEvent[]): Array<{ url: string; clicks: number; uniqueClicks: number }> | undefined {
|
|
193
|
+
const clickEvents = events.filter(e => e.type === EmailEventType.CLICKED && e.metadata.url);
|
|
194
|
+
if (clickEvents.length === 0) return undefined;
|
|
195
|
+
|
|
196
|
+
const linkStats = new Map<string, { clicks: number; recipients: Set<string> }>();
|
|
197
|
+
|
|
198
|
+
for (const event of clickEvents) {
|
|
199
|
+
const url = event.metadata.url!;
|
|
200
|
+
const existing = linkStats.get(url) || { clicks: 0, recipients: new Set<string>() };
|
|
201
|
+
existing.clicks++;
|
|
202
|
+
existing.recipients.add(event.recipient);
|
|
203
|
+
linkStats.set(url, existing);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return Array.from(linkStats.entries())
|
|
207
|
+
.map(([url, stats]) => ({
|
|
208
|
+
url,
|
|
209
|
+
clicks: stats.clicks,
|
|
210
|
+
uniqueClicks: stats.recipients.size
|
|
211
|
+
}))
|
|
212
|
+
.sort((a, b) => b.clicks - a.clicks)
|
|
213
|
+
.slice(0, 10);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
private calculateDeviceBreakdown(events: EmailEvent[]): { desktop: number; mobile: number; tablet: number; unknown: number } | undefined {
|
|
217
|
+
const interactionEvents = events.filter(
|
|
218
|
+
e => (e.type === EmailEventType.OPENED || e.type === EmailEventType.CLICKED) && e.metadata.deviceType
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
if (interactionEvents.length === 0) return undefined;
|
|
222
|
+
|
|
223
|
+
return {
|
|
224
|
+
desktop: interactionEvents.filter(e => e.metadata.deviceType === 'desktop').length,
|
|
225
|
+
mobile: interactionEvents.filter(e => e.metadata.deviceType === 'mobile').length,
|
|
226
|
+
tablet: interactionEvents.filter(e => e.metadata.deviceType === 'tablet').length,
|
|
227
|
+
unknown: interactionEvents.filter(e => e.metadata.deviceType === 'unknown').length
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2025 Bernier LLC
|
|
3
|
+
|
|
4
|
+
This file is licensed to the client under a limited-use license.
|
|
5
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export { EmailWebhookEventsService } from './EmailWebhookEventsService';
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
EmailEvent,
|
|
13
|
+
EmailEventType,
|
|
14
|
+
EmailProvider,
|
|
15
|
+
EmailEventMetadata,
|
|
16
|
+
EmailAnalytics,
|
|
17
|
+
ProviderAnalytics,
|
|
18
|
+
EmailWebhookEventsConfig,
|
|
19
|
+
ProviderWebhookConfig,
|
|
20
|
+
NotificationChannel,
|
|
21
|
+
EmailWebhookResult,
|
|
22
|
+
WebhookPayload,
|
|
23
|
+
AnalyticsFilters,
|
|
24
|
+
EventQuery,
|
|
25
|
+
EmailWebhookError,
|
|
26
|
+
EmailWebhookErrorCode
|
|
27
|
+
} from './types';
|
|
28
|
+
|
|
29
|
+
export { EventNormalizer } from './normalizers/EventNormalizer';
|
|
30
|
+
export { SendGridNormalizer } from './normalizers/SendGridNormalizer';
|
|
31
|
+
export { MailgunNormalizer } from './normalizers/MailgunNormalizer';
|
|
32
|
+
export { SESNormalizer } from './normalizers/SESNormalizer';
|
|
33
|
+
export { PostmarkNormalizer } from './normalizers/PostmarkNormalizer';
|
|
34
|
+
export { SMTP2GONormalizer } from './normalizers/SMTP2GONormalizer';
|
|
35
|
+
|
|
36
|
+
export { AnalyticsEngine } from './analytics/AnalyticsEngine';
|
|
37
|
+
|
|
38
|
+
export { PersistenceAdapter } from './persistence/PersistenceAdapter';
|
|
39
|
+
export { InMemoryPersistenceAdapter } from './persistence/InMemoryPersistenceAdapter';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2025 Bernier LLC
|
|
3
|
+
|
|
4
|
+
This file is licensed to the client under a limited-use license.
|
|
5
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { EmailEvent, WebhookPayload } from '../types';
|
|
10
|
+
|
|
11
|
+
export interface EventNormalizer {
|
|
12
|
+
normalize(webhook: WebhookPayload): EmailEvent;
|
|
13
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2025 Bernier LLC
|
|
3
|
+
|
|
4
|
+
This file is licensed to the client under a limited-use license.
|
|
5
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { EventNormalizer } from './EventNormalizer';
|
|
10
|
+
import { EmailEvent, EmailEventType, EmailProvider, WebhookPayload } from '../types';
|
|
11
|
+
|
|
12
|
+
export class MailgunNormalizer implements EventNormalizer {
|
|
13
|
+
normalize(webhook: WebhookPayload): EmailEvent {
|
|
14
|
+
const payload = webhook.payload as Record<string, unknown>;
|
|
15
|
+
const event = payload['event-data'] as Record<string, unknown>;
|
|
16
|
+
const message = (event.message as Record<string, unknown>) || {};
|
|
17
|
+
const headers = (message.headers as Record<string, string>) || {};
|
|
18
|
+
const deliveryStatus = event['delivery-status'] as Record<string, unknown> | undefined;
|
|
19
|
+
const clientInfo = event['client-info'] as Record<string, unknown> | undefined;
|
|
20
|
+
const geolocation = event.geolocation as Record<string, string> | undefined;
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
id: this.generateEventId(event),
|
|
24
|
+
emailId: headers['message-id'] || '',
|
|
25
|
+
messageId: (event.id as string) || '',
|
|
26
|
+
type: this.mapEventType(event.event as string),
|
|
27
|
+
recipient: (event.recipient as string) || '',
|
|
28
|
+
timestamp: new Date((event.timestamp as number) * 1000),
|
|
29
|
+
provider: EmailProvider.MAILGUN,
|
|
30
|
+
metadata: {
|
|
31
|
+
url: event.url as string | undefined,
|
|
32
|
+
bounceType: (event.severity as string) === 'permanent' ? 'hard' : 'soft',
|
|
33
|
+
bounceReason: deliveryStatus?.message as string | undefined,
|
|
34
|
+
bounceCode: deliveryStatus?.code?.toString(),
|
|
35
|
+
ipAddress: clientInfo?.['client-ip'] as string | undefined,
|
|
36
|
+
userAgent: clientInfo?.['user-agent'] as string | undefined,
|
|
37
|
+
deviceType: this.detectDeviceType(clientInfo?.['user-agent'] as string | undefined),
|
|
38
|
+
location: {
|
|
39
|
+
country: geolocation?.country,
|
|
40
|
+
region: geolocation?.region,
|
|
41
|
+
city: geolocation?.city
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
rawEvent: event
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private mapEventType(mailgunEvent: string): EmailEventType {
|
|
49
|
+
const mapping: Record<string, EmailEventType> = {
|
|
50
|
+
'delivered': EmailEventType.DELIVERED,
|
|
51
|
+
'opened': EmailEventType.OPENED,
|
|
52
|
+
'clicked': EmailEventType.CLICKED,
|
|
53
|
+
'bounced': EmailEventType.BOUNCED,
|
|
54
|
+
'complained': EmailEventType.COMPLAINED,
|
|
55
|
+
'unsubscribed': EmailEventType.UNSUBSCRIBED,
|
|
56
|
+
'failed': EmailEventType.FAILED
|
|
57
|
+
};
|
|
58
|
+
return mapping[mailgunEvent] || EmailEventType.FAILED;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private generateEventId(event: Record<string, unknown>): string {
|
|
62
|
+
return `mailgun-${event.id as string}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private detectDeviceType(userAgent?: string): 'desktop' | 'mobile' | 'tablet' | 'unknown' {
|
|
66
|
+
if (!userAgent) return 'unknown';
|
|
67
|
+
const ua = userAgent.toLowerCase();
|
|
68
|
+
if (ua.includes('mobile')) return 'mobile';
|
|
69
|
+
if (ua.includes('tablet') || ua.includes('ipad')) return 'tablet';
|
|
70
|
+
return 'desktop';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2025 Bernier LLC
|
|
3
|
+
|
|
4
|
+
This file is licensed to the client under a limited-use license.
|
|
5
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { EventNormalizer } from './EventNormalizer';
|
|
10
|
+
import { EmailEvent, EmailEventType, EmailProvider, WebhookPayload } from '../types';
|
|
11
|
+
|
|
12
|
+
export class PostmarkNormalizer implements EventNormalizer {
|
|
13
|
+
normalize(webhook: WebhookPayload): EmailEvent {
|
|
14
|
+
const event = webhook.payload as Record<string, unknown>;
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
id: this.generateEventId(event),
|
|
18
|
+
emailId: (event.MessageID as string) || '',
|
|
19
|
+
messageId: (event.MessageID as string) || '',
|
|
20
|
+
type: this.mapEventType(event.RecordType as string),
|
|
21
|
+
recipient: (event.Recipient as string) || '',
|
|
22
|
+
timestamp: new Date(event.ReceivedAt as string),
|
|
23
|
+
provider: EmailProvider.POSTMARK,
|
|
24
|
+
metadata: {
|
|
25
|
+
url: event.OriginalLink as string | undefined,
|
|
26
|
+
bounceType: this.mapBounceType(event.Type as string | undefined),
|
|
27
|
+
bounceReason: event.Description as string | undefined,
|
|
28
|
+
bounceCode: event.TypeCode?.toString(),
|
|
29
|
+
complaintFeedbackType: event.Type as string | undefined,
|
|
30
|
+
userAgent: event.UserAgent as string | undefined,
|
|
31
|
+
deviceType: this.detectDeviceType(event.UserAgent as string | undefined),
|
|
32
|
+
location: {
|
|
33
|
+
country: (event.Geo as Record<string, string> | undefined)?.Country,
|
|
34
|
+
region: (event.Geo as Record<string, string> | undefined)?.Region,
|
|
35
|
+
city: (event.Geo as Record<string, string> | undefined)?.City
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
rawEvent: event
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private mapEventType(postmarkEvent: string): EmailEventType {
|
|
43
|
+
const mapping: Record<string, EmailEventType> = {
|
|
44
|
+
'Delivery': EmailEventType.DELIVERED,
|
|
45
|
+
'Open': EmailEventType.OPENED,
|
|
46
|
+
'Click': EmailEventType.CLICKED,
|
|
47
|
+
'Bounce': EmailEventType.BOUNCED,
|
|
48
|
+
'SpamComplaint': EmailEventType.COMPLAINED,
|
|
49
|
+
'SubscriptionChange': EmailEventType.UNSUBSCRIBED
|
|
50
|
+
};
|
|
51
|
+
return mapping[postmarkEvent] || EmailEventType.FAILED;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private mapBounceType(bounceType?: string): 'hard' | 'soft' | undefined {
|
|
55
|
+
if (!bounceType) return undefined;
|
|
56
|
+
if (bounceType.includes('Hard')) return 'hard';
|
|
57
|
+
if (bounceType.includes('Soft')) return 'soft';
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private generateEventId(event: Record<string, unknown>): string {
|
|
62
|
+
return `postmark-${(event.MessageID as string) || ''}-${Date.now()}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private detectDeviceType(userAgent?: string): 'desktop' | 'mobile' | 'tablet' | 'unknown' {
|
|
66
|
+
if (!userAgent) return 'unknown';
|
|
67
|
+
const ua = userAgent.toLowerCase();
|
|
68
|
+
if (ua.includes('mobile')) return 'mobile';
|
|
69
|
+
if (ua.includes('tablet') || ua.includes('ipad')) return 'tablet';
|
|
70
|
+
return 'desktop';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2025 Bernier LLC
|
|
3
|
+
|
|
4
|
+
This file is licensed to the client under a limited-use license.
|
|
5
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { EventNormalizer } from './EventNormalizer';
|
|
10
|
+
import { EmailEvent, EmailEventType, EmailProvider, WebhookPayload } from '../types';
|
|
11
|
+
|
|
12
|
+
export class SESNormalizer implements EventNormalizer {
|
|
13
|
+
normalize(webhook: WebhookPayload): EmailEvent {
|
|
14
|
+
const payload = webhook.payload as Record<string, unknown>;
|
|
15
|
+
const message = JSON.parse(payload.Message as string) as Record<string, unknown>;
|
|
16
|
+
const eventType = message.eventType as string;
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
id: this.generateEventId(message),
|
|
20
|
+
emailId: this.extractMessageId(message),
|
|
21
|
+
messageId: this.extractMessageId(message),
|
|
22
|
+
type: this.mapEventType(eventType),
|
|
23
|
+
recipient: this.extractRecipient(message),
|
|
24
|
+
timestamp: new Date((message.mail as Record<string, unknown>).timestamp as string),
|
|
25
|
+
provider: EmailProvider.AWS_SES,
|
|
26
|
+
metadata: {
|
|
27
|
+
bounceType: this.extractBounceType(message),
|
|
28
|
+
bounceReason: this.extractBounceReason(message),
|
|
29
|
+
complaintFeedbackType: this.extractComplaintType(message),
|
|
30
|
+
url: this.extractClickUrl(message),
|
|
31
|
+
ipAddress: this.extractIpAddress(message),
|
|
32
|
+
userAgent: this.extractUserAgent(message)
|
|
33
|
+
},
|
|
34
|
+
rawEvent: message
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private mapEventType(sesEvent: string): EmailEventType {
|
|
39
|
+
const mapping: Record<string, EmailEventType> = {
|
|
40
|
+
'Delivery': EmailEventType.DELIVERED,
|
|
41
|
+
'Open': EmailEventType.OPENED,
|
|
42
|
+
'Click': EmailEventType.CLICKED,
|
|
43
|
+
'Bounce': EmailEventType.BOUNCED,
|
|
44
|
+
'Complaint': EmailEventType.COMPLAINED,
|
|
45
|
+
'Reject': EmailEventType.FAILED
|
|
46
|
+
};
|
|
47
|
+
return mapping[sesEvent] || EmailEventType.FAILED;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private extractMessageId(message: Record<string, unknown>): string {
|
|
51
|
+
const mail = message.mail as Record<string, unknown>;
|
|
52
|
+
return (mail?.messageId as string) || '';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private extractRecipient(message: Record<string, unknown>): string {
|
|
56
|
+
const bounce = message.bounce as Record<string, unknown> | undefined;
|
|
57
|
+
const complaint = message.complaint as Record<string, unknown> | undefined;
|
|
58
|
+
const mail = message.mail as Record<string, unknown>;
|
|
59
|
+
|
|
60
|
+
if (bounce) {
|
|
61
|
+
const bouncedRecipients = bounce.bouncedRecipients as Array<Record<string, string>> | undefined;
|
|
62
|
+
if (bouncedRecipients?.[0]?.emailAddress) {
|
|
63
|
+
return bouncedRecipients[0].emailAddress;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (complaint) {
|
|
68
|
+
const complainedRecipients = complaint.complainedRecipients as Array<Record<string, string>> | undefined;
|
|
69
|
+
if (complainedRecipients?.[0]?.emailAddress) {
|
|
70
|
+
return complainedRecipients[0].emailAddress;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const destination = mail.destination as string[] | undefined;
|
|
75
|
+
return destination?.[0] || '';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private extractBounceType(message: Record<string, unknown>): 'hard' | 'soft' | undefined {
|
|
79
|
+
const bounce = message.bounce as Record<string, unknown> | undefined;
|
|
80
|
+
if (!bounce) return undefined;
|
|
81
|
+
return (bounce.bounceType as string)?.toLowerCase() as 'hard' | 'soft' | undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private extractBounceReason(message: Record<string, unknown>): string | undefined {
|
|
85
|
+
const bounce = message.bounce as Record<string, unknown> | undefined;
|
|
86
|
+
if (!bounce) return undefined;
|
|
87
|
+
const bouncedRecipients = bounce.bouncedRecipients as Array<Record<string, string>> | undefined;
|
|
88
|
+
return bouncedRecipients?.[0]?.diagnosticCode;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private extractComplaintType(message: Record<string, unknown>): string | undefined {
|
|
92
|
+
const complaint = message.complaint as Record<string, unknown> | undefined;
|
|
93
|
+
return complaint?.complaintFeedbackType as string | undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private extractClickUrl(message: Record<string, unknown>): string | undefined {
|
|
97
|
+
const click = message.click as Record<string, unknown> | undefined;
|
|
98
|
+
return click?.link as string | undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private extractIpAddress(message: Record<string, unknown>): string | undefined {
|
|
102
|
+
const open = message.open as Record<string, unknown> | undefined;
|
|
103
|
+
const click = message.click as Record<string, unknown> | undefined;
|
|
104
|
+
return (open?.ipAddress as string) || (click?.ipAddress as string) || undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private extractUserAgent(message: Record<string, unknown>): string | undefined {
|
|
108
|
+
const open = message.open as Record<string, unknown> | undefined;
|
|
109
|
+
const click = message.click as Record<string, unknown> | undefined;
|
|
110
|
+
return (open?.userAgent as string) || (click?.userAgent as string) || undefined;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private generateEventId(message: Record<string, unknown>): string {
|
|
114
|
+
const mail = message.mail as Record<string, unknown>;
|
|
115
|
+
return `ses-${(mail?.messageId as string) || ''}-${Date.now()}`;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2025 Bernier LLC
|
|
3
|
+
|
|
4
|
+
This file is licensed to the client under a limited-use license.
|
|
5
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { EventNormalizer } from './EventNormalizer';
|
|
10
|
+
import { EmailEvent, EmailEventType, EmailProvider, WebhookPayload } from '../types';
|
|
11
|
+
|
|
12
|
+
export class SMTP2GONormalizer implements EventNormalizer {
|
|
13
|
+
normalize(webhook: WebhookPayload): EmailEvent {
|
|
14
|
+
const event = webhook.payload as Record<string, unknown>;
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
id: this.generateEventId(event),
|
|
18
|
+
emailId: (event.email_id as string) || (event.message_id as string) || '',
|
|
19
|
+
messageId: (event.message_id as string) || '',
|
|
20
|
+
type: this.mapEventType(event.event as string),
|
|
21
|
+
recipient: (event.rcpt_to as string) || '',
|
|
22
|
+
timestamp: new Date(event.timestamp as string),
|
|
23
|
+
provider: EmailProvider.SMTP2GO,
|
|
24
|
+
metadata: {
|
|
25
|
+
bounceType: this.mapBounceType(event.bounce_type as string | undefined),
|
|
26
|
+
bounceReason: event.reason as string | undefined,
|
|
27
|
+
failureReason: event.reason as string | undefined
|
|
28
|
+
},
|
|
29
|
+
rawEvent: event
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private mapEventType(smtp2goEvent: string): EmailEventType {
|
|
34
|
+
const mapping: Record<string, EmailEventType> = {
|
|
35
|
+
'delivered': EmailEventType.DELIVERED,
|
|
36
|
+
'open': EmailEventType.OPENED,
|
|
37
|
+
'click': EmailEventType.CLICKED,
|
|
38
|
+
'bounce': EmailEventType.BOUNCED,
|
|
39
|
+
'failed': EmailEventType.FAILED
|
|
40
|
+
};
|
|
41
|
+
return mapping[smtp2goEvent] || EmailEventType.FAILED;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private mapBounceType(bounceType?: string): 'hard' | 'soft' | undefined {
|
|
45
|
+
if (!bounceType) return undefined;
|
|
46
|
+
if (bounceType.toLowerCase().includes('hard')) return 'hard';
|
|
47
|
+
if (bounceType.toLowerCase().includes('soft')) return 'soft';
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private generateEventId(event: Record<string, unknown>): string {
|
|
52
|
+
return `smtp2go-${(event.message_id as string) || ''}-${Date.now()}`;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2025 Bernier LLC
|
|
3
|
+
|
|
4
|
+
This file is licensed to the client under a limited-use license.
|
|
5
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { EventNormalizer } from './EventNormalizer';
|
|
10
|
+
import { EmailEvent, EmailEventType, EmailProvider, WebhookPayload } from '../types';
|
|
11
|
+
|
|
12
|
+
export class SendGridNormalizer implements EventNormalizer {
|
|
13
|
+
normalize(webhook: WebhookPayload): EmailEvent {
|
|
14
|
+
const event = webhook.payload as Record<string, unknown>;
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
id: this.generateEventId(event),
|
|
18
|
+
emailId: (event.email_id as string) || (event.sg_message_id as string) || '',
|
|
19
|
+
messageId: (event.sg_message_id as string) || '',
|
|
20
|
+
type: this.mapEventType(event.event as string),
|
|
21
|
+
recipient: (event.email as string) || '',
|
|
22
|
+
timestamp: new Date((event.timestamp as number) * 1000),
|
|
23
|
+
provider: EmailProvider.SENDGRID,
|
|
24
|
+
metadata: {
|
|
25
|
+
url: event.url as string | undefined,
|
|
26
|
+
bounceType: event.event === 'bounce'
|
|
27
|
+
? ((event.status as string)?.startsWith('5') ? 'hard' : 'soft')
|
|
28
|
+
: undefined,
|
|
29
|
+
bounceReason: event.reason as string | undefined,
|
|
30
|
+
userAgent: event.useragent as string | undefined,
|
|
31
|
+
ipAddress: event.ip as string | undefined,
|
|
32
|
+
deviceType: this.detectDeviceType(event.useragent as string | undefined)
|
|
33
|
+
},
|
|
34
|
+
rawEvent: event
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private mapEventType(sendgridEvent: string): EmailEventType {
|
|
39
|
+
const mapping: Record<string, EmailEventType> = {
|
|
40
|
+
'delivered': EmailEventType.DELIVERED,
|
|
41
|
+
'open': EmailEventType.OPENED,
|
|
42
|
+
'click': EmailEventType.CLICKED,
|
|
43
|
+
'bounce': EmailEventType.BOUNCED,
|
|
44
|
+
'dropped': EmailEventType.DROPPED,
|
|
45
|
+
'spamreport': EmailEventType.COMPLAINED,
|
|
46
|
+
'unsubscribe': EmailEventType.UNSUBSCRIBED,
|
|
47
|
+
'deferred': EmailEventType.DEFERRED
|
|
48
|
+
};
|
|
49
|
+
return mapping[sendgridEvent] || EmailEventType.FAILED;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private generateEventId(event: Record<string, unknown>): string {
|
|
53
|
+
return `sendgrid-${(event.sg_event_id as string) || Date.now()}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private detectDeviceType(userAgent?: string): 'desktop' | 'mobile' | 'tablet' | 'unknown' {
|
|
57
|
+
if (!userAgent) return 'unknown';
|
|
58
|
+
const ua = userAgent.toLowerCase();
|
|
59
|
+
if (ua.includes('mobile')) return 'mobile';
|
|
60
|
+
if (ua.includes('tablet') || ua.includes('ipad')) return 'tablet';
|
|
61
|
+
return 'desktop';
|
|
62
|
+
}
|
|
63
|
+
}
|