Skip to content

feat(webhooks): implement webhook system for contract event notifications (#13) - #44

Open
Ahbiz wants to merge 3 commits into
EquipChain:mainfrom
Ahbiz:feature/issue-13-webhooks
Open

feat(webhooks): implement webhook system for contract event notifications (#13)#44
Ahbiz wants to merge 3 commits into
EquipChain:mainfrom
Ahbiz:feature/issue-13-webhooks

Conversation

@Ahbiz

@Ahbiz Ahbiz commented Jul 28, 2026

Copy link
Copy Markdown

Summary

Closes #13

This PR implements an asynchronous Webhook Notification System that allows external services to register HTTP endpoints and receive real-time HTTP POST notifications when contract or system events occur (e.g., meter reading creation/updates, contract state changes, system alerts, user registration, and admin actions).


Key Changes Made

1. Central Event Emitter (src/services/eventEmitter.js)

  • Added an application-wide AppEventEmitter extending Node's EventEmitter to dispatch typed contract and system events (meter.reading.created, contract.state.changed, etc.) asynchronously across services.

2. Webhook Service (src/services/webhook.js)

  • Webhook Registration Store: Supports CRUD operations (registerWebhook, unregisterWebhook, listWebhooks, updateWebhook).
  • Standard Payload Formatting: Enforces standard event schema { id: "evt_...", type, created, data, webhookId: "wh_..." }.
  • HMAC-SHA256 Payload Signing: Generates X-Webhook-Signature headers using node:crypto HMAC-SHA256 based on the request body and shared secret.
  • Asynchronous Delivery & Exponential Backoff: Non-blocking delivery using Node fetch() with configurable timeouts and automatic retries (up to 3 retries with 1min, 5min, 15min backoff).
  • Delivery Logging: Logs all attempt timestamps, HTTP status codes, response bodies, and errors for auditing and debugging.

3. Admin Webhook Management API (src/routes/admin/webhooks.js)

  • Implemented protected admin CRUD endpoints mounted under /api/admin/webhooks:
    • POST /api/admin/webhooks — Register a webhook
    • GET /api/admin/webhooks — List registered webhooks (supports pagination and filtering)
    • GET /api/admin/webhooks/:id — Get single webhook details
    • PATCH /api/admin/webhooks/:id — Update webhook registration
    • DELETE /api/admin/webhooks/:id — Unregister webhook
    • GET /api/admin/webhooks/:id/logs — Retrieve delivery logs for a webhook

4. Admin Auth & Validation (src/middleware/auth.js, src/schemas/webhook.schema.js)

  • Added adminAuth middleware enforcing Bearer token or API key authentication for admin endpoints.
  • Added Zod schemas (createWebhookSchema, updateWebhookSchema, webhookQuerySchema) for strict request validation.

5. Repository Enhancements (src/repositories/WebhookRepository.js)

  • Enhanced WebhookRepository to support matching multiple event types per webhook, wildcard (*) filters, and detailed delivery attempt history.

6. App Integration & Tests

  • Updated index.js to mount admin webhook routes and initialize the event listener service on startup.
  • Created test/unit/webhook.test.js: Covers registration CRUD, HMAC signature generation/verification, event dispatching, retry logic, and delivery failure logging.
  • Created test/integration/webhook.test.js: Verifies full end-to-end flow over local HTTP server listener and REST API endpoints.

Verification & Testing

  • Run npm test
  • All 259 test cases pass cleanly (246 existing tests + 13 new webhook unit and integration tests).
  • Verified CI environment readiness.

@KarenZita01

Copy link
Copy Markdown
Member

Failed CI. Fix it

@KarenZita01

Copy link
Copy Markdown
Member

Had to fix your conflict because I waited & you didn't fix it. Your CI still didn't pass

@Ahbiz

Ahbiz commented Jul 30, 2026 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Implement Webhook System for Contract Event Notifications to External Services

2 participants