Skip to content

Commit 7feec10

Browse files
committed
✨ tweak: enhance logging for webhooks
1 parent 1665750 commit 7feec10

File tree

3 files changed

+21
-35
lines changed

3 files changed

+21
-35
lines changed

src/controllers/webhookController.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ export class WebhookController {
1212
try {
1313
const { event, eventId } = req.body;
1414

15+
// Log raw incoming webhook data
16+
LogEngine.debug(`🌐 RAW WEBHOOK RECEIVED:`, {
17+
eventId,
18+
completeRawData: req.body
19+
});
20+
1521
// Handle URL verification event (required by Unthread)
1622
if (event === 'url_verification') {
1723
LogEngine.debug('URL verification event processed');
1824
return res.status(200).json({ message: 'URL verified' });
1925
}
2026

21-
// Log event processing for debugging
22-
LogEngine.debug(`Processing ${event} event (ID: ${eventId})`);
23-
2427
// Validate the event structure
2528
const validationResult = this.webhookService.validateEvent(req.body);
2629
if (!validationResult.isValid) {
30+
LogEngine.error(`❌ Event validation failed:`, validationResult.errors);
2731
return res.status(400).json({
2832
error: 'Invalid event structure',
2933
details: validationResult.errors
@@ -39,7 +43,7 @@ export class WebhookController {
3943
timestamp: new Date().toISOString()
4044
});
4145
} catch (error) {
42-
LogEngine.error(`Error handling webhook: ${error}`);
46+
LogEngine.error(`💥 Error handling webhook: ${error}`);
4347
return res.status(500).json({
4448
error: 'Internal server error',
4549
timestamp: new Date().toISOString()

src/services/redisService.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,18 @@ export class RedisService {
4343
const queueName = config.unthreadQueueName;
4444

4545
try {
46+
const eventJson = JSON.stringify(event);
47+
48+
// Log the complete transformed event data
49+
LogEngine.debug(`� TRANSFORMED WEBHOOK EVENT:`, {
50+
eventId: event.data?.eventId || 'unknown',
51+
completeTransformedData: event
52+
});
53+
4654
// Use Redis LIST for FIFO queue (LPUSH + BRPOP pattern)
47-
const result = await this.client.lPush(queueName, JSON.stringify(event));
48-
LogEngine.debug(`Event queued: ${event.data?.eventId || 'unknown'} -> ${queueName}`);
55+
const result = await this.client.lPush(queueName, eventJson);
56+
57+
LogEngine.info(`✅ Event successfully queued: ${event.data?.eventId || 'unknown'} -> ${queueName} (${result} items in queue)`);
4958
return result;
5059
} catch (err) {
5160
LogEngine.error(`Error publishing event to queue: ${err}`);

src/services/webhookService.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { LogEngine } from '@wgtechlabs/log-engine';
22
import { RedisService } from './redisService';
33
import { config } from '../config/env';
4+
import { redisEventConfig } from '../config/redis';
45
import {
56
UnthreadWebhookEvent,
67
RedisQueueMessage,
@@ -36,43 +37,15 @@ export class WebhookService {
3637
return;
3738
}
3839

39-
// Detect platform source with enhanced logging
40+
// Detect platform source
4041
const sourcePlatform = this.detectPlatformSource(event);
4142

42-
// Enhanced audit logging for detection validation
43-
if ((event.event === 'message_created' || event.event === 'conversation_updated') && event.data) {
44-
if (event.event === 'message_created') {
45-
const detectionSummary = [
46-
`Detection: ${sourcePlatform}`,
47-
`Event: ${event.eventId}`,
48-
`Conversation: ${event.data.conversationId}`,
49-
`ConversationUpdates: ${event.data.metadata?.event_payload?.conversationUpdates !== undefined ? 'present' : 'missing'}`,
50-
`BotName: ${event.data.botName}`,
51-
`External: ${event.data.isExternal}`
52-
].join(' | ');
53-
54-
LogEngine.info(`Platform detection completed - ${detectionSummary}`);
55-
} else if (event.event === 'conversation_updated') {
56-
const detectionSummary = [
57-
`Detection: ${sourcePlatform}`,
58-
`Event: ${event.eventId}`,
59-
`Conversation: ${event.data.id}`,
60-
`Type: conversation_updated`,
61-
`Reason: administrative action`
62-
].join(' | ');
63-
64-
LogEngine.info(`Platform detection completed - ${detectionSummary}`);
65-
}
66-
}
67-
6843
// Transform and queue event
6944
const transformedEvent = this.transformEvent(event, sourcePlatform);
7045
await this.redisService.publishEvent(transformedEvent);
7146

7247
// Mark as processed
7348
await this.redisService.markEventProcessed(event.eventId);
74-
75-
LogEngine.debug(`Event processed: ${event.event} (${event.eventId}) from ${sourcePlatform}`);
7649
}
7750

7851
private transformEvent(unthreadEvent: UnthreadWebhookEvent, sourcePlatform: PlatformSource): RedisQueueMessage {

0 commit comments

Comments
 (0)