wulf-pulse/docs/WEBHOOKS_README.md

128 lines
3.4 KiB
Markdown
Raw Permalink Normal View History

feat: add webhook support for real-time Autotask updates Implements comprehensive webhook infrastructure to receive and process real-time entity updates from Autotask, reducing API calls and improving data freshness. Features: - Webhook receiver endpoint: POST /api/webhooks/autotask - Automatic entity mapping and upsert to PostgreSQL - Event logging and tracking in webhook_logs table - Duplicate event prevention via unique event_id - Failed event tracking with error messages - Statistics and monitoring APIs - Support for 8 entity types: Companies, Tickets, Tasks, Projects, Time Entries, Contacts, Contracts, Configuration Items Architecture: - WebhookService: Core processing logic - Database tables: webhook_logs, webhook_configs - API endpoints: /autotask (receiver), /logs, /stats - Automatic data mapping using existing entity-mapper Benefits: - Near real-time updates (<1 minute vs 24 hours) - Reduced API usage (webhooks vs polling) - Complements daily incremental sync for redundancy - Automatic recovery from webhook failures Files Added: - lib/types/webhook.ts - TypeScript types and interfaces - lib/services/webhook-service.ts - Webhook processing service - app/api/webhooks/autotask/route.ts - Webhook receiver - app/api/webhooks/logs/route.ts - Logs API - app/api/webhooks/stats/route.ts - Statistics API - migrations/004_webhook_support.sql - Database schema - docs/WEBHOOK_SETUP.md - Complete setup guide (47 sections) - docs/WEBHOOKS_README.md - Quick start guide Next Steps: 1. Run database migration 2. Configure webhooks in Autotask 3. Test endpoint and monitor logs See docs/WEBHOOK_SETUP.md for detailed setup instructions.
2026-01-24 10:07:20 -05:00
# Webhook Integration - Quick Start
## What's Been Implemented
**Webhook Infrastructure**
- Webhook receiver endpoint: `/api/webhooks/autotask`
- Database tables for tracking webhook events
- Webhook processing service with automatic entity upsert
- Monitoring APIs for logs and statistics
**Supported Entities**
- Companies, Tickets, Tasks, Projects, Time Entries, Contacts, Contracts, Configuration Items
**Features**
- Real-time entity updates from Autotask
- Automatic data mapping and upsert to PostgreSQL
- Event logging and error tracking
- Duplicate event prevention
- Performance monitoring
## Quick Setup
### 1. Run Database Migration
```bash
docker exec pulse-postgres psql -U pulse_user -d pulse_autotask -f /app/migrations/004_webhook_support.sql
```
### 2. Configure Webhooks in Autotask
For each entity (Companies, Tickets, Tasks, etc.):
1. Go to Autotask → Admin → Webhooks
2. Create new webhook:
- **URL:** `https://your-domain.com/api/webhooks/autotask`
- **Entity:** Select entity type
- **Events:** Create, Update
- **Include Entity Data:** ✅ Enabled
3. Save and note the webhook ID
### 3. Test the Endpoint
```bash
# Health check
curl https://your-domain.com/api/webhooks/autotask
# View recent webhooks
curl https://your-domain.com/api/webhooks/logs?limit=10
# View statistics
curl https://your-domain.com/api/webhooks/stats?hours=24
```
## Recommended Sync Strategy
**Webhooks (Real-Time)** + **Daily Incremental Sync** + **Weekly Full Sync**
This combination ensures:
- ✅ Near-instant updates via webhooks
- ✅ Backup sync catches missed events
- ✅ Weekly refresh ensures data integrity
## Files Created
```
/lib/types/webhook.ts - TypeScript types
/lib/services/webhook-service.ts - Webhook processing logic
/app/api/webhooks/autotask/route.ts - Webhook receiver endpoint
/app/api/webhooks/logs/route.ts - Logs API
/app/api/webhooks/stats/route.ts - Statistics API
/migrations/004_webhook_support.sql - Database schema
/docs/WEBHOOK_SETUP.md - Complete setup guide
```
## Next Steps
1. **Deploy the changes** - Rebuild and restart the application
2. **Run migration** - Create webhook tables in database
3. **Configure Autotask** - Set up webhooks for desired entities
4. **Monitor** - Check logs and statistics to verify webhooks are working
## Full Documentation
See [WEBHOOK_SETUP.md](./WEBHOOK_SETUP.md) for complete setup instructions, troubleshooting, and best practices.
## Architecture
```
Autotask → Webhook Event
/api/webhooks/autotask (Receiver)
WebhookService.processWebhook()
1. Log event to webhook_logs
2. Validate entity type is active
3. Map Autotask data to PostgreSQL schema
4. Upsert to appropriate table
5. Update log status (processed/failed)
Real-time data in PostgreSQL ✅
```
## Monitoring Queries
```sql
-- Recent webhooks
SELECT event_id, entity_type, entity_id, status, processing_time_ms
FROM webhook_logs
ORDER BY received_at DESC
LIMIT 20;
-- Failed webhooks
SELECT event_id, entity_type, error_message
FROM webhook_logs
WHERE status = 'failed'
ORDER BY received_at DESC;
-- Statistics by entity
SELECT entity_type, COUNT(*) as total,
COUNT(*) FILTER (WHERE status = 'processed') as processed,
COUNT(*) FILTER (WHERE status = 'failed') as failed,
AVG(processing_time_ms) as avg_ms
FROM webhook_logs
WHERE received_at >= NOW() - INTERVAL '24 hours'
GROUP BY entity_type;
```