-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (62 loc) · 2.67 KB
/
Copy pathindex.js
File metadata and controls
80 lines (62 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const { ethers } = require('ethers');
// MongoDB Models
const Patient = require('./models/Patient');
const Doctor = require('./models/Doctor');
const HealthData = require('./models/HealthData');
// Initialize Express app
const app = express();
app.use(bodyParser.json());
// MongoDB connection
mongoose.connect('mongodb://localhost:27017/healthcare', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => console.log('MongoDB connected'))
.catch(err => console.error('MongoDB connection error:', err));
// Set up ethers provider and contract
const provider = new ethers.providers.JsonRpcProvider('http://localhost:8545'); // Replace with your Ethereum node URL
const contractAddress = '0xa853EA63856f0BaB7F80729382932BeeE0bedE32';
const contractJson = fs.readFileSync(path.join(__dirname, 'artifacts/contracts/DPHRS.json'), 'utf8');
const ABI = JSON.parse(contractJson);
const abi = ABI.abi;
const contract = new ethers.Contract(contractAddress, abi, provider);
// Set up event listeners
contract.on('PatientRegistered', async (patientId, name, photo, age) => {
// Create new patient entry in MongoDB
await Patient.findOneAndUpdate({ patientId: patientId }, { name, photo, age }, { upsert: true });
});
contract.on('DoctorRegistered', async (doctorId, name, specialization, licenseNumber) => {
// Create new doctor entry in MongoDB
await Doctor.findOneAndUpdate({ doctorId: doctorId }, { name, specialization, licenseNumber }, { upsert: true });
});
contract.on('DataRegistered', async (patientId, timestamp, dataType, dataHash) => {
// Save health data to MongoDB
await HealthData.findOneAndUpdate({ patientId, timestamp }, { dataType, dataHash }, { upsert: true });
});
// Function to get patient data
app.get('/patient/:id', async (req, res) => {
const patientId = req.params.id;
const patient = await Patient.findOne({ patientId });
if (!patient) {
return res.status(404).send('Patient not found');
}
// Check if the requesting user is authorized
// (You need to handle authorization logic here)
res.json(patient);
});
// Function to get health data
app.get('/patient/:id/healthData', async (req, res) => {
const patientId = req.params.id;
const healthData = await HealthData.find({ patientId });
if (!healthData.length) {
return res.status(404).send('No health data found');
}
// Check if the requesting user is authorized
// (You need to handle authorization logic here)
res.json(healthData);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});