# kavinraj7305/workflowguard architecture

> Architecture diagram of the GitHub repository kavinraj7305/workflowguard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kavinraj7305/workflowguard
- Repository: https://github.com/kavinraj7305/workflowguard

## Overview

#### Purpose and runtime
WorkFlowGuard is a Next.js 16 employee productivity, retention, HR, and work-management system. It runs on Node.js 20+ with Postgres (intended for Neon) through Drizzle ORM. The main HTTP application is the App Router under `src/app/`; database access and relational model are centralized in `src/db/index.ts` and `src/db/schema.ts`.

#### Application entry points and audiences
The root application routes direct users into setup, login, or role-oriented experiences: `src/app/setup/page.tsx`, `src/app/login/page.tsx`, and `src/app/page.tsx`. HR/manager functions live in the admin console rooted by `src/app/admin/layout.tsx` and `src/app/admin/page.tsx`. Individual developers work in assignment-scoped workspaces rooted at `src/app/workspace/[assignmentId]/layout.tsx` and `src/app/workspace/[assignmentId]/page.tsx`.

#### Identity, sessions, and authorization
Authentication is API-driven: login/logout/current-user endpoints are at `src/app/api/auth/login/route.ts`, `src/app/api/auth/logout/route.ts`, and `src/app/api/auth/me/route.ts`. Session and password handling are separated in `src/lib/auth/session.ts` and `src/lib/auth/password.ts`; route-level protection is enforced by `src/middleware.ts`. Authorization boundaries additionally include assignment/workflow access logic in `src/lib/workflow-access.ts`, separating employee workspace access from administrative operations.

#### Organizational and workforce management
Organizations, users, HR profiles, leave, and payroll form the personnel domain exposed through route handlers such as `src/app/api/orgs/route.ts`, `src/app/api/users/route.ts`, and `src/app/api/hrm/profiles/route.ts`. Admin UI modules consolidate operational views, including `src/app/admin/hrm/page.tsx`, `src/app/admin/payroll/page.tsx`, and `src/app/admin/orgs/page.tsx`. Optional Resend-backed credentials notifications flow through `src/lib/notify/credentials-email.ts`; account creation remains functional when email is not configured.

#### Work assignment, tickets, and activity capture
Tickets are the primary work-unit API, with collection and per-ticket mutation boundaries at `src/app/api/tickets/route.ts` and `src/app/api/tickets/[ticketId]/route.ts`. Specialized actions—assignment, status transitions, activity, and screenshots—are separated into handlers under `src/app/api/tickets/[ticketId]/`. The developer workspace coordinates task actions, timer/activity tracking, and extension enrollment through `src/components/workspace/TaskActions.tsx`, `src/components/workspace/PomodoroTimer.tsx`, and `src/components/workspace/WorkspaceActivityTracker.tsx`.

#### Browser extension integration
A standalone Chrome extension captures tab activity from `browser-extension/background.js` and is configured by `browser-extension/manifest.json`. It authenticates using an extension-specific token issued by `src/app/api/auth/extension-token/route.ts`, rather than browser session credentials. Client activity resolution is abstracted behind `src/lib/api/resolve-activity-client.ts`, preserving the boundary between extension events and application domain handling.

#### Analytics, reporting, and AI
Database-derived productivity, retention, ticket, mood, and daily-report calculations are encapsulated in `src/lib/ticket-analytics.ts`, `src/lib/analytics/mood.ts`, and `src/lib/reports/daily.ts`. They feed explicit APIs including `src/app/api/analytics/my-productivity/route.ts`, `src/app/api/analytics/org-retention/route.ts`, and `src/app/api/reports/daily/route.ts`, consumed by the corresponding workspace and admin dashboards. The admin assistant endpoint at `src/app/api/admin/ai/chat/route.ts` invokes constrained admin tools from `src/lib/admin/ai-tools.ts` through an OpenAI-compatible provider adapter in `src/lib/llm/openai-compatible.ts`.

#### Persistence and lifecycle
Drizzle schema definitions are versioned by `drizzle/` SQL migrations and managed through `drizzle.config.ts`; operational scripts include `scripts/apply-schema.ts` and `scripts/seed.ts`. First-run onboarding is deliberately limited to an empty user database through `src/app/api/setup/route.ts`, establishing the initial organization and HR authority before normal authenticated workflows begin.

Main entry points: [`src/db/index.ts`](https://github.com/kavinraj7305/workflowguard/blob/master/src/db/index.ts) (Drizzle data layer), [`src/app/admin/layout.tsx`](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/admin/layout.tsx) (Admin console), [`src/app/api/tickets/route.ts`](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/tickets/route.ts) (Tickets API), [`src/app/page.tsx`](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/page.tsx) (Entry routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Next.js application"]
  node_root_routes["Entry routes<br/>App Router pages<br/>[page.tsx]"]
  node_admin_console["Admin console<br/>Admin UI<br/>[layout.tsx]"]
  node_personnel_api["Personnel APIs<br/>Route handlers<br/>[route.ts]"]
end

subgraph group_identity["Identity &amp; access"]
  node_auth_api["Auth API<br/>Route handlers<br/>[route.ts]"]
  node_session_auth["Sessions &amp; passwords<br/>Auth library<br/>[session.ts]"]
  node_middleware{{"Route protection<br/>Next middleware<br/>[middleware.ts]"}}
  node_workflow_access["Workflow access<br/>Authorization library<br/>[workflow-access.ts]"]
  node_setup_api["First-run setup<br/>Route handler<br/>[route.ts]"]
end

subgraph group_work["Work management"]
  node_workspace["Developer workspace<br/>Assignment UI<br/>[page.tsx]"]
  node_workspace_actions["Workspace activity<br/>Client components"]
  node_tickets_api["Tickets API<br/>Route handlers<br/>[route.ts]"]
end

subgraph group_data["Data &amp; insights"]
  node_analytics_reports["Analytics &amp; reports<br/>Domain calculations"]
  node_analytics_api["Analytics APIs<br/>Route handlers<br/>[route.ts]"]
  node_admin_ai["Admin AI assistant<br/>Route handler<br/>[route.ts]"]
  node_ai_tools["Admin AI tools<br/>Tool boundary<br/>[ai-tools.ts]"]
  node_llm_adapter["LLM provider adapter<br/>OpenAI-compatible adapter"]
  node_drizzle_data[("Drizzle data layer<br/>Database &amp; schema<br/>[index.ts]")]
  node_migrations["Schema lifecycle<br/>Migrations &amp; scripts"]
end

subgraph group_extension["Browser extension"]
  node_extension_token["Extension token API<br/>Route handler<br/>[route.ts]"]
  node_chrome_extension{{"Chrome activity capture<br/>Extension background<br/>[background.js]"}}
  node_activity_resolver["Activity resolver<br/>Client integration"]
end

node_root_routes -->|"administrators"| node_admin_console
node_root_routes -->|"employees"| node_workspace
node_root_routes -->|"first run"| node_setup_api
node_middleware -->|"checks session"| node_auth_api
node_auth_api -->|"creates and validates"| node_session_auth
node_auth_api -->|"accounts"| node_drizzle_data
node_setup_api -->|"initial authority"| node_drizzle_data
node_admin_console -->|"manages workforce"| node_personnel_api
node_personnel_api -->|"organizations and people"| node_drizzle_data
node_workspace -->|"coordinates"| node_workspace_actions
node_workspace_actions -->|"task and activity actions"| node_tickets_api
node_tickets_api -->|"authorizes"| node_workflow_access
node_tickets_api -->|"tickets and activity"| node_drizzle_data
node_chrome_extension -->|"obtains token"| node_extension_token
node_chrome_extension -->|"tab events"| node_activity_resolver
node_activity_resolver -->|"resolved activity"| node_tickets_api
node_workspace -->|"personal metrics"| node_analytics_api
node_admin_console -->|"organization reporting"| node_analytics_api
node_analytics_api -->|"calculates"| node_analytics_reports
node_analytics_reports -->|"derives from"| node_drizzle_data
node_admin_console -->|"assistant requests"| node_admin_ai
node_admin_ai -->|"constrained tools"| node_ai_tools
node_admin_ai -->|"model calls"| node_llm_adapter
node_ai_tools -->|"admin data"| node_drizzle_data
node_migrations -->|"evolves schema"| node_drizzle_data

click node_root_routes "https://github.com/kavinraj7305/workflowguard/blob/master/src/app/page.tsx"
click node_admin_console "https://github.com/kavinraj7305/workflowguard/blob/master/src/app/admin/layout.tsx"
click node_workspace "https://github.com/kavinraj7305/workflowguard/blob/master/src/app/workspace/%5BassignmentId%5D/page.tsx"
click node_workspace_actions "https://github.com/kavinraj7305/workflowguard/blob/master/src/components/workspace/WorkspaceActivityTracker.tsx"
click node_auth_api "https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/auth/login/route.ts"
click node_session_auth "https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/auth/session.ts"
click node_middleware "https://github.com/kavinraj7305/workflowguard/blob/master/src/middleware.ts"
click node_workflow_access "https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/workflow-access.ts"
click node_setup_api "https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/setup/route.ts"
click node_personnel_api "https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/users/route.ts"
click node_tickets_api "https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/tickets/route.ts"
click node_extension_token "https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/auth/extension-token/route.ts"
click node_chrome_extension "https://github.com/kavinraj7305/workflowguard/blob/master/browser-extension/background.js"
click node_activity_resolver "https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/api/resolve-activity-client.ts"
click node_analytics_reports "https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/ticket-analytics.ts"
click node_analytics_api "https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/analytics/my-productivity/route.ts"
click node_admin_ai "https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/admin/ai/chat/route.ts"
click node_ai_tools "https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/admin/ai-tools.ts"
click node_llm_adapter "https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/llm/openai-compatible.ts"
click node_drizzle_data "https://github.com/kavinraj7305/workflowguard/blob/master/src/db/index.ts"

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_root_routes,node_admin_console,node_personnel_api toneBlue
class node_auth_api,node_session_auth,node_middleware,node_workflow_access,node_setup_api toneAmber
class node_workspace,node_workspace_actions,node_tickets_api toneMint
class node_analytics_reports,node_analytics_api,node_admin_ai,node_ai_tools,node_llm_adapter,node_drizzle_data,node_migrations toneRose
class node_extension_token,node_chrome_extension,node_activity_resolver toneIndigo
```

## Components

### Next.js application

- [Entry routes](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/page.tsx): App Router pages · Routes users to setup, login, and role-specific experiences. · `src/app/page.tsx`
- [Admin console](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/admin/layout.tsx): Admin UI · HR, payroll, organization, and management dashboards. · `src/app/admin/layout.tsx`
- [Personnel APIs](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/users/route.ts): Route handlers · Organization, user, and HR-profile operations. · `src/app/api/users/route.ts`

### Identity & access

- [Auth API](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/auth/login/route.ts): Route handlers · Login, logout, and current-user endpoints. · `src/app/api/auth/login/route.ts`
- [Sessions & passwords](https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/auth/session.ts): Auth library · `src/lib/auth/session.ts`
- [Route protection](https://github.com/kavinraj7305/workflowguard/blob/master/src/middleware.ts): Next middleware · `src/middleware.ts`
- [Workflow access](https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/workflow-access.ts): Authorization library · Separates assignment access from administrative operations. · `src/lib/workflow-access.ts`
- [First-run setup](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/setup/route.ts): Route handler · Creates the initial organization and HR authority only on an empty user database. · `src/app/api/setup/route.ts`

### Work management

- [Developer workspace](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/workspace/%5BassignmentId%5D/page.tsx): Assignment UI · Assignment-scoped task execution and activity tracking. · `src/app/workspace/[assignmentId]/page.tsx`
- [Workspace activity](https://github.com/kavinraj7305/workflowguard/blob/master/src/components/workspace/WorkspaceActivityTracker.tsx): Client components · Task actions, Pomodoro timer, and activity tracker. · `src/components/workspace/WorkspaceActivityTracker.tsx`
- [Tickets API](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/tickets/route.ts): Route handlers · Ticket collection, mutation, assignment, status, activity, and screenshots. · `src/app/api/tickets/route.ts`

### Data & insights

- [Analytics & reports](https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/ticket-analytics.ts): Domain calculations · Productivity, retention, mood, ticket, and daily-report calculations. · `src/lib/ticket-analytics.ts`
- [Analytics APIs](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/analytics/my-productivity/route.ts): Route handlers · Serves individual productivity, organization retention, and daily reports. · `src/app/api/analytics/my-productivity/route.ts`
- [Admin AI assistant](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/admin/ai/chat/route.ts): Route handler · Constrained assistant endpoint for administrators. · `src/app/api/admin/ai/chat/route.ts`
- [Admin AI tools](https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/admin/ai-tools.ts): Tool boundary · Constrained administrative operations exposed to the assistant. · `src/lib/admin/ai-tools.ts`
- [LLM provider adapter](https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/llm/openai-compatible.ts): OpenAI-compatible adapter · `src/lib/llm/openai-compatible.ts`
- [Drizzle data layer](https://github.com/kavinraj7305/workflowguard/blob/master/src/db/index.ts): Database & schema · Centralized Postgres access and relational model. · `src/db/index.ts`
- **Schema lifecycle**: Migrations & scripts · Versioned SQL migrations, schema application, and seed operations.

### Browser extension

- [Extension token API](https://github.com/kavinraj7305/workflowguard/blob/master/src/app/api/auth/extension-token/route.ts): Route handler · Issues extension-specific tokens instead of browser sessions. · `src/app/api/auth/extension-token/route.ts`
- [Chrome activity capture](https://github.com/kavinraj7305/workflowguard/blob/master/browser-extension/background.js): Extension background · Captures tab activity and sends authenticated events. · `browser-extension/background.js`
- [Activity resolver](https://github.com/kavinraj7305/workflowguard/blob/master/src/lib/api/resolve-activity-client.ts): Client integration · Boundary between extension events and application domain handling. · `src/lib/api/resolve-activity-client.ts`

## Connections

- Entry routes → Admin console: administrators
- Entry routes → Developer workspace: employees
- Entry routes → First-run setup: first run
- Route protection → Auth API: checks session
- Auth API → Sessions & passwords: creates and validates
- Auth API → Drizzle data layer: accounts
- First-run setup → Drizzle data layer: initial authority
- Admin console → Personnel APIs: manages workforce
- Personnel APIs → Drizzle data layer: organizations and people
- Developer workspace → Workspace activity: coordinates
- Workspace activity → Tickets API: task and activity actions
- Tickets API → Workflow access: authorizes
- Tickets API → Drizzle data layer: tickets and activity
- Chrome activity capture → Extension token API: obtains token
- Chrome activity capture → Activity resolver: tab events
- Activity resolver → Tickets API: resolved activity
- Developer workspace → Analytics APIs: personal metrics
- Admin console → Analytics APIs: organization reporting
- Analytics APIs → Analytics & reports: calculates
- Analytics & reports → Drizzle data layer: derives from
- Admin console → Admin AI assistant: assistant requests
- Admin AI assistant → Admin AI tools: constrained tools
- Admin AI assistant → LLM provider adapter: model calls
- Admin AI tools → Drizzle data layer: admin data
- Schema lifecycle → Drizzle data layer: evolves schema
