# gavinarori/livestock-mangement-system architecture

> Architecture diagram of the GitHub repository gavinarori/livestock-mangement-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gavinarori/livestock-mangement-system
- Repository: https://github.com/gavinarori/livestock-mangement-system

## Overview

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: [`app/(app)/layout.tsx`](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/layout.tsx) (Product Shell), [`lib/prisma.ts`](https://github.com/gavinarori/livestock-mangement-system/blob/main/lib/prisma.ts) (Prisma), [`lib/auth/multi-tenancy.ts`](https://github.com/gavinarori/livestock-mangement-system/blob/main/lib/auth/multi-tenancy.ts) (Tenancy), [`app/api/animals/route.ts`](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/animals/route.ts) (Animals API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_app["App Router"]
  node_node_root["App Shell<br/>nextjs root layout<br/>[layout.tsx]"]
  node_node_public_login["Login<br/>public auth page<br/>[page.tsx]"]
  node_node_public_signup["Signup<br/>public auth page<br/>[page.tsx]"]
  node_node_app_layout["Product Shell<br/>authenticated layout<br/>[layout.tsx]"]
  node_node_dashboard["Dashboard<br/>overview page<br/>[page.tsx]"]
  node_node_animals["Animals<br/>record pages<br/>[page.tsx]"]
  node_node_breeding["Breeding<br/>workflow page<br/>[page.tsx]"]
  node_node_health["Health<br/>workflow page<br/>[page.tsx]"]
  node_node_inventory["Inventory<br/>operations page<br/>[page.tsx]"]
  node_node_settings["Settings<br/>admin page<br/>[page.tsx]"]
  node_node_share_page["Public Share<br/>share page<br/>[page.tsx]"]
end

subgraph group_group_api["API Layer"]
  node_node_api_auth["Auth API<br/>route handlers<br/>[route.ts]"]
  node_node_api_animals["Animals API<br/>route handlers<br/>[route.ts]"]
  node_node_api_health["Health API<br/>route handlers<br/>[route.ts]"]
  node_node_api_breeding["Breeding API<br/>route handlers<br/>[route.ts]"]
  node_node_api_inventory["Inventory API<br/>route handlers<br/>[route.ts]"]
  node_node_api_org["Org API<br/>route handlers<br/>[route.ts]"]
  node_node_api_search["Search API<br/>search endpoint<br/>[route.ts]"]
  node_node_api_analytics["Analytics API<br/>aggregation endpoint<br/>[route.ts]"]
end

subgraph group_group_domain["Domain & Data"]
  node_node_tenancy{{"Tenancy<br/>authz boundary<br/>[multi-tenancy.ts]"}}
  node_node_auth_utils{{"Auth Utils<br/>jwt/bcrypt helpers<br/>[utils.ts]"}}
  node_node_prisma[("Prisma<br/>data access<br/>[prisma.ts]")]
  node_node_schema["Schema<br/>domain model<br/>[schema.prisma]"]
  node_node_validations["Validations<br/>input contracts<br/>[index.ts]"]
end

subgraph group_group_shared["Shared UI"]
  node_node_sidebar["Sidebar<br/>navigation component<br/>[sidebar.tsx]"]
  node_node_animal_form["Animal Form<br/>feature component<br/>[animal-form.tsx]"]
  node_node_health_timeline["Health Timeline<br/>feature component"]
  node_node_ui_kit["UI Kit<br/>design system<br/>[button.tsx]"]
end

node_node_root -->|"public route"| node_node_public_login
node_node_root -->|"public route"| node_node_public_signup
node_node_root -->|"mounts"| node_node_app_layout
node_node_app_layout -->|"uses"| node_node_sidebar
node_node_app_layout -->|"composes"| node_node_ui_kit
node_node_app_layout -->|"hosts"| node_node_dashboard
node_node_app_layout -->|"hosts"| node_node_animals
node_node_app_layout -->|"hosts"| node_node_breeding
node_node_app_layout -->|"hosts"| node_node_health
node_node_app_layout -->|"hosts"| node_node_inventory
node_node_app_layout -->|"hosts"| node_node_settings
node_node_app_layout -.->|"calls"| node_node_api_animals
node_node_public_login -.->|"submits to"| node_node_api_auth
node_node_public_signup -.->|"submits to"| node_node_api_auth
node_node_share_page -.->|"fetches share"| node_node_api_animals
node_node_animals -.->|"reads/writes"| node_node_api_animals
node_node_animals -->|"uses"| node_node_animal_form
node_node_health -.->|"reads/writes"| node_node_api_health
node_node_health -->|"uses"| node_node_health_timeline
node_node_breeding -.->|"reads/writes"| node_node_api_breeding
node_node_inventory -.->|"reads/writes"| node_node_api_inventory
node_node_settings -.->|"reads/writes"| node_node_api_org
node_node_dashboard -.->|"reads"| node_node_api_analytics
node_node_app_layout -.->|"searches"| node_node_api_search
node_node_api_auth -->|"uses"| node_node_auth_utils
node_node_api_auth -->|"authorizes"| node_node_tenancy
node_node_api_animals -->|"scopes by org"| node_node_tenancy
node_node_api_health -->|"scopes by org"| node_node_tenancy
node_node_api_breeding -->|"scopes by org"| node_node_tenancy
node_node_api_inventory -->|"scopes by org"| node_node_tenancy
node_node_api_org -->|"checks roles"| node_node_tenancy
node_node_api_search -->|"filters access"| node_node_tenancy
node_node_api_analytics -->|"aggregates within org"| node_node_tenancy
node_node_api_animals -->|"validates input"| node_node_validations
node_node_api_health -->|"validates input"| node_node_validations
node_node_api_breeding -->|"validates input"| node_node_validations
node_node_api_inventory -->|"validates input"| node_node_validations
node_node_api_org -->|"validates input"| node_node_validations
node_node_api_auth -->|"persists via"| node_node_prisma
node_node_api_animals -->|"persists via"| node_node_prisma
node_node_api_health -->|"persists via"| node_node_prisma
node_node_api_breeding -->|"persists via"| node_node_prisma
node_node_api_inventory -->|"persists via"| node_node_prisma
node_node_api_org -->|"persists via"| node_node_prisma
node_node_api_analytics -->|"reads summaries"| node_node_prisma
node_node_prisma -->|"maps models"| node_node_schema

click node_node_root "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/layout.tsx"
click node_node_public_login "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/login/page.tsx"
click node_node_public_signup "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/signup/page.tsx"
click node_node_app_layout "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/layout.tsx"
click node_node_dashboard "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/dashboard/page.tsx"
click node_node_animals "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/animals/page.tsx"
click node_node_breeding "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/breeding/page.tsx"
click node_node_health "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/healthy/page.tsx"
click node_node_inventory "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/inventory/page.tsx"
click node_node_settings "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/settings/page.tsx"
click node_node_share_page "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/share/[token]/page.tsx"
click node_node_api_auth "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/auth/login/route.ts"
click node_node_api_animals "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/animals/route.ts"
click node_node_api_health "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/health/records/route.ts"
click node_node_api_breeding "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/breeding/records/route.ts"
click node_node_api_inventory "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/inventory/feed/route.ts"
click node_node_api_org "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/organization/route.ts"
click node_node_api_search "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/search/route.ts"
click node_node_api_analytics "https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/analytics/route.ts"
click node_node_tenancy "https://github.com/gavinarori/livestock-mangement-system/blob/main/lib/auth/multi-tenancy.ts"
click node_node_auth_utils "https://github.com/gavinarori/livestock-mangement-system/blob/main/lib/auth/utils.ts"
click node_node_prisma "https://github.com/gavinarori/livestock-mangement-system/blob/main/lib/prisma.ts"
click node_node_schema "https://github.com/gavinarori/livestock-mangement-system/blob/main/prisma/schema.prisma"
click node_node_validations "https://github.com/gavinarori/livestock-mangement-system/blob/main/lib/validations/index.ts"
click node_node_sidebar "https://github.com/gavinarori/livestock-mangement-system/blob/main/components/sidebar.tsx"
click node_node_animal_form "https://github.com/gavinarori/livestock-mangement-system/blob/main/components/animals/animal-form.tsx"
click node_node_health_timeline "https://github.com/gavinarori/livestock-mangement-system/blob/main/components/health/health-timeline.tsx"
click node_node_ui_kit "https://github.com/gavinarori/livestock-mangement-system/blob/main/components/ui/button.tsx"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_root,node_node_public_login,node_node_public_signup,node_node_app_layout,node_node_dashboard,node_node_animals,node_node_breeding,node_node_health,node_node_inventory,node_node_settings,node_node_share_page toneBlue
class node_node_api_auth,node_node_api_animals,node_node_api_health,node_node_api_breeding,node_node_api_inventory,node_node_api_org,node_node_api_search,node_node_api_analytics toneAmber
class node_node_tenancy,node_node_auth_utils,node_node_prisma,node_node_schema,node_node_validations toneMint
class node_node_sidebar,node_node_animal_form,node_node_health_timeline,node_node_ui_kit toneRose
```

## Components

### App Router

User-facing routes and layouts

- [App Shell](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/layout.tsx): nextjs root layout · Global document and app wrapper · `app/layout.tsx`
- [Login](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/login/page.tsx): public auth page · Sign-in surface · `app/login/page.tsx`
- [Signup](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/signup/page.tsx): public auth page · Account creation surface · `app/signup/page.tsx`
- [Product Shell](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/layout.tsx): authenticated layout · Shared post-login navigation and theme · `app/(app)/layout.tsx`
- [Dashboard](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/dashboard/page.tsx): overview page · Primary authenticated landing page · `app/(app)/dashboard/page.tsx`
- [Animals](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/animals/page.tsx): record pages · List, detail, create, edit, and shares · `app/(app)/animals/page.tsx`
- [Breeding](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/breeding/page.tsx): workflow page · Breeding cycles and records UI · `app/(app)/breeding/page.tsx`
- [Health](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/healthy/page.tsx): workflow page · Health operations overview · `app/(app)/healthy/page.tsx`
- [Inventory](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/inventory/page.tsx): operations page · Feed, medicine, and equipment UI · `app/(app)/inventory/page.tsx`
- [Settings](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/(app)/settings/page.tsx): admin page · Organization and account settings · `app/(app)/settings/page.tsx`
- [Public Share](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/share/[token]/page.tsx): share page · Token-based animal record access · `app/share/[token]/page.tsx`

### API Layer

Route handlers for business operations

- [Auth API](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/auth/login/route.ts): route handlers · Login and signup endpoints · `app/api/auth/login/route.ts`
- [Animals API](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/animals/route.ts): route handlers · Animal CRUD and subresources · `app/api/animals/route.ts`
- [Health API](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/health/records/route.ts): route handlers · Records, diseases, treatments, vets, vaccinations · `app/api/health/records/route.ts`
- [Breeding API](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/breeding/records/route.ts): route handlers · Heat cycles and breeding records · `app/api/breeding/records/route.ts`
- [Inventory API](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/inventory/feed/route.ts): route handlers · Feed, medicine, and equipment endpoints · `app/api/inventory/feed/route.ts`
- [Org API](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/organization/route.ts): route handlers · Organization and member management · `app/api/organization/route.ts`
- [Search API](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/search/route.ts): search endpoint · Cross-domain lookup and filtering · `app/api/search/route.ts`
- [Analytics API](https://github.com/gavinarori/livestock-mangement-system/blob/main/app/api/analytics/route.ts): aggregation endpoint · Read-only metrics and summaries · `app/api/analytics/route.ts`

### Domain & Data

Core models, tenancy, and persistence

- [Tenancy](https://github.com/gavinarori/livestock-mangement-system/blob/main/lib/auth/multi-tenancy.ts): authz boundary · Organization isolation and role checks · `lib/auth/multi-tenancy.ts`
- [Auth Utils](https://github.com/gavinarori/livestock-mangement-system/blob/main/lib/auth/utils.ts): jwt/bcrypt helpers · Authentication primitives and session logic · `lib/auth/utils.ts`
- [Prisma](https://github.com/gavinarori/livestock-mangement-system/blob/main/lib/prisma.ts): data access · Typed bridge to MongoDB · `lib/prisma.ts`
- [Schema](https://github.com/gavinarori/livestock-mangement-system/blob/main/prisma/schema.prisma): domain model · Organizations, users, animals, health, breeding, shares · `prisma/schema.prisma`
- [Validations](https://github.com/gavinarori/livestock-mangement-system/blob/main/lib/validations/index.ts): input contracts · Shared request and form validation · `lib/validations/index.ts`

### Shared UI

Reusable components and helpers

- [Sidebar](https://github.com/gavinarori/livestock-mangement-system/blob/main/components/sidebar.tsx): navigation component · Primary authenticated navigation · `components/sidebar.tsx`
- [Animal Form](https://github.com/gavinarori/livestock-mangement-system/blob/main/components/animals/animal-form.tsx): feature component · Create and edit animal records · `components/animals/animal-form.tsx`
- [Health Timeline](https://github.com/gavinarori/livestock-mangement-system/blob/main/components/health/health-timeline.tsx): feature component · Chronological health view · `components/health/health-timeline.tsx`
- [UI Kit](https://github.com/gavinarori/livestock-mangement-system/blob/main/components/ui/button.tsx): design system · Shared shadcn primitives · `components/ui/button.tsx`

## Connections

- 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
