feat: initial commit — HermesMessages SaaS platform
Backend (FastAPI + Python 3.12): - Multi-tenant auth with JWT: login, register, refresh, Meta OAuth - Business & BusinessConfig management - WhatsApp webhook with HMAC signature verification - Bot engine powered by Claude AI - Calendar availability with Redis caching - Reservations CRUD with status management - Dashboard analytics (stats, agenda, peak hours) - Billing & plan management - Admin panel with platform-wide stats - Async bcrypt via asyncio.to_thread - IntegrityError handling for concurrent registration race conditions Frontend (React 18 + Vite + Tailwind CSS): - Multi-step guided registration form with helper text on every field - Login page with show/hide password toggle - Protected routes with AuthContext - Dashboard with stats cards, bar chart, and daily agenda - Reservations list with search, filters, and inline status actions - Calendar with weekly view, slot availability, and date blocking - Config page: business info, schedules, bot personality - Billing page with plan comparison and usage bar Design system: - Bricolage Grotesque + DM Sans typography - Emerald primary palette with semantic color tokens - scale(0.97) button press feedback, ease-out animations - Skeleton loaders, stagger animations, prefers-reduced-motion support - Accessible: aria-labels, visible focus rings, 4.5:1 contrast Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
0
backend/app/modules/reservations/__init__.py
Normal file
0
backend/app/modules/reservations/__init__.py
Normal file
31
backend/app/modules/reservations/models.py
Normal file
31
backend/app/modules/reservations/models.py
Normal file
@ -0,0 +1,31 @@
|
||||
from sqlalchemy import Column, Date, Enum, ForeignKey, Integer, String, Time, func
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class Reservation(Base):
|
||||
__tablename__ = "reservations"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
business_id = Column(Integer, ForeignKey("businesses.id", ondelete="CASCADE"), nullable=False)
|
||||
client_name = Column(String, nullable=False)
|
||||
client_phone = Column(String, nullable=False)
|
||||
date = Column(Date, nullable=False, index=True)
|
||||
time_start = Column(Time, nullable=False)
|
||||
time_end = Column(Time, nullable=False)
|
||||
party_size = Column(Integer, nullable=False, default=1)
|
||||
status = Column(
|
||||
Enum("pending", "confirmed", "cancelled", "no_show", name="reservation_status"),
|
||||
nullable=False,
|
||||
default="pending",
|
||||
)
|
||||
source = Column(
|
||||
Enum("whatsapp", "manual", name="reservation_source"),
|
||||
nullable=False,
|
||||
default="manual",
|
||||
)
|
||||
notes = Column(String, nullable=True)
|
||||
created_at = Column(Date, server_default=func.current_date())
|
||||
|
||||
business = relationship("Business", back_populates="reservations")
|
||||
74
backend/app/modules/reservations/router.py
Normal file
74
backend/app/modules/reservations/router.py
Normal file
@ -0,0 +1,74 @@
|
||||
from datetime import date
|
||||
|
||||
import redis.asyncio as aioredis
|
||||
from fastapi import APIRouter, Depends, Response
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.dependencies import get_current_business
|
||||
from app.core.redis import get_redis
|
||||
from app.modules.reservations import schemas, service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=list[schemas.ReservationRead])
|
||||
async def list_reservations(
|
||||
date: date | None = None,
|
||||
status: str | None = None,
|
||||
business_id: int = Depends(get_current_business),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
return await service.list_reservations(db, business_id, date, status)
|
||||
|
||||
|
||||
@router.post("/", response_model=schemas.ReservationRead, status_code=201)
|
||||
async def create_reservation(
|
||||
body: schemas.ReservationCreate,
|
||||
business_id: int = Depends(get_current_business),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
redis: aioredis.Redis = Depends(get_redis),
|
||||
):
|
||||
return await service.create_reservation(db, redis, business_id, body)
|
||||
|
||||
|
||||
@router.get("/{reservation_id}", response_model=schemas.ReservationRead)
|
||||
async def get_reservation(
|
||||
reservation_id: int,
|
||||
business_id: int = Depends(get_current_business),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
return await service.get_reservation(db, business_id, reservation_id)
|
||||
|
||||
|
||||
@router.put("/{reservation_id}", response_model=schemas.ReservationRead)
|
||||
async def update_reservation(
|
||||
reservation_id: int,
|
||||
body: schemas.ReservationUpdate,
|
||||
business_id: int = Depends(get_current_business),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
redis: aioredis.Redis = Depends(get_redis),
|
||||
):
|
||||
return await service.update_reservation(db, redis, business_id, reservation_id, body)
|
||||
|
||||
|
||||
@router.delete("/{reservation_id}", status_code=204)
|
||||
async def delete_reservation(
|
||||
reservation_id: int,
|
||||
business_id: int = Depends(get_current_business),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
redis: aioredis.Redis = Depends(get_redis),
|
||||
):
|
||||
await service.delete_reservation(db, redis, business_id, reservation_id)
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@router.patch("/{reservation_id}/status", response_model=schemas.ReservationRead)
|
||||
async def update_status(
|
||||
reservation_id: int,
|
||||
body: schemas.StatusUpdate,
|
||||
business_id: int = Depends(get_current_business),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
redis: aioredis.Redis = Depends(get_redis),
|
||||
):
|
||||
return await service.update_status(db, redis, business_id, reservation_id, body.status)
|
||||
44
backend/app/modules/reservations/schemas.py
Normal file
44
backend/app/modules/reservations/schemas.py
Normal file
@ -0,0 +1,44 @@
|
||||
from datetime import date as Date, time as Time
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class ReservationCreate(BaseModel):
|
||||
client_name: str
|
||||
client_phone: str
|
||||
date: Date
|
||||
time_start: Time
|
||||
party_size: int = 1
|
||||
notes: Optional[str] = None
|
||||
source: str = "manual"
|
||||
|
||||
|
||||
class ReservationUpdate(BaseModel):
|
||||
client_name: Optional[str] = None
|
||||
client_phone: Optional[str] = None
|
||||
date: Optional[Date] = None
|
||||
time_start: Optional[Time] = None
|
||||
party_size: Optional[int] = None
|
||||
notes: Optional[str] = None
|
||||
|
||||
|
||||
class StatusUpdate(BaseModel):
|
||||
status: str
|
||||
|
||||
|
||||
class ReservationRead(BaseModel):
|
||||
id: int
|
||||
business_id: int
|
||||
client_name: str
|
||||
client_phone: str
|
||||
date: Date
|
||||
time_start: Time
|
||||
time_end: Time
|
||||
party_size: int
|
||||
status: str
|
||||
source: str
|
||||
notes: Optional[str]
|
||||
created_at: Date
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
140
backend/app/modules/reservations/service.py
Normal file
140
backend/app/modules/reservations/service.py
Normal file
@ -0,0 +1,140 @@
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
import redis.asyncio as aioredis
|
||||
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 app.modules.reservations.models import Reservation
|
||||
from app.modules.reservations.schemas import ReservationCreate, ReservationUpdate
|
||||
|
||||
VALID_STATUSES = {"pending", "confirmed", "cancelled", "no_show"}
|
||||
|
||||
|
||||
async def _get_or_404(db: AsyncSession, business_id: int, reservation_id: int) -> Reservation:
|
||||
result = await db.execute(
|
||||
select(Reservation).where(
|
||||
and_(Reservation.id == reservation_id, Reservation.business_id == business_id)
|
||||
)
|
||||
)
|
||||
reservation = result.scalar_one_or_none()
|
||||
if not reservation:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Reserva no encontrada")
|
||||
return reservation
|
||||
|
||||
|
||||
def _compute_time_end(time_start, slot_duration: int):
|
||||
base = datetime.combine(date.today(), time_start)
|
||||
return (base + timedelta(minutes=slot_duration)).time()
|
||||
|
||||
|
||||
async def list_reservations(
|
||||
db: AsyncSession,
|
||||
business_id: int,
|
||||
filter_date: date | None = None,
|
||||
filter_status: str | None = None,
|
||||
) -> list[Reservation]:
|
||||
query = select(Reservation).where(Reservation.business_id == business_id)
|
||||
if filter_date:
|
||||
query = query.where(Reservation.date == filter_date)
|
||||
if filter_status:
|
||||
query = query.where(Reservation.status == filter_status)
|
||||
query = query.order_by(Reservation.date, Reservation.time_start)
|
||||
result = await db.execute(query)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
async def create_reservation(
|
||||
db: AsyncSession,
|
||||
redis: aioredis.Redis,
|
||||
business_id: int,
|
||||
data: ReservationCreate,
|
||||
) -> Reservation:
|
||||
config = await get_business_config(db, business_id)
|
||||
time_end = _compute_time_end(data.time_start, config.slot_duration)
|
||||
|
||||
reservation = Reservation(
|
||||
business_id=business_id,
|
||||
client_name=data.client_name,
|
||||
client_phone=data.client_phone,
|
||||
date=data.date,
|
||||
time_start=data.time_start,
|
||||
time_end=time_end,
|
||||
party_size=data.party_size,
|
||||
source=data.source,
|
||||
notes=data.notes,
|
||||
status="pending",
|
||||
)
|
||||
db.add(reservation)
|
||||
await db.commit()
|
||||
await db.refresh(reservation)
|
||||
await invalidate_slots_cache(redis, business_id, data.date)
|
||||
return reservation
|
||||
|
||||
|
||||
async def get_reservation(
|
||||
db: AsyncSession, business_id: int, reservation_id: int
|
||||
) -> Reservation:
|
||||
return await _get_or_404(db, business_id, reservation_id)
|
||||
|
||||
|
||||
async def update_reservation(
|
||||
db: AsyncSession,
|
||||
redis: aioredis.Redis,
|
||||
business_id: int,
|
||||
reservation_id: int,
|
||||
data: ReservationUpdate,
|
||||
) -> Reservation:
|
||||
reservation = await _get_or_404(db, business_id, reservation_id)
|
||||
old_date = reservation.date
|
||||
|
||||
for field, value in data.model_dump(exclude_none=True).items():
|
||||
setattr(reservation, field, value)
|
||||
|
||||
if data.time_start or data.date:
|
||||
config = await get_business_config(db, business_id)
|
||||
reservation.time_end = _compute_time_end(reservation.time_start, config.slot_duration)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(reservation)
|
||||
|
||||
await invalidate_slots_cache(redis, business_id, old_date)
|
||||
if reservation.date != old_date:
|
||||
await invalidate_slots_cache(redis, business_id, reservation.date)
|
||||
|
||||
return reservation
|
||||
|
||||
|
||||
async def delete_reservation(
|
||||
db: AsyncSession, redis: aioredis.Redis, business_id: int, reservation_id: int
|
||||
) -> None:
|
||||
reservation = await _get_or_404(db, business_id, reservation_id)
|
||||
target_date = reservation.date
|
||||
await db.delete(reservation)
|
||||
await db.commit()
|
||||
await invalidate_slots_cache(redis, business_id, target_date)
|
||||
|
||||
|
||||
async def update_status(
|
||||
db: AsyncSession,
|
||||
redis: aioredis.Redis,
|
||||
business_id: int,
|
||||
reservation_id: int,
|
||||
new_status: str,
|
||||
) -> Reservation:
|
||||
if new_status not in VALID_STATUSES:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail=f"Estado inválido. Valores permitidos: {', '.join(VALID_STATUSES)}",
|
||||
)
|
||||
reservation = await _get_or_404(db, business_id, reservation_id)
|
||||
reservation.status = new_status
|
||||
await db.commit()
|
||||
await db.refresh(reservation)
|
||||
|
||||
if new_status in ("cancelled", "no_show"):
|
||||
await invalidate_slots_cache(redis, business_id, reservation.date)
|
||||
|
||||
return reservation
|
||||
Reference in New Issue
Block a user