# sadmanarian004/fu_prompt_coder_smuct architecture

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

- Interactive diagram: https://gitdiagram.com/sadmanarian004/fu_prompt_coder_smuct
- Repository: https://github.com/sadmanarian004/fu_prompt_coder_smuct

## Overview

#### 1. Purpose and deployment shape
DokanKhata AI is a multi-tenant retail-management SaaS for inventory, sales, invoices, customer credit/due tracking, analytics, and Gemini-backed business insights. It is split into a Next.js client and an Express/Prisma API, intended for Vercel serverless deployment with MySQL hosted on Aiven.
- `frontend/src/app/page.tsx`
- `backend/api/index.ts`
- `backend/vercel.json`

#### 2. Frontend application and entry flow
The Next.js App Router frontend provides public landing/auth routes and a protected dashboard shell with module pages. UI state and API access are centralized enough to make the frontend a thin authenticated client over REST.
- App roots and dashboard shell: `frontend/src/app/layout.tsx`, `frontend/src/app/dashboard/layout.tsx`
- Auth entry points: `frontend/src/app/(auth)/login/page.tsx`, `frontend/src/app/(auth)/register/page.tsx`
- HTTP/auth boundary: `frontend/src/lib/apiClient.ts`, `frontend/src/hooks/useAuth.ts`

#### 3. API composition and request pipeline
The Express application mounts versioned domain routes, then delegates request handling to controllers and services. Runtime entry points support local Node execution and Vercel’s serverless handler.
- Application/bootstrap: `backend/src/app.ts`, `backend/src/index.ts`, `backend/src/server.ts`
- Serverless adapter: `backend/api/index.ts`
- Route modules: `backend/src/routes/auth.routes.ts`, `backend/src/routes/sale.routes.ts`, `backend/src/routes/ai.routes.ts`

#### 4. Identity, roles, and tenant isolation
Authentication issues/consumes JWTs, while request middleware establishes authenticated identity and shop scope. Tenant scoping is the critical data-isolation boundary: all shop-owned resource access must derive scope from middleware rather than client-provided shop identifiers.
- Auth implementation: `backend/src/controllers/auth.controller.ts`, `backend/src/services/auth.service.ts`
- Middleware chain: `backend/src/middleware/auth.middleware.ts`, `backend/src/middleware/tenant.middleware.ts`
- Request typing: `backend/src/types/express.d.ts`

#### 5. Transactional retail domains
Products, customers, sales, payments, and invoices form the core operational model. Sales are the principal write workflow: server-side logic should recalculate product prices/stock, persist a sale/invoice snapshot, and update customer dues for credit sales. The frontend exposes these domains as dashboard modules and modal-based entry flows.
- Persistent model and migrations: `backend/prisma/schema.prisma`, `backend/prisma/migrations/20260715141147_init/migration.sql`
- Sales/invoice boundary: `backend/src/controllers/sale.controller.ts`, `backend/src/controllers/invoice.controller.ts`
- Inventory and credit UI: `frontend/src/app/dashboard/inventory/page.tsx`, `frontend/src/app/dashboard/customers/page.tsx`, `frontend/src/app/dashboard/sales/page.tsx`

#### 6. AI insight pipeline
AI requests do not send raw database access to Gemini. The backend first builds a shop-scoped aggregate snapshot, passes structured context to the Gemini service, and returns a localized natural-language response. Daily summaries and/or query results are cached in the database through the AI insight log model to constrain external API cost.
- AI endpoint/controller: `backend/src/routes/ai.routes.ts`, `backend/src/controllers/ai.controller.ts`
- Grounding and model integration: `backend/src/services/shopSnapshot.service.ts`, `backend/src/services/gemini.service.ts`
- Client integration: `frontend/src/hooks/useAIInsights.tsx`, `frontend/src/app/components/ai/ChatPanel.tsx`

#### 7. Infrastructure and external boundaries
Node.js/Express and Next.js are TypeScript-based application runtimes; Prisma mediates all MySQL access. Material external services are Aiven MySQL, Google Gemini API, and Vercel. Environment/database initialization is separated from business controllers.
- Database configuration: `backend/src/config/db.ts`, `backend/src/config/env.ts`
- Prisma schema/seed boundary: `backend/prisma/schema.prisma`, `backend/prisma/seed.ts`
- UI localization is client-side through `frontend/src/context/LanguageContext.tsx`.

Main entry points: [`backend/src/controllers/ai.controller.ts`](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/controllers/ai.controller.ts) (AI insight endpoint), [`backend/src/routes/sale.routes.ts`](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/routes/sale.routes.ts) (Domain routes), [`frontend/src/lib/apiClient.ts`](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/lib/apiClient.ts) (Client auth & API boundary), [`backend/prisma/schema.prisma`](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/prisma/schema.prisma) (Prisma data boundary)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Next.js client"]
  node_public_routes["Public auth routes<br/>Next.js pages<br/>[page.tsx]"]
  node_dashboard["Dashboard shell &amp; modules<br/>Next.js App Router<br/>[layout.tsx]"]
  node_client_auth["Client auth &amp; API boundary<br/>[apiClient.ts]"]
  node_ai_chat["AI chat client<br/>React integration<br/>[ChatPanel.tsx]"]
  node_localization["Client localization<br/>React context"]
end

subgraph group_api["Express API"]
  node_express_app["Express application<br/>API bootstrap<br/>[app.ts]"]
  node_serverless_adapter{{"Vercel API adapter<br/>Serverless handler<br/>[index.ts]"}}
  node_domain_routes["Domain routes<br/>Express routes<br/>[sale.routes.ts]"]
  node_auth_tenant["JWT and tenant scope<br/>Request middleware"]
  node_auth_service["Authentication service<br/>Identity service<br/>[auth.service.ts]"]
  node_retail_workflow["Sales, invoice &amp; credit workflow<br/>Domain controllers<br/>[sale.controller.ts]"]
  node_ai_controller["AI insight endpoint<br/>AI controller<br/>[ai.controller.ts]"]
end

subgraph group_data["Data and AI"]
  node_shop_snapshot["Shop aggregate snapshot<br/>Grounding service"]
  node_gemini_service["Gemini integration<br/>AI service<br/>[gemini.service.ts]"]
  node_prisma["Prisma data boundary<br/>ORM and schema<br/>[schema.prisma]"]
  node_mysql[("Aiven MySQL<br/>Managed database")]
  node_gemini_api{{"Google Gemini API<br/>External AI API"}}
end

node_vercel{{"Vercel<br/>Deployment platform<br/>[vercel.json]"}}

node_public_routes -->|"sign in / register"| node_client_auth
node_dashboard -->|"REST requests"| node_client_auth
node_ai_chat -->|"AI requests"| node_client_auth
node_localization -->|"localized UI"| node_dashboard
node_client_auth -->|"JWT-authenticated REST"| node_domain_routes
node_vercel -->|"hosts"| node_serverless_adapter
node_serverless_adapter -->|"adapts request"| node_express_app
node_express_app -->|"mounts"| node_domain_routes
node_domain_routes -->|"protects scoped routes"| node_auth_tenant
node_domain_routes -->|"auth endpoints"| node_auth_service
node_auth_tenant -->|"tenant-scoped request"| node_retail_workflow
node_auth_tenant -->|"tenant-scoped request"| node_ai_controller
node_retail_workflow -->|"sales, invoices, stock, dues"| node_prisma
node_ai_controller -->|"builds context"| node_shop_snapshot
node_shop_snapshot -->|"aggregate shop data"| node_prisma
node_ai_controller -->|"structured prompt"| node_gemini_service
node_gemini_service -->|"model request"| node_gemini_api
node_ai_controller -->|"cache insight log"| node_prisma
node_prisma -->|"queries and transactions"| node_mysql

click node_public_routes "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/app/(auth)/login/page.tsx"
click node_dashboard "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/app/dashboard/layout.tsx"
click node_client_auth "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/lib/apiClient.ts"
click node_ai_chat "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/app/components/ai/ChatPanel.tsx"
click node_express_app "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/app.ts"
click node_serverless_adapter "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/api/index.ts"
click node_domain_routes "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/routes/sale.routes.ts"
click node_auth_tenant "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/middleware/tenant.middleware.ts"
click node_auth_service "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/services/auth.service.ts"
click node_retail_workflow "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/controllers/sale.controller.ts"
click node_ai_controller "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/controllers/ai.controller.ts"
click node_shop_snapshot "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/services/shopSnapshot.service.ts"
click node_gemini_service "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/services/gemini.service.ts"
click node_prisma "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/prisma/schema.prisma"
click node_vercel "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/vercel.json"
click node_localization "https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/context/LanguageContext.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_public_routes,node_dashboard,node_client_auth,node_ai_chat,node_localization toneBlue
class node_express_app,node_serverless_adapter,node_domain_routes,node_auth_tenant,node_auth_service,node_retail_workflow,node_ai_controller toneAmber
class node_shop_snapshot,node_gemini_service,node_prisma,node_mysql,node_gemini_api toneMint
class node_vercel toneNeutral
```

## Components

### Next.js client

Authenticated dashboard and public app routes.

- [Public auth routes](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/app/(auth)/login/page.tsx): Next.js pages · `frontend/src/app/(auth)/login/page.tsx`
- [Dashboard shell & modules](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/app/dashboard/layout.tsx): Next.js App Router · Inventory, customer, and sales workflows run within the protected shell. · `frontend/src/app/dashboard/layout.tsx`
- [Client auth & API boundary](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/lib/apiClient.ts): React hooks / HTTP client · `frontend/src/lib/apiClient.ts`
- [AI chat client](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/app/components/ai/ChatPanel.tsx): React integration · `frontend/src/app/components/ai/ChatPanel.tsx`
- [Client localization](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/frontend/src/context/LanguageContext.tsx): React context · `frontend/src/context/LanguageContext.tsx`

### Express API

REST request handling and tenant-scoped business logic.

- [Express application](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/app.ts): API bootstrap · Mounts versioned domain routes for local and serverless execution. · `backend/src/app.ts`
- [Vercel API adapter](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/api/index.ts): Serverless handler · `backend/api/index.ts`
- [Domain routes](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/routes/sale.routes.ts): Express routes · Auth, sale, and AI endpoints enter the middleware and controller pipeline. · `backend/src/routes/sale.routes.ts`
- [JWT and tenant scope](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/middleware/tenant.middleware.ts): Request middleware · Derives authenticated identity and shop scope; client shop IDs are not trusted. · `backend/src/middleware/tenant.middleware.ts`
- [Authentication service](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/services/auth.service.ts): Identity service · `backend/src/services/auth.service.ts`
- [Sales, invoice & credit workflow](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/controllers/sale.controller.ts): Domain controllers · Recalculates stock and prices, persists sale snapshots, and updates credit dues. · `backend/src/controllers/sale.controller.ts`
- [AI insight endpoint](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/controllers/ai.controller.ts): AI controller · `backend/src/controllers/ai.controller.ts`

### Data and AI

Persistence, grounded insight generation, and external services.

- [Shop aggregate snapshot](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/services/shopSnapshot.service.ts): Grounding service · Builds tenant-scoped business context before model calls. · `backend/src/services/shopSnapshot.service.ts`
- [Gemini integration](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/src/services/gemini.service.ts): AI service · Produces localized natural-language insights from structured context. · `backend/src/services/gemini.service.ts`
- [Prisma data boundary](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/prisma/schema.prisma): ORM and schema · Defines transactional retail and AI insight-log persistence. · `backend/prisma/schema.prisma`
- **Aiven MySQL**: Managed database
- **Google Gemini API**: External AI API

### Other components

- [Vercel](https://github.com/sadmanarian004/fu_prompt_coder_smuct/blob/main/backend/vercel.json): Deployment platform · Hosts the Next.js client and Express serverless handler. · `backend/vercel.json`

## Connections

- Public auth routes → Client auth & API boundary: sign in / register
- Dashboard shell & modules → Client auth & API boundary: REST requests
- AI chat client → Client auth & API boundary: AI requests
- Client localization → Dashboard shell & modules: localized UI
- Client auth & API boundary → Domain routes: JWT-authenticated REST
- Vercel → Vercel API adapter: hosts
- Vercel API adapter → Express application: adapts request
- Express application → Domain routes: mounts
- Domain routes → JWT and tenant scope: protects scoped routes
- Domain routes → Authentication service: auth endpoints
- JWT and tenant scope → Sales, invoice & credit workflow: tenant-scoped request
- JWT and tenant scope → AI insight endpoint: tenant-scoped request
- Sales, invoice & credit workflow → Prisma data boundary: sales, invoices, stock, dues
- AI insight endpoint → Shop aggregate snapshot: builds context
- Shop aggregate snapshot → Prisma data boundary: aggregate shop data
- AI insight endpoint → Gemini integration: structured prompt
- Gemini integration → Google Gemini API: model request
- AI insight endpoint → Prisma data boundary: cache insight log
- Prisma data boundary → Aiven MySQL: queries and transactions
