Spaces:
Paused
Paused
Upload 15 files
Browse files- README.md +137 -9
- package-lock.json +1 -224
- package.json +2 -3
- public/admin.html +241 -42
- public/server-logs.html +52 -27
- public/status.html +171 -60
- server.js +263 -72
README.md
CHANGED
|
@@ -1,9 +1,137 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SLD GC Portal
|
| 2 |
+
|
| 3 |
+
A modern, secure, and user-friendly gift card management system that allows users to convert their gift cards to cash through a streamlined process.
|
| 4 |
+
|
| 5 |
+
## 🌟 Features
|
| 6 |
+
|
| 7 |
+
- **Secure Authentication**
|
| 8 |
+
- Email-based login/registration
|
| 9 |
+
- OTP verification system
|
| 10 |
+
- Session management
|
| 11 |
+
|
| 12 |
+
- **Gift Card Management**
|
| 13 |
+
- Support for multiple gift card types:
|
| 14 |
+
- Roblox (100, 400, 1000)
|
| 15 |
+
- Overwatch (200, 500, 1000)
|
| 16 |
+
- Amazon (50, 100, 500)
|
| 17 |
+
- Video proof upload system
|
| 18 |
+
- Real-time status tracking
|
| 19 |
+
|
| 20 |
+
- **Payment Options**
|
| 21 |
+
- UPI transfers
|
| 22 |
+
- Paytm integration
|
| 23 |
+
- Crypto transfer support
|
| 24 |
+
|
| 25 |
+
- **Admin Dashboard**
|
| 26 |
+
- Real-time submission monitoring
|
| 27 |
+
- Status management
|
| 28 |
+
- User communication system
|
| 29 |
+
- Video proof verification
|
| 30 |
+
|
| 31 |
+
- **Modern UI/UX**
|
| 32 |
+
- Responsive design
|
| 33 |
+
- Dark theme with glass morphism
|
| 34 |
+
- Animated transitions
|
| 35 |
+
- Interactive elements
|
| 36 |
+
|
| 37 |
+
## 🚀 Getting Started
|
| 38 |
+
|
| 39 |
+
### Prerequisites
|
| 40 |
+
|
| 41 |
+
- Node.js (v14 or higher)
|
| 42 |
+
- npm or yarn
|
| 43 |
+
- Modern web browser
|
| 44 |
+
|
| 45 |
+
### Installation
|
| 46 |
+
|
| 47 |
+
1. Clone the repository:
|
| 48 |
+
```bash
|
| 49 |
+
git clone https://github.com/yourusername/sld-gc-portal.git
|
| 50 |
+
cd sld-gc-portal
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
2. Install dependencies:
|
| 54 |
+
```bash
|
| 55 |
+
npm install
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
3. Set up environment variables:
|
| 59 |
+
```bash
|
| 60 |
+
cp .env.example .env
|
| 61 |
+
```
|
| 62 |
+
Edit `.env` with your configuration.
|
| 63 |
+
|
| 64 |
+
4. Start the development server:
|
| 65 |
+
```bash
|
| 66 |
+
npm run dev
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
## 📁 Project Structure
|
| 70 |
+
|
| 71 |
+
```
|
| 72 |
+
sld-gc-portal/
|
| 73 |
+
├── public/
|
| 74 |
+
│ ├── index.html # Main application page
|
| 75 |
+
│ ├── status.html # Status tracking page
|
| 76 |
+
│ └── admin.html # Admin dashboard
|
| 77 |
+
├── src/
|
| 78 |
+
│ ├── api/ # API endpoints
|
| 79 |
+
│ ├── config/ # Configuration files
|
| 80 |
+
│ ├── models/ # Database models
|
| 81 |
+
│ └── utils/ # Utility functions
|
| 82 |
+
├── .env # Environment variables
|
| 83 |
+
└── package.json # Project dependencies
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
## 🔒 Security Features
|
| 87 |
+
|
| 88 |
+
- OTP-based authentication
|
| 89 |
+
- Secure session management
|
| 90 |
+
- File upload validation
|
| 91 |
+
- Input sanitization
|
| 92 |
+
- XSS protection
|
| 93 |
+
- CSRF protection
|
| 94 |
+
|
| 95 |
+
## 💻 Technology Stack
|
| 96 |
+
|
| 97 |
+
- **Frontend**
|
| 98 |
+
- HTML5
|
| 99 |
+
- Tailwind CSS
|
| 100 |
+
- JavaScript (ES6+)
|
| 101 |
+
- Font Awesome icons
|
| 102 |
+
|
| 103 |
+
- **Backend**
|
| 104 |
+
- Node.js
|
| 105 |
+
- Express.js
|
| 106 |
+
- MongoDB
|
| 107 |
+
|
| 108 |
+
- **Services**
|
| 109 |
+
- File2Link for video uploads
|
| 110 |
+
- Email service for OTP
|
| 111 |
+
- Payment gateway integration
|
| 112 |
+
|
| 113 |
+
## 🤝 Contributing
|
| 114 |
+
|
| 115 |
+
1. Fork the repository
|
| 116 |
+
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
|
| 117 |
+
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
|
| 118 |
+
4. Push to the branch (`git push origin feature/AmazingFeature`)
|
| 119 |
+
5. Open a Pull Request
|
| 120 |
+
|
| 121 |
+
## 📝 License
|
| 122 |
+
|
| 123 |
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
| 124 |
+
|
| 125 |
+
## 👥 Team
|
| 126 |
+
|
| 127 |
+
- **Shirt Less Digital** - *Initial work* - [YouTube Channel](https://www.youtube.com/@shirtlessdigital)
|
| 128 |
+
|
| 129 |
+
## 🙏 Acknowledgments
|
| 130 |
+
|
| 131 |
+
- Thanks to all contributors
|
| 132 |
+
- Inspired by the need for a secure gift card management system
|
| 133 |
+
- Built with modern web technologies
|
| 134 |
+
|
| 135 |
+
## 📞 Support
|
| 136 |
+
|
| 137 |
+
For support, email contact@shirtlessdigital.com or visit our [YouTube Channel](https://www.youtube.com/@shirtlessdigital)
|
package-lock.json
CHANGED
|
@@ -22,8 +22,7 @@
|
|
| 22 |
"morgan": "^1.10.0",
|
| 23 |
"multer": "^1.4.5-lts.1",
|
| 24 |
"nodemailer": "^6.9.5",
|
| 25 |
-
"pg": "^8.16.0"
|
| 26 |
-
"socket.io": "^4.8.1"
|
| 27 |
},
|
| 28 |
"devDependencies": {
|
| 29 |
"nodemon": "^3.0.1"
|
|
@@ -32,27 +31,6 @@
|
|
| 32 |
"node": ">=14.0.0"
|
| 33 |
}
|
| 34 |
},
|
| 35 |
-
"node_modules/@socket.io/component-emitter": {
|
| 36 |
-
"version": "3.1.2",
|
| 37 |
-
"resolved": "https://registry.npmjs.org/@socket.io/component-emitter/-/component-emitter-3.1.2.tgz",
|
| 38 |
-
"integrity": "sha512-9BCxFwvbGg/RsZK9tjXd8s4UcwR0MWeFQ1XEKIQVVvAGJyINdrqKMcTRyLoK8Rse1GjzLV9cwjWV1olXRWEXVA==",
|
| 39 |
-
"license": "MIT"
|
| 40 |
-
},
|
| 41 |
-
"node_modules/@types/cors": {
|
| 42 |
-
"version": "2.8.19",
|
| 43 |
-
"resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.19.tgz",
|
| 44 |
-
"integrity": "sha512-mFNylyeyqN93lfe/9CSxOGREz8cpzAhH+E93xJ4xWQf62V8sQ/24reV2nyzUWM6H6Xji+GGHpkbLe7pVoUEskg==",
|
| 45 |
-
"license": "MIT",
|
| 46 |
-
"dependencies": {
|
| 47 |
-
"@types/node": "*"
|
| 48 |
-
}
|
| 49 |
-
},
|
| 50 |
-
"node_modules/@types/node": {
|
| 51 |
-
"version": "10.17.60",
|
| 52 |
-
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.60.tgz",
|
| 53 |
-
"integrity": "sha512-F0KIgDJfy2nA3zMLmWGKxcH2ZVEtCZXHHdOQs2gSaQ27+lNeEfGxzkIw90aXswATX7AZ33tahPbzy6KAfUreVw==",
|
| 54 |
-
"license": "MIT"
|
| 55 |
-
},
|
| 56 |
"node_modules/accepts": {
|
| 57 |
"version": "1.3.8",
|
| 58 |
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz",
|
|
@@ -182,15 +160,6 @@
|
|
| 182 |
],
|
| 183 |
"license": "MIT"
|
| 184 |
},
|
| 185 |
-
"node_modules/base64id": {
|
| 186 |
-
"version": "2.0.0",
|
| 187 |
-
"resolved": "https://registry.npmjs.org/base64id/-/base64id-2.0.0.tgz",
|
| 188 |
-
"integrity": "sha512-lGe34o6EHj9y3Kts9R4ZYs/Gr+6N7MCaMlIFA3F1R2O5/m7K06AxfSeO5530PEERE6/WyEg3lsuyw4GHlPZHog==",
|
| 189 |
-
"license": "MIT",
|
| 190 |
-
"engines": {
|
| 191 |
-
"node": "^4.5.0 || >= 5.9"
|
| 192 |
-
}
|
| 193 |
-
},
|
| 194 |
"node_modules/basic-auth": {
|
| 195 |
"version": "2.0.1",
|
| 196 |
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
|
|
@@ -622,67 +591,6 @@
|
|
| 622 |
"node": ">= 0.8"
|
| 623 |
}
|
| 624 |
},
|
| 625 |
-
"node_modules/engine.io": {
|
| 626 |
-
"version": "6.6.4",
|
| 627 |
-
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.6.4.tgz",
|
| 628 |
-
"integrity": "sha512-ZCkIjSYNDyGn0R6ewHDtXgns/Zre/NT6Agvq1/WobF7JXgFff4SeDroKiCO3fNJreU9YG429Sc81o4w5ok/W5g==",
|
| 629 |
-
"license": "MIT",
|
| 630 |
-
"dependencies": {
|
| 631 |
-
"@types/cors": "^2.8.12",
|
| 632 |
-
"@types/node": ">=10.0.0",
|
| 633 |
-
"accepts": "~1.3.4",
|
| 634 |
-
"base64id": "2.0.0",
|
| 635 |
-
"cookie": "~0.7.2",
|
| 636 |
-
"cors": "~2.8.5",
|
| 637 |
-
"debug": "~4.3.1",
|
| 638 |
-
"engine.io-parser": "~5.2.1",
|
| 639 |
-
"ws": "~8.17.1"
|
| 640 |
-
},
|
| 641 |
-
"engines": {
|
| 642 |
-
"node": ">=10.2.0"
|
| 643 |
-
}
|
| 644 |
-
},
|
| 645 |
-
"node_modules/engine.io-parser": {
|
| 646 |
-
"version": "5.2.3",
|
| 647 |
-
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.2.3.tgz",
|
| 648 |
-
"integrity": "sha512-HqD3yTBfnBxIrbnM1DoD6Pcq8NECnh8d4As1Qgh0z5Gg3jRRIqijury0CL3ghu/edArpUYiYqQiDUQBIs4np3Q==",
|
| 649 |
-
"license": "MIT",
|
| 650 |
-
"engines": {
|
| 651 |
-
"node": ">=10.0.0"
|
| 652 |
-
}
|
| 653 |
-
},
|
| 654 |
-
"node_modules/engine.io/node_modules/cookie": {
|
| 655 |
-
"version": "0.7.2",
|
| 656 |
-
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz",
|
| 657 |
-
"integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==",
|
| 658 |
-
"license": "MIT",
|
| 659 |
-
"engines": {
|
| 660 |
-
"node": ">= 0.6"
|
| 661 |
-
}
|
| 662 |
-
},
|
| 663 |
-
"node_modules/engine.io/node_modules/debug": {
|
| 664 |
-
"version": "4.3.7",
|
| 665 |
-
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz",
|
| 666 |
-
"integrity": "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==",
|
| 667 |
-
"license": "MIT",
|
| 668 |
-
"dependencies": {
|
| 669 |
-
"ms": "^2.1.3"
|
| 670 |
-
},
|
| 671 |
-
"engines": {
|
| 672 |
-
"node": ">=6.0"
|
| 673 |
-
},
|
| 674 |
-
"peerDependenciesMeta": {
|
| 675 |
-
"supports-color": {
|
| 676 |
-
"optional": true
|
| 677 |
-
}
|
| 678 |
-
}
|
| 679 |
-
},
|
| 680 |
-
"node_modules/engine.io/node_modules/ms": {
|
| 681 |
-
"version": "2.1.3",
|
| 682 |
-
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
| 683 |
-
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
|
| 684 |
-
"license": "MIT"
|
| 685 |
-
},
|
| 686 |
"node_modules/es-define-property": {
|
| 687 |
"version": "1.0.1",
|
| 688 |
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
|
|
@@ -2173,116 +2081,6 @@
|
|
| 2173 |
"node": ">=10"
|
| 2174 |
}
|
| 2175 |
},
|
| 2176 |
-
"node_modules/socket.io": {
|
| 2177 |
-
"version": "4.8.1",
|
| 2178 |
-
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.8.1.tgz",
|
| 2179 |
-
"integrity": "sha512-oZ7iUCxph8WYRHHcjBEc9unw3adt5CmSNlppj/5Q4k2RIrhl8Z5yY2Xr4j9zj0+wzVZ0bxmYoGSzKJnRl6A4yg==",
|
| 2180 |
-
"license": "MIT",
|
| 2181 |
-
"dependencies": {
|
| 2182 |
-
"accepts": "~1.3.4",
|
| 2183 |
-
"base64id": "~2.0.0",
|
| 2184 |
-
"cors": "~2.8.5",
|
| 2185 |
-
"debug": "~4.3.2",
|
| 2186 |
-
"engine.io": "~6.6.0",
|
| 2187 |
-
"socket.io-adapter": "~2.5.2",
|
| 2188 |
-
"socket.io-parser": "~4.2.4"
|
| 2189 |
-
},
|
| 2190 |
-
"engines": {
|
| 2191 |
-
"node": ">=10.2.0"
|
| 2192 |
-
}
|
| 2193 |
-
},
|
| 2194 |
-
"node_modules/socket.io-adapter": {
|
| 2195 |
-
"version": "2.5.5",
|
| 2196 |
-
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.5.5.tgz",
|
| 2197 |
-
"integrity": "sha512-eLDQas5dzPgOWCk9GuuJC2lBqItuhKI4uxGgo9aIV7MYbk2h9Q6uULEh8WBzThoI7l+qU9Ast9fVUmkqPP9wYg==",
|
| 2198 |
-
"license": "MIT",
|
| 2199 |
-
"dependencies": {
|
| 2200 |
-
"debug": "~4.3.4",
|
| 2201 |
-
"ws": "~8.17.1"
|
| 2202 |
-
}
|
| 2203 |
-
},
|
| 2204 |
-
"node_modules/socket.io-adapter/node_modules/debug": {
|
| 2205 |
-
"version": "4.3.7",
|
| 2206 |
-
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz",
|
| 2207 |
-
"integrity": "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==",
|
| 2208 |
-
"license": "MIT",
|
| 2209 |
-
"dependencies": {
|
| 2210 |
-
"ms": "^2.1.3"
|
| 2211 |
-
},
|
| 2212 |
-
"engines": {
|
| 2213 |
-
"node": ">=6.0"
|
| 2214 |
-
},
|
| 2215 |
-
"peerDependenciesMeta": {
|
| 2216 |
-
"supports-color": {
|
| 2217 |
-
"optional": true
|
| 2218 |
-
}
|
| 2219 |
-
}
|
| 2220 |
-
},
|
| 2221 |
-
"node_modules/socket.io-adapter/node_modules/ms": {
|
| 2222 |
-
"version": "2.1.3",
|
| 2223 |
-
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
| 2224 |
-
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
|
| 2225 |
-
"license": "MIT"
|
| 2226 |
-
},
|
| 2227 |
-
"node_modules/socket.io-parser": {
|
| 2228 |
-
"version": "4.2.4",
|
| 2229 |
-
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.4.tgz",
|
| 2230 |
-
"integrity": "sha512-/GbIKmo8ioc+NIWIhwdecY0ge+qVBSMdgxGygevmdHj24bsfgtCmcUUcQ5ZzcylGFHsN3k4HB4Cgkl96KVnuew==",
|
| 2231 |
-
"license": "MIT",
|
| 2232 |
-
"dependencies": {
|
| 2233 |
-
"@socket.io/component-emitter": "~3.1.0",
|
| 2234 |
-
"debug": "~4.3.1"
|
| 2235 |
-
},
|
| 2236 |
-
"engines": {
|
| 2237 |
-
"node": ">=10.0.0"
|
| 2238 |
-
}
|
| 2239 |
-
},
|
| 2240 |
-
"node_modules/socket.io-parser/node_modules/debug": {
|
| 2241 |
-
"version": "4.3.7",
|
| 2242 |
-
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz",
|
| 2243 |
-
"integrity": "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==",
|
| 2244 |
-
"license": "MIT",
|
| 2245 |
-
"dependencies": {
|
| 2246 |
-
"ms": "^2.1.3"
|
| 2247 |
-
},
|
| 2248 |
-
"engines": {
|
| 2249 |
-
"node": ">=6.0"
|
| 2250 |
-
},
|
| 2251 |
-
"peerDependenciesMeta": {
|
| 2252 |
-
"supports-color": {
|
| 2253 |
-
"optional": true
|
| 2254 |
-
}
|
| 2255 |
-
}
|
| 2256 |
-
},
|
| 2257 |
-
"node_modules/socket.io-parser/node_modules/ms": {
|
| 2258 |
-
"version": "2.1.3",
|
| 2259 |
-
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
| 2260 |
-
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
|
| 2261 |
-
"license": "MIT"
|
| 2262 |
-
},
|
| 2263 |
-
"node_modules/socket.io/node_modules/debug": {
|
| 2264 |
-
"version": "4.3.7",
|
| 2265 |
-
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz",
|
| 2266 |
-
"integrity": "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==",
|
| 2267 |
-
"license": "MIT",
|
| 2268 |
-
"dependencies": {
|
| 2269 |
-
"ms": "^2.1.3"
|
| 2270 |
-
},
|
| 2271 |
-
"engines": {
|
| 2272 |
-
"node": ">=6.0"
|
| 2273 |
-
},
|
| 2274 |
-
"peerDependenciesMeta": {
|
| 2275 |
-
"supports-color": {
|
| 2276 |
-
"optional": true
|
| 2277 |
-
}
|
| 2278 |
-
}
|
| 2279 |
-
},
|
| 2280 |
-
"node_modules/socket.io/node_modules/ms": {
|
| 2281 |
-
"version": "2.1.3",
|
| 2282 |
-
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
| 2283 |
-
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
|
| 2284 |
-
"license": "MIT"
|
| 2285 |
-
},
|
| 2286 |
"node_modules/split2": {
|
| 2287 |
"version": "4.2.0",
|
| 2288 |
"resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz",
|
|
@@ -2457,27 +2255,6 @@
|
|
| 2457 |
"url": "https://github.com/sponsors/ljharb"
|
| 2458 |
}
|
| 2459 |
},
|
| 2460 |
-
"node_modules/ws": {
|
| 2461 |
-
"version": "8.17.1",
|
| 2462 |
-
"resolved": "https://registry.npmjs.org/ws/-/ws-8.17.1.tgz",
|
| 2463 |
-
"integrity": "sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==",
|
| 2464 |
-
"license": "MIT",
|
| 2465 |
-
"engines": {
|
| 2466 |
-
"node": ">=10.0.0"
|
| 2467 |
-
},
|
| 2468 |
-
"peerDependencies": {
|
| 2469 |
-
"bufferutil": "^4.0.1",
|
| 2470 |
-
"utf-8-validate": ">=5.0.2"
|
| 2471 |
-
},
|
| 2472 |
-
"peerDependenciesMeta": {
|
| 2473 |
-
"bufferutil": {
|
| 2474 |
-
"optional": true
|
| 2475 |
-
},
|
| 2476 |
-
"utf-8-validate": {
|
| 2477 |
-
"optional": true
|
| 2478 |
-
}
|
| 2479 |
-
}
|
| 2480 |
-
},
|
| 2481 |
"node_modules/xml2js": {
|
| 2482 |
"version": "0.6.2",
|
| 2483 |
"resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.6.2.tgz",
|
|
|
|
| 22 |
"morgan": "^1.10.0",
|
| 23 |
"multer": "^1.4.5-lts.1",
|
| 24 |
"nodemailer": "^6.9.5",
|
| 25 |
+
"pg": "^8.16.0"
|
|
|
|
| 26 |
},
|
| 27 |
"devDependencies": {
|
| 28 |
"nodemon": "^3.0.1"
|
|
|
|
| 31 |
"node": ">=14.0.0"
|
| 32 |
}
|
| 33 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"node_modules/accepts": {
|
| 35 |
"version": "1.3.8",
|
| 36 |
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz",
|
|
|
|
| 160 |
],
|
| 161 |
"license": "MIT"
|
| 162 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
"node_modules/basic-auth": {
|
| 164 |
"version": "2.0.1",
|
| 165 |
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
|
|
|
|
| 591 |
"node": ">= 0.8"
|
| 592 |
}
|
| 593 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
"node_modules/es-define-property": {
|
| 595 |
"version": "1.0.1",
|
| 596 |
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
|
|
|
|
| 2081 |
"node": ">=10"
|
| 2082 |
}
|
| 2083 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2084 |
"node_modules/split2": {
|
| 2085 |
"version": "4.2.0",
|
| 2086 |
"resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz",
|
|
|
|
| 2255 |
"url": "https://github.com/sponsors/ljharb"
|
| 2256 |
}
|
| 2257 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2258 |
"node_modules/xml2js": {
|
| 2259 |
"version": "0.6.2",
|
| 2260 |
"resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.6.2.tgz",
|
package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
| 11 |
"keywords": [
|
| 12 |
"gift-card",
|
| 13 |
"portal",
|
| 14 |
-
"
|
| 15 |
"real-time",
|
| 16 |
"monitoring"
|
| 17 |
],
|
|
@@ -31,8 +31,7 @@
|
|
| 31 |
"morgan": "^1.10.0",
|
| 32 |
"multer": "^1.4.5-lts.1",
|
| 33 |
"nodemailer": "^6.9.5",
|
| 34 |
-
"pg": "^8.16.0"
|
| 35 |
-
"socket.io": "^4.8.1"
|
| 36 |
},
|
| 37 |
"devDependencies": {
|
| 38 |
"nodemon": "^3.0.1"
|
|
|
|
| 11 |
"keywords": [
|
| 12 |
"gift-card",
|
| 13 |
"portal",
|
| 14 |
+
"event-stream",
|
| 15 |
"real-time",
|
| 16 |
"monitoring"
|
| 17 |
],
|
|
|
|
| 31 |
"morgan": "^1.10.0",
|
| 32 |
"multer": "^1.4.5-lts.1",
|
| 33 |
"nodemailer": "^6.9.5",
|
| 34 |
+
"pg": "^8.16.0"
|
|
|
|
| 35 |
},
|
| 36 |
"devDependencies": {
|
| 37 |
"nodemon": "^3.0.1"
|
public/admin.html
CHANGED
|
@@ -201,31 +201,84 @@
|
|
| 201 |
|
| 202 |
<script>
|
| 203 |
let currentUserId = null;
|
| 204 |
-
let
|
|
|
|
|
|
|
| 205 |
|
| 206 |
-
// Initialize
|
| 207 |
-
function
|
| 208 |
-
const protocol = window.location.protocol === 'https:' ? '
|
| 209 |
const host = window.location.host;
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
}
|
| 224 |
-
}
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
}
|
| 230 |
|
| 231 |
// Add message to admin chat without reloading
|
|
@@ -233,21 +286,113 @@
|
|
| 233 |
const container = document.getElementById('chatMessages');
|
| 234 |
if (!container) return;
|
| 235 |
|
|
|
|
|
|
|
|
|
|
| 236 |
const div = document.createElement('div');
|
| 237 |
-
div.className = `flex ${message.sender === 'admin' ? 'justify-end' : 'justify-start'}`;
|
| 238 |
div.innerHTML = `
|
| 239 |
<div class="max-w-xs px-4 py-2 rounded-2xl ${message.sender === 'admin' ? 'bg-blue-600' : 'bg-gray-700'}">
|
| 240 |
-
<p class="text-sm">${message.content}</p>
|
| 241 |
-
<p class="text-xs opacity-70 mt-1">${new Date(message.created_at).toLocaleString()}</p>
|
| 242 |
</div>
|
| 243 |
`;
|
| 244 |
container.appendChild(div);
|
| 245 |
container.scrollTop = container.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
}
|
| 247 |
|
| 248 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
document.addEventListener('DOMContentLoaded', function() {
|
| 250 |
-
|
| 251 |
});
|
| 252 |
|
| 253 |
// Auth Functions
|
|
@@ -434,16 +579,42 @@
|
|
| 434 |
loadMessages();
|
| 435 |
|
| 436 |
// Join admin chat room
|
| 437 |
-
if (
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
}
|
| 440 |
}
|
| 441 |
|
| 442 |
function closeChat() {
|
| 443 |
-
//
|
| 444 |
-
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
document.getElementById('chatModal').classList.add('hidden');
|
| 448 |
currentUserId = null;
|
| 449 |
}
|
|
@@ -455,40 +626,68 @@
|
|
| 455 |
const response = await fetch(`/api/admin/messages/${currentUserId}?limit=50`);
|
| 456 |
const data = await response.json();
|
| 457 |
if (data.success) {
|
| 458 |
-
document.getElementById('chatMessages')
|
| 459 |
-
|
|
|
|
| 460 |
<div class="max-w-xs px-4 py-2 rounded-2xl ${m.sender === 'admin' ? 'bg-blue-600' : 'bg-gray-700'}">
|
| 461 |
-
<p class="text-sm">${m.content}</p>
|
| 462 |
-
<p class="text-xs opacity-70 mt-1">${new Date(m.created_at).toLocaleString()}</p>
|
| 463 |
</div>
|
| 464 |
</div>
|
| 465 |
`).join('');
|
| 466 |
-
|
|
|
|
|
|
|
|
|
|
| 467 |
}
|
| 468 |
} catch (error) {
|
|
|
|
| 469 |
document.getElementById('chatMessages').innerHTML = '<div class="text-center text-red-400">Error loading messages</div>';
|
| 470 |
}
|
| 471 |
}
|
| 472 |
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
const message =
|
| 476 |
if (!message || !currentUserId) return;
|
| 477 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
try {
|
|
|
|
| 479 |
const response = await fetch('/api/admin/messages', {
|
| 480 |
method: 'POST',
|
| 481 |
headers: { 'Content-Type': 'application/json' },
|
| 482 |
body: JSON.stringify({ userId: currentUserId, content: message })
|
| 483 |
});
|
|
|
|
| 484 |
const data = await response.json();
|
| 485 |
if (data.success) {
|
| 486 |
-
|
| 487 |
-
// Message will be added via
|
|
|
|
|
|
|
| 488 |
}
|
| 489 |
} catch (error) {
|
| 490 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
});
|
| 493 |
|
| 494 |
// Utility Functions
|
|
|
|
| 201 |
|
| 202 |
<script>
|
| 203 |
let currentUserId = null;
|
| 204 |
+
let eventSource = null;
|
| 205 |
+
let isTyping = false;
|
| 206 |
+
let typingTimeout = null;
|
| 207 |
|
| 208 |
+
// Initialize EventSource connection for admin
|
| 209 |
+
function initializeAdminEventSource() {
|
| 210 |
+
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
|
| 211 |
const host = window.location.host;
|
| 212 |
|
| 213 |
+
eventSource = new EventSource(`${protocol}//${host}/events/admin`);
|
| 214 |
+
|
| 215 |
+
eventSource.onopen = () => {
|
| 216 |
+
console.log('Admin connected to event stream');
|
| 217 |
+
showNotification('Connected to chat server', 'success');
|
| 218 |
+
};
|
| 219 |
+
|
| 220 |
+
eventSource.onerror = (error) => {
|
| 221 |
+
console.error('EventSource error:', error);
|
| 222 |
+
showNotification('Chat connection error', 'error');
|
| 223 |
+
};
|
| 224 |
+
|
| 225 |
+
eventSource.onmessage = (event) => {
|
| 226 |
+
try {
|
| 227 |
+
const data = JSON.parse(event.data);
|
| 228 |
+
handleEvent(data);
|
| 229 |
+
} catch (error) {
|
| 230 |
+
console.error('Error parsing event data:', error);
|
| 231 |
}
|
| 232 |
+
};
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
// Handle different event types
|
| 236 |
+
function handleEvent(data) {
|
| 237 |
+
switch (data.event) {
|
| 238 |
+
case 'connected':
|
| 239 |
+
console.log('Connected to event stream:', data.data);
|
| 240 |
+
break;
|
| 241 |
+
|
| 242 |
+
case 'new-message':
|
| 243 |
+
console.log('New message received:', data.data);
|
| 244 |
+
// Add message to current chat if it's the active user
|
| 245 |
+
if (data.data.message.user_id === currentUserId) {
|
| 246 |
+
addMessageToAdminChat(data.data.message);
|
| 247 |
+
}
|
| 248 |
+
break;
|
| 249 |
+
|
| 250 |
+
case 'user-typing':
|
| 251 |
+
if (data.data.userId === currentUserId) {
|
| 252 |
+
showTypingIndicator(data.data.userEmail);
|
| 253 |
+
}
|
| 254 |
+
break;
|
| 255 |
+
|
| 256 |
+
case 'user-stopped-typing':
|
| 257 |
+
if (data.data.userId === currentUserId) {
|
| 258 |
+
hideTypingIndicator();
|
| 259 |
+
}
|
| 260 |
+
break;
|
| 261 |
+
|
| 262 |
+
case 'message-sent':
|
| 263 |
+
console.log('Message sent successfully:', data.data);
|
| 264 |
+
break;
|
| 265 |
+
|
| 266 |
+
case 'message-error':
|
| 267 |
+
console.error('Message error:', data.data);
|
| 268 |
+
showNotification('Failed to send message: ' + data.data.error, 'error');
|
| 269 |
+
break;
|
| 270 |
+
|
| 271 |
+
case 'server-log':
|
| 272 |
+
// Handle server logs if needed
|
| 273 |
+
break;
|
| 274 |
+
|
| 275 |
+
case 'system-status':
|
| 276 |
+
// Handle system status updates if needed
|
| 277 |
+
break;
|
| 278 |
+
|
| 279 |
+
default:
|
| 280 |
+
console.log('Unknown event:', data.event, data.data);
|
| 281 |
+
}
|
| 282 |
}
|
| 283 |
|
| 284 |
// Add message to admin chat without reloading
|
|
|
|
| 286 |
const container = document.getElementById('chatMessages');
|
| 287 |
if (!container) return;
|
| 288 |
|
| 289 |
+
// Remove typing indicator if present
|
| 290 |
+
hideTypingIndicator();
|
| 291 |
+
|
| 292 |
const div = document.createElement('div');
|
| 293 |
+
div.className = `flex ${message.sender === 'admin' ? 'justify-end' : 'justify-start'} mb-3`;
|
| 294 |
div.innerHTML = `
|
| 295 |
<div class="max-w-xs px-4 py-2 rounded-2xl ${message.sender === 'admin' ? 'bg-blue-600' : 'bg-gray-700'}">
|
| 296 |
+
<p class="text-sm text-white">${escapeHtml(message.content)}</p>
|
| 297 |
+
<p class="text-xs opacity-70 mt-1 text-white">${new Date(message.created_at).toLocaleString()}</p>
|
| 298 |
</div>
|
| 299 |
`;
|
| 300 |
container.appendChild(div);
|
| 301 |
container.scrollTop = container.scrollHeight;
|
| 302 |
+
|
| 303 |
+
// Add message to chat history
|
| 304 |
+
addMessageToHistory(message);
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
// Show typing indicator
|
| 308 |
+
function showTypingIndicator(userEmail) {
|
| 309 |
+
const container = document.getElementById('chatMessages');
|
| 310 |
+
if (!container) return;
|
| 311 |
+
|
| 312 |
+
// Remove existing typing indicator
|
| 313 |
+
hideTypingIndicator();
|
| 314 |
+
|
| 315 |
+
const typingDiv = document.createElement('div');
|
| 316 |
+
typingDiv.id = 'typing-indicator';
|
| 317 |
+
typingDiv.className = 'flex justify-start mb-3';
|
| 318 |
+
typingDiv.innerHTML = `
|
| 319 |
+
<div class="max-w-xs px-4 py-2 rounded-2xl bg-gray-700">
|
| 320 |
+
<p class="text-sm text-white opacity-70">${userEmail} is typing...</p>
|
| 321 |
+
</div>
|
| 322 |
+
`;
|
| 323 |
+
container.appendChild(typingDiv);
|
| 324 |
+
container.scrollTop = container.scrollHeight;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
// Hide typing indicator
|
| 328 |
+
function hideTypingIndicator() {
|
| 329 |
+
const typingIndicator = document.getElementById('typing-indicator');
|
| 330 |
+
if (typingIndicator) {
|
| 331 |
+
typingIndicator.remove();
|
| 332 |
+
}
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
// Add message to chat history for persistence
|
| 336 |
+
function addMessageToHistory(message) {
|
| 337 |
+
// Store in session storage for persistence during session
|
| 338 |
+
let chatHistory = JSON.parse(sessionStorage.getItem(`chat_${currentUserId}`) || '[]');
|
| 339 |
+
chatHistory.push(message);
|
| 340 |
+
|
| 341 |
+
// Keep only last 100 messages
|
| 342 |
+
if (chatHistory.length > 100) {
|
| 343 |
+
chatHistory = chatHistory.slice(-100);
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
sessionStorage.setItem(`chat_${currentUserId}`, JSON.stringify(chatHistory));
|
| 347 |
}
|
| 348 |
|
| 349 |
+
// Escape HTML to prevent XSS
|
| 350 |
+
function escapeHtml(text) {
|
| 351 |
+
const div = document.createElement('div');
|
| 352 |
+
div.textContent = text;
|
| 353 |
+
return div.innerHTML;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
// Handle typing events
|
| 357 |
+
async function handleTyping() {
|
| 358 |
+
if (!currentUserId) return;
|
| 359 |
+
|
| 360 |
+
if (!isTyping) {
|
| 361 |
+
isTyping = true;
|
| 362 |
+
try {
|
| 363 |
+
await fetch('/api/admin/typing', {
|
| 364 |
+
method: 'POST',
|
| 365 |
+
headers: { 'Content-Type': 'application/json' },
|
| 366 |
+
body: JSON.stringify({ userId: currentUserId })
|
| 367 |
+
});
|
| 368 |
+
} catch (error) {
|
| 369 |
+
console.error('Error sending typing indicator:', error);
|
| 370 |
+
}
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
// Clear existing timeout
|
| 374 |
+
if (typingTimeout) {
|
| 375 |
+
clearTimeout(typingTimeout);
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
// Set timeout to stop typing indicator
|
| 379 |
+
typingTimeout = setTimeout(async () => {
|
| 380 |
+
isTyping = false;
|
| 381 |
+
try {
|
| 382 |
+
await fetch('/api/admin/stop-typing', {
|
| 383 |
+
method: 'POST',
|
| 384 |
+
headers: { 'Content-Type': 'application/json' },
|
| 385 |
+
body: JSON.stringify({ userId: currentUserId })
|
| 386 |
+
});
|
| 387 |
+
} catch (error) {
|
| 388 |
+
console.error('Error sending stop typing indicator:', error);
|
| 389 |
+
}
|
| 390 |
+
}, 1000);
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
// Initialize EventSource when page loads
|
| 394 |
document.addEventListener('DOMContentLoaded', function() {
|
| 395 |
+
initializeAdminEventSource();
|
| 396 |
});
|
| 397 |
|
| 398 |
// Auth Functions
|
|
|
|
| 579 |
loadMessages();
|
| 580 |
|
| 581 |
// Join admin chat room
|
| 582 |
+
if (eventSource) {
|
| 583 |
+
console.log('Joined chat room for user:', userId);
|
| 584 |
+
} else {
|
| 585 |
+
console.warn('EventSource not connected, cannot join chat room');
|
| 586 |
+
}
|
| 587 |
+
|
| 588 |
+
// Add typing event listener to message input
|
| 589 |
+
const messageInput = document.getElementById('messageInput');
|
| 590 |
+
if (messageInput) {
|
| 591 |
+
messageInput.addEventListener('input', handleTyping);
|
| 592 |
+
messageInput.addEventListener('keydown', (e) => {
|
| 593 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
| 594 |
+
e.preventDefault();
|
| 595 |
+
sendMessage();
|
| 596 |
+
}
|
| 597 |
+
});
|
| 598 |
}
|
| 599 |
}
|
| 600 |
|
| 601 |
function closeChat() {
|
| 602 |
+
// Remove typing event listeners
|
| 603 |
+
const messageInput = document.getElementById('messageInput');
|
| 604 |
+
if (messageInput) {
|
| 605 |
+
messageInput.removeEventListener('input', handleTyping);
|
| 606 |
+
}
|
| 607 |
+
|
| 608 |
+
// Clear typing state
|
| 609 |
+
isTyping = false;
|
| 610 |
+
if (typingTimeout) {
|
| 611 |
+
clearTimeout(typingTimeout);
|
| 612 |
+
typingTimeout = null;
|
| 613 |
}
|
| 614 |
+
|
| 615 |
+
// Hide typing indicator
|
| 616 |
+
hideTypingIndicator();
|
| 617 |
+
|
| 618 |
document.getElementById('chatModal').classList.add('hidden');
|
| 619 |
currentUserId = null;
|
| 620 |
}
|
|
|
|
| 626 |
const response = await fetch(`/api/admin/messages/${currentUserId}?limit=50`);
|
| 627 |
const data = await response.json();
|
| 628 |
if (data.success) {
|
| 629 |
+
const container = document.getElementById('chatMessages');
|
| 630 |
+
container.innerHTML = data.messages.map(m => `
|
| 631 |
+
<div class="flex ${m.sender === 'admin' ? 'justify-end' : 'justify-start'} mb-3">
|
| 632 |
<div class="max-w-xs px-4 py-2 rounded-2xl ${m.sender === 'admin' ? 'bg-blue-600' : 'bg-gray-700'}">
|
| 633 |
+
<p class="text-sm text-white">${escapeHtml(m.content)}</p>
|
| 634 |
+
<p class="text-xs opacity-70 mt-1 text-white">${new Date(m.created_at).toLocaleString()}</p>
|
| 635 |
</div>
|
| 636 |
</div>
|
| 637 |
`).join('');
|
| 638 |
+
container.scrollTop = container.scrollHeight;
|
| 639 |
+
|
| 640 |
+
// Store messages in session storage
|
| 641 |
+
sessionStorage.setItem(`chat_${currentUserId}`, JSON.stringify(data.messages));
|
| 642 |
}
|
| 643 |
} catch (error) {
|
| 644 |
+
console.error('Error loading messages:', error);
|
| 645 |
document.getElementById('chatMessages').innerHTML = '<div class="text-center text-red-400">Error loading messages</div>';
|
| 646 |
}
|
| 647 |
}
|
| 648 |
|
| 649 |
+
async function sendMessage() {
|
| 650 |
+
const messageInput = document.getElementById('messageInput');
|
| 651 |
+
const message = messageInput.value.trim();
|
| 652 |
if (!message || !currentUserId) return;
|
| 653 |
|
| 654 |
+
// Clear input immediately for better UX
|
| 655 |
+
messageInput.value = '';
|
| 656 |
+
|
| 657 |
+
// Stop typing indicator
|
| 658 |
+
isTyping = false;
|
| 659 |
+
if (typingTimeout) {
|
| 660 |
+
clearTimeout(typingTimeout);
|
| 661 |
+
typingTimeout = null;
|
| 662 |
+
}
|
| 663 |
+
|
| 664 |
try {
|
| 665 |
+
// Send message via API to save to database
|
| 666 |
const response = await fetch('/api/admin/messages', {
|
| 667 |
method: 'POST',
|
| 668 |
headers: { 'Content-Type': 'application/json' },
|
| 669 |
body: JSON.stringify({ userId: currentUserId, content: message })
|
| 670 |
});
|
| 671 |
+
|
| 672 |
const data = await response.json();
|
| 673 |
if (data.success) {
|
| 674 |
+
console.log('Message sent and saved to database:', data.message);
|
| 675 |
+
// Message will be added via EventSource event for real-time display
|
| 676 |
+
} else {
|
| 677 |
+
throw new Error(data.error || 'Failed to send message');
|
| 678 |
}
|
| 679 |
} catch (error) {
|
| 680 |
+
console.error('Error sending message:', error);
|
| 681 |
+
showNotification('Failed to send message: ' + error.message, 'error');
|
| 682 |
+
|
| 683 |
+
// Restore message in input if failed
|
| 684 |
+
messageInput.value = message;
|
| 685 |
}
|
| 686 |
+
}
|
| 687 |
+
|
| 688 |
+
document.getElementById('chatForm').addEventListener('submit', async (e) => {
|
| 689 |
+
e.preventDefault();
|
| 690 |
+
await sendMessage();
|
| 691 |
});
|
| 692 |
|
| 693 |
// Utility Functions
|
public/server-logs.html
CHANGED
|
@@ -283,7 +283,7 @@
|
|
| 283 |
</div>
|
| 284 |
|
| 285 |
<script>
|
| 286 |
-
let
|
| 287 |
let logs = [];
|
| 288 |
let autoScroll = true;
|
| 289 |
let filters = {
|
|
@@ -305,41 +305,66 @@
|
|
| 305 |
setInterval(updateTime, 1000);
|
| 306 |
updateTime();
|
| 307 |
|
| 308 |
-
// Initialize
|
| 309 |
-
function
|
| 310 |
-
const protocol = window.location.protocol === 'https:' ? '
|
| 311 |
const host = window.location.host;
|
| 312 |
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
socket.on('connect', () => {
|
| 318 |
updateConnectionStatus('Connected', 'connected');
|
| 319 |
-
addLog('info', 'Connected to server logs'
|
| 320 |
-
}
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
updateConnectionStatus('Disconnected', 'disconnected');
|
| 324 |
-
addLog('warning', 'Disconnected from server logs');
|
| 325 |
-
});
|
| 326 |
-
|
| 327 |
-
socket.on('server-log', (logData) => {
|
| 328 |
-
addLog(logData.type, logData.message, logData.details);
|
| 329 |
-
});
|
| 330 |
-
|
| 331 |
-
socket.on('error', (error) => {
|
| 332 |
updateConnectionStatus('Error', 'disconnected');
|
| 333 |
-
addLog('error', '
|
| 334 |
-
}
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
setTimeout(() => {
|
| 337 |
-
if (!
|
| 338 |
updateConnectionStatus('Connection Failed', 'disconnected');
|
| 339 |
addLog('error', 'Failed to connect to server logs');
|
| 340 |
}
|
| 341 |
}, 5000);
|
| 342 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
function updateConnectionStatus(status, indicatorClass) {
|
| 345 |
const statusElement = document.getElementById('connectionStatus');
|
|
@@ -507,7 +532,7 @@
|
|
| 507 |
|
| 508 |
// Initialize
|
| 509 |
document.addEventListener('DOMContentLoaded', function() {
|
| 510 |
-
|
| 511 |
|
| 512 |
setTimeout(() => {
|
| 513 |
addLog('info', 'Terminal ready - monitoring server activity');
|
|
|
|
| 283 |
</div>
|
| 284 |
|
| 285 |
<script>
|
| 286 |
+
let eventSource;
|
| 287 |
let logs = [];
|
| 288 |
let autoScroll = true;
|
| 289 |
let filters = {
|
|
|
|
| 305 |
setInterval(updateTime, 1000);
|
| 306 |
updateTime();
|
| 307 |
|
| 308 |
+
// Initialize EventSource connection
|
| 309 |
+
function initializeEventSource() {
|
| 310 |
+
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
|
| 311 |
const host = window.location.host;
|
| 312 |
|
| 313 |
+
eventSource = new EventSource(`${protocol}//${host}/events/admin`);
|
| 314 |
+
|
| 315 |
+
eventSource.onopen = () => {
|
|
|
|
|
|
|
| 316 |
updateConnectionStatus('Connected', 'connected');
|
| 317 |
+
addLog('info', 'Connected to server logs');
|
| 318 |
+
};
|
| 319 |
+
|
| 320 |
+
eventSource.onerror = (error) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
updateConnectionStatus('Error', 'disconnected');
|
| 322 |
+
addLog('error', 'EventSource connection error', { error: error.message });
|
| 323 |
+
};
|
| 324 |
+
|
| 325 |
+
eventSource.onmessage = (event) => {
|
| 326 |
+
try {
|
| 327 |
+
const data = JSON.parse(event.data);
|
| 328 |
+
handleEvent(data);
|
| 329 |
+
} catch (error) {
|
| 330 |
+
console.error('Error parsing event data:', error);
|
| 331 |
+
addLog('error', 'Failed to parse event data', { error: error.message });
|
| 332 |
+
}
|
| 333 |
+
};
|
| 334 |
+
|
| 335 |
setTimeout(() => {
|
| 336 |
+
if (eventSource.readyState !== EventSource.OPEN) {
|
| 337 |
updateConnectionStatus('Connection Failed', 'disconnected');
|
| 338 |
addLog('error', 'Failed to connect to server logs');
|
| 339 |
}
|
| 340 |
}, 5000);
|
| 341 |
}
|
| 342 |
+
|
| 343 |
+
function handleEvent(data) {
|
| 344 |
+
switch (data.event) {
|
| 345 |
+
case 'connected':
|
| 346 |
+
updateConnectionStatus('Connected', 'connected');
|
| 347 |
+
addLog('info', 'Connected to server logs', data.data);
|
| 348 |
+
break;
|
| 349 |
+
|
| 350 |
+
case 'server-log':
|
| 351 |
+
addLog(data.data.type, data.data.message, data.data.details);
|
| 352 |
+
break;
|
| 353 |
+
|
| 354 |
+
case 'historical-logs':
|
| 355 |
+
data.data.forEach(log => {
|
| 356 |
+
addLog(log.type, log.message, log.details);
|
| 357 |
+
});
|
| 358 |
+
break;
|
| 359 |
+
|
| 360 |
+
case 'system-status':
|
| 361 |
+
addLog('info', 'System status update', data.data);
|
| 362 |
+
break;
|
| 363 |
+
|
| 364 |
+
default:
|
| 365 |
+
console.log('Unknown event:', data.event, data.data);
|
| 366 |
+
}
|
| 367 |
+
}
|
| 368 |
|
| 369 |
function updateConnectionStatus(status, indicatorClass) {
|
| 370 |
const statusElement = document.getElementById('connectionStatus');
|
|
|
|
| 532 |
|
| 533 |
// Initialize
|
| 534 |
document.addEventListener('DOMContentLoaded', function() {
|
| 535 |
+
initializeEventSource();
|
| 536 |
|
| 537 |
setTimeout(() => {
|
| 538 |
addLog('info', 'Terminal ready - monitoring server activity');
|
public/status.html
CHANGED
|
@@ -249,33 +249,80 @@
|
|
| 249 |
|
| 250 |
<script>
|
| 251 |
let userId = null, userEmail = null, currentOtp = null, userName = null, chatInterval = null;
|
| 252 |
-
let
|
| 253 |
|
| 254 |
window.onload = () => clearSession();
|
| 255 |
|
| 256 |
-
// Initialize
|
| 257 |
-
function
|
| 258 |
-
|
|
|
|
|
|
|
| 259 |
const host = window.location.host;
|
| 260 |
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
console.log('Connected to chat server');
|
| 267 |
-
});
|
| 268 |
-
|
| 269 |
-
socket.on('new-message', (data) => {
|
| 270 |
-
// Only add message if it's for current user
|
| 271 |
-
if (data.message.user_id === userId) {
|
| 272 |
-
addMessageToChat(data.message);
|
| 273 |
}
|
| 274 |
});
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
console.log('
|
| 278 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
}
|
| 280 |
|
| 281 |
// Add message to chat without reloading all messages
|
|
@@ -283,16 +330,90 @@
|
|
| 283 |
const container = document.getElementById('chatMessages');
|
| 284 |
if (!container) return;
|
| 285 |
|
|
|
|
|
|
|
|
|
|
| 286 |
const div = document.createElement('div');
|
| 287 |
-
div.className = `flex ${message.sender === 'user' ? 'justify-end' : 'justify-start'}`;
|
| 288 |
div.innerHTML = `
|
| 289 |
<div class="max-w-[80%] ${message.sender === 'user' ? 'bg-blue-500' : 'bg-white bg-opacity-20'} rounded-2xl px-4 py-3">
|
| 290 |
-
<p class="text-white text-sm">${message.content}</p>
|
| 291 |
<p class="text-white text-opacity-60 text-xs mt-1">${new Date(message.created_at).toLocaleTimeString()}</p>
|
| 292 |
</div>
|
| 293 |
`;
|
| 294 |
container.appendChild(div);
|
| 295 |
container.scrollTop = container.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
}
|
| 297 |
|
| 298 |
// Loading state management
|
|
@@ -337,38 +458,38 @@
|
|
| 337 |
}
|
| 338 |
|
| 339 |
// Email validation
|
| 340 |
-
function
|
| 341 |
-
const email = document.getElementById('email').value;
|
| 342 |
-
const emailError = document.getElementById('emailError');
|
| 343 |
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
| 344 |
-
|
| 345 |
-
if (email && !emailRegex.test(email)) {
|
| 346 |
-
emailError.classList.remove('hidden');
|
| 347 |
-
return false;
|
| 348 |
-
} else {
|
| 349 |
-
emailError.classList.add('hidden');
|
| 350 |
-
return true;
|
| 351 |
-
}
|
| 352 |
}
|
| 353 |
|
|
|
|
| 354 |
function clearSession() {
|
| 355 |
-
userId =
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
document.getElementById('submissionsList').innerHTML = '';
|
| 360 |
-
hideLoadingModal();
|
| 361 |
-
hideLoginError();
|
| 362 |
if (chatInterval) {
|
| 363 |
clearInterval(chatInterval);
|
| 364 |
chatInterval = null;
|
| 365 |
}
|
| 366 |
-
if (
|
| 367 |
-
|
| 368 |
-
|
| 369 |
}
|
| 370 |
}
|
| 371 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
function logout() { clearSession(); }
|
| 373 |
|
| 374 |
async function sendOtp() {
|
|
@@ -381,7 +502,7 @@
|
|
| 381 |
return;
|
| 382 |
}
|
| 383 |
|
| 384 |
-
if (!
|
| 385 |
showLoginError('Please enter a valid email address');
|
| 386 |
return;
|
| 387 |
}
|
|
@@ -542,14 +663,15 @@
|
|
| 542 |
chat.classList.remove('hidden');
|
| 543 |
loadMessages();
|
| 544 |
// Join chat room for real-time updates
|
| 545 |
-
if (
|
| 546 |
-
|
| 547 |
}
|
| 548 |
} else {
|
| 549 |
chat.classList.add('hidden');
|
| 550 |
// Leave chat room
|
| 551 |
-
if (
|
| 552 |
-
|
|
|
|
| 553 |
}
|
| 554 |
}
|
| 555 |
}
|
|
@@ -596,7 +718,7 @@
|
|
| 596 |
const data = await response.json();
|
| 597 |
if (data.success) {
|
| 598 |
input.value = '';
|
| 599 |
-
// Message will be added via
|
| 600 |
} else throw new Error(data.error);
|
| 601 |
} catch (error) {
|
| 602 |
console.error('Error:', error);
|
|
@@ -621,17 +743,6 @@
|
|
| 621 |
document.getElementById('authSection').classList.remove('hidden');
|
| 622 |
document.getElementById('email').focus();
|
| 623 |
});
|
| 624 |
-
|
| 625 |
-
// Initialize socket when user logs in
|
| 626 |
-
function initializeUserSession(userData, otp) {
|
| 627 |
-
userId = userData.id;
|
| 628 |
-
userEmail = userData.email;
|
| 629 |
-
userName = userData.name;
|
| 630 |
-
currentOtp = otp;
|
| 631 |
-
|
| 632 |
-
// Initialize socket connection for chat
|
| 633 |
-
initializeSocket();
|
| 634 |
-
}
|
| 635 |
</script>
|
| 636 |
</body>
|
| 637 |
</html>
|
|
|
|
| 249 |
|
| 250 |
<script>
|
| 251 |
let userId = null, userEmail = null, currentOtp = null, userName = null, chatInterval = null;
|
| 252 |
+
let eventSource = null;
|
| 253 |
|
| 254 |
window.onload = () => clearSession();
|
| 255 |
|
| 256 |
+
// Initialize EventSource connection
|
| 257 |
+
function initializeEventSource() {
|
| 258 |
+
if (!userId) return;
|
| 259 |
+
|
| 260 |
+
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
|
| 261 |
const host = window.location.host;
|
| 262 |
|
| 263 |
+
eventSource = new EventSource(`${protocol}//${host}/events/user/${userId}`, {
|
| 264 |
+
headers: {
|
| 265 |
+
'x-otp-token': currentOtp,
|
| 266 |
+
'x-user-email': userEmail,
|
| 267 |
+
'x-user-id': userId
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
}
|
| 269 |
});
|
| 270 |
+
|
| 271 |
+
eventSource.onopen = () => {
|
| 272 |
+
console.log('Connected to chat event stream');
|
| 273 |
+
};
|
| 274 |
+
|
| 275 |
+
eventSource.onerror = (error) => {
|
| 276 |
+
console.error('EventSource error:', error);
|
| 277 |
+
};
|
| 278 |
+
|
| 279 |
+
eventSource.onmessage = (event) => {
|
| 280 |
+
try {
|
| 281 |
+
const data = JSON.parse(event.data);
|
| 282 |
+
handleEvent(data);
|
| 283 |
+
} catch (error) {
|
| 284 |
+
console.error('Error parsing event data:', error);
|
| 285 |
+
}
|
| 286 |
+
};
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
// Handle different event types
|
| 290 |
+
function handleEvent(data) {
|
| 291 |
+
switch (data.event) {
|
| 292 |
+
case 'connected':
|
| 293 |
+
console.log('Connected to event stream:', data.data);
|
| 294 |
+
break;
|
| 295 |
+
|
| 296 |
+
case 'new-message':
|
| 297 |
+
// Only add message if it's for current user
|
| 298 |
+
if (data.data.message.user_id === userId) {
|
| 299 |
+
addMessageToChat(data.data.message);
|
| 300 |
+
}
|
| 301 |
+
break;
|
| 302 |
+
|
| 303 |
+
case 'user-typing':
|
| 304 |
+
if (data.data.userId === userId) {
|
| 305 |
+
showTypingIndicator(data.data.userEmail);
|
| 306 |
+
}
|
| 307 |
+
break;
|
| 308 |
+
|
| 309 |
+
case 'user-stopped-typing':
|
| 310 |
+
if (data.data.userId === userId) {
|
| 311 |
+
hideTypingIndicator();
|
| 312 |
+
}
|
| 313 |
+
break;
|
| 314 |
+
|
| 315 |
+
case 'message-sent':
|
| 316 |
+
console.log('Message sent successfully:', data.data);
|
| 317 |
+
break;
|
| 318 |
+
|
| 319 |
+
case 'message-error':
|
| 320 |
+
console.error('Message error:', data.data);
|
| 321 |
+
break;
|
| 322 |
+
|
| 323 |
+
default:
|
| 324 |
+
console.log('Unknown event:', data.event, data.data);
|
| 325 |
+
}
|
| 326 |
}
|
| 327 |
|
| 328 |
// Add message to chat without reloading all messages
|
|
|
|
| 330 |
const container = document.getElementById('chatMessages');
|
| 331 |
if (!container) return;
|
| 332 |
|
| 333 |
+
// Remove typing indicator if present
|
| 334 |
+
hideTypingIndicator();
|
| 335 |
+
|
| 336 |
const div = document.createElement('div');
|
| 337 |
+
div.className = `flex ${message.sender === 'user' ? 'justify-end' : 'justify-start'} mb-3`;
|
| 338 |
div.innerHTML = `
|
| 339 |
<div class="max-w-[80%] ${message.sender === 'user' ? 'bg-blue-500' : 'bg-white bg-opacity-20'} rounded-2xl px-4 py-3">
|
| 340 |
+
<p class="text-white text-sm">${escapeHtml(message.content)}</p>
|
| 341 |
<p class="text-white text-opacity-60 text-xs mt-1">${new Date(message.created_at).toLocaleTimeString()}</p>
|
| 342 |
</div>
|
| 343 |
`;
|
| 344 |
container.appendChild(div);
|
| 345 |
container.scrollTop = container.scrollHeight;
|
| 346 |
+
|
| 347 |
+
// Add message to chat history
|
| 348 |
+
addMessageToHistory(message);
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
// Show typing indicator
|
| 352 |
+
function showTypingIndicator(userEmail) {
|
| 353 |
+
const container = document.getElementById('chatMessages');
|
| 354 |
+
if (!container) return;
|
| 355 |
+
|
| 356 |
+
// Remove existing typing indicator
|
| 357 |
+
hideTypingIndicator();
|
| 358 |
+
|
| 359 |
+
const typingDiv = document.createElement('div');
|
| 360 |
+
typingDiv.id = 'typing-indicator';
|
| 361 |
+
typingDiv.className = 'flex justify-start mb-3';
|
| 362 |
+
typingDiv.innerHTML = `
|
| 363 |
+
<div class="max-w-[80%] bg-white bg-opacity-20 rounded-2xl px-4 py-3">
|
| 364 |
+
<p class="text-white text-sm opacity-70">${userEmail} is typing...</p>
|
| 365 |
+
</div>
|
| 366 |
+
`;
|
| 367 |
+
container.appendChild(typingDiv);
|
| 368 |
+
container.scrollTop = container.scrollHeight;
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
// Hide typing indicator
|
| 372 |
+
function hideTypingIndicator() {
|
| 373 |
+
const typingIndicator = document.getElementById('typing-indicator');
|
| 374 |
+
if (typingIndicator) {
|
| 375 |
+
typingIndicator.remove();
|
| 376 |
+
}
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
// Add message to chat history for persistence
|
| 380 |
+
function addMessageToHistory(message) {
|
| 381 |
+
// Store in session storage for persistence during session
|
| 382 |
+
let chatHistory = JSON.parse(sessionStorage.getItem(`chat_${userId}`) || '[]');
|
| 383 |
+
chatHistory.push(message);
|
| 384 |
+
|
| 385 |
+
// Keep only last 100 messages
|
| 386 |
+
if (chatHistory.length > 100) {
|
| 387 |
+
chatHistory = chatHistory.slice(-100);
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
sessionStorage.setItem(`chat_${userId}`, JSON.stringify(chatHistory));
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
// Escape HTML to prevent XSS
|
| 394 |
+
function escapeHtml(text) {
|
| 395 |
+
const div = document.createElement('div');
|
| 396 |
+
div.textContent = text;
|
| 397 |
+
return div.innerHTML;
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
// Handle typing events
|
| 401 |
+
async function handleTyping() {
|
| 402 |
+
if (!userId) return;
|
| 403 |
+
|
| 404 |
+
try {
|
| 405 |
+
await fetch('/api/typing', {
|
| 406 |
+
method: 'POST',
|
| 407 |
+
headers: {
|
| 408 |
+
'Content-Type': 'application/json',
|
| 409 |
+
'x-otp-token': currentOtp,
|
| 410 |
+
'x-user-email': userEmail
|
| 411 |
+
},
|
| 412 |
+
body: JSON.stringify({ userId })
|
| 413 |
+
});
|
| 414 |
+
} catch (error) {
|
| 415 |
+
console.error('Error sending typing indicator:', error);
|
| 416 |
+
}
|
| 417 |
}
|
| 418 |
|
| 419 |
// Loading state management
|
|
|
|
| 458 |
}
|
| 459 |
|
| 460 |
// Email validation
|
| 461 |
+
function isValidEmail(email) {
|
|
|
|
|
|
|
| 462 |
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
| 463 |
+
return emailRegex.test(email);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
}
|
| 465 |
|
| 466 |
+
// Session management
|
| 467 |
function clearSession() {
|
| 468 |
+
userId = null;
|
| 469 |
+
userEmail = null;
|
| 470 |
+
currentOtp = null;
|
| 471 |
+
userName = null;
|
|
|
|
|
|
|
|
|
|
| 472 |
if (chatInterval) {
|
| 473 |
clearInterval(chatInterval);
|
| 474 |
chatInterval = null;
|
| 475 |
}
|
| 476 |
+
if (eventSource) {
|
| 477 |
+
eventSource.close();
|
| 478 |
+
eventSource = null;
|
| 479 |
}
|
| 480 |
}
|
| 481 |
|
| 482 |
+
// Initialize EventSource when user logs in
|
| 483 |
+
function initializeUserSession(userData, otp) {
|
| 484 |
+
userId = userData.id;
|
| 485 |
+
userEmail = userData.email;
|
| 486 |
+
userName = userData.name;
|
| 487 |
+
currentOtp = otp;
|
| 488 |
+
|
| 489 |
+
// Initialize EventSource connection for chat
|
| 490 |
+
initializeEventSource();
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
function logout() { clearSession(); }
|
| 494 |
|
| 495 |
async function sendOtp() {
|
|
|
|
| 502 |
return;
|
| 503 |
}
|
| 504 |
|
| 505 |
+
if (!isValidEmail(email)) {
|
| 506 |
showLoginError('Please enter a valid email address');
|
| 507 |
return;
|
| 508 |
}
|
|
|
|
| 663 |
chat.classList.remove('hidden');
|
| 664 |
loadMessages();
|
| 665 |
// Join chat room for real-time updates
|
| 666 |
+
if (eventSource && userId && userEmail) {
|
| 667 |
+
eventSource.addEventListener('message', handleEvent);
|
| 668 |
}
|
| 669 |
} else {
|
| 670 |
chat.classList.add('hidden');
|
| 671 |
// Leave chat room
|
| 672 |
+
if (eventSource && userId) {
|
| 673 |
+
eventSource.close();
|
| 674 |
+
eventSource = null;
|
| 675 |
}
|
| 676 |
}
|
| 677 |
}
|
|
|
|
| 718 |
const data = await response.json();
|
| 719 |
if (data.success) {
|
| 720 |
input.value = '';
|
| 721 |
+
// Message will be added via EventSource event
|
| 722 |
} else throw new Error(data.error);
|
| 723 |
} catch (error) {
|
| 724 |
console.error('Error:', error);
|
|
|
|
| 743 |
document.getElementById('authSection').classList.remove('hidden');
|
| 744 |
document.getElementById('email').focus();
|
| 745 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 746 |
</script>
|
| 747 |
</body>
|
| 748 |
</html>
|
server.js
CHANGED
|
@@ -9,19 +9,10 @@ const { getPool, initializeDatabase, closePool } = require('./db');
|
|
| 9 |
const otpService = require('./otpService');
|
| 10 |
const axios = require('axios');
|
| 11 |
const http = require('http');
|
| 12 |
-
const socketIo = require('socket.io');
|
| 13 |
const fs = require('fs');
|
| 14 |
|
| 15 |
const app = express();
|
| 16 |
const server = http.createServer(app);
|
| 17 |
-
const io = socketIo(server, {
|
| 18 |
-
cors: {
|
| 19 |
-
origin: "*",
|
| 20 |
-
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
| 21 |
-
allowedHeaders: ["Content-Type", "Authorization", "x-otp-token", "x-user-email", "x-user-id"],
|
| 22 |
-
credentials: true
|
| 23 |
-
}
|
| 24 |
-
});
|
| 25 |
const port = process.env.PORT || 3000;
|
| 26 |
|
| 27 |
// Initialize database and start server
|
|
@@ -76,6 +67,10 @@ let serverLogs = [];
|
|
| 76 |
let messageCache = new Map(); // userId -> { messages: [], lastUpdated: timestamp }
|
| 77 |
const CACHE_TTL = 30000; // 30 seconds
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
// Cache management functions
|
| 80 |
function getCachedMessages(userId) {
|
| 81 |
const cached = messageCache.get(userId);
|
|
@@ -96,6 +91,79 @@ function invalidateUserCache(userId) {
|
|
| 96 |
messageCache.delete(userId);
|
| 97 |
}
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
// Logging middleware
|
| 100 |
function logRequest(req, res, next) {
|
| 101 |
const start = Date.now();
|
|
@@ -169,7 +237,8 @@ function emitLog(logData) {
|
|
| 169 |
serverLogs = serverLogs.slice(0, 1000);
|
| 170 |
}
|
| 171 |
|
| 172 |
-
|
|
|
|
| 173 |
}
|
| 174 |
|
| 175 |
// Manual logging function
|
|
@@ -675,14 +744,35 @@ app.post('/api/messages', checkOtpAuth, async (req, res) => {
|
|
| 675 |
invalidateUserCache(userId);
|
| 676 |
|
| 677 |
// Emit real-time update to chat room
|
| 678 |
-
|
| 679 |
message: newMessage,
|
| 680 |
userEmail: userEmail
|
| 681 |
});
|
| 682 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 683 |
res.json({ success: true, message: newMessage });
|
| 684 |
} catch (error) {
|
| 685 |
console.error('Error sending message:', error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 686 |
res.status(500).json({ success: false, error: 'Failed to send message' });
|
| 687 |
}
|
| 688 |
});
|
|
@@ -709,6 +799,138 @@ app.get('/isubmissionopen', (req, res) => {
|
|
| 709 |
});
|
| 710 |
});
|
| 711 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 712 |
// Admin routes
|
| 713 |
app.get('/api/admin/users', async (req, res) => {
|
| 714 |
try {
|
|
@@ -763,14 +985,34 @@ app.post('/api/admin/messages', async (req, res) => {
|
|
| 763 |
invalidateUserCache(userId);
|
| 764 |
|
| 765 |
// Emit real-time update to chat room
|
| 766 |
-
|
| 767 |
message: newMessage,
|
| 768 |
userEmail: 'admin'
|
| 769 |
});
|
| 770 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
res.json({ success: true, message: newMessage });
|
| 772 |
} catch (error) {
|
| 773 |
-
console.error('Error sending message:', error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 774 |
res.status(500).json({ success: false, error: 'Failed to send message' });
|
| 775 |
}
|
| 776 |
});
|
|
@@ -897,62 +1139,6 @@ app.get('/api/admin/submissions/:submissionId', async (req, res) => {
|
|
| 897 |
}
|
| 898 |
});
|
| 899 |
|
| 900 |
-
// Socket.IO connection handling
|
| 901 |
-
io.on('connection', (socket) => {
|
| 902 |
-
log('info', 'Client connected to server logs', {
|
| 903 |
-
socketId: socket.id,
|
| 904 |
-
totalConnections: io.engine.clientsCount
|
| 905 |
-
});
|
| 906 |
-
|
| 907 |
-
// Send existing logs to new client
|
| 908 |
-
socket.emit('server-log', {
|
| 909 |
-
type: 'info',
|
| 910 |
-
message: 'Connected to server logs',
|
| 911 |
-
details: {
|
| 912 |
-
socketId: socket.id,
|
| 913 |
-
totalLogs: serverLogs.length
|
| 914 |
-
}
|
| 915 |
-
});
|
| 916 |
-
|
| 917 |
-
// Chat functionality
|
| 918 |
-
socket.on('join-chat', (data) => {
|
| 919 |
-
const { userId, userEmail } = data;
|
| 920 |
-
if (userId && userEmail) {
|
| 921 |
-
socket.join(`chat-${userId}`);
|
| 922 |
-
log('info', 'User joined chat room', {
|
| 923 |
-
socketId: socket.id,
|
| 924 |
-
userId: userId,
|
| 925 |
-
userEmail: userEmail
|
| 926 |
-
});
|
| 927 |
-
}
|
| 928 |
-
});
|
| 929 |
-
|
| 930 |
-
socket.on('leave-chat', (data) => {
|
| 931 |
-
const { userId } = data;
|
| 932 |
-
if (userId) {
|
| 933 |
-
socket.leave(`chat-${userId}`);
|
| 934 |
-
log('info', 'User left chat room', {
|
| 935 |
-
socketId: socket.id,
|
| 936 |
-
userId: userId
|
| 937 |
-
});
|
| 938 |
-
}
|
| 939 |
-
});
|
| 940 |
-
|
| 941 |
-
socket.on('disconnect', () => {
|
| 942 |
-
log('info', 'Client disconnected from server logs', {
|
| 943 |
-
socketId: socket.id,
|
| 944 |
-
totalConnections: io.engine.clientsCount
|
| 945 |
-
});
|
| 946 |
-
});
|
| 947 |
-
|
| 948 |
-
socket.on('request-logs', () => {
|
| 949 |
-
log('info', 'Client requested historical logs', { socketId: socket.id });
|
| 950 |
-
// Send last 100 logs
|
| 951 |
-
const recentLogs = serverLogs.slice(0, 100);
|
| 952 |
-
socket.emit('historical-logs', recentLogs);
|
| 953 |
-
});
|
| 954 |
-
});
|
| 955 |
-
|
| 956 |
// Error handling
|
| 957 |
process.on('uncaughtException', (error) => {
|
| 958 |
log('error', 'Uncaught Exception', {
|
|
@@ -971,16 +1157,21 @@ process.on('unhandledRejection', (reason, promise) => {
|
|
| 971 |
// Periodic system logs
|
| 972 |
setInterval(() => {
|
| 973 |
const memoryUsage = process.memoryUsage();
|
| 974 |
-
|
| 975 |
memory: {
|
| 976 |
rss: Math.round(memoryUsage.rss / 1024 / 1024) + 'MB',
|
| 977 |
heapUsed: Math.round(memoryUsage.heapUsed / 1024 / 1024) + 'MB',
|
| 978 |
heapTotal: Math.round(memoryUsage.heapTotal / 1024 / 1024) + 'MB'
|
| 979 |
},
|
| 980 |
uptime: Math.round(process.uptime()) + 's',
|
| 981 |
-
connections:
|
| 982 |
cacheSize: messageCache.size
|
| 983 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 984 |
}, 30000); // Every 30 seconds
|
| 985 |
|
| 986 |
// Periodic cache cleanup
|
|
|
|
| 9 |
const otpService = require('./otpService');
|
| 10 |
const axios = require('axios');
|
| 11 |
const http = require('http');
|
|
|
|
| 12 |
const fs = require('fs');
|
| 13 |
|
| 14 |
const app = express();
|
| 15 |
const server = http.createServer(app);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
const port = process.env.PORT || 3000;
|
| 17 |
|
| 18 |
// Initialize database and start server
|
|
|
|
| 67 |
let messageCache = new Map(); // userId -> { messages: [], lastUpdated: timestamp }
|
| 68 |
const CACHE_TTL = 30000; // 30 seconds
|
| 69 |
|
| 70 |
+
// Event Stream clients management
|
| 71 |
+
let eventStreamClients = new Map(); // userId -> Set of response objects
|
| 72 |
+
let adminClients = new Set(); // Set of admin response objects
|
| 73 |
+
|
| 74 |
// Cache management functions
|
| 75 |
function getCachedMessages(userId) {
|
| 76 |
const cached = messageCache.get(userId);
|
|
|
|
| 91 |
messageCache.delete(userId);
|
| 92 |
}
|
| 93 |
|
| 94 |
+
// Event Stream management functions
|
| 95 |
+
function addEventStreamClient(userId, res) {
|
| 96 |
+
if (!eventStreamClients.has(userId)) {
|
| 97 |
+
eventStreamClients.set(userId, new Set());
|
| 98 |
+
}
|
| 99 |
+
eventStreamClients.get(userId).add(res);
|
| 100 |
+
|
| 101 |
+
// Set up connection cleanup
|
| 102 |
+
res.on('close', () => {
|
| 103 |
+
removeEventStreamClient(userId, res);
|
| 104 |
+
});
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
function removeEventStreamClient(userId, res) {
|
| 108 |
+
const clients = eventStreamClients.get(userId);
|
| 109 |
+
if (clients) {
|
| 110 |
+
clients.delete(res);
|
| 111 |
+
if (clients.size === 0) {
|
| 112 |
+
eventStreamClients.delete(userId);
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
function addAdminClient(res) {
|
| 118 |
+
adminClients.add(res);
|
| 119 |
+
|
| 120 |
+
// Set up connection cleanup
|
| 121 |
+
res.on('close', () => {
|
| 122 |
+
adminClients.delete(res);
|
| 123 |
+
});
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
function broadcastToUser(userId, event, data) {
|
| 127 |
+
const clients = eventStreamClients.get(userId);
|
| 128 |
+
if (clients) {
|
| 129 |
+
const message = `data: ${JSON.stringify({ event, data, timestamp: new Date().toISOString() })}\n\n`;
|
| 130 |
+
clients.forEach(client => {
|
| 131 |
+
if (!client.destroyed) {
|
| 132 |
+
client.write(message);
|
| 133 |
+
}
|
| 134 |
+
});
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
function broadcastToAdmins(event, data) {
|
| 139 |
+
const message = `data: ${JSON.stringify({ event, data, timestamp: new Date().toISOString() })}\n\n`;
|
| 140 |
+
adminClients.forEach(client => {
|
| 141 |
+
if (!client.destroyed) {
|
| 142 |
+
client.write(message);
|
| 143 |
+
}
|
| 144 |
+
});
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
function broadcastToAll(event, data) {
|
| 148 |
+
const message = `data: ${JSON.stringify({ event, data, timestamp: new Date().toISOString() })}\n\n`;
|
| 149 |
+
|
| 150 |
+
// Broadcast to all user clients
|
| 151 |
+
eventStreamClients.forEach((clients, userId) => {
|
| 152 |
+
clients.forEach(client => {
|
| 153 |
+
if (!client.destroyed) {
|
| 154 |
+
client.write(message);
|
| 155 |
+
}
|
| 156 |
+
});
|
| 157 |
+
});
|
| 158 |
+
|
| 159 |
+
// Broadcast to admin clients
|
| 160 |
+
adminClients.forEach(client => {
|
| 161 |
+
if (!client.destroyed) {
|
| 162 |
+
client.write(message);
|
| 163 |
+
}
|
| 164 |
+
});
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
// Logging middleware
|
| 168 |
function logRequest(req, res, next) {
|
| 169 |
const start = Date.now();
|
|
|
|
| 237 |
serverLogs = serverLogs.slice(0, 1000);
|
| 238 |
}
|
| 239 |
|
| 240 |
+
// Broadcast to all admin clients for server logs
|
| 241 |
+
broadcastToAdmins('server-log', logData);
|
| 242 |
}
|
| 243 |
|
| 244 |
// Manual logging function
|
|
|
|
| 744 |
invalidateUserCache(userId);
|
| 745 |
|
| 746 |
// Emit real-time update to chat room
|
| 747 |
+
broadcastToUser(userId, 'new-message', {
|
| 748 |
message: newMessage,
|
| 749 |
userEmail: userEmail
|
| 750 |
});
|
| 751 |
|
| 752 |
+
// Emit message sent confirmation
|
| 753 |
+
broadcastToUser(userId, 'message-sent', {
|
| 754 |
+
messageId: newMessage.id,
|
| 755 |
+
userId: userId,
|
| 756 |
+
timestamp: new Date().toISOString()
|
| 757 |
+
});
|
| 758 |
+
|
| 759 |
+
log('info', 'Message sent and saved to database', {
|
| 760 |
+
messageId: newMessage.id,
|
| 761 |
+
userId: userId,
|
| 762 |
+
sender: sender,
|
| 763 |
+
contentLength: content.length
|
| 764 |
+
});
|
| 765 |
+
|
| 766 |
res.json({ success: true, message: newMessage });
|
| 767 |
} catch (error) {
|
| 768 |
console.error('Error sending message:', error);
|
| 769 |
+
|
| 770 |
+
// Emit error to client
|
| 771 |
+
broadcastToUser(userId, 'message-error', {
|
| 772 |
+
error: 'Failed to send message',
|
| 773 |
+
userId: userId
|
| 774 |
+
});
|
| 775 |
+
|
| 776 |
res.status(500).json({ success: false, error: 'Failed to send message' });
|
| 777 |
}
|
| 778 |
});
|
|
|
|
| 799 |
});
|
| 800 |
});
|
| 801 |
|
| 802 |
+
// Event Stream endpoints
|
| 803 |
+
app.get('/events/user/:userId', checkOtpAuth, (req, res) => {
|
| 804 |
+
const { userId } = req.params;
|
| 805 |
+
const userEmail = req.headers['x-user-email'];
|
| 806 |
+
|
| 807 |
+
// Verify that the user is requesting their own events
|
| 808 |
+
if (parseInt(userId) !== parseInt(req.headers['x-user-id'])) {
|
| 809 |
+
return res.status(403).json({ success: false, error: 'Unauthorized access' });
|
| 810 |
+
}
|
| 811 |
+
|
| 812 |
+
// Set up Event Stream headers
|
| 813 |
+
res.writeHead(200, {
|
| 814 |
+
'Content-Type': 'text/event-stream',
|
| 815 |
+
'Cache-Control': 'no-cache',
|
| 816 |
+
'Connection': 'keep-alive',
|
| 817 |
+
'Access-Control-Allow-Origin': '*',
|
| 818 |
+
'Access-Control-Allow-Headers': 'Cache-Control'
|
| 819 |
+
});
|
| 820 |
+
|
| 821 |
+
// Send initial connection message
|
| 822 |
+
res.write(`data: ${JSON.stringify({
|
| 823 |
+
event: 'connected',
|
| 824 |
+
data: { userId, userEmail, timestamp: new Date().toISOString() }
|
| 825 |
+
})}\n\n`);
|
| 826 |
+
|
| 827 |
+
// Add client to event stream
|
| 828 |
+
addEventStreamClient(userId, res);
|
| 829 |
+
|
| 830 |
+
log('info', 'User connected to event stream', { userId, userEmail });
|
| 831 |
+
});
|
| 832 |
+
|
| 833 |
+
app.get('/events/admin', (req, res) => {
|
| 834 |
+
// Set up Event Stream headers
|
| 835 |
+
res.writeHead(200, {
|
| 836 |
+
'Content-Type': 'text/event-stream',
|
| 837 |
+
'Cache-Control': 'no-cache',
|
| 838 |
+
'Connection': 'keep-alive',
|
| 839 |
+
'Access-Control-Allow-Origin': '*',
|
| 840 |
+
'Access-Control-Allow-Headers': 'Cache-Control'
|
| 841 |
+
});
|
| 842 |
+
|
| 843 |
+
// Send initial connection message
|
| 844 |
+
res.write(`data: ${JSON.stringify({
|
| 845 |
+
event: 'connected',
|
| 846 |
+
data: { type: 'admin', timestamp: new Date().toISOString() }
|
| 847 |
+
})}\n\n`);
|
| 848 |
+
|
| 849 |
+
// Send existing logs
|
| 850 |
+
const recentLogs = serverLogs.slice(0, 100);
|
| 851 |
+
res.write(`data: ${JSON.stringify({
|
| 852 |
+
event: 'historical-logs',
|
| 853 |
+
data: recentLogs
|
| 854 |
+
})}\n\n`);
|
| 855 |
+
|
| 856 |
+
// Add client to admin event stream
|
| 857 |
+
addAdminClient(res);
|
| 858 |
+
|
| 859 |
+
log('info', 'Admin connected to event stream');
|
| 860 |
+
});
|
| 861 |
+
|
| 862 |
+
// Typing indicator endpoints
|
| 863 |
+
app.post('/api/typing', checkOtpAuth, (req, res) => {
|
| 864 |
+
const { userId } = req.body;
|
| 865 |
+
const userEmail = req.headers['x-user-email'];
|
| 866 |
+
|
| 867 |
+
if (!userId) {
|
| 868 |
+
return res.status(400).json({ success: false, error: 'User ID required' });
|
| 869 |
+
}
|
| 870 |
+
|
| 871 |
+
// Broadcast typing indicator to other users in the chat
|
| 872 |
+
broadcastToUser(userId, 'user-typing', {
|
| 873 |
+
userId: userId,
|
| 874 |
+
userEmail: userEmail,
|
| 875 |
+
timestamp: new Date().toISOString()
|
| 876 |
+
});
|
| 877 |
+
|
| 878 |
+
res.json({ success: true });
|
| 879 |
+
});
|
| 880 |
+
|
| 881 |
+
app.post('/api/stop-typing', checkOtpAuth, (req, res) => {
|
| 882 |
+
const { userId } = req.body;
|
| 883 |
+
const userEmail = req.headers['x-user-email'];
|
| 884 |
+
|
| 885 |
+
if (!userId) {
|
| 886 |
+
return res.status(400).json({ success: false, error: 'User ID required' });
|
| 887 |
+
}
|
| 888 |
+
|
| 889 |
+
// Broadcast stop typing indicator
|
| 890 |
+
broadcastToUser(userId, 'user-stopped-typing', {
|
| 891 |
+
userId: userId,
|
| 892 |
+
userEmail: userEmail,
|
| 893 |
+
timestamp: new Date().toISOString()
|
| 894 |
+
});
|
| 895 |
+
|
| 896 |
+
res.json({ success: true });
|
| 897 |
+
});
|
| 898 |
+
|
| 899 |
+
// Admin typing indicator endpoints
|
| 900 |
+
app.post('/api/admin/typing', (req, res) => {
|
| 901 |
+
const { userId } = req.body;
|
| 902 |
+
|
| 903 |
+
if (!userId) {
|
| 904 |
+
return res.status(400).json({ success: false, error: 'User ID required' });
|
| 905 |
+
}
|
| 906 |
+
|
| 907 |
+
// Broadcast typing indicator to user
|
| 908 |
+
broadcastToUser(userId, 'user-typing', {
|
| 909 |
+
userId: userId,
|
| 910 |
+
userEmail: 'admin',
|
| 911 |
+
timestamp: new Date().toISOString()
|
| 912 |
+
});
|
| 913 |
+
|
| 914 |
+
res.json({ success: true });
|
| 915 |
+
});
|
| 916 |
+
|
| 917 |
+
app.post('/api/admin/stop-typing', (req, res) => {
|
| 918 |
+
const { userId } = req.body;
|
| 919 |
+
|
| 920 |
+
if (!userId) {
|
| 921 |
+
return res.status(400).json({ success: false, error: 'User ID required' });
|
| 922 |
+
}
|
| 923 |
+
|
| 924 |
+
// Broadcast stop typing indicator
|
| 925 |
+
broadcastToUser(userId, 'user-stopped-typing', {
|
| 926 |
+
userId: userId,
|
| 927 |
+
userEmail: 'admin',
|
| 928 |
+
timestamp: new Date().toISOString()
|
| 929 |
+
});
|
| 930 |
+
|
| 931 |
+
res.json({ success: true });
|
| 932 |
+
});
|
| 933 |
+
|
| 934 |
// Admin routes
|
| 935 |
app.get('/api/admin/users', async (req, res) => {
|
| 936 |
try {
|
|
|
|
| 985 |
invalidateUserCache(userId);
|
| 986 |
|
| 987 |
// Emit real-time update to chat room
|
| 988 |
+
broadcastToUser(userId, 'new-message', {
|
| 989 |
message: newMessage,
|
| 990 |
userEmail: 'admin'
|
| 991 |
});
|
| 992 |
|
| 993 |
+
// Emit message sent confirmation
|
| 994 |
+
broadcastToUser(userId, 'message-sent', {
|
| 995 |
+
messageId: newMessage.id,
|
| 996 |
+
userId: userId,
|
| 997 |
+
timestamp: new Date().toISOString()
|
| 998 |
+
});
|
| 999 |
+
|
| 1000 |
+
log('info', 'Admin message sent and saved to database', {
|
| 1001 |
+
messageId: newMessage.id,
|
| 1002 |
+
userId: userId,
|
| 1003 |
+
contentLength: content.length
|
| 1004 |
+
});
|
| 1005 |
+
|
| 1006 |
res.json({ success: true, message: newMessage });
|
| 1007 |
} catch (error) {
|
| 1008 |
+
console.error('Error sending admin message:', error);
|
| 1009 |
+
|
| 1010 |
+
// Emit error to client
|
| 1011 |
+
broadcastToUser(userId, 'message-error', {
|
| 1012 |
+
error: 'Failed to send message',
|
| 1013 |
+
userId: userId
|
| 1014 |
+
});
|
| 1015 |
+
|
| 1016 |
res.status(500).json({ success: false, error: 'Failed to send message' });
|
| 1017 |
}
|
| 1018 |
});
|
|
|
|
| 1139 |
}
|
| 1140 |
});
|
| 1141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1142 |
// Error handling
|
| 1143 |
process.on('uncaughtException', (error) => {
|
| 1144 |
log('error', 'Uncaught Exception', {
|
|
|
|
| 1157 |
// Periodic system logs
|
| 1158 |
setInterval(() => {
|
| 1159 |
const memoryUsage = process.memoryUsage();
|
| 1160 |
+
const systemStatus = {
|
| 1161 |
memory: {
|
| 1162 |
rss: Math.round(memoryUsage.rss / 1024 / 1024) + 'MB',
|
| 1163 |
heapUsed: Math.round(memoryUsage.heapUsed / 1024 / 1024) + 'MB',
|
| 1164 |
heapTotal: Math.round(memoryUsage.heapTotal / 1024 / 1024) + 'MB'
|
| 1165 |
},
|
| 1166 |
uptime: Math.round(process.uptime()) + 's',
|
| 1167 |
+
connections: eventStreamClients.size + adminClients.size,
|
| 1168 |
cacheSize: messageCache.size
|
| 1169 |
+
};
|
| 1170 |
+
|
| 1171 |
+
log('info', 'System status', systemStatus);
|
| 1172 |
+
|
| 1173 |
+
// Broadcast system status to admin clients
|
| 1174 |
+
broadcastToAdmins('system-status', systemStatus);
|
| 1175 |
}, 30000); // Every 30 seconds
|
| 1176 |
|
| 1177 |
// Periodic cache cleanup
|