How codexgautam/assetsphere works
1. Overall shape
AssetSphere is a single Next.js 15 application using the App Router. There is no separate backend service; the same Node.js server renders pages and serves /api/* route handlers. The architecture is split into route-level UI, API handlers, business services, and Mongoose models.
2. Primary domain boundary: multi-tenant organisation
Almost every business object is scoped to an organisation: assets, bookings, categories, members, notifications, audit logs, and analytics. The platform has three roles with distinct scopes: SUPERADMIN at the platform level, ORG_ADMIN within one organisation, and MEMBER for end users inside an organisation.
3. Presentation layer
The src/app tree is organised by route groups and functional areas:
- (landing) for public marketing content
- (auth) for login/register
- dashboard, assets, bookings, notifications, onboard, and invite/[token] for member-facing flows
- admin/* for organisation administration
- superadmin/* for platform moderation
Shared UI lives in src/components, grouped by domain such as assets, bookings, dashboard, landing, charts, forms, and design-system primitives.
4. Access control and session enforcement
Authentication is handled by NextAuth v5 with JWT sessions. src/middleware.ts enforces route protection at the edge for private areas, while API handlers perform their own session and role checks. This creates two security boundaries: page navigation protection and server-side authorization for data access.
5. API layer as the orchestration boundary
All mutations and most reads go through /api/* route handlers under src/app/api. These handlers are the coordination point between validation, permissions, services, and persistence. The API is split by domain: assets, bookings, categories, organisations, notifications, audit logs, analytics, auth, invite, health, admin users, and superadmin operations.
6. Business logic layer
src/services contains the domain workflows that sit between route handlers and database models. The service layer covers assets, bookings, categories, users, notifications, and audit logging. This is where state transitions, side effects, and cross-entity updates are centralized instead of being spread across route handlers.
7. Persistence model
src/models contains the Mongoose schemas for the core domain entities: user, organisation, asset, category, booking, invite, notification, and audit log. MongoDB is the system of record, and src/lib/db.ts provides the shared connection layer. The data model supports the multi-tenant boundary and booking lifecycle state.
8. Booking workflow and asset availability
The booking subsystem is the most important transactional flow. Members create booking requests, admins approve/reject/issue/return them, and asset availability is adjusted automatically when statuses change. This means bookings are not just records; they drive inventory accounting for available quantity and overdue state.
9. Invites, onboarding, and membership management
Organisation onboarding starts with a public application flow, then platform approval. After approval, organisation admins can invite members via tokenised email links with TTL-based invite tokens. Accepting an invite creates or connects the user and joins them to the organisation, making invite handling a key boundary between public onboarding and tenant membership.
10. Notifications, audit logs, and operational traceability
Notifications are created for important lifecycle events such as booking status changes and invites, and they surface in an in-app notification center with unread state. Audit logs capture immutable records of significant actions with actor and metadata. Together, these provide both user-facing feedback and administrative traceability.
11. Analytics and admin dashboard
The org admin dashboard is powered by dedicated analytics endpoints for summary KPIs, utilisation, category distribution, and booking status breakdown. These endpoints feed chart components under src/components/charts and stat cards in the dashboard. The analytics subsystem is read-heavy and derived from the same operational MongoDB data.
12. External integrations
Three integrations materially affect the architecture:
- SendGrid for email delivery around invites and approvals
- Cloudinary for asset image uploads
- QR generation/scanning for asset identification and browser-based scanning
These are optional or gracefully degraded where noted, but they extend the asset and onboarding workflows beyond the database.
13. Validation and shared contract layer
src/validators holds Zod schemas for auth, assets, bookings, and categories, so request validation is explicit and consistent across API endpoints. src/types and src/constants provide shared enums and typed contracts used by both UI and server code, reducing drift between the routes, services, and components.
14. Deployment and runtime assumptions
The app is designed for deployment as a single Next.js server, commonly on Vercel, with MongoDB Atlas in production. Docker Compose supports local development by running the app alongside MongoDB. The architecture does not depend on any separate backend process or message queue; side effects happen synchronously inside API requests.
Main entry points: assets/route.ts Assets API · booking-service.ts Services · bookings/route.ts Bookings API · permissions.ts Permissions
Components 24
Presentation
App Router pages and shared UI.
- Landing · Marketing and public-facing entry pages.
src/app/(landing)/page.tsx - Auth · Authentication entry pages.
src/app/(auth)/login/page.tsx - Dashboard · Member and admin landing area.
src/app/dashboard/page.tsx - Assets · Asset listing and detail pages.
src/app/assets/page.tsx - Bookings · Booking request and review pages.
src/app/bookings/page.tsx - Notifications · Notification center for users.
src/app/notifications/page.tsx - Org Admin · Organisation administration pages.
src/app/admin/page.tsx - Superadmin · Platform moderation pages.
src/app/superadmin/orgs/page.tsx
API
Route handlers that coordinate requests.
- Assets API · Asset CRUD and uploads orchestration.
src/app/api/assets/route.ts - Bookings API · Booking lifecycle and availability updates.
src/app/api/bookings/route.ts - Admin API · Organisation admin and member management endpoints.
src/app/api/orgs/members/route.ts - Analytics API · Derived KPI, utilisation, and status summaries.
src/app/api/analytics/summary/route.ts - Notifications API · Notification listing and read-state updates.
src/app/api/notifications/route.ts - Invite API · Invite creation and acceptance boundary.
src/app/api/invite/[token]/route.ts - Orgs API · Organisation application, approval, rejection, and membership endpoints.
src/app/api/orgs/route.ts - Superadmin API · Platform-level organisation moderation and seeding.
src/app/api/superadmin/orgs/route.ts
Core
Shared runtime, services, models, and integration layers.
- Middleware · Route protection for private areas.
src/middleware.ts - Auth Core · JWT session setup and auth configuration.
src/lib/auth.ts - Permissions · Role and scope authorization helpers.
src/lib/permissions.ts - Database · Shared MongoDB connection layer.
src/lib/db.ts - Services · Business logic for assets, bookings, users, categories, notifications, and audit logs.
src/services/booking-service.ts - Models · Organisation-scoped persistence models.
src/models/booking.ts - Validators · Request validation contracts for core domains.
src/validators/booking.ts - Integrations · Email, uploads, and QR tooling.
src/lib/sendgrid.ts
Connections 28
- Landing → Auth: entry · Public users move into authentication.
- Auth → Auth Core: sessions · Login and registration use the shared NextAuth setup.
- Dashboard → Analytics API: feeds charts · Dashboard metrics are read from analytics endpoints.
- Assets → Assets API: reads/writes · Asset pages rely on API handlers for data.
- Bookings → Bookings API: reads/writes · Booking pages use the booking lifecycle API.
- Notifications → Notifications API: inbox · Notification UI loads and updates through the API.
- Org Admin → Admin API: manage org · Admin pages manage members and organisation settings.
- Superadmin → Superadmin API: moderate · Platform pages call superadmin endpoints.
- Middleware → Dashboard: protects · Private page navigation is guarded at the edge.
- Middleware → Assets: protects · Private page navigation is guarded at the edge.
- Middleware → Bookings: protects · Private page navigation is guarded at the edge.
- Assets API → Validators: validates · Asset requests are schema-validated.
- Bookings API → Validators: validates · Booking requests are schema-validated.
- Orgs API → Validators: validates · Organisation and invite requests are schema-validated.
- Assets API → Permissions: authorizes · Asset access is scoped by role and organisation.
- Bookings API → Permissions: authorizes · Booking actions enforce role and tenant checks.
- Admin API → Permissions: authorizes · Admin endpoints enforce org-admin boundaries.
- Superadmin API → Permissions: authorizes · Platform endpoints enforce superadmin access.
- Assets API → Services: orchestrates · API handlers delegate workflows to services.
- Bookings API → Services: orchestrates · Booking transitions are coordinated in services.
- Admin API → Services: orchestrates · Member and user operations flow through services.
- Analytics API → Database: reads · Analytics are derived from operational data.
- Services → Database: persists · Domain workflows read and write MongoDB.
- Services → Models: uses · Services operate on Mongoose models.
- Models → Database: stores · Models are backed by the shared database connection.
- Invite API → Integrations: emails · Invites are delivered through email integration.
- Assets API → Integrations: uploads/qr · Asset uploads and QR flows use external helpers.
- Orgs API → Integrations: emails · Onboarding and invite flows send email.