feat: implement MailGun webhook service for logging email events

Implemented a production-ready TypeScript/Express.js service to receive
and log MailGun webhook events (delivered, failed, opened, clicked, etc.).

Key features:
- Webhook endpoint (POST /webhook) with comprehensive event logging
- Full TypeScript type definitions for all MailGun event types
- Prometheus metrics integration for monitoring
- Health check endpoint (GET /ping)
- Comprehensive Jest test suite with 87.76% coverage
- Docker containerization with build scripts

Removed template/example code:
- All SQL/MSSQL dependencies and related code
- Example auth router and middleware
- PRTG metrics support (simplified to Prometheus only)
- Unused middleware (CORS, IP whitelist, request parsing/validation)
- Template documentation (kept only MailGun webhook API spec)

The service is clean, minimal, and focused solely on receiving and
logging MailGun webhook events to the console.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Knee Cola
2026-01-02 20:56:22 +01:00
parent 4371a9a20a
commit 7aeea9353d
39 changed files with 12015 additions and 14 deletions

View File

@@ -0,0 +1,59 @@
/**
* @fileoverview
* This file is a part of an "auth" example router, which was taken from an existing integration.
* It is to be used only as a reference for how an API router for a web service should be structured.
* In a real-live implementation all files related to `/auth` route example should be removed
* by new set of files implementing the new API.
*/
import { Request, Response, NextFunction } from 'express';
import { AppLocals } from "../../src/types/AppLocals";
interface IMockHttpParams {
sessionID?: number
sessionGUID?: string
table_id?: string
clientIp?: string
free_play: 0 | 1
}
interface IMockHttpContext {
clientIpAddress?:string
reqPath?:string
headersSent?:boolean
writableEnded?:boolean
method?:string
params?: IMockHttpParams
}
export const defaultMockParams:IMockHttpParams = {
sessionID: 123,
sessionGUID: '016e6812-b915-4e5e-94fe-193582239b96',
table_id: 'mock-table-id',
clientIp: '192.168.1.10',
free_play: 0
}
export const mockHttpContext = ({reqPath="/", headersSent=false, writableEnded=false, method="GET", params=defaultMockParams}:IMockHttpContext|undefined = {}) => {
const req = {
path:reqPath,
method,
url:`https://localhost${reqPath}`,
params,
} as unknown as Request;
const res = {
end: jest.fn(),
status: jest.fn(),
setHeader: jest.fn(),
params,
locals: {
stopPrometheusTimer: jest.fn(),
} as unknown as AppLocals,
headersSent,
writableEnded,
} as unknown as Response;
const next:NextFunction = jest.fn();
return({req,res,next})
}

View File

@@ -0,0 +1,115 @@
/**
* Helper for creating mock MailGun webhook events for testing
*/
import {
MailgunWebhookEvent,
MailgunDeliveredEvent,
MailgunFailedEvent,
MailgunOpenedEvent,
MailgunClickedEvent,
MailgunBouncedEvent,
MailgunComplainedEvent,
MailgunUnsubscribedEvent
} from '../../src/types/MailgunWebhookEvent';
/**
* Base event data common to all webhook events
*/
const baseEventData = {
timestamp: '1234567890',
token: 'mock-token-123',
signature: 'mock-signature-abc123',
recipient: 'user@example.com',
domain: 'mail.example.com',
'message-id': '<20240101120000.1.ABC123@mail.example.com>',
'Message-Id': '<20240101120000.1.ABC123@mail.example.com>'
};
/**
* Creates a mock 'delivered' event
*/
export const createMockDeliveredEvent = (): MailgunDeliveredEvent => ({
...baseEventData,
event: 'delivered',
'message-headers': JSON.stringify([
['Subject', 'Test Email'],
['From', 'sender@example.com']
])
});
/**
* Creates a mock 'failed' event
*/
export const createMockFailedEvent = (): MailgunFailedEvent => ({
...baseEventData,
event: 'failed',
severity: 'permanent',
reason: 'bounce',
notification: 'User inbox is full'
});
/**
* Creates a mock 'opened' event
*/
export const createMockOpenedEvent = (): MailgunOpenedEvent => ({
...baseEventData,
event: 'opened',
city: 'San Francisco',
country: 'US',
'device-type': 'desktop',
'client-os': 'macOS',
'client-name': 'Apple Mail',
ip: '192.168.1.100'
});
/**
* Creates a mock 'clicked' event
*/
export const createMockClickedEvent = (): MailgunClickedEvent => ({
...baseEventData,
event: 'clicked',
url: 'https://example.com/link',
city: 'New York',
country: 'US',
'device-type': 'mobile',
'client-os': 'iOS',
'client-name': 'Gmail',
ip: '192.168.1.101'
});
/**
* Creates a mock 'bounced' event
*/
export const createMockBouncedEvent = (): MailgunBouncedEvent => ({
...baseEventData,
event: 'bounced',
code: '550',
error: 'User not found',
notification: 'The email account that you tried to reach does not exist'
});
/**
* Creates a mock 'complained' event
*/
export const createMockComplainedEvent = (): MailgunComplainedEvent => ({
...baseEventData,
event: 'complained'
});
/**
* Creates a mock 'unsubscribed' event
*/
export const createMockUnsubscribedEvent = (): MailgunUnsubscribedEvent => ({
...baseEventData,
event: 'unsubscribed'
});
/**
* Creates an invalid event missing required fields
*/
export const createInvalidEvent = () => ({
timestamp: '1234567890',
token: 'mock-token-123'
// Missing 'event' and 'recipient' fields
});