/** * POST /api/debug/history/fetch * Request message history on-demand */ import { baileysManager } from '../../../services/baileys/manager' export default defineEventHandler(async (event) => { const username = getHeader(event, 'x-authentik-username') if (!username) { throw createError({ statusCode: 401, message: 'Unauthorized' }) } const body = await readBody(event) const { instanceId, count, oldestMsgKey, oldestMsgTimestamp } = body if (!instanceId) { throw createError({ statusCode: 400, message: 'instanceId is required' }) } if (!count || typeof count !== 'number') { throw createError({ statusCode: 400, message: 'count (number) is required' }) } const socket = baileysManager.getSocket(instanceId) if (!socket) { throw createError({ statusCode: 400, message: 'Instance not connected' }) } // Verificar que el método existe if (typeof socket.fetchMessageHistory !== 'function') { console.error('[History] fetchMessageHistory method not available on socket') console.error('[History] Available methods:', Object.keys(socket).filter(k => typeof (socket as any)[k] === 'function').join(', ')) throw createError({ statusCode: 500, message: 'fetchMessageHistory not available on this Baileys version' }) } // Log de parámetros console.log('[History] Requesting history with params:', { instanceId, count, oldestMsgKey: oldestMsgKey ? JSON.stringify(oldestMsgKey) : 'undefined', oldestMsgTimestamp: oldestMsgTimestamp || 'undefined' }) try { const result = await socket.fetchMessageHistory(count, oldestMsgKey, oldestMsgTimestamp) console.log('[History] fetchMessageHistory returned:', result) return { success: true, result, message: `Requested ${count} messages from history. Check messaging-history.set event for results.` } } catch (error) { console.error('[History] fetchMessageHistory error:', error) throw createError({ statusCode: 500, message: `Failed to fetch message history: ${(error as Error).message}` }) } })