fix: TTLock backend — use snake_case request body keys

req() auto-transforms camelCase → snake_case before sending.
Backend was destructuring camelCase (roomId, lockMac, isEnabled etc)
but receiving snake_case (room_id, lock_mac, is_enabled) — all values
were undefined, so nothing was ever saved.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-01 00:35:40 +03:00
parent f837a84db5
commit 1c1c88a37f

View File

@@ -64,10 +64,10 @@ const ttlockRoutes: FastifyPluginAsync = async (fastify) => {
// ── PATCH /api/hotels/:slug/ttlock/config ─────────────────────────────────── // ── PATCH /api/hotels/:slug/ttlock/config ───────────────────────────────────
fastify.patch<SlugParam & { fastify.patch<SlugParam & {
Body: { Body: {
isEnabled?: boolean is_enabled?: boolean
clientId?: string client_id?: string
clientSecret?: string client_secret?: string
cardSectors?: string card_sectors?: string
} }
}>('/api/hotels/:slug/ttlock/config', { onRequest: [fastify.authenticate] }, async (req, reply) => { }>('/api/hotels/:slug/ttlock/config', { onRequest: [fastify.authenticate] }, async (req, reply) => {
const { slug } = req.params const { slug } = req.params
@@ -76,7 +76,7 @@ const ttlockRoutes: FastifyPluginAsync = async (fastify) => {
const hotelId = await getHotelId(slug) const hotelId = await getHotelId(slug)
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' }) if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
const { isEnabled, clientId, clientSecret, cardSectors } = req.body const { is_enabled: isEnabled, client_id: clientId, client_secret: clientSecret, card_sectors: cardSectors } = req.body
await db.query( await db.query(
`INSERT INTO hotel_ttlock_config (hotel_id, is_enabled, client_id, client_secret, card_sectors) `INSERT INTO hotel_ttlock_config (hotel_id, is_enabled, client_id, client_secret, card_sectors)
@@ -100,7 +100,7 @@ const ttlockRoutes: FastifyPluginAsync = async (fastify) => {
// ── POST /api/hotels/:slug/ttlock/test ────────────────────────────────────── // ── POST /api/hotels/:slug/ttlock/test ──────────────────────────────────────
// Проверяем: агент онлайн, энкодер отвечает на COM-порту // Проверяем: агент онлайн, энкодер отвечает на COM-порту
fastify.post<SlugParam & { Body: { workstationId: string } }>( fastify.post<SlugParam & { Body: { workstation_id: string } }>(
'/api/hotels/:slug/ttlock/test', '/api/hotels/:slug/ttlock/test',
{ onRequest: [fastify.authenticate] }, { onRequest: [fastify.authenticate] },
async (req, reply) => { async (req, reply) => {
@@ -110,8 +110,8 @@ const ttlockRoutes: FastifyPluginAsync = async (fastify) => {
const hotelId = await getHotelId(slug) const hotelId = await getHotelId(slug)
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' }) if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
const { workstationId } = req.body const workstationId = req.body.workstation_id
if (!workstationId) return reply.code(400).send({ error: 'workstationId required' }) if (!workstationId) return reply.code(400).send({ error: 'workstation_id required' })
const { rows: wsRows } = await db.query( const { rows: wsRows } = await db.query(
'SELECT ttlock_com_port FROM workstations WHERE id = $1 AND hotel_id = $2', 'SELECT ttlock_com_port FROM workstations WHERE id = $1 AND hotel_id = $2',
@@ -163,7 +163,7 @@ const ttlockRoutes: FastifyPluginAsync = async (fastify) => {
}) })
// ── POST /api/hotels/:slug/ttlock/room-locks ───────────────────────────────── // ── POST /api/hotels/:slug/ttlock/room-locks ─────────────────────────────────
fastify.post<SlugParam & { Body: { roomId: string; lockMac: string; lockName?: string } }>( fastify.post<SlugParam & { Body: { room_id: string; lock_mac: string; lock_name?: string } }>(
'/api/hotels/:slug/ttlock/room-locks', '/api/hotels/:slug/ttlock/room-locks',
{ onRequest: [fastify.authenticate] }, { onRequest: [fastify.authenticate] },
async (req, reply) => { async (req, reply) => {
@@ -173,9 +173,9 @@ const ttlockRoutes: FastifyPluginAsync = async (fastify) => {
const hotelId = await getHotelId(slug) const hotelId = await getHotelId(slug)
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' }) if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
const { roomId, lockMac, lockName } = req.body const { room_id: roomId, lock_mac: lockMac, lock_name: lockName } = req.body
// Нормализуем MAC: убираем двоеточия, приводим к верхнему регистру // Нормализуем MAC: убираем двоеточия, приводим к верхнему регистру
const mac = lockMac.replace(/:/g, '').toUpperCase() const mac = (lockMac ?? '').replace(/:/g, '').toUpperCase()
if (!/^[0-9A-F]{12}$/.test(mac)) { if (!/^[0-9A-F]{12}$/.test(mac)) {
return reply.code(400).send({ error: 'MAC-адрес должен быть 12 HEX-символов (напр. 42A6BBF5ECE5)' }) return reply.code(400).send({ error: 'MAC-адрес должен быть 12 HEX-символов (напр. 42A6BBF5ECE5)' })
} }
@@ -203,7 +203,7 @@ const ttlockRoutes: FastifyPluginAsync = async (fastify) => {
}) })
// ── POST /api/hotels/:slug/bookings/:bookingId/issue-card ──────────────────── // ── POST /api/hotels/:slug/bookings/:bookingId/issue-card ────────────────────
fastify.post<BookingParam & { Body: { workstationId?: string } }>( fastify.post<BookingParam & { Body: { workstation_id?: string } }>(
'/api/hotels/:slug/bookings/:bookingId/issue-card', '/api/hotels/:slug/bookings/:bookingId/issue-card',
{ onRequest: [fastify.authenticate] }, { onRequest: [fastify.authenticate] },
async (req, reply) => { async (req, reply) => {
@@ -244,11 +244,11 @@ const ttlockRoutes: FastifyPluginAsync = async (fastify) => {
let workstationId: string let workstationId: string
let comPort: string let comPort: string
if (req.body?.workstationId) { if (req.body?.workstation_id) {
// Явно указано рабочее место // Явно указано рабочее место
const { rows: wsRows } = await db.query( const { rows: wsRows } = await db.query(
'SELECT id, ttlock_com_port FROM workstations WHERE id = $1 AND hotel_id = $2', 'SELECT id, ttlock_com_port FROM workstations WHERE id = $1 AND hotel_id = $2',
[req.body.workstationId, hotelId], [req.body.workstation_id, hotelId],
) )
const ws = wsRows[0] const ws = wsRows[0]
if (!ws || !ws.ttlock_com_port) { if (!ws || !ws.ttlock_com_port) {