Skip to content

Instantly share code, notes, and snippets.

@MichalZalecki
Created July 30, 2018 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichalZalecki/69f618ca570b1ccde22a21063afde000 to your computer and use it in GitHub Desktop.
Save MichalZalecki/69f618ca570b1ccde22a21063afde000 to your computer and use it in GitHub Desktop.
func (s *SmartContract) transferPrescription(stub shim.ChaincodeStubInterface, args []string) peer.Response {
// Check arguments
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
role, err := s.getRole(stub)
if err != nil {
return shim.Error(err.Error())
}
if strings.HasPrefix(role, "PAT") {
return shim.Error("Only patients can transfer prescriptions")
}
// Get prescription
key := "PRE" + args[0]
prescriptionBytes, err := stub.GetPrivateData("healthcarePrescriptions", key)
if err != nil {
return shim.Error(err.Error())
}
if prescriptionBytes == nil {
return shim.Error("Prescription not found")
}
prescription := Prescription{}
json.Unmarshal(prescriptionBytes, &prescription)
// Check permissions
if prescription.Patient != role {
return shim.Error("Prescription not found")
}
// Set FilledBy
prescription.FilledBy = args[1]
prescriptionBytes, _ = json.Marshal(prescription)
err = stub.PutPrivateData("healthcarePrescriptions", key, prescriptionBytes)
if err != nil {
return shim.Error(err.Error())
}
// Save pharmacy prescription
err = stub.PutPrivateData("pharmacyPrescriptions", key, prescriptionBytes)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment