How balakamatchi/ai_travel_planner works
Purpose and entry points
AI Travel Planner is a MERN application for authenticated trip planning, AI itinerary generation, budgeting, maps, and photo journals. The React/Vite client starts at client/src/main.jsx and composes routes/UI in client/src/App.jsx. The Express API starts at server/server.js, connects MongoDB through server/config/db.js, and mounts resource routes.
Client application shell and session boundary
client/src/context/AuthContext.jsx restores the session through /api/auth/me, owns login/logout state, and supplies credentials to protected UI. client/src/services/api.js is the Axios boundary: it attaches the locally stored JWT and handles global 401 logout behavior. client/src/components/ProtectedRoute.jsx guards private routes; client/src/components/DashboardLayout.jsx provides the authenticated navigation shell. Theme state is independently managed and persisted by client/src/context/ThemeContext.jsx.
API security and request pipeline
server/server.js configures Express, CORS scoped to CLIENT_URL, cookie parsing, JSON handling, and centralized errors. Protected resource routes pass through JWT authentication in server/middleware/auth.js, accepting bearer token or cookie and attaching the owning user to req.user. Validation, multipart handling, and normalized API failures form key boundaries in server/middleware/validate.js, server/middleware/upload.js, and server/middleware/errorHandler.js.
Domain persistence and ownership model
MongoDB/Mongoose persists four user-owned collections: server/models/User.js, server/models/Trip.js, server/models/Expense.js, and server/models/Journal.js. Trips are the aggregate center: expenses and journal records reference both trip and user. Controllers scope reads and mutations to req.user._id; permanent trip deletion cascades related expenses and journals. Non-cancelled trip state is computed from travel dates via server/utils/getTripStatus.js, rather than persisted.
Trip planning and AI generation flow
The planner UI invokes client/src/services/aiService.js from client/src/pages/AITripPlannerPage.jsx. API endpoints in server/routes/aiRoutes.js route to server/controllers/aiController.js, which either returns a preview or persists an AI-generated Trip. server/utils/aiService.js calls Google Gemini for structured itinerary JSON, sanitizes model-derived values, and falls back to deterministic mock plans when unavailable or invalid. It separately queries Unsplash for destination cover images. AI endpoints are rate-limited to bound cost and abuse.
Trip lifecycle, budgets, and visualization
Trip CRUD flows through client/src/services/tripService.js and server/controllers/tripController.js; UI pages include trip list/details and dashboard views. Expense CRUD flows through client/src/services/expenseService.js and server/controllers/expenseController.js, producing per-trip totals/category breakdowns for the budget tracker. A Trip’s AI-produced places data crosses into client/src/pages/MapViewPage.jsx, where React Leaflet renders valid coordinates on OpenStreetMap tiles with type filtering.
Media and journal subsystem
Journal UI/service calls (client/src/pages/TravelJournalPage.jsx, client/src/services/journalService.js) reach server/controllers/journalController.js. Multer streams avatar and journal images using Cloudinary storage configured in server/config/cloudinary.js; journal deletion also removes Cloudinary assets. Profile operations are handled by server/controllers/userController.js, including avatar replacement and dashboard aggregation.
Runtime and external dependencies
The frontend is React 18/Vite with React Router, Tailwind, Leaflet, Recharts, and Framer Motion. The backend is Node.js 18+ and Express with MongoDB/Mongoose. Required infrastructure is MongoDB and Cloudinary; Gemini and Unsplash are optional due to graceful AI/mock and cover-image fallbacks. The root project runs client and server concurrently; client production output is independently deployable.
Main entry points: api.js API client · auth.js JWT authentication · journalController.js Journal and profile APIs · aiController.js AI generation API
Components 20
React client
- App routes and UI
client/src/App.jsx - Session state · Restores the authenticated user and owns login/logout state.
client/src/context/AuthContext.jsx - API client · Attaches JWTs and triggers global logout on 401 responses.
client/src/services/api.js - Protected app shell
client/src/components/ProtectedRoute.jsx - AI trip planner
client/src/pages/AITripPlannerPage.jsx - Trips and budgets · Trip and expense UI presents trip details, totals, and category breakdowns.
client/src/services/tripService.js - Map view · Renders valid AI trip places on OpenStreetMap tiles.
client/src/pages/MapViewPage.jsx - Travel journal
client/src/pages/TravelJournalPage.jsx
Express API
- Express server · Configures CORS, cookies, JSON parsing, routes, and centralized errors.
server/server.js - JWT authentication · Accepts bearer tokens or cookies and attaches the owner to req.user.
server/middleware/auth.js - Trip and expense APIs · Scopes CRUD and budget aggregations to the authenticated user.
server/controllers/tripController.js - AI generation API · Returns previews or persists generated trips; endpoints are rate-limited.
server/controllers/aiController.js - Itinerary generation · Sanitizes Gemini output and falls back to deterministic mock plans.
server/utils/aiService.js - Journal and profile APIs · Manages journals, images, avatars, and dashboard aggregation.
server/controllers/journalController.js - Media upload pipeline
server/middleware/upload.js
Persistence
- MongoDB · Stores user-owned users, trips, expenses, and journals.
server/config/db.js - Trip aggregate · Expenses and journals reference this user-owned aggregate.
server/models/Trip.js
External services
- Cloudinary · Stores avatars and journal images; deleted journal assets are removed.
server/config/cloudinary.js - Google Gemini
- Unsplash · Provides optional destination cover images.
Connections 22
- App routes and UI → Session state: uses
- Session state → API client: restores /api/auth/me
- App routes and UI → Protected app shell: private routes
- AI trip planner → API client: generation requests
- Trips and budgets → API client: trip and expense CRUD
- Travel journal → API client: journal requests
- Map view → Trips and budgets: uses trip places
- API client → Express server: REST API
- Express server → JWT authentication: protects resources
- JWT authentication → Trip and expense APIs: req.user
- JWT authentication → AI generation API: req.user
- JWT authentication → Journal and profile APIs: req.user
- Trip and expense APIs → Trip aggregate: CRUD and cascade deletion
- Trip aggregate → MongoDB: persists
- AI generation API → Itinerary generation: generate plan
- AI generation API → Trip aggregate: persist generated trip
- Itinerary generation → Google Gemini: structured itinerary
- Itinerary generation → Unsplash: cover image
- Journal and profile APIs → Media upload pipeline: multipart media
- Media upload pipeline → Cloudinary: stream uploads
- Journal and profile APIs → MongoDB: journals and users
- Journal and profile APIs → Cloudinary: remove deleted assets