Skip to content

Instantly share code, notes, and snippets.

@MichalZalecki MichalZalecki/prescriptions.go Secret
Created Jul 30, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.