# SmartBotic CRM A flexible, schema-driven CRM foundation built with C++20 and React. ## Overview SmartBotic CRM consists of three main components: 1. **Database Node**: An in-memory document database with gRPC interface, supporting collections, JSON documents with metadata, relations, encryption, TTL, and persistent snapshots. 2. **Web Server Node**: A REST API server with WebSocket support that communicates with the database via gRPC, handling authentication (JWT + API keys), workspace/user management, and real-time updates. 3. **Web UI**: A React + TypeScript + Vite + TailwindCSS admin dashboard for managing workspaces, users, collections, and schema-defined views. ## Features - Multi-tenant workspace support - JWT and API key authentication - Schema-driven dynamic forms and views - Real-time updates via WebSocket - Field-level encryption support - Persistent snapshots for data durability - Role-based access control ## Requirements - GCC 11+ or Clang 14+ (C++20 support) - CMake 3.20+ - Ninja build system - Node.js 18+ with pnpm - libwebsockets development package ### Installing Dependencies (Ubuntu/Debian) ```bash # Build tools sudo apt install cmake ninja-build build-essential pkg-config # libwebsockets sudo apt install libwebsockets-dev # Node.js (via nvm recommended) curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash nvm install 18 npm install -g pnpm ``` ## Quick Start ### 1. Setup and Build ```bash # Clone the repository git clone https://github.com/smartbotic/smartbotic-crm.git cd smartbotic-crm # Run setup script ./scripts/setup.sh ``` ### 2. Start Services Open three terminal windows: **Terminal 1 - Database:** ```bash ./scripts/run-db.sh ``` **Terminal 2 - Webserver:** ```bash ./scripts/run-server.sh ``` **Terminal 3 - WebUI Development:** ```bash cd webui pnpm dev ``` ### 3. Bootstrap the System On first run, create the admin user: ```bash curl -X POST http://localhost:18080/api/bootstrap \ -H "Content-Type: application/json" \ -d '{"email": "admin@example.com", "password": "your-password", "name": "Admin"}' ``` ### 4. Access the Application - **WebUI**: http://localhost:3200 - **API**: http://localhost:18080 - **WebSocket**: ws://localhost:18081/ws ## Development with Systemd User Services For persistent local testing, use systemd user services instead of terminal scripts. ### Setup User Services 1. Create the config directory: ```bash mkdir -p ~/.config/systemd/user ~/.config/smartbotic-crm ~/.local/share/smartbotic-crm ``` 2. Copy and customize the service files: ```bash # Database service cat > ~/.config/systemd/user/smartbotic-crm-database.service << 'EOF' [Unit] Description=SmartBotic CRM Database (Development) [Service] Type=simple WorkingDirectory=/data/smartbotic-crm ExecStart=/data/smartbotic-crm/build/database/smartbotic-crm-database --data-dir %h/.local/share/smartbotic-crm Restart=on-failure Environment="SMARTBOTIC_DB_PORT=50151" [Install] WantedBy=default.target EOF # Webserver service cat > ~/.config/systemd/user/smartbotic-crm-webserver.service << 'EOF' [Unit] Description=SmartBotic CRM Webserver (Development) After=smartbotic-crm-database.service Requires=smartbotic-crm-database.service [Service] Type=simple WorkingDirectory=/data/smartbotic-crm ExecStart=/data/smartbotic-crm/build/webserver/smartbotic-crm-webserver EnvironmentFile=%h/.config/smartbotic-crm/server.env Restart=on-failure [Install] WantedBy=default.target EOF ``` 3. Create the config file: ```bash cat > ~/.config/smartbotic-crm/server.env << 'EOF' SMARTBOTIC_SERVER_JWT_SECRET=dev-testing-secret-change-in-production SMARTBOTIC_SERVER_STATIC_PATH=/data/smartbotic-crm/webui/dist EOF ``` 4. Enable and start services: ```bash systemctl --user daemon-reload systemctl --user enable smartbotic-crm-database smartbotic-crm-webserver systemctl --user start smartbotic-crm-database smartbotic-crm-webserver ``` 5. Check status: ```bash systemctl --user status smartbotic-crm-database smartbotic-crm-webserver ``` 6. View logs: ```bash journalctl --user -u smartbotic-crm-database -f journalctl --user -u smartbotic-crm-webserver -f ``` ### Data Locations (User Services) - **Config**: `~/.config/smartbotic-crm/` - **Data**: `~/.local/share/smartbotic-crm/` - **Logs**: `journalctl --user` ## Project Structure ``` smartbotic-crm/ ├── database/ # Database node (C++) │ ├── include/ # Header files │ └── src/ # Source files ├── webserver/ # Webserver node (C++) │ ├── include/ # Header files │ └── src/ # Source files ├── webui/ # Web UI (React) │ ├── src/ # React components │ └── dist/ # Built static files ├── proto/ # Protocol Buffer definitions ├── common/ # Shared utilities ├── scripts/ # Utility scripts ├── systemd/ # Systemd service files ├── build/ # CMake build output └── data/ # Runtime data directory ``` ## Configuration ### Database Node Environment variables: - `SMARTBOTIC_DB_PORT` - gRPC port (default: 50151) - `SMARTBOTIC_DB_DATA_DIR` - Data directory path ### Webserver Node Environment variables: - `SMARTBOTIC_SERVER_HTTP_PORT` - HTTP port (default: 18080) - `SMARTBOTIC_SERVER_WS_PORT` - WebSocket port (default: 18081) - `SMARTBOTIC_SERVER_DATABASE_HOST` - Database host (default: localhost) - `SMARTBOTIC_SERVER_DATABASE_PORT` - Database port (default: 50151) - `SMARTBOTIC_SERVER_JWT_SECRET` - JWT signing secret - `SMARTBOTIC_SERVER_STATIC_PATH` - Static files directory ## API Endpoints ### Authentication - `POST /api/auth/login` - Login with email/password - `POST /api/auth/refresh` - Refresh access token - `POST /api/auth/logout` - Logout ### Users - `POST /api/users` - Create user (superadmin) - `GET /api/users` - List users - `GET /api/users/:id` - Get user - `PATCH /api/users/:id` - Update user - `DELETE /api/users/:id` - Delete user ### Workspaces - `POST /api/workspaces` - Create workspace - `GET /api/workspaces` - List workspaces - `GET /api/workspaces/:id` - Get workspace - `PATCH /api/workspaces/:id` - Update workspace - `DELETE /api/workspaces/:id` - Delete workspace ### Collections - `POST /api/workspaces/:wid/collections` - Create collection - `GET /api/workspaces/:wid/collections` - List collections - `GET /api/workspaces/:wid/collections/:name` - Get collection - `PUT /api/workspaces/:wid/collections/:name` - Update collection - `DELETE /api/workspaces/:wid/collections/:name` - Delete collection ### Documents - `POST /api/workspaces/:wid/collections/:name/documents` - Create document - `GET /api/workspaces/:wid/collections/:name/documents` - List documents - `GET /api/workspaces/:wid/collections/:name/documents/:id` - Get document - `PUT /api/workspaces/:wid/collections/:name/documents/:id` - Update document - `DELETE /api/workspaces/:wid/collections/:name/documents/:id` - Delete document ### Views - `POST /api/workspaces/:wid/views` - Create view - `GET /api/workspaces/:wid/views` - List views - `GET /api/workspaces/:wid/views/:id` - Get view - `PATCH /api/workspaces/:wid/views/:id` - Update view - `DELETE /api/workspaces/:wid/views/:id` - Delete view ## WebSocket Protocol Connect to `ws://localhost:18081/ws` and authenticate: ```json { "type": "auth", "token": "your-jwt-token" } ``` Subscribe to collection changes: ```json { "type": "subscribe", "workspace_id": "...", "collection": "name" } ``` Receive document events: ```json { "type": "document", "action": "create|update|delete", "workspace_id": "...", "collection": "...", "document_id": "...", "data": { ... } } ``` ## Development ### Building ```bash # Configure cmake --preset release # Build cmake --build build -j$(nproc) ``` ### Code Quality ```bash # C++ formatting clang-format -i database/src/*.cpp webserver/src/*.cpp # C++ linting clang-tidy database/src/*.cpp webserver/src/*.cpp # WebUI cd webui pnpm typecheck pnpm lint ``` ## Production Deployment ### Using Systemd Copy the service files and customize: ```bash sudo cp systemd/*.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl enable smartbotic-database smartbotic-webserver sudo systemctl start smartbotic-database smartbotic-webserver ``` ### Environment Variables Create `/etc/smartbotic/server.env`: ```bash SMARTBOTIC_SERVER_JWT_SECRET=your-secure-secret-key SMARTBOTIC_SERVER_STATIC_PATH=/opt/smartbotic-crm/webui/dist ``` ## License Proprietary - All rights reserved.