Files
hotelsync/backend/src/routes/categories.ts

106 lines
4.5 KiB
TypeScript

import { FastifyPluginAsync } from 'fastify'
import { db } from '../db'
type SlugParam = { Params: { slug: string } }
type SlugIdParam = { Params: { slug: string; id: string } }
const categories: 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/categories
fastify.get<SlugParam>(
'/api/hotels/:slug/categories',
{ 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 } = await db.query(
`SELECT * FROM room_categories WHERE hotel_id = $1 ORDER BY sort_order, created_at`,
[hotelId],
)
return rows
},
)
// POST /api/hotels/:slug/categories
fastify.post<SlugParam & { Body: {
name: string; description?: string; color?: string
amenities?: string[]; photos?: string[]; sort_order?: number
} }>(
'/api/hotels/:slug/categories',
{ 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 { name, description = '', color = '#4F46E5', amenities = [], photos = [], sort_order = 0 } = request.body
const { rows } = await db.query(
`INSERT INTO room_categories (hotel_id, name, description, color, amenities, photos, sort_order)
VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING *`,
[hotelId, name, description, color, amenities, photos, sort_order],
)
return reply.code(201).send(rows[0])
},
)
// PATCH /api/hotels/:slug/categories/:id
fastify.patch<SlugIdParam & { Body: {
name?: string; description?: string; color?: string
amenities?: string[]; photos?: string[]; sort_order?: number
} }>(
'/api/hotels/:slug/categories/:id',
{ onRequest: [fastify.authenticate] },
async (request, reply) => {
const { slug, id } = 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 b = request.body
const sets: string[] = ['updated_at = NOW()']
const vals: unknown[] = [hotelId, id]
let i = 3
if (b.name !== undefined) { sets.push(`name = $${i++}`); vals.push(b.name) }
if (b.description !== undefined) { sets.push(`description = $${i++}`); vals.push(b.description) }
if (b.color !== undefined) { sets.push(`color = $${i++}`); vals.push(b.color) }
if (b.amenities !== undefined) { sets.push(`amenities = $${i++}`); vals.push(b.amenities) }
if (b.photos !== undefined) { sets.push(`photos = $${i++}`); vals.push(b.photos) }
if (b.sort_order !== undefined) { sets.push(`sort_order = $${i++}`); vals.push(b.sort_order) }
const { rows } = await db.query(
`UPDATE room_categories SET ${sets.join(', ')} WHERE hotel_id=$1 AND id=$2 RETURNING *`,
vals,
)
if (!rows[0]) return reply.code(404).send({ error: 'Not found' })
return rows[0]
},
)
// DELETE /api/hotels/:slug/categories/:id
fastify.delete<SlugIdParam>(
'/api/hotels/:slug/categories/:id',
{ onRequest: [fastify.authenticate] },
async (request, reply) => {
const { slug, id } = 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' })
await db.query('DELETE FROM room_categories WHERE hotel_id=$1 AND id=$2', [hotelId, id])
return reply.code(204).send()
},
)
}
export default categories