# AccManager Backend Setup Guide ## 📋 Database Information **Server:** 172.20.235.176 **Database:** AccManager **User:** sa **Password:** robotics@2020 ## 📊 Database Structure ### Tables Created #### 1. **Users** - Quản lý người dùng - `UserId` (INT) - Primary Key - `Username` (NVARCHAR) - Unique - `Password` (NVARCHAR) - `Email` (NVARCHAR) - `FullName` (NVARCHAR) - `Role` (NVARCHAR) - admin, user, viewer - `Status` (NVARCHAR) - Active/Inactive - `CreatedDate` (DATETIME) - `LastLogin` (DATETIME) - `IsActive` (BIT) #### 2. **Applications** - Danh sách ứng dụng - `AppId` (INT) - Primary Key - `Name` (NVARCHAR) - `Type` (NVARCHAR) - Cloud, VCS, Collaboration, Infra - `Status` (NVARCHAR) - online/offline - `Icon` (NVARCHAR) - `Description` (NVARCHAR) - `CreatedDate` (DATETIME) - `UpdatedDate` (DATETIME) #### 3. **Accounts** - Tài khoản ứng dụng - `AccountId` (INT) - Primary Key - `UserId` (INT) - Foreign Key - `AppId` (INT) - Foreign Key - `AccountUsername` (NVARCHAR) - `AccountPassword` (NVARCHAR) - `Email` (NVARCHAR) - `AccessLevel` (NVARCHAR) - `Status` (NVARCHAR) - `Notes` (NVARCHAR) - `CreatedDate` (DATETIME) - `UpdatedDate` (DATETIME) #### 4. **AuditLog** - Nhật ký hoạt động - `LogId` (INT) - Primary Key - `UserId` (INT) - Foreign Key - `Action` (NVARCHAR) - INSERT, UPDATE, DELETE - `TableName` (NVARCHAR) - `RecordId` (INT) - `OldValue` (NVARCHAR) - `NewValue` (NVARCHAR) - `Timestamp` (DATETIME) ## 🔐 Default Admin Account **Username:** admin **Password:** admin **Role:** admin **Status:** Active ## 🚀 Installation & Setup ### 1. Install Node.js Dependencies ```bash npm install ``` ### 2. Run Backend Server ```bash npm start ``` Server sẽ chạy tại: **http://localhost:3000** ### 3. Kiểm tra Database Connection ```bash curl http://localhost:3000/api/health ``` Response: ```json { "status": "OK", "database": "Connected" } ``` ## 📡 API Endpoints ### Authentication #### Login ```bash POST /api/auth/login Content-Type: application/json { "username": "admin", "password": "admin" } ``` ### Users #### Get All Users ```bash GET /api/users ``` #### Get User Details ```bash GET /api/users/:id ``` #### Create New User ```bash POST /api/users Content-Type: application/json { "username": "newuser", "password": "password123", "email": "user@example.com", "fullname": "Full Name", "role": "user" } ``` ### Applications #### Get All Applications ```bash GET /api/applications ``` #### Create Application ```bash POST /api/applications Content-Type: application/json { "name": "New App", "type": "Cloud", "status": "online", "icon": "cloud", "description": "Application description" } ``` ### Accounts #### Get User Accounts ```bash GET /api/accounts/user/:userId ``` #### Create Account ```bash POST /api/accounts Content-Type: application/json { "userId": 1, "appId": 1, "accountUsername": "account_user", "accountPassword": "account_pass", "email": "account@example.com", "accessLevel": "Admin", "notes": "Account notes" } ``` ### Database Info #### Get Database Statistics ```bash GET /api/database/info ``` Response: ```json { "success": true, "database": "AccManager", "server": "172.20.235.176", "tables": [ { "TableName": "Accounts", "ColumnCount": 11 }, { "TableName": "Applications", "ColumnCount": 7 }, { "TableName": "AuditLog", "ColumnCount": 8 }, { "TableName": "Users", "ColumnCount": 10 } ], "statistics": { "users": 1, "applications": 4, "accounts": 0 } } ``` ## 🔧 Frontend Integration Update frontend API calls to use the backend server: ```javascript // Change from localStorage to API calls const API_URL = 'http://localhost:3000/api'; // Example: Login async function login(username, password) { const response = await fetch(`${API_URL}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) }); return response.json(); } // Example: Get user accounts async function getAccounts(userId) { const response = await fetch(`${API_URL}/accounts/user/${userId}`); return response.json(); } ``` ## 📝 Initial Data Created ### Users - **admin** (admin role) ### Applications - AWS (Cloud) - online - GitHub (VCS) - online - Google Workspace (Collaboration) - online - Nginx Proxy (Infra) - offline ## 🐛 Troubleshooting ### Connection Error ``` Database connection failed: Error ``` **Solution:** - Kiểm tra SQL Server đang chạy - Kiểm tra network connectivity đến 172.20.235.176 - Kiểm tra username/password đúng - Kiểm tra SQL Server Authentication được enable ### Port Already in Use ``` listen EADDRINUSE: address already in use :::3000 ``` **Solution:** ```bash # Change port in .env PORT=3001 ``` ### MSSQL Module Not Found ```bash npm install mssql ``` ## 📚 References - [ExpressJS Documentation](https://expressjs.com/) - [MSSQL Package](https://github.com/tediousjs/node-mssql) - [SQL Server Documentation](https://docs.microsoft.com/en-us/sql/) --- **Status:** ✓ Database created, ✓ Tables created, ✓ Admin user created, ✓ Backend ready