BE-025: Implement Webhook Management Service - #314
Open
aojomo wants to merge 1 commit into
Open
Conversation
…, retry logic, secret rotation, and Prometheus metrics - Create Webhook, WebhookSubscription, WebhookDelivery TypeORM entities with full indexes - Add Prisma models for PostgreSQL compatibility - Implement WebhooksService with CRUD, HMAC-SHA256 signed event dispatch, and dual-secret rotation with 24h grace period - Implement WebhookProcessor with BullMQ for async delivery with exponential backoff retry and auto-disable after repeated failures - Create REST API controller with endpoints for registration, delivery history, retry, secret management, and status metrics - Add comprehensive DTOs with validation for HTTPS, event types, and pagination - Add Prometheus metrics: delivery counters, latency histograms, disabled endpoints gauge, active subscriptions gauge - Register WebhooksModule in AppModule and add 'webhooks' Swagger tag - Write 38 unit tests covering registration, validation, dispatch, retry, secret rotation, signature verification, and delivery history Closes DigiNodes#294
Contributor
|
resolve conflict @aojomo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📋 Summary
Closes #294
Implements BE-025 — Webhook Management Service, enabling external systems to subscribe to TruthBounty protocol events in real time via signed HTTP callbacks.
This service acts as the protocol's outbound integration layer, allowing wallets, analytics platforms, governance dashboards, exchanges, enterprise integrations, AI agents, and automation platforms to react immediately to protocol activity.
✅ Acceptance Criteria
🚀 What Was Implemented
1. Webhook Registration & Management
POST/GET/PATCH/DELETE /webhooks2. Event Subscriptions
Supports all 12 protocol event types:
claim.created,claim.updatedverification.started,verification.completeddispute.created,dispute.resolvedreward.distributedgovernance.created,governance.executedreputation.updated,treasury.updated,staking.updated3. Secure Delivery
X-Webhook-ID— unique request identifierX-Webhook-Signature— HMAC-SHA256 signatureX-Webhook-Nonce— single-use nonceX-Webhook-Timestamp— ISO 8601 timestampX-Webhook-Event— event typeX-Webhook-Delivery— delivery record IDcrypto.timingSafeEqualused for constant-time signature verification4. Retry System
DEAD_LETTERstatus after exhausting retriesPOST /webhooks/:id/deliveries/:deliveryId/retry5. Delivery History
6. Secret Rotation
POST /webhooks/:id/revoke-secret)7. Prometheus Monitoring Metrics
Six metrics exposed via the existing
/metricsendpoint:webhook_deliveries_totalwebhook_id,event_type,statuswebhook_delivery_latency_secondswebhook_id,event_type,statuswebhook_delivery_attempts_totalwebhook_id,event_type,statuswebhook_delivery_attempt_latency_secondswebhook_id,event_type,resultwebhook_disabled_endpoints_totalwebhook_active_subscriptions_total📂 Files Changed
New Files (13)
src/webhooks/entities/webhook.entity.tsWebhookEventTypeenumsrc/webhooks/entities/webhook-subscription.entity.tssrc/webhooks/entities/webhook-delivery.entity.tsDeliveryStatusenumsrc/webhooks/dto/create-webhook.dto.tssrc/webhooks/dto/update-webhook.dto.tssrc/webhooks/dto/webhook-filter.dto.tssrc/webhooks/webhooks.service.tssrc/webhooks/webhooks.processor.tssrc/webhooks/webhooks.controller.tssrc/webhooks/webhooks.module.tssrc/webhooks/webhooks.service.spec.tssrc/webhooks/webhooks.controller.spec.tsModified Files (3)
prisma/schema.prismaWebhook,WebhookSubscription,WebhookDeliverymodelssrc/app.module.tsWebhooksModulesrc/bootstrap.tswebhooksSwagger tag🧪 Testing
timingSafeEqual), delivery history pagination, status metrics🏗 Architecture
🔗 Dependencies
dispatchEvent()method provides the integration point. Once the event bus is implemented, it will calldispatchEvent()for each domain event.AuditTrailServicecan be injected intoWebhooksServiceto log mutations (create/update/delete/rotate). Ready for integration.WebhooksService.dispatchEvent()to send notification events to subscribed endpoints.🔜 Future Extensibility
The service is designed to support:
dispatchEvent()can be extended to produce to Kafka topicsPOST /webhooks/:id/testfor sender-side verification📚 Documentation
/apiwith fullwebhookstag@ApiOperation,@ApiResponse,@ApiParam@ApiPropertyfor OpenAPI schema generation