feat: auto-assign strategy (round_robin/least_loaded/first_free); urgent tasks always use first_free

This commit is contained in:
2026-03-24 12:29:32 +03:00
parent 68e05895e3
commit 8b55bc58f1
5 changed files with 147 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
import { FastifyPluginAsync } from 'fastify'
import { db } from '../db'
import { getHkSettings } from './housekeeping-settings'
import { getHkSettings, autoAssignHousekeeper } from './housekeeping-settings'
import { broadcast } from './ws'
type SlugParam = { Params: { slug: string } }
@@ -73,7 +73,19 @@ const housekeeping: FastifyPluginAsync = async (fastify) => {
const hotelId = await getHotelId(slug)
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
const { room_id, type, priority = 'medium', assignee_id, notes, due_date, category = 'housekeeping', photos = [] } = request.body
const { room_id, type, priority = 'medium', notes, due_date, category = 'housekeeping', photos = [] } = request.body
let { assignee_id } = request.body
// Auto-assign if enabled and no assignee provided
if (!assignee_id) {
const settings = await getHkSettings(hotelId).catch(() => null)
if (settings?.auto_assign) {
// Urgent tasks always use first_free strategy; otherwise use configured strategy
const strategy = priority === 'urgent' ? 'first_free' : (settings.auto_assign_strategy ?? 'least_loaded')
assignee_id = await autoAssignHousekeeper(hotelId, strategy).catch(() => null) ?? undefined
}
}
const { rows: inserted } = await db.query(
`INSERT INTO housekeeping_tasks
(hotel_id, room_id, type, priority, assignee_id, notes, due_date, category, photos)