import express from 'express';
import cors from 'cors';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const port = 3001;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// Store messages in memory (for demo purposes)
const messages = [];
const cases = [];
const users = [
{ id: 1, name: 'John Doe', phone: '+1234567890', role: 'user' },
{ id: 2, name: 'NGO Worker', phone: '+0987654321', role: 'ngo' },
{ id: 3, name: 'Legal Advocate', phone: '+1122334455', role: 'advocate' }
];
// Bot responses based on role
const botResponses = {
user: [
"I understand your legal concern. Based on the information provided, I would recommend...",
"That's a complex legal situation. Here are the key points to consider...",
"According to legal precedents in similar cases...",
"You may want to consider the following legal options..."
],
ngo: [
"Case has been registered. I'll assign an advocate to review it.",
"I've updated the case status and notified relevant parties.",
"The documentation has been processed. Next steps include...",
"I've scheduled a follow-up consultation for this case."
],
advocate: [
"I've reviewed the case details. Here's my legal analysis...",
"Based on the provided documentation, I recommend...",
"I'll prepare the necessary legal documents for...",
"Let me outline the legal strategy for this case..."
]
};
// Helper function to get random response
function getRandomResponse(role) {
const responses = botResponses[role];
return responses[Math.floor(Math.random() * responses.length)];
}
// Routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.post('/api/chat', (req, res) => {
const { text, role } = req.body;
// Store user message
messages.push({
text,
role,
timestamp: new Date(),
isUser: true
});
// Generate bot response
const botResponse = {
text: getRandomResponse(role),
timestamp: new Date(),
isUser: false
};
// Store bot response
messages.push(botResponse);
// Send response back to client
res.json(botResponse);
});
app.get('/api/messages', (req, res) => {
res.json(messages);
});
// Create a case
app.post('/api/cases', (req, res) => {
const { type, description } = req.body;
const newCase = {
id: Date.now(),
type,
description,
status: 'new',
createdAt: new Date()
};
cases.push(newCase);
res.json(newCase);
});
// Get cases
app.get('/api/cases', (req, res) => {
res.json(cases);
});
// Update case status
app.patch('/api/cases/:id', (req, res) => {
const { id } = req.params;
const { status } = req.body;
const caseToUpdate = cases.find(c => c.id === parseInt(id));
if (caseToUpdate) {
caseToUpdate.status = status;
res.json(caseToUpdate);
} else {
res.status(404).json({ error: 'Case not found' });
}
});
// Get user by phone
app.get('/api/users/:phone', (req, res) => {
const { phone } = req.params;
const user = users.find(u => u.phone === phone);
if (user) {
res.json(user);
} else {
res.status(404).json({ error: 'User not found' });
}
});
// Start server
app.listen(port, () => {
console.log(`Demo server running at http://localhost:${port}`);
});