44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
/**
|
|
* @module routes/webhookRoutes
|
|
*/
|
|
|
|
import { Router } from 'express';
|
|
import { asyncHandler, AppError } from '../errors.js';
|
|
import { subscribe, unsubscribe } from '../services/webhookService.js';
|
|
import { listWebhooksForTenant } from '../store/webhooks.js';
|
|
|
|
export const webhookRoutes = Router();
|
|
|
|
webhookRoutes.post(
|
|
'/subscribe',
|
|
asyncHandler(async (req, res) => {
|
|
const { targetUrl, event = 'timestamp.completed' } = req.body ?? {};
|
|
try {
|
|
const hook = subscribe(req.auth, { targetUrl, event });
|
|
res.status(201).json(hook);
|
|
} catch (err) {
|
|
throw new AppError(err.message, { status: 400, code: 'VALIDATION_ERROR' });
|
|
}
|
|
}),
|
|
);
|
|
|
|
webhookRoutes.delete(
|
|
'/unsubscribe',
|
|
asyncHandler(async (req, res) => {
|
|
const { hookId, targetUrl } = req.body ?? req.query ?? {};
|
|
try {
|
|
const result = unsubscribe(req.auth, { hookId, targetUrl });
|
|
res.json(result);
|
|
} catch (err) {
|
|
throw new AppError(err.message, { status: 404, code: 'NOT_FOUND' });
|
|
}
|
|
}),
|
|
);
|
|
|
|
webhookRoutes.get(
|
|
'/',
|
|
asyncHandler(async (req, res) => {
|
|
const hooks = listWebhooksForTenant(req.auth.tenantId);
|
|
res.json({ webhooks: hooks });
|
|
}),
|
|
);
|