Files
whatsappNucleo/server/api/messages/[instanceId]/chats.get.ts
josedario87 a848adf4f8
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m9s
Fix: Preservar alias al recargar lista de chats
El endpoint chats.get no retornaba el campo alias, lo que causaba
que al recargar los chats (cuando llega un mensaje nuevo) el alias
se perdiera.

Cambios:
- Agregar alias a la query de chats.get
- Priorizar alias sobre name en la respuesta
- Incluir originalName para referencia en el modal
2025-12-04 16:04:47 -06:00

59 lines
1.8 KiB
TypeScript

/**
* GET /api/messages/:instanceId/chats
* Get all chats for an instance
*/
import { query } from '../../../utils/database'
interface ChatRow {
id: string
jid: string
name: string | null
alias: string | null
is_group: boolean
unread_count: number
last_message_at: Date | null
last_message: string | null
last_message_type: string | null
}
export default defineEventHandler(async (event) => {
const username = getHeader(event, 'x-authentik-username')
if (!username) {
throw createError({ statusCode: 401, message: 'Unauthorized' })
}
const instanceId = getRouterParam(event, 'instanceId')
// Verify instance exists
const instanceCheck = await query('SELECT id FROM instances WHERE id = $1', [instanceId])
if (instanceCheck.rows.length === 0) {
throw createError({ statusCode: 404, message: 'Instance not found' })
}
// Get chats with last message and type
const result = await query<ChatRow>(
`SELECT c.id, c.jid, c.name, c.alias, c.is_group, c.unread_count, c.last_message_at,
c.last_message_type,
(SELECT COALESCE(content, caption) FROM messages m WHERE m.chat_id = c.id ORDER BY timestamp DESC LIMIT 1) as last_message
FROM chats c
WHERE c.instance_id = $1
ORDER BY c.last_message_at DESC NULLS LAST`,
[instanceId]
)
return result.rows.map(row => ({
id: row.id,
jid: row.jid,
// Alias has priority over name
name: row.alias || row.name || row.jid.split('@')[0],
// Keep original name for reference
originalName: row.name || row.jid.split('@')[0],
alias: row.alias,
isGroup: row.is_group,
unreadCount: row.unread_count,
lastMessageAt: row.last_message_at,
lastMessage: row.last_message || '',
lastMessageType: row.last_message_type || 'text'
}))
})