hvac: add gateway delete (retire the old Modbus gateway config)

There was no way to remove an mhi_gateways row either, alongside the
already-fixed device-delete gap — exposed by the same Modbus->MQTT
switchover. Adds DELETE /api/mhi-gateways/:id (manage_devices cap) and a
'Delete gateway' button. Devices referencing the gateway are NOT deleted
(mhi_gateway_id just goes NULL per the existing ON DELETE SET NULL FK) —
the confirm dialog says so and points at the separate device-delete flow
for retiring the stale mhi_modbus rows too.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-08-13 14:19:02 +00:00
parent 7ce95a31da
commit d7cbb0c452
3 changed files with 37 additions and 2 deletions

View file

@ -54,6 +54,19 @@ export async function mhiGatewayRoutes(app) {
return rows[0]
})
// DELETE /api/mhi-gateways/:id — remove a gateway config entirely. Main use:
// retiring a gateway after it's switched to native MQTT (see lib/mqtt.js) and
// its Modbus register-map config is no longer needed. Devices that reference
// this gateway (device_type='mhi_modbus') are NOT deleted — mhi_gateway_id
// just goes NULL (ON DELETE SET NULL) — delete those rows separately via
// DELETE /api/devices/:id if they're being retired too.
app.delete('/api/mhi-gateways/:id', { preHandler: requireCap('manage_devices') }, async (req, reply) => {
const { rows } = await pool.query('SELECT id FROM mhi_gateways WHERE id = $1', [req.params.id])
if (!rows.length) return reply.status(404).send({ error: 'Gateway not found' })
await pool.query('DELETE FROM mhi_gateways WHERE id = $1', [req.params.id])
return { ok: true }
})
// POST /api/mhi-gateways/:id/test-connection — live Modbus read of the
// gateway comm status register (same check as scripts/test-modbus-connection.py)
app.post('/api/mhi-gateways/:id/test-connection', { preHandler: requireCap('manage_devices') }, async (req, reply) => {