All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m8s
- Crear endpoint DELETE /api/messages/:instanceId/:chatId - Agregar boton de eliminar en ChatItem - Crear modal de confirmacion con advertencia - Elimina el chat y todos los mensajes relacionados (CASCADE) - Muestra cantidad de mensajes eliminados en notificacion
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
/**
|
|
* DELETE /api/messages/:instanceId/:chatId
|
|
* Delete a chat and all its related data (messages, etc.)
|
|
*/
|
|
import { query } from '../../../../utils/database'
|
|
|
|
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')
|
|
const chatId = getRouterParam(event, 'chatId')
|
|
|
|
if (!instanceId) {
|
|
throw createError({ statusCode: 400, message: 'Missing instanceId' })
|
|
}
|
|
|
|
if (!chatId) {
|
|
throw createError({ statusCode: 400, message: 'Missing chatId' })
|
|
}
|
|
|
|
try {
|
|
// First, get chat info for logging
|
|
const chatResult = await query(
|
|
`SELECT id, jid, name, alias FROM chats WHERE id = $1 AND instance_id = $2`,
|
|
[chatId, instanceId]
|
|
)
|
|
|
|
if (chatResult.rows.length === 0) {
|
|
throw createError({ statusCode: 404, message: 'Chat not found' })
|
|
}
|
|
|
|
const chat = chatResult.rows[0]
|
|
|
|
// Count messages that will be deleted
|
|
const messageCount = await query(
|
|
`SELECT COUNT(*) as count FROM messages WHERE chat_id = $1`,
|
|
[chatId]
|
|
)
|
|
|
|
// Delete the chat (messages will be deleted via CASCADE)
|
|
await query(
|
|
`DELETE FROM chats WHERE id = $1 AND instance_id = $2`,
|
|
[chatId, instanceId]
|
|
)
|
|
|
|
console.log(`[Chat API] Deleted chat ${chat.jid} (${chat.name || chat.alias || 'unnamed'}) with ${messageCount.rows[0].count} messages`)
|
|
|
|
return {
|
|
success: true,
|
|
deleted: {
|
|
chatId: chat.id,
|
|
jid: chat.jid,
|
|
name: chat.name || chat.alias,
|
|
messagesDeleted: parseInt(messageCount.rows[0].count)
|
|
}
|
|
}
|
|
} catch (error: any) {
|
|
console.error('[Chat API] Error deleting chat:', error)
|
|
|
|
if (error.statusCode) {
|
|
throw error
|
|
}
|
|
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: error.message || 'Error deleting chat'
|
|
})
|
|
}
|
|
})
|