How gavinarori/livestock-mangement-system works
1. Overall shape
This is a Next.js App Router monolith: the same repository contains the UI, server-rendered pages, and API route handlers. The app is organized around a multi-tenant livestock management domain, with MongoDB accessed through Prisma.
2. Route boundaries
The application has three major UX surfaces:
- app/login and app/signup for public authentication
- app/(app) for the authenticated product shell and internal pages
- app/share/[token] for public or semi-public animal record sharing
3. Authenticated application shell
The app/(app)/layout.tsx and components/sidebar.tsx suggest a shared post-login layout with navigation and theme support. Pages under this group split by business domain: dashboard, analytics, animals, breeding, healthy, inventory, and settings.
4. Backend API boundary
All business operations flow through app/api/**/route.ts handlers. These cover:
- auth (/api/auth/login, /api/auth/signup)
- animals and animal subresources
- health, breeding, inventory, organization, search, and analytics
The UI pages likely call these endpoints rather than talking directly to the database.
5. Core domain model
The repo is centered on a small set of enterprise entities defined in Prisma:
- Organization
- User
- Animal
- HealthRecord
- VeterinaryNote
- Breeding
- AnimalShare
The Organization sits at the top of the tenancy boundary; almost every operational record is scoped to organizationId.
6. Multi-tenancy and permissions
lib/auth/multi-tenancy.ts and the README indicate organization isolation plus role-based access control. The system supports predefined roles and permission checks on API endpoints, so the main security boundary is not just “logged in vs logged out,” but “which organization and which role.”
7. Animal management flow
Animals are the central workflow object. The UI has list, detail, create, edit, and share pages; the API has collection and item routes plus subroutes for health, veterinary, and sharing. That implies a record-centric flow: create animal → inspect/update detail → attach operational records → optionally generate a share link.
8. Health and veterinary subsystem
Health data is split into structured operational records and veterinarian notes. The frontend components include a timeline and forms, so the intended interaction is append-only event capture plus chronological viewing rather than a single mutable health profile.
9. Breeding, inventory, and analytics
Breeding has dedicated UI/API surface for cycles and records, while inventory exposes feed, medicine, and equipment endpoints. Analytics is a separate read-only aggregation boundary, likely summarizing data across animals, health, breeding, and inventory for dashboards and trend views.
10. Sharing and public access
AnimalShare and the /share/[token] page represent a public-facing access path. Share routes include token lookup, logs, and revocation, which means share links are managed as first-class resources with lifecycle controls, not ad hoc URLs.
11. Auth and session infrastructure
The auth subsystem is custom: login/signup endpoints, JWT, and bcrypt are called out in the README, with utilities in lib/auth/utils.ts and auth middleware in lib/auth/middleware.ts. This means authentication and authorization are enforced at the application layer rather than through an external identity provider.
12. Persistence layer
lib/prisma.ts, lib/db/*, and prisma/schema.prisma define the persistence boundary. Prisma is the typed bridge from route handlers to MongoDB, and prisma/seed.ts suggests initial data/bootstrap support for organizations, users, or sample livestock.
13. UI composition
The UI is built from reusable feature components plus a large shadcn/ui component library in components/ui. Feature-specific components exist for animals, auth, and health, while shared primitives handle forms, dialogs, tables, charts, navigation, toasts, and responsive behavior.
14. Validation and shared utilities
lib/validations/index.ts and lib/utils.ts hold shared cross-cutting logic. This is the repository’s main input-validation and formatting boundary, helping keep the API handlers and forms consistent.
15. Diagram-friendly mental model
A good architecture diagram would show:
- browser clients hitting App Router pages
- authenticated pages using shared shell components
- route handlers as the backend API layer
- Prisma as the data-access layer
- MongoDB as the persistence store
- a separate public share path
- cross-cutting auth/tenant checks wrapping all organization-scoped data flows
Main entry points: layout.tsx Product Shell · prisma.ts Prisma · multi-tenancy.ts Tenancy · route.ts Animals API
Components 28
App Router
User-facing routes and layouts
- App Shell · Global document and app wrapper
app/layout.tsx - Login · Sign-in surface
app/login/page.tsx - Signup · Account creation surface
app/signup/page.tsx - Product Shell · Shared post-login navigation and theme
app/(app)/layout.tsx - Dashboard · Primary authenticated landing page
app/(app)/dashboard/page.tsx - Animals · List, detail, create, edit, and shares
app/(app)/animals/page.tsx - Breeding · Breeding cycles and records UI
app/(app)/breeding/page.tsx - Health · Health operations overview
app/(app)/healthy/page.tsx - Inventory · Feed, medicine, and equipment UI
app/(app)/inventory/page.tsx - Settings · Organization and account settings
app/(app)/settings/page.tsx - Public Share · Token-based animal record access
app/share/[token]/page.tsx
API Layer
Route handlers for business operations
- Auth API · Login and signup endpoints
app/api/auth/login/route.ts - Animals API · Animal CRUD and subresources
app/api/animals/route.ts - Health API · Records, diseases, treatments, vets, vaccinations
app/api/health/records/route.ts - Breeding API · Heat cycles and breeding records
app/api/breeding/records/route.ts - Inventory API · Feed, medicine, and equipment endpoints
app/api/inventory/feed/route.ts - Org API · Organization and member management
app/api/organization/route.ts - Search API · Cross-domain lookup and filtering
app/api/search/route.ts - Analytics API · Read-only metrics and summaries
app/api/analytics/route.ts
Domain & Data
Core models, tenancy, and persistence
- Tenancy · Organization isolation and role checks
lib/auth/multi-tenancy.ts - Auth Utils · Authentication primitives and session logic
lib/auth/utils.ts - Prisma · Typed bridge to MongoDB
lib/prisma.ts - Schema · Organizations, users, animals, health, breeding, shares
prisma/schema.prisma - Validations · Shared request and form validation
lib/validations/index.ts
Shared UI
Reusable components and helpers
- Sidebar · Primary authenticated navigation
components/sidebar.tsx - Animal Form · Create and edit animal records
components/animals/animal-form.tsx - Health Timeline · Chronological health view
components/health/health-timeline.tsx - UI Kit · Shared shadcn primitives
components/ui/button.tsx
Connections 46
- App Shell → Login: public route
- App Shell → Signup: public route
- App Shell → Product Shell: mounts
- Product Shell → Sidebar: uses
- Product Shell → UI Kit: composes
- Product Shell → Dashboard: hosts
- Product Shell → Animals: hosts
- Product Shell → Breeding: hosts
- Product Shell → Health: hosts
- Product Shell → Inventory: hosts
- Product Shell → Settings: hosts
- Product Shell → Animals API: calls
- Login → Auth API: submits to
- Signup → Auth API: submits to
- Public Share → Animals API: fetches share
- Animals → Animals API: reads/writes
- Animals → Animal Form: uses
- Health → Health API: reads/writes
- Health → Health Timeline: uses
- Breeding → Breeding API: reads/writes
- Inventory → Inventory API: reads/writes
- Settings → Org API: reads/writes
- Dashboard → Analytics API: reads
- Product Shell → Search API: searches
- Auth API → Auth Utils: uses
- Auth API → Tenancy: authorizes
- Animals API → Tenancy: scopes by org
- Health API → Tenancy: scopes by org
- Breeding API → Tenancy: scopes by org
- Inventory API → Tenancy: scopes by org
- Org API → Tenancy: checks roles
- Search API → Tenancy: filters access
- Analytics API → Tenancy: aggregates within org
- Animals API → Validations: validates input
- Health API → Validations: validates input
- Breeding API → Validations: validates input
- Inventory API → Validations: validates input
- Org API → Validations: validates input
- Auth API → Prisma: persists via
- Animals API → Prisma: persists via
- Health API → Prisma: persists via
- Breeding API → Prisma: persists via
- Inventory API → Prisma: persists via
- Org API → Prisma: persists via
- Analytics API → Prisma: reads summaries
- Prisma → Schema: maps models