-
-
Save MichalZalecki/69f618ca570b1ccde22a21063afde000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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