Add debug logging for conversation routes and store

This commit is contained in:
josedario87
2025-06-05 13:58:54 -06:00
parent e135868cb7
commit 84798a014f
2 changed files with 12 additions and 0 deletions

View File

@@ -8,30 +8,36 @@ import {
export function registerConversationRoutes(app: Application, openWaUrl: string | undefined) {
app.get('/conversations', (req, res) => {
console.log('[routes] GET /conversations');
res.json({ conversations: listConversations() });
});
app.get('/conversations/:id', async (req, res) => {
console.log(`[routes] GET /conversations/${req.params.id}`);
if (!openWaUrl) return res.status(500).json({ error: 'Service URLs not configured' });
try {
const conv = await getConversation(req.params.id, openWaUrl);
res.json(conv);
} catch (err: any) {
console.error('Failed to get conversation:', err.message);
res.status(500).json({ error: err.message });
}
});
app.post('/conversations/:id/update', async (req, res) => {
console.log(`[routes] POST /conversations/${req.params.id}/update`);
if (!openWaUrl) return res.status(500).json({ error: 'Service URLs not configured' });
try {
const conv = await buildConversation(req.params.id, openWaUrl);
res.json(conv);
} catch (err: any) {
console.error('Failed to update conversation:', err.message);
res.status(500).json({ error: err.message });
}
});
app.delete('/conversations/:id', (req, res) => {
console.log(`[routes] DELETE /conversations/${req.params.id}`);
const deleted = deleteConversation(req.params.id);
res.json({ success: deleted });
});

View File

@@ -7,6 +7,7 @@ async function loadMessages(
chatId: string,
openWaUrl: string
): Promise<WhatsAppMessage[]> {
console.log(`[conversationStore] Loading messages for ${chatId}`);
const { data } = await axios.post(`${openWaUrl}/loadAndGetAllMessagesInChat`, {
args: {
chatId,
@@ -22,6 +23,7 @@ export async function getConversation(
chatId: string,
openWaUrl: string
): Promise<Conversation> {
console.log(`[conversationStore] Retrieving conversation for ${chatId}`);
let conv = conversations.get(chatId);
if (!conv) {
conv = await buildConversation(chatId, openWaUrl);
@@ -30,6 +32,7 @@ export async function getConversation(
}
export function listConversations(): Conversation[] {
console.log('[conversationStore] Listing conversations');
return Array.from(conversations.values());
}
@@ -37,6 +40,7 @@ export async function buildConversation(
chatId: string,
openWaUrl: string
): Promise<Conversation> {
console.log(`[conversationStore] Building conversation for ${chatId}`);
const messages = await loadMessages(chatId, openWaUrl);
const now = Date.now();
const conv: Conversation = {
@@ -51,6 +55,7 @@ export async function buildConversation(
}
export function deleteConversation(chatId: string): boolean {
console.log(`[conversationStore] Deleting conversation ${chatId}`);
return conversations.delete(chatId);
}
@@ -59,6 +64,7 @@ export async function addMessageToConversation(
msg: WhatsAppMessage,
openWaUrl: string
): Promise<Conversation> {
console.log(`[conversationStore] Adding message to ${chatId}`);
const conv = await getConversation(chatId, openWaUrl);
conv.messages.push(msg);
conv.messageCount = conv.messages.length;