Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions listener/src/api/events-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { PreferencesUpdateInput } from '../types/preferences';
import { NotificationAPI } from '../services/notification-api';
import { NotificationType } from '../types/scheduled-notification';
import logger from '../utils/logger';
import { generateRequestId } from '../utils/request-id';
import { NotificationHistoryService } from '../services/notification-history';
import { generateRequestId, resolveCorrelationId } from '../utils/request-id';
import { NotificationHistoryService } from '../services/notification-history';
import {
verifySignature,
extractSignature,
Expand Down Expand Up @@ -411,6 +410,7 @@ export function createEventsServer(options: EventsServerOptions): http.Server {

logger.info('Handling GET /api/notifications/history', {
requestId,
correlationId,
limit,
offset,
status,
Expand All @@ -431,13 +431,14 @@ export function createEventsServer(options: EventsServerOptions): http.Server {

logger.info('GET /api/notifications/history complete', {
requestId,
correlationId,
returned: result.records.length,
total: result.total,
durationMs: Date.now() - startTime,
});
})
.catch((error) => {
logger.error('Failed to retrieve notification history', { error, requestId });
logger.error('Failed to retrieve notification history', { error, requestId, correlationId });
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: (error as Error).message }));
});
Expand All @@ -453,7 +454,9 @@ export function createEventsServer(options: EventsServerOptions): http.Server {
const getPrefsMatch = url.pathname.match(/^\/api\/preferences\/([^/]+)$/);
if (req.method === 'GET' && getPrefsMatch) {
const userId = decodeURIComponent(getPrefsMatch[1]);
logger.info('Handling GET /api/preferences/:userId', { requestId, correlationId, userId });
const prefs = preferenceStore.get(userId);
logger.info('GET /api/preferences/:userId complete', { requestId, correlationId, userId, durationMs: Date.now() - startTime });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(prefs));
return;
Expand All @@ -463,20 +466,24 @@ export function createEventsServer(options: EventsServerOptions): http.Server {
const putPrefsMatch = url.pathname.match(/^\/api\/preferences\/([^/]+)$/);
if (req.method === 'PUT' && putPrefsMatch) {
const userId = decodeURIComponent(putPrefsMatch[1]);
logger.info('Handling PUT /api/preferences/:userId', { requestId, correlationId, userId });
let body = '';
req.on('data', (chunk) => { body += chunk; });
req.on('end', () => {
try {
const input: PreferencesUpdateInput = JSON.parse(body);
if (!input || typeof input.categories !== 'object') {
logger.warn('PUT /api/preferences/:userId invalid body', { requestId, correlationId, userId });
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid body: expected { categories: { [key]: boolean } }' }));
return;
}
const updated = preferenceStore.update(userId, input);
logger.info('PUT /api/preferences/:userId complete', { requestId, correlationId, userId, durationMs: Date.now() - startTime });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(updated));
} catch {
logger.error('PUT /api/preferences/:userId invalid JSON', { requestId, correlationId, userId });
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
}
Expand Down
Loading