The Stats Service is a comprehensive backend API system built with Node.js, TypeScript, and Express.js that provides statistics and analytics for business operations. This project demonstrates the implementation of a multi-role business intelligence system with commercial, administrative, and decision-making capabilities.
- Commercial Dashboard: KPI tracking, sales analytics, and revenue reporting
- Device Management: IoT device tracking and monitoring
- Report Generation: Automated report creation in multiple formats (PDF, Excel, CSV)
- Zone Management: Geographic and organizational zone administration
- Real-time Analytics: Live dashboard updates and statistical calculations
- RESTful API: Well-documented API endpoints with Swagger integration
- Runtime: Node.js with TypeScript
- Framework: Express.js
- Database: PostgreSQL with Prisma ORM
- API Documentation: Swagger/OpenAPI
- Testing: Jest with Supertest
- Development: Nodemon for hot reloading
- MVC Architecture: Controllers, Services, and Routes separation
- Service Layer Pattern: Business logic abstraction
- Repository Pattern: Data access layer through Prisma
- Middleware Pattern: Request processing and validation
- Factory Pattern: Used in service instantiation
stats_service/
├── prisma/ # Database configuration and migrations
│ ├── schema.prisma # Database schema definition
│ └── migrations/ # Database migration files
├── src/ # Main source code
│ ├── index.ts # Application entry point
│ ├── config/ # Configuration files
│ │ └── swagger.ts # Swagger/OpenAPI configuration
│ ├── controllers/ # Request handlers and business logic
│ │ ├── dashboardController.ts # Dashboard analytics
│ │ ├── deviceController.ts # Device management
│ │ ├── reportController.ts # Report generation
│ │ ├── saleController.ts # Sales operations
│ │ ├── userController.ts # User management
│ │ ├── zoneController.ts # Zone administration
│ │ └── commercial/ # Commercial module
│ │ ├── clientController.ts # Client management
│ │ ├── dashboardController.ts # Commercial dashboard
│ │ ├── productController.ts # Product catalog
│ │ └── salesController.ts # Sales tracking
│ ├── services/ # Business logic layer
│ │ ├── deviceService.ts # Device business logic
│ │ ├── financialKpiService.ts # Financial calculations
│ │ ├── reportService.ts # Report generation logic
│ │ ├── saleService.ts # Sales processing
│ │ ├── userService.ts # User operations
│ │ ├── zoneService.ts # Zone management
│ │ └── commercial/ # Commercial services
│ ├── routes/ # API route definitions
│ │ ├── dashboardRoutes.ts # Dashboard endpoints
│ │ ├── deviceRoutes.ts # Device endpoints
│ │ ├── reportRoutes.ts # Report endpoints
│ │ ├── saleRoutes.ts # Sales endpoints
│ │ ├── userRoutes.ts # User endpoints
│ │ ├── zoneRoutes.ts # Zone endpoints
│ │ └── commercialRoutes.ts # Commercial endpoints
│ ├── middlewares/ # Express middlewares
│ │ └── authMiddleware.ts # Request validation
│ ├── lib/ # Utility libraries
│ │ └── prisma.ts # Prisma client configuration
│ └── tests/ # Test suites
│ ├── deviceRoutes.test.ts # Device API tests
│ ├── sale.test.ts # Sales logic tests
│ ├── userRoutes.test.ts # User API tests
│ └── zoneRoutes.test.ts # Zone API tests
├── scripts/ # Utility scripts
│ ├── seed.ts # Database seeding
│ ├── kpi_seed.ts # KPI data seeding
│ └── verify_users.ts # User verification
├── docs/ # Additional documentation
└── Configuration files # Package.json, tsconfig, etc.
Before running this project, ensure you have the following installed:
- Node.js: Version 16.x or higher
- npm: Version 8.x or higher (comes with Node.js)
- PostgreSQL: Version 12.x or higher
- Git: For version control
- Operating System: Windows, macOS, or Linux
- Memory: Minimum 4GB RAM
- Storage: At least 1GB free space
git clone <repository-url>
cd stats_servicenpm install- Download and install PostgreSQL from postgresql.org
- Create a new database for the project
Create a .env file in the root directory with the following variables:
# Database Configuration
DATABASE_URL="postgresql://username:password@localhost:5432/stats_service_db"
# Server Configuration
NODE_ENV="development"
PORT=5001
# Optional: Additional Configuration
CORS_ORIGIN="http://localhost:3000"# Generate Prisma client
npx prisma generate
# Run database migrations
npx prisma migrate dev
# Seed the database with initial data
npm run seed# Start with hot reloading
npm run devThe server will start on http://localhost:5001
# Build the project
npm run build
# Start the production server
npm run servenpm start- Run with ts-nodenpm run dev- Development mode with nodemonnpm run build- Compile TypeScript to JavaScriptnpm run serve- Run compiled JavaScriptnpm test- Run test suitenpm run seed- Seed database with sample data
Once the server is running, access the interactive API documentation at:
- Swagger UI:
http://localhost:5001/api-docs - JSON Schema:
http://localhost:5001/swagger.json
GET /users- List usersGET /users/:id- Get user detailsPOST /users- Create new userPUT /users/:id- Update user
GET /dashboard/stats- Get dashboard KPIsGET /dashboard/sales-chart- Sales trend dataGET /dashboard/revenue-growth- Revenue analytics
GET /commercial/dashboard/stats- Commercial KPIsGET /commercial/clients- Client managementGET /commercial/products- Product catalogGET /commercial/sales- Sales tracking
GET /reports/sales- Sales reportsGET /reports/devices- Device reportsPOST /reports/generate- Generate custom reports
GET /devices- List all devicesPOST /devices- Create new devicePUT /devices/:id- Update deviceDELETE /devices/:id- Remove device
# Run all tests
npm test
# Run tests with coverage
npm test -- --coverage
# Run specific test file
npm test -- deviceRoutes.test.ts- Unit Tests: Individual function and service testing
- Integration Tests: API endpoint testing
- Database Tests: Data persistence and retrieval testing
deviceRoutes.test.ts- Device API endpointssale.test.ts- Sales business logicuserRoutes.test.ts- User authentication and managementzoneRoutes.test.ts- Zone management functionality