This update introduces several utility functions in `utils.js` to improve the handling of ObjectId paths, schema types, and filtering logic. Key additions include `isObjectIdPath`, `getBaseProperty`, and `getFilterFieldKind`, which enhance the flexibility of property handling. Additionally, all route handlers in the finance and inventory modules have been refactored to support asynchronous operations, ensuring better performance and responsiveness when processing requests.
FarmControl API
A comprehensive REST API for managing 3D printing farms, inventory, and production workflows. Built with Node.js, Express, and MongoDB, featuring authentication with Keycloak and comprehensive logging.
🚀 Features
- Production Management: Manage printers, jobs, G-code files, and filaments
- Inventory Control: Track part stocks, filament stocks, and stock events
- User Management: Role-based access control with admin, creator, and member roles
- Audit Logging: Comprehensive audit trails for all operations
- Authentication: Secure authentication using Keycloak
- Real-time Monitoring: Spotlight functionality for real-time system monitoring
- Scheduled Operations: Automated data reseeding and maintenance tasks
📋 Prerequisites
- Node.js (v16 or higher)
- MongoDB
- Keycloak server (for authentication)
- npm or yarn package manager
🛠️ Installation
-
Clone the repository
git clone <repository-url> cd farmcontrol-api -
Install dependencies
npm install -
Environment Configuration Create a
.envfile in the root directory with the following variables:# Server Configuration PORT=8080 NODE_ENV=development # Database Configuration DB_LINK=mongodb://localhost:27017 # Keycloak Configuration KEYCLOAK_AUTH_SERVER_URL=http://localhost:8080/auth KEYCLOAK_REALM=your-realm KEYCLOAK_CLIENT_ID=your-client-id KEYCLOAK_SECRET=your-client-secret # Application Configuration APP_URL_CLIENT=http://localhost:5173 LOG_LEVEL=info # Optional: Scheduled Operations SCHEDULE_HOUR=24 -
Database Setup
# Seed the database with initial data npm run seed # Clear database (if needed) npm run clear -
Start the server
# Development mode with auto-reload npm run dev # Production mode npm start
📚 API Endpoints
Authentication
POST /auth/login- User loginPOST /auth/logout- User logoutGET /auth/verify- Verify authentication status
Production
GET /printers- Get all printersPOST /printers- Create new printerGET /jobs- Get all jobsPOST /jobs- Create new jobGET /gcodefiles- Get all G-code filesPOST /gcodefiles- Upload G-code fileGET /filaments- Get all filaments
Inventory
GET /partstocks- Get part stock levelsPOST /partstocks- Update part stockGET /filamentstocks- Get filament stock levelsPOST /filamentstocks- Update filament stockGET /stockevents- Get stock eventsGET /stockaudits- Get stock audits
Management
GET /users- Get all usersPOST /users- Create new userGET /users/:id- Get user by IDPUT /users/:id- Update userDELETE /users/:id- Delete userGET /parts- Get all partsPOST /parts- Create new partGET /products- Get all productsPOST /products- Create new productGET /vendors- Get all vendorsPOST /vendors- Create new vendorGET /materials- Get all materialsPOST /materials- Create new materialGET /auditlogs- Get audit logsGET /notetypes- Get note typesPOST /notetypes- Create note type
Miscellaneous
GET /spotlight- Get system spotlight dataGET /notes- Get all notesPOST /notes- Create new note
🏗️ Project Structure
farmcontrol-api/
├── src/
│ ├── database/ # Database connection and utilities
│ │ ├── inventory/ # Inventory-related routes
│ │ ├── management/ # Management routes
│ │ ├── misc/ # Miscellaneous routes
│ │ └── production/ # Production-related routes
│ ├── schemas/ # MongoDB schemas
│ ├── services/ # Business logic services
│ ├── keycloak.js # Keycloak configuration
│ └── index.js # Main application entry point
├── images/ # Static images
├── package.json # Project dependencies and scripts
└── README.md # This file
🔧 Configuration
Keycloak Setup
The application uses Keycloak for authentication. Configure your Keycloak server with:
- A realm for your application
- A client with confidential access type
- Appropriate roles (admin, creator, member)
- Valid redirect URIs
Database Configuration
The application uses MongoDB. Ensure your MongoDB instance is running and accessible at the configured DB_LINK.
CORS Configuration
Allowed request origins are configured via server.corsOrigins in config.json. When omitted, the API falls back to app.urlClient and app.urlElectronClient.
🚀 Deployment
Development
npm run dev
Production
npm start
Docker (if applicable)
docker build -t farmcontrol-api .
docker run -p 8080:8080 farmcontrol-api
📊 Monitoring and Logging
The application uses log4js for comprehensive logging. Log levels can be configured via the LOG_LEVEL environment variable:
trace- Most verbosedebug- Debug informationinfo- General informationwarn- Warning messageserror- Error messagesfatal- Fatal errors only
🔄 Scheduled Operations
The application includes scheduled operations for data maintenance:
- Automatic database reseeding (configurable via
SCHEDULE_HOUR) - Audit log maintenance
- System health checks
🤝 Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📝 License
This project is licensed under the ISC License.
🆘 Support
For support and questions:
- Check the API documentation
- Review the audit logs for debugging
- Contact the development team
🔄 Version History
- v1.0.0 - Initial release with core functionality
- Production management
- Inventory control
- User management
- Authentication with Keycloak
- Comprehensive audit logging