feat: housekeeping automation — auto-task on checkout + room status on done
Backend: - Migration 023: housekeeping_settings table (checkout_auto, checkout_priority, inspection_after_clean) - New route: GET/PATCH /api/hotels/:slug/housekeeping-settings - bookings.ts: on checked_out → auto-create turnover task + mark room dirty + broadcast WS - housekeeping.ts: on task done → update room status (inspect|clean per setting) + broadcast WS - ws.ts: export broadcast() for use in other routes Frontend: - HousekeepingPage: load/save settings to API, live Loader on save button - useHotelSocket: add housekeeping WS message types - HousekeepingPage: subscribe to WS — new tasks appear instantly without refresh - BookingModal: show total section when room + nights selected (not just when total > 0) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { FastifyPluginAsync } from 'fastify'
|
||||
import { db } from '../db'
|
||||
import { notifyNetupCheckin, notifyNetupCheckout } from './netup'
|
||||
import { getHkSettings } from './housekeeping-settings'
|
||||
import { broadcast } from './ws'
|
||||
|
||||
type SlugParam = { Params: { slug: string } }
|
||||
type SlugIdParam = { Params: { slug: string; id: string } }
|
||||
@@ -175,6 +177,29 @@ const bookings: FastifyPluginAsync = async (fastify) => {
|
||||
notifyNetupCheckin(hotelId, updated.room_id, updated.guest_name, updated.id).catch(() => {})
|
||||
} else if (request.body.status === 'checked_out') {
|
||||
notifyNetupCheckout(hotelId, updated.room_id).catch(() => {})
|
||||
|
||||
// Auto-create housekeeping task if enabled
|
||||
const hkSettings = await getHkSettings(hotelId).catch(() => null)
|
||||
if (hkSettings?.checkout_auto && updated.room_id) {
|
||||
const today = new Date().toISOString().slice(0, 10)
|
||||
const { rows: taskRows } = await db.query(
|
||||
`INSERT INTO housekeeping_tasks
|
||||
(hotel_id, room_id, type, priority, notes, due_date)
|
||||
VALUES ($1,$2,'turnover',$3,$4,$5)
|
||||
RETURNING *`,
|
||||
[hotelId, updated.room_id, hkSettings.checkout_priority,
|
||||
`Уборка после выезда гостя${updated.guest_name ? ': ' + updated.guest_name : ''}`,
|
||||
today],
|
||||
)
|
||||
const task = taskRows[0]
|
||||
// Update room housekeeping status to 'dirty'
|
||||
await db.query(
|
||||
`UPDATE rooms SET housekeeping_status = 'dirty' WHERE id = $1`,
|
||||
[updated.room_id],
|
||||
)
|
||||
// WebSocket: notify all connected clients of this hotel
|
||||
broadcast(slug, { type: 'housekeeping_task_created', task })
|
||||
}
|
||||
}
|
||||
|
||||
return updated
|
||||
|
||||
81
backend/src/routes/housekeeping-settings.ts
Normal file
81
backend/src/routes/housekeeping-settings.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { FastifyPluginAsync } from 'fastify'
|
||||
import { db } from '../db'
|
||||
|
||||
type SlugParam = { Params: { slug: string } }
|
||||
|
||||
export interface HkSettings {
|
||||
checkout_auto: boolean
|
||||
checkout_priority: string
|
||||
inspection_after_clean: boolean
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: HkSettings = {
|
||||
checkout_auto: true,
|
||||
checkout_priority: 'high',
|
||||
inspection_after_clean: true,
|
||||
}
|
||||
|
||||
export async function getHkSettings(hotelId: string): Promise<HkSettings> {
|
||||
const { rows } = await db.query(
|
||||
'SELECT checkout_auto, checkout_priority, inspection_after_clean FROM housekeeping_settings WHERE hotel_id = $1',
|
||||
[hotelId],
|
||||
)
|
||||
return rows[0] ?? DEFAULT_SETTINGS
|
||||
}
|
||||
|
||||
const housekeepingSettings: FastifyPluginAsync = async (fastify) => {
|
||||
const getHotelId = async (slug: string): Promise<string | null> => {
|
||||
const { rows } = await db.query('SELECT id FROM hotels WHERE slug = $1', [slug])
|
||||
return rows[0]?.id ?? null
|
||||
}
|
||||
|
||||
const canAccess = (userSlug: string | null, role: string, slug: string) =>
|
||||
role === 'super_admin' || userSlug === slug
|
||||
|
||||
// GET /api/hotels/:slug/housekeeping-settings
|
||||
fastify.get<SlugParam>(
|
||||
'/api/hotels/:slug/housekeeping-settings',
|
||||
{ onRequest: [fastify.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { slug } = request.params
|
||||
if (!canAccess(request.user.hotelSlug, request.user.role, slug))
|
||||
return reply.code(403).send({ error: 'Forbidden' })
|
||||
const hotelId = await getHotelId(slug)
|
||||
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
|
||||
return getHkSettings(hotelId)
|
||||
},
|
||||
)
|
||||
|
||||
// PATCH /api/hotels/:slug/housekeeping-settings
|
||||
fastify.patch<SlugParam & { Body: Partial<HkSettings> }>(
|
||||
'/api/hotels/:slug/housekeeping-settings',
|
||||
{ onRequest: [fastify.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { slug } = request.params
|
||||
if (!canAccess(request.user.hotelSlug, request.user.role, slug))
|
||||
return reply.code(403).send({ error: 'Forbidden' })
|
||||
const hotelId = await getHotelId(slug)
|
||||
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
|
||||
|
||||
const { checkout_auto, checkout_priority, inspection_after_clean } = request.body
|
||||
await db.query(
|
||||
`INSERT INTO housekeeping_settings (hotel_id, checkout_auto, checkout_priority, inspection_after_clean)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (hotel_id) DO UPDATE SET
|
||||
checkout_auto = COALESCE(EXCLUDED.checkout_auto, housekeeping_settings.checkout_auto),
|
||||
checkout_priority = COALESCE(EXCLUDED.checkout_priority, housekeeping_settings.checkout_priority),
|
||||
inspection_after_clean = COALESCE(EXCLUDED.inspection_after_clean, housekeeping_settings.inspection_after_clean),
|
||||
updated_at = NOW()`,
|
||||
[
|
||||
hotelId,
|
||||
checkout_auto ?? DEFAULT_SETTINGS.checkout_auto,
|
||||
checkout_priority ?? DEFAULT_SETTINGS.checkout_priority,
|
||||
inspection_after_clean ?? DEFAULT_SETTINGS.inspection_after_clean,
|
||||
],
|
||||
)
|
||||
return getHkSettings(hotelId)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
export default housekeepingSettings
|
||||
@@ -1,5 +1,7 @@
|
||||
import { FastifyPluginAsync } from 'fastify'
|
||||
import { db } from '../db'
|
||||
import { getHkSettings } from './housekeeping-settings'
|
||||
import { broadcast } from './ws'
|
||||
|
||||
type SlugParam = { Params: { slug: string } }
|
||||
type SlugIdParam = { Params: { slug: string; id: string } }
|
||||
@@ -118,7 +120,21 @@ const housekeeping: FastifyPluginAsync = async (fastify) => {
|
||||
values,
|
||||
)
|
||||
if (!rows[0]) return reply.code(404).send({ error: 'Task not found' })
|
||||
return rows[0]
|
||||
const task = rows[0]
|
||||
|
||||
// When task is marked done → update room housekeeping status
|
||||
if (request.body.status === 'done' && task.room_id) {
|
||||
const settings = await getHkSettings(hotelId).catch(() => null)
|
||||
const newRoomStatus = settings?.inspection_after_clean ? 'inspect' : 'clean'
|
||||
await db.query(
|
||||
`UPDATE rooms SET housekeeping_status = $1 WHERE id = $2`,
|
||||
[newRoomStatus, task.room_id],
|
||||
)
|
||||
broadcast(slug, { type: 'housekeeping_done', taskId: task.id, roomId: task.room_id, roomStatus: newRoomStatus })
|
||||
}
|
||||
|
||||
broadcast(slug, { type: 'housekeeping_updated', task })
|
||||
return task
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -6,6 +6,15 @@ import type { RawData } from 'ws'
|
||||
// hotel slug → set of connected streams
|
||||
const hotelRooms = new Map<string, Set<SocketStream>>()
|
||||
|
||||
export function broadcast(hotelSlug: string, message: object) {
|
||||
const peers = hotelRooms.get(hotelSlug)
|
||||
if (!peers) return
|
||||
const payload = JSON.stringify(message)
|
||||
peers.forEach(peer => {
|
||||
if (peer.socket.readyState === 1) peer.socket.send(payload)
|
||||
})
|
||||
}
|
||||
|
||||
const PING_INTERVAL_MS = 25_000 // ping every 25s — keeps nginx proxy_read_timeout alive
|
||||
|
||||
const ws: FastifyPluginAsync = async (fastify) => {
|
||||
|
||||
Reference in New Issue
Block a user