Files
hotelsync/backend/src/routes/hotel-settings.ts
HotelSync edd493e24e feat: early check-in / late checkout fees with room-level configuration
- DB migration 016: add early_checkin_fee, late_checkout_fee to rooms table
- Backend rooms: support new fee columns in POST/PATCH
- Backend hotel-settings GET: also returns check_in_time, check_out_time
- Room type + RoomPayload: add earlyCheckinFee, lateCheckoutFee fields
- RoomModal: add fee amount inputs in pricing section
- SettingsPage: add toggles for early_checkin_enabled / late_checkout_enabled
- BookingDetailPanel: on check-in shows fee warning + adds payment record if
  current time is before checkInTime and fee is set on the room; same logic
  for check-out after checkOutTime

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 19:24:36 +03:00

71 lines
2.7 KiB
TypeScript

import { FastifyPluginAsync } from 'fastify'
import { db } from '../db'
type SlugParam = { Params: { slug: string } }
const hotelSettings: 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/hotel-settings ─────────────────────────────────
fastify.get<SlugParam>(
'/api/hotels/:slug/hotel-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 [{ rows: settingsRows }, { rows: hotelRows }] = await Promise.all([
db.query('SELECT key, value FROM hotel_settings WHERE hotel_id = $1', [hotelId]),
db.query('SELECT check_in_time, check_out_time FROM hotels WHERE id = $1', [hotelId]),
])
const out: Record<string, unknown> = {}
for (const row of settingsRows) {
out[row.key] = row.value
}
if (hotelRows[0]) {
out.check_in_time = hotelRows[0].check_in_time
out.check_out_time = hotelRows[0].check_out_time
}
return out
},
)
// ── PATCH /api/hotels/:slug/hotel-settings ───────────────────────────────
fastify.patch<SlugParam & { Body: Record<string, unknown> }>(
'/api/hotels/:slug/hotel-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 updates = request.body
for (const [key, value] of Object.entries(updates)) {
await db.query(
`INSERT INTO hotel_settings (hotel_id, key, value, updated_at)
VALUES ($1, $2, $3::jsonb, NOW())
ON CONFLICT (hotel_id, key) DO UPDATE
SET value = EXCLUDED.value, updated_at = NOW()`,
[hotelId, key, JSON.stringify(value)],
)
}
return { ok: true }
},
)
}
export default hotelSettings