AI Workspace Booking API
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 Booking — OpenAI function calling parses free-text into structured booking parameters (date, time, duration, desk type, preferences)
- Smart Desk Matching — Preference scoring algorithm ranks available desks by feature overlap and type match, returning the best fit
- Concurrency-Safe Scheduling — Pessimistic locking (SELECT FOR UPDATE) prevents double-booking at the database level with 15-minute buffer gaps
- Business Rule Engine — Validates operating hours (Mon-Fri 7-22, Sat 9-18), duration limits (30min-8hr), max 30-day advance, and 5 active booking cap
- Async Event Pipeline — Observer pattern dispatches Redis queue jobs for confirmation emails, cancellation notices, auto-cancel no-shows, and AI usage logging
- Full CRUD + Admin — RESTful 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
ARCHITECTURE
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 -dStarts 5 containers (app, MySQL, Redis, queue worker, scheduler). Migrations run automatically. API at :8000, Swagger UI at /api/documentation.
API ENDPOINTS
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/ai/book | AI booking (natural language) |
| GET | /api/ai/suggestions | AI desk suggestions |
| GET | /api/ai/logs | AI conversation history |
| POST | /api/auth/register | Register user |
| POST | /api/auth/login | Login, get token |
| GET | /api/bookings | List my bookings |
| POST | /api/bookings | Create manual booking |
| PUT | /api/bookings/{id} | Update booking |
| DELETE | /api/bookings/{id} | Cancel booking |
| GET | /api/spaces | List spaces |
| GET | /api/spaces/{id}/desks | List desks in space |
| GET | /api/desks/{id}/availability | Desk availability |
| POST | /api/admin/spaces | Create space (admin) |
| POST | /api/admin/desks | Create 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