Skip to content

Instantly share code, notes, and snippets.

@MichalZalecki MichalZalecki/prescriptions.go Secret
Last active Jul 30, 2018

Embed
What would you like to do?
func (s *SmartContract) getPrescription(stub shim.ChaincodeStubInterface, args []string) peer.Response {
// Check arguments
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
// Get prescription
role, err := s.getRole(stub)
if err != nil {
return shim.Error(err.Error())
}
key := "PRE" + args[0]
var prescriptionBytes []byte
if strings.HasPrefix(role, "PAT") || strings.HasPrefix(role, "DOC") {
// When patient or doctor
prescriptionBytes, err = stub.GetPrivateData("healthcarePrescriptions", key)
if prescriptionBytes != nil {
prescription := Prescription{}
json.Unmarshal(prescriptionBytes, &prescription)
if prescription.Patient == role || prescription.Doctor == role {
return shim.Success(prescriptionBytes)
}
}
} else if strings.HasPrefix(role, "PHR") {
// When pharmacy
prescriptionBytes, err = stub.GetPrivateData("pharmacyPrescriptions", key)
if prescriptionBytes != nil {
return shim.Success(prescriptionBytes)
}
} else {
// When other
return shim.Error("Only patients, doctors and pharmacies can access prescriptions")
}
return shim.Error("Prescription not found")
}
func (s *SmartContract) getRole(stub shim.ChaincodeStubInterface) (string, error) {
role, ok, err := cid.GetAttributeValue(stub, "role")
if err != nil {
return "", err
}
if !ok {
return "", errors.New("role attribute is missing")
}
return role, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.