From 7f546d809e999afa827853919fd9786b81df4d9f Mon Sep 17 00:00:00 2001 From: HotelSync Date: Sat, 28 Mar 2026 17:10:09 +0300 Subject: [PATCH] feat: terminate agent socket and send unpair on workstation delete Co-Authored-By: Claude Sonnet 4.6 --- backend/src/routes/workstations.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backend/src/routes/workstations.ts b/backend/src/routes/workstations.ts index d177871..7f16aa9 100644 --- a/backend/src/routes/workstations.ts +++ b/backend/src/routes/workstations.ts @@ -16,7 +16,7 @@ import { FastifyPluginAsync } from 'fastify' import { db } from '../db' import crypto from 'crypto' -import { sendCommand, sendCommandAndWait } from '../agent-ws' +import { sendCommand, sendCommandAndWait, getAgentSocket } from '../agent-ws' type SlugParam = { Params: { slug: 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)) 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]) return reply.code(204).send() },