tematicas por tipo de bot

This commit is contained in:
2026-04-29 09:39:56 -05:00
parent f548a2d9bd
commit dcd77a3982
14 changed files with 1284 additions and 83 deletions

View File

@ -26,6 +26,8 @@ class Reservation(Base):
default="manual",
)
notes = Column(String, nullable=True)
table_type_id = Column(Integer, ForeignKey("table_types.id", ondelete="SET NULL"), nullable=True)
created_at = Column(Date, server_default=func.current_date())
business = relationship("Business", back_populates="reservations")
table_type = relationship("TableType")

View File

@ -5,8 +5,10 @@ from fastapi import HTTPException, status
from sqlalchemy import and_, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.modules.business.service import get_business_config
from app.modules.calendar.service import invalidate_slots_cache
from sqlalchemy import func
from app.modules.business.service import get_business_config, list_table_types
from app.modules.calendar.service import find_best_table_for_party, invalidate_slots_cache
from app.modules.reservations.models import Reservation
from app.modules.reservations.schemas import ReservationCreate, ReservationUpdate
@ -55,6 +57,34 @@ async def create_reservation(
config = await get_business_config(db, business_id)
time_end = _compute_time_end(data.time_start, config.slot_duration)
# Asignar mesa automáticamente si el negocio tiene tipos de mesa configurados
table_type_id = None
table_types = await list_table_types(db, business_id)
if table_types:
# Contar reservas por table_type en ese slot
result = await db.execute(
select(Reservation.table_type_id, func.count(Reservation.id))
.where(
and_(
Reservation.business_id == business_id,
Reservation.date == data.date,
Reservation.time_start == data.time_start,
Reservation.status.in_(["pending", "confirmed"]),
Reservation.table_type_id.isnot(None),
)
)
.group_by(Reservation.table_type_id)
)
slot_table_counts = {row[0]: row[1] for row in result.all()}
best_table = await find_best_table_for_party(table_types, slot_table_counts, data.party_size)
if best_table is None:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="No hay mesas disponibles para ese número de personas en ese horario.",
)
table_type_id = best_table.id
reservation = Reservation(
business_id=business_id,
client_name=data.client_name,
@ -66,6 +96,7 @@ async def create_reservation(
source=data.source,
notes=data.notes,
status="pending",
table_type_id=table_type_id,
)
db.add(reservation)
await db.commit()