-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealthcareAccessPlatform.sol
446 lines (389 loc) · 15.9 KB
/
HealthcareAccessPlatform.sol
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyNFT is ERC721URIStorage, Ownable {
using Counters for Counters.Counter;
Counters.Counter private patientIdCounter;
Counters.Counter private hospitalIdCounter;
Counters.Counter private appointmentIdCounter;
// Struct to store patient details
struct Patient {
string name;
uint256 age;
string dob; // Date of Birth
string gender;
string contactNumber;
string email;
string medicalHistory;
address patientWallet; // Wallet address of the patient
address[] authorizedViewers; // Array to store authorized viewers
string height; // Additional health-related field
string weight; // Additional health-related field
string bloodPressure; // Additional health-related field
string[] healthRecordIpfsHashes; // Array to store IPFS hashes of health records
}
// Struct to store hospital details
struct Hospital {
string name;
string h_type;
string h_address;
string state;
string district;
string link;
address hospitalWallet; // Wallet address of the hospital
}
struct Appointment {
uint256 patientId;
uint256 hospitalId;
string date;
string description;
string stringField1;
string stringField2;
string stringField3;
string stringField4;
string stringField5;
string followUpDate;
}
// Mapping from patient ID to patient details
mapping(uint256 => Patient) private patients;
// Mapping from hospital ID to hospital details
mapping(uint256 => Hospital) private hospitals;
mapping(uint256 => Appointment) private appointments;
mapping(uint256 => uint256[]) private patientAppointments;
mapping(uint256 => uint256[]) private hospitalAppointments;
event AppointmentCreated(uint256 appointmentId, uint256 patientId, uint256 hospitalId, string date, string description);
// Event to log patient registration
event PatientRegistered(uint256 patientId, string name, uint256 age, string dob, string gender, string contactNumber, string email, address patientWallet);
// Event to log authorized viewer added
event AuthorizedViewerAdded(uint256 patientId, address viewer);
// Event to log authorized viewer removed
event AuthorizedViewerRemoved(uint256 patientId, address viewer);
// Event to log health record added
event HealthRecordAdded(uint256 patientId, string ipfsHash);
// Event to log hospital registration
event HospitalRegistered(uint256 hospitalId, string name, string h_type, string hospitalAddress, string state, string district, string link, address hospitalWallet);
// Constructor initializes the NFT with a name and symbol
constructor() ERC721("HealthcareNFT", "HCN") {}
modifier onlyPatientOwner(uint256 _patientId) {
require(patients[_patientId].patientWallet == msg.sender, "Not the owner of the patient record");
_;
}
// Function to check if a patient is registered
function checkPatientRegistration() external view returns (bool) {
uint256 patientId = _getPatientIdByAddress(msg.sender);
return (patientId != 0);
}
// Function to register a patient and mint an NFT
function registerPatient(
string memory _name,
uint256 _age,
string memory _dob,
string memory _gender,
string memory _contactNumber,
string memory _email,
string memory _medicalHistory
) external returns (uint256) {
require(bytes(_name).length > 0, "Patient name cannot be empty");
require(_getPatientIdByAddress(msg.sender) == 0, "Patient is already registered");
// Mint a new NFT for the patient
patientIdCounter.increment();
uint256 newPatientId = patientIdCounter.current();
_safeMint(msg.sender, newPatientId);
// Create a new patient record
Patient storage patient = patients[newPatientId];
patient.name = _name;
patient.age = _age;
patient.dob = _dob;
patient.gender = _gender;
patient.contactNumber = _contactNumber;
patient.email = _email;
patient.medicalHistory = _medicalHistory;
patient.patientWallet = msg.sender; // Store the wallet address
emit PatientRegistered(newPatientId, _name, _age, _dob, _gender, _contactNumber, _email, msg.sender);
// Increment the patient ID counter
patientIdCounter.increment();
// Return the patient ID
return newPatientId;
}
// Function to get patient details by ID
function getPatientDetails(uint256 _patientId)
external
view
returns (
string memory name,
uint256 age,
string memory dob,
string memory gender,
string memory contactNumber,
string memory email,
string memory medicalHistory,
string memory height,
string memory weight,
string memory bloodPressure
)
{
Patient storage patient = patients[_patientId];
require(msg.sender == patients[_patientId].patientWallet || isAuthorizedViewer(_patientId, msg.sender), "Not authorized to view patient details");
return (
patient.name,
patient.age,
patient.dob,
patient.gender,
patient.contactNumber,
patient.email,
patient.medicalHistory,
patient.height,
patient.weight,
patient.bloodPressure
);
}
// Function to update medical history
function updateMedicalHistory(uint256 _patientId, string memory _newMedicalHistory) external onlyPatientOwner(_patientId) {
Patient storage patient = patients[_patientId];
patient.medicalHistory = _newMedicalHistory;
// You can add additional logic or events as needed
}
// Function to add an authorized viewer
function addAuthorizedViewer(uint256 _patientId, address _viewer) external onlyPatientOwner(_patientId) {
Patient storage patient = patients[_patientId];
patient.authorizedViewers.push(_viewer);
emit AuthorizedViewerAdded(_patientId, _viewer);
}
// Function to remove an authorized viewer
function removeAuthorizedViewer(uint256 _patientId, address _viewer) external onlyPatientOwner(_patientId) {
Patient storage patient = patients[_patientId];
require(_viewer != address(0), "Invalid viewer address");
for (uint256 i = 0; i < patient.authorizedViewers.length; i++) {
if (patient.authorizedViewers[i] == _viewer) {
patient.authorizedViewers[i] = patient.authorizedViewers[patient.authorizedViewers.length - 1];
patient.authorizedViewers.pop();
emit AuthorizedViewerRemoved(_patientId, _viewer);
break;
}
}
}
// Function to check if an address is an authorized viewer
function isAuthorizedViewer(uint256 _patientId, address _viewer) public view returns (bool) {
Patient storage patient = patients[_patientId];
for (uint256 i = 0; i < patient.authorizedViewers.length; i++) {
if (patient.authorizedViewers[i] == _viewer) {
return true;
}
}
return false;
}
// Function to create a health record with additional details
function createHealthRecord(
uint256 _patientId,
string memory _height,
string memory _weight,
string memory _bloodPressure
) external onlyPatientOwner(_patientId) {
Patient storage patient = patients[_patientId];
patient.height = _height;
patient.weight = _weight;
patient.bloodPressure = _bloodPressure;
}
// Function to add a health record using IPFS hash
function addHealthRecord(uint256 _patientId, string memory _ipfsHash) external onlyPatientOwner(_patientId) {
Patient storage patient = patients[_patientId];
patient.healthRecordIpfsHashes.push(_ipfsHash);
emit HealthRecordAdded(_patientId, _ipfsHash);
}
// Function to get all health records for a patient
function getHealthRecords(uint256 _patientId) external view onlyPatientOwner(_patientId) returns (string[] memory) {
Patient storage patient = patients[_patientId];
return patient.healthRecordIpfsHashes;
}
// Function to get patient details by wallet address
function getPatientDetailsByAddress()
external
view
returns (
string memory name,
uint256 age,
string memory dob,
string memory gender,
string memory contactNumber,
string memory email,
string memory medicalHistory,
string memory height,
string memory weight,
string memory bloodPressure
)
{
uint256 patientId = _getPatientIdByAddress(msg.sender);
Patient storage patient = patients[patientId];
require(patientId != 0, "Patient not found");
return (
patient.name,
patient.age,
patient.dob,
patient.gender,
patient.contactNumber,
patient.email,
patient.medicalHistory,
patient.height,
patient.weight,
patient.bloodPressure
);
}
// Internal function to get patient ID by wallet address
function _getPatientIdByAddress(address _patientAddress) internal view returns (uint256) {
for (uint256 i = 1; i <= patientIdCounter.current(); i++) {
if (patients[i].patientWallet == _patientAddress) {
return i;
}
}
return 0; // Return 0 if patient not found
}
// Function to get patient ID by wallet address
function getPatientIdByAddress() external view returns (uint256) {
return _getPatientIdByAddress(msg.sender);
}
// Function to register a hospital
function registerHospital(
string memory _name,
string memory _h_type,
string memory _address,
string memory _state,
string memory _district,
string memory _link
) external {
// Mint a new NFT for the hospital
hospitalIdCounter.increment();
uint256 newHospitalId = hospitalIdCounter.current();
// _safeMint(msg.sender, newHospitalId);
// Create a new hospital record
Hospital storage hospital = hospitals[newHospitalId];
hospital.name = _name;
hospital.h_type = _h_type;
hospital.h_address = _address;
hospital.state = _state;
hospital.district = _district;
hospital.link = _link;
hospital.hospitalWallet = msg.sender; // Store the wallet address
emit HospitalRegistered(newHospitalId, _name, _h_type, _address, _state, _district, _link, msg.sender);
}
// Function to get hospital details by ID
function getHospitalDetails(uint256 _hospitalId)
external
view
returns (
string memory name,
string memory h_type,
string memory hospitalAddress,
string memory state,
string memory district,
string memory link
)
{
Hospital storage hospital = hospitals[_hospitalId];
return (
hospital.name,
hospital.h_type,
hospital.h_address,
hospital.state,
hospital.district,
hospital.link
);
}
// Function to get hospital details by wallet address
function getHospitalDetailsByAddress() external view returns (string memory name, string memory h_type, string memory hospitalAddress, string memory state, string memory district, string memory link ) {
uint256 hospitalId = _getHospitalIdByAddress(msg.sender);
Hospital storage hospital = hospitals[hospitalId];
require(hospitalId != 0, "Hospital not found");
return (hospital.name, hospital.h_type, hospital.h_address, hospital.state, hospital.district, hospital.link);
}
// Internal function to get hospital ID by wallet address
function _getHospitalIdByAddress(address _hospitalAddress) internal view returns (uint256) {
for (uint256 i = 1; i <= hospitalIdCounter.current(); i++) {
if (hospitals[i].hospitalWallet == _hospitalAddress) {
return i;
}
}
return 0; // Return 0 if hospital not found
}
// Function to get hospital ID by wallet address
function getHospitalIdByAddress() external view returns (uint256) {
return _getHospitalIdByAddress(msg.sender);
}
function getAllHospitals() external view returns (uint256[] memory) {
uint256[] memory hospitalIds = new uint256[](hospitalIdCounter.current());
for (uint256 i = 1; i <= hospitalIdCounter.current(); i++) {
hospitalIds[i - 1] = i;
}
return hospitalIds;
}
function getHealthRecordIpfsHashes(uint256 _patientId) external view returns (string[] memory) {
require(msg.sender == patients[_patientId].patientWallet || isAuthorizedViewer(_patientId, msg.sender), "Not authorized to view health records");
return patients[_patientId].healthRecordIpfsHashes;
}
function createAppointment(
uint256 _patientId,
uint256 _hospitalId,
string memory _date,
string memory _description
) external {
require(bytes(_description).length > 0, "Description cannot be empty");
appointmentIdCounter.increment();
uint256 newAppointmentId = appointmentIdCounter.current();
// Create a new appointment record
Appointment storage appointment = appointments[newAppointmentId];
appointment.patientId = _patientId;
appointment.hospitalId = _hospitalId;
appointment.date = _date;
appointment.description = _description;
patientAppointments[_patientId].push(newAppointmentId);
hospitalAppointments[_patientId].push(newAppointmentId);
emit AppointmentCreated(newAppointmentId, _patientId, _hospitalId, _date, _description);
}
function createPrescription(
uint256 _app_id,
string memory _feild1,
string memory _f3,
string memory _f4,
string memory _f5
) external {
Appointment storage appointment = appointments[_app_id];
// Create a new appointment record
appointment.stringField1 = _feild1;
appointment.stringField2 = _f3;
appointment.stringField3 = _f4;
appointment.followUpDate = _f5;
}
function getAppointmentDetails(uint256 _appointmentId)
external
view
returns (
uint256 patientId,
uint256 hospitalId,
string memory date,
string memory description,
string memory stringField1,
string memory stringField2,
string memory stringField3,
string memory followUpDate
)
{
Appointment storage appointment = appointments[_appointmentId];
return (
appointment.patientId,
appointment.hospitalId,
appointment.date,
appointment.description,
appointment.stringField1,
appointment.stringField2,
appointment.stringField3,
appointment.followUpDate
);
}
function getPatientAppointments(uint256 _patientId) external view returns (uint256[] memory) {
return patientAppointments[_patientId];
}
function getHospitalAppointments(uint256 _HospitalId) external view returns (uint256[] memory) {
return hospitalAppointments[_HospitalId];
}
}