feat: terminate agent socket and send unpair on workstation delete

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-28 17:10:09 +03:00
parent ad01bc7f4d
commit 7f546d809e

View File

@@ -16,7 +16,7 @@
import { FastifyPluginAsync } from 'fastify' import { FastifyPluginAsync } from 'fastify'
import { db } from '../db' import { db } from '../db'
import crypto from 'crypto' import crypto from 'crypto'
import { sendCommand, sendCommandAndWait } from '../agent-ws' import { sendCommand, sendCommandAndWait, getAgentSocket } from '../agent-ws'
type SlugParam = { Params: { slug: string } } type SlugParam = { Params: { slug: string } }
type WsParam = { Params: { slug: string; id: string } } type WsParam = { Params: { slug: string; id: string } }
@@ -106,6 +106,13 @@ const workstationRoutes: FastifyPluginAsync = async (fastify) => {
if (!canManage(req.user.hotelSlug, req.user.role, slug)) if (!canManage(req.user.hotelSlug, req.user.role, slug))
return reply.code(403).send({ error: 'Forbidden' }) return reply.code(403).send({ error: 'Forbidden' })
// Notify agent to unpair, then terminate the socket
const socket = getAgentSocket(id)
if (socket) {
try { socket.send(JSON.stringify({ type: 'unpair' })) } catch { /* ignore */ }
setTimeout(() => socket.terminate(), 500)
}
await db.query('DELETE FROM workstations WHERE id = $1', [id]) await db.query('DELETE FROM workstations WHERE id = $1', [id])
return reply.code(204).send() return reply.code(204).send()
}, },