import { Router, type Request, type Response } from 'express'; import { MockDataService } from '../services/mock-data-service'; const router = Router(); // POST /v2/webhooks - Configure webhook endpoints router.post('/', (req: Request, res: Response) => { try { const { url, events, secret, description } = req.body; // Validate required fields if (!url) { return res.status(400).json({ error: 'validation_error', message: 'url is required', status_code: 400 }); } if (!events || !Array.isArray(events) || events.length === 0) { return res.status(400).json({ error: 'validation_error', message: 'events array is required and must not be empty', status_code: 400 }); } if (!secret) { return res.status(400).json({ error: 'validation_error', message: 'secret is required', status_code: 400 }); } // Create webhook const webhook = MockDataService.createWebhook({ url, events, secret, description }); const response = { webhook_id: webhook.webhook_id, url: webhook.url, events: webhook.events, secret: webhook.secret, description: webhook.description, status: webhook.status, created_at: webhook.created_at, updated_at: webhook.updated_at }; res.status(201).json(response); } catch (_error) { res.status(500).json({ error: 'internal_server_error', message: 'An unexpected error occurred', status_code: 500 }); } }); // GET /v2/webhooks - List webhook configurations router.get('/', (req: Request, res: Response) => { try { const { limit = 10, offset = 0 } = req.query; const webhooks = MockDataService.getWebhooks(); // Apply pagination const total = webhooks.length; const paginatedWebhooks = webhooks.slice( parseInt(offset as string), parseInt(offset as string) + parseInt(limit as string) ); const response = { webhooks: paginatedWebhooks.map(webhook => ({ webhook_id: webhook.webhook_id, url: webhook.url, events: webhook.events, status: webhook.status, created_at: webhook.created_at, updated_at: webhook.updated_at })), pagination: { total, limit: parseInt(limit as string), offset: parseInt(offset as string), has_more: parseInt(offset as string) + parseInt(limit as string) < total } }; res.json(response); } catch (_error) { res.status(500).json({ error: 'internal_server_error', message: 'An unexpected error occurred', status_code: 500 }); } }); // DELETE /v2/webhooks/:id - Delete webhook configuration router.delete('/:id', (req: Request, res: Response) => { try { const { id } = req.params; const deleted = MockDataService.deleteWebhook(id); if (!deleted) { return res.status(404).json({ error: 'webhook_not_found', message: `Webhook with ID ${id} not found`, status_code: 404 }); } const response = { webhook_id: id, message: 'Webhook deleted successfully' }; res.json(response); } catch (_error) { res.status(500).json({ error: 'internal_server_error', message: 'An unexpected error occurred', status_code: 500 }); } }); export default router;