-
Notifications
You must be signed in to change notification settings - Fork 1
/
MedArchive.sol
102 lines (87 loc) · 3.18 KB
/
MedArchive.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
//SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
error MedArchive__isNotYourPatient();
contract MedArchive {
address public s_owner;
struct PatientRecord {
uint256 id;
uint256 date;
string description;
string cid;
}
struct Patient {
uint256 patientId;
string patientName;
address hospitalAddr;
}
mapping(address => uint256) private s_patientIds;
mapping(address => mapping(uint256 => Patient)) private s_hospitalPatients;
mapping(address => mapping(uint256 => PatientRecord[])) private s_hospitalRecords;
mapping(address => PatientRecord[]) private s_sharedRecords;
event PatientAdded(address indexed hospitalAddress, uint256 patientId);
event NewRecord(address indexed hospitalAddress, uint256 indexed patientId, uint256 recordId);
event PatientShared(
address indexed hospitalAddress,
uint256 indexed patientId,
uint256 recordId
);
modifier isYourPatient(uint256 _patientId) {
// only authorized address can alter patient record
if (s_hospitalPatients[msg.sender][_patientId].hospitalAddr != msg.sender) {
revert MedArchive__isNotYourPatient();
}
_;
}
constructor() {
s_owner = msg.sender;
}
function addPatient(string calldata _patientName) external {
uint256 id = s_patientIds[msg.sender] += 1;
s_hospitalPatients[msg.sender][id] = Patient({
patientId: id,
patientName: _patientName,
hospitalAddr: msg.sender
});
emit PatientAdded(msg.sender, id);
}
function addRecord(
uint256 _patientId,
string calldata _description,
string calldata _cid
) external isYourPatient(_patientId) {
// isYourPatient Modifier already takes care of if patient does not exist
uint256 recordId = s_hospitalRecords[msg.sender][_patientId].length;
s_hospitalRecords[msg.sender][_patientId].push(
PatientRecord({
id: recordId,
date: block.timestamp,
description: _description,
cid: _cid
})
);
emit NewRecord(msg.sender, _patientId, recordId);
}
function sharePatientsRecord(
address _to,
uint256 _patientId,
uint256 _recordId
) external isYourPatient(_patientId) {
s_sharedRecords[_to].push(s_hospitalRecords[msg.sender][_patientId][_recordId]);
emit PatientShared(msg.sender, _patientId, _recordId);
}
function getPatientRecords(uint256 _patientId) external view returns (PatientRecord[] memory) {
return s_hospitalRecords[msg.sender][_patientId];
}
function getPatientRecord(
uint256 _patientId,
uint256 _recordId
) external view returns (PatientRecord memory) {
return s_hospitalRecords[msg.sender][_patientId][_recordId];
}
function getPatient(uint256 _patientId) external view returns (Patient memory) {
return s_hospitalPatients[msg.sender][_patientId];
}
function getSharedRecords() external view returns (PatientRecord[] memory) {
return s_sharedRecords[msg.sender];
}
}