How sadmanarian004/fu_prompt_coder_smuct works
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: ai.controller.ts AI insight endpoint · sale.routes.ts Domain routes · apiClient.ts Client auth & API boundary · schema.prisma Prisma data boundary
Components 18
Next.js client
Authenticated dashboard and public app routes.
- Public auth routes
frontend/src/app/(auth)/login/page.tsx - Dashboard shell & modules · Inventory, customer, and sales workflows run within the protected shell.
frontend/src/app/dashboard/layout.tsx - Client auth & API boundary
frontend/src/lib/apiClient.ts - AI chat client
frontend/src/app/components/ai/ChatPanel.tsx - Client localization
frontend/src/context/LanguageContext.tsx
Express API
REST request handling and tenant-scoped business logic.
- Express application · Mounts versioned domain routes for local and serverless execution.
backend/src/app.ts - Vercel API adapter
backend/api/index.ts - Domain routes · Auth, sale, and AI endpoints enter the middleware and controller pipeline.
backend/src/routes/sale.routes.ts - JWT and tenant scope · Derives authenticated identity and shop scope; client shop IDs are not trusted.
backend/src/middleware/tenant.middleware.ts - Authentication service
backend/src/services/auth.service.ts - Sales, invoice & credit workflow · Recalculates stock and prices, persists sale snapshots, and updates credit dues.
backend/src/controllers/sale.controller.ts - AI insight endpoint
backend/src/controllers/ai.controller.ts
Data and AI
Persistence, grounded insight generation, and external services.
- Shop aggregate snapshot · Builds tenant-scoped business context before model calls.
backend/src/services/shopSnapshot.service.ts - Gemini integration · Produces localized natural-language insights from structured context.
backend/src/services/gemini.service.ts - Prisma data boundary · Defines transactional retail and AI insight-log persistence.
backend/prisma/schema.prisma - Aiven MySQL
- Google Gemini API
Other components
- Vercel · Hosts the Next.js client and Express serverless handler.
backend/vercel.json
Connections 19
- 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