diff --git a/app/composables/useInstances.ts b/app/composables/useInstances.ts index 68eada5..b986882 100644 --- a/app/composables/useInstances.ts +++ b/app/composables/useInstances.ts @@ -99,6 +99,17 @@ export const useInstances = () => { return await $fetch(`/api/instances/${id}/status`) } + const resetInstance = async (id: string) => { + await $fetch(`/api/instances/${id}/reset`, { method: 'POST' }) + + // Update local state + const idx = instances.value.findIndex(i => i.id === id) + if (idx !== -1) { + instances.value[idx].status = 'disconnected' + instances.value[idx].qrCode = null + } + } + // Computed helpers const connectedCount = computed(() => instances.value.filter(i => i.status === 'connected').length @@ -115,6 +126,7 @@ export const useInstances = () => { deleteInstance, connectInstance, disconnectInstance, + resetInstance, getQRCode, requestPairingCode, getInstanceStatus, diff --git a/app/pages/index.vue b/app/pages/index.vue index b20e22b..950927b 100644 --- a/app/pages/index.vue +++ b/app/pages/index.vue @@ -128,6 +128,16 @@ > Ver QR + + + Reiniciar + @@ -179,7 +189,7 @@ definePageMeta({ title: 'Dashboard', }) -const { instances, loading, fetchInstances, createInstance: apiCreateInstance, connectInstance: apiConnect } = useInstances() +const { instances, loading, fetchInstances, createInstance: apiCreateInstance, connectInstance: apiConnect, resetInstance: apiReset } = useInstances() const showCreateModal = ref(false) const showQRModal = ref(false) @@ -216,6 +226,12 @@ const createInstance = async () => { const connectInstance = async (id: string) => { await apiConnect(id) + // Refresh to get updated status/QR + await fetchInstances() +} + +const resetInstanceHandler = async (id: string) => { + await apiReset(id) } const showQR = (instance: any) => { diff --git a/server/api/instances/[id]/reset.post.ts b/server/api/instances/[id]/reset.post.ts new file mode 100644 index 0000000..c6b570d --- /dev/null +++ b/server/api/instances/[id]/reset.post.ts @@ -0,0 +1,33 @@ +/** + * POST /api/instances/[id]/reset + * Reset instance connection (disconnect and clear QR) + */ +import { query } from '../../../utils/database' +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 id = getRouterParam(event, 'id') + if (!id) { + throw createError({ statusCode: 400, message: 'Instance ID required' }) + } + + // Disconnect if connected + try { + await baileysManager.disconnect(id) + } catch (e) { + // Ignore if not connected + } + + // Reset in database + await query( + `UPDATE instances SET status = 'disconnected', qr_code = NULL, pairing_code = NULL WHERE id = $1`, + [id] + ) + + return { success: true, message: 'Instance reset successfully' } +})