Back to projects
[09]Backend, AIFEATURED

AI Workspace Booking API

2026
110TESTS
89.7%COVERAGE
16ENDPOINTS
5CONTAINERS

WHAT IT DOES

A REST API for coworking space desk booking with an AI-powered natural language interface. Users say "Book me a quiet desk near a window tomorrow at 2pm for 3 hours" and the system parses the request via OpenAI function calling, finds the best-matching desk using preference scoring, and creates a confirmed reservation — all in one API call.

  • AI Natural Language BookingOpenAI function calling parses free-text into structured booking parameters (date, time, duration, desk type, preferences)
  • Smart Desk MatchingPreference scoring algorithm ranks available desks by feature overlap and type match, returning the best fit
  • Concurrency-Safe SchedulingPessimistic locking (SELECT FOR UPDATE) prevents double-booking at the database level with 15-minute buffer gaps
  • Business Rule EngineValidates operating hours (Mon-Fri 7-22, Sat 9-18), duration limits (30min-8hr), max 30-day advance, and 5 active booking cap
  • Async Event PipelineObserver pattern dispatches Redis queue jobs for confirmation emails, cancellation notices, auto-cancel no-shows, and AI usage logging
  • Full CRUD + AdminRESTful booking management with Sanctum token auth, plus admin endpoints for space and desk provisioning

WHY I BUILT IT

I wanted to demonstrate end-to-end integration of AI with real business logic — not just calling an API, but connecting OpenAI function calling to actual booking constraints like concurrency control, time validation, and resource matching. This project covers patterns directly relevant to workspace management platforms: pessimistic locking for shared resources, event-driven side effects via queues, and API-first design with comprehensive testing.

TECH STACK

API & FRAMEWORK
Laravel 11PHP 8.4Laravel Sanctum
AI INTEGRATION
OpenAI GPT-4o-miniFunction Callingopenai-php/laravel
DATABASE & QUEUE
MySQL 8.0Eloquent ORMRedis 7Laravel Queue
TESTING & DOCS
PHPUnitPCOVOpenAPI 3.0l5-swagger
INFRA
Docker Compose5 ContainersPostman Collection

ARCHITECTURE

Client | POST /api/ai/book { "message": "quiet desk tomorrow 2pm" } | Laravel API (Sanctum Auth) | AIParsingService |── OpenAI Chat API (function calling) |── Returns: create_booking(date, time, | duration, type, preferences) | BookingService |── findBestDesk() ← preference scoring |── validateRules() ← hours/duration/limits |── acquireLock() ← SELECT FOR UPDATE |── 15-min buffer gap check |── Booking::create() | BookingObserver |── dispatch(SendBookingConfirmation) |── dispatch(LogAIUsage) | ┌─────────┐ ┌─────────┐ │ MySQL │ │ Redis │ │ (data) │ │ (queue) │ └─────────┘ └─────────┘

AI booking flow — natural language → function calling → business validation → confirmed reservation

KEY TECHNICAL CHALLENGES

AI ↔ Business Logic Integration

OpenAI function calling returns structured parameters, but these must pass through real business rules (operating hours, duration limits, active booking caps). The AI system prompt includes current date/time and operating hours so the model can pre-validate before calling the function — reducing unnecessary API calls.

Pessimistic Locking with Buffer Gaps

Concurrent booking requests on the same desk could cause double-booking. The solution uses SELECT FOR UPDATE to acquire a row-level lock, then checks for conflicts with an expanded 15-minute buffer window (bufferedStart = start - 15min, bufferedEnd = end + 15min) to ensure realistic desk turnover time.

Observer → Queue Pipeline

Side effects (email notifications, AI usage logging) are decoupled from the booking transaction. BookingObserver watches model events and dispatches queue jobs — ensuring the booking response is fast while emails and logs are processed asynchronously by Redis workers.

OpenAI Mocking in Tests

Testing AI integration without real API calls required OpenAI::fake() with CreateResponse::fake() using override arrays for tool_calls. This pattern lets tests verify the full flow — from natural language input to confirmed booking — in under 100ms per test.

Concurrent Booking Test Strategy

True process-level concurrency isn't practical in PHPUnit. The solution simulates it by testing the pessimistic lock path directly: first request acquires the lock and creates a booking, second request on the same slot gets a 409 Conflict. The 15-minute gap is tested separately with boundary cases.

HOW TO RUN

git clone https://github.com/moolair/workspace-booking-api.git
cd workspace-booking-api
docker compose up -d

Starts 5 containers (app, MySQL, Redis, queue worker, scheduler). Migrations run automatically. API at :8000, Swagger UI at /api/documentation.

API ENDPOINTS

MethodEndpointDescription
POST/api/ai/bookAI booking (natural language)
GET/api/ai/suggestionsAI desk suggestions
GET/api/ai/logsAI conversation history
POST/api/auth/registerRegister user
POST/api/auth/loginLogin, get token
GET/api/bookingsList my bookings
POST/api/bookingsCreate manual booking
PUT/api/bookings/{id}Update booking
DELETE/api/bookings/{id}Cancel booking
GET/api/spacesList spaces
GET/api/spaces/{id}/desksList desks in space
GET/api/desks/{id}/availabilityDesk availability
POST/api/admin/spacesCreate space (admin)
POST/api/admin/desksCreate desk (admin)

FUTURE IMPROVEMENTS

  • Multi-turn AI conversation with context memory for complex booking modifications
  • WebSocket push notifications for real-time booking confirmations and reminders
  • CI/CD pipeline with GitHub Actions for automated testing and Docker image publishing
  • Check-in QR code generation and walk-in booking support
  • Analytics dashboard with booking trends, peak hours, and desk utilization heatmaps