github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/build-blockchain-insurance-app-master/web/chaincode/src/bcins/invoke_police.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/hyperledger/fabric/core/chaincode/shim" 7 pb "github.com/hyperledger/fabric/protos/peer" 8 ) 9 10 func listTheftClaims(stub shim.ChaincodeStubInterface, args []string) pb.Response { 11 results := []interface{}{} 12 resultsIterator, err := stub.GetStateByPartialCompositeKey(prefixClaim, []string{}) 13 if err != nil { 14 return shim.Error(err.Error()) 15 } 16 defer resultsIterator.Close() 17 18 for resultsIterator.HasNext() { 19 kvResult, err := resultsIterator.Next() 20 if err != nil { 21 return shim.Error(err.Error()) 22 } 23 24 claim := claim{} 25 err = json.Unmarshal(kvResult.Value, &claim) 26 if err != nil { 27 return shim.Error(err.Error()) 28 } 29 // Filter out the irrelevant claims 30 if !claim.IsTheft || claim.Status != ClaimStatusNew { 31 continue 32 } 33 34 contract, err := claim.Contract(stub) 35 if err != nil { 36 return shim.Error(err.Error()) 37 } 38 if contract == nil { 39 return shim.Error("Error acquiring contracts.") 40 } 41 42 result := struct { 43 UUID string `json:"uuid"` 44 ContractUUID string `json:"contract_uuid"` 45 Item item `json:"item"` 46 Description string `json:"description"` 47 Name string `json:"name"` 48 }{} 49 50 // Fetch key and set other properties 51 prefix, keyParts, err := stub.SplitCompositeKey(kvResult.Key) 52 if len(keyParts) < 2 { 53 result.UUID = prefix 54 } else { 55 result.ContractUUID = keyParts[0] 56 result.UUID = keyParts[1] 57 } 58 user, err := contract.User(stub) 59 if err != nil { 60 return shim.Error(err.Error()) 61 } 62 if user == nil { 63 return shim.Error("Error acquiring user.") 64 } 65 66 result.Item = contract.Item 67 result.Description = claim.Description 68 result.Name = fmt.Sprintf("%s %s", user.FirstName, user.LastName) 69 70 results = append(results, result) 71 } 72 73 claimsAsBytes, err := json.Marshal(results) 74 if err != nil { 75 return shim.Error(err.Error()) 76 } 77 return shim.Success(claimsAsBytes) 78 } 79 80 func processTheftClaim(stub shim.ChaincodeStubInterface, args []string) pb.Response { 81 if len(args) != 1 { 82 return shim.Error("Invalid argument count.") 83 } 84 85 dto := struct { 86 UUID string `json:"uuid"` 87 ContractUUID string `json:"contract_uuid"` 88 IsTheft bool `json:"is_theft"` 89 FileReference string `json:"file_reference"` 90 }{} 91 err := json.Unmarshal([]byte(args[0]), &dto) 92 if err != nil { 93 return shim.Error(err.Error()) 94 } 95 96 key, err := stub.CreateCompositeKey(prefixClaim, []string{dto.ContractUUID, dto.UUID}) 97 if err != nil { 98 return shim.Error(err.Error()) 99 } 100 101 claimAsBytes, _ := stub.GetState(key) 102 if len(claimAsBytes) == 0 { 103 return shim.Error("Claim cannot be found.") 104 } 105 106 claim := claim{} 107 err = json.Unmarshal(claimAsBytes, &claim) 108 if err != nil { 109 return shim.Error(err.Error()) 110 } 111 112 // Check if the correct kind of claim is about to be processed 113 if !claim.IsTheft || claim.Status != ClaimStatusNew { 114 return shim.Error("Claim is either not related to theft, or has invalid status.") 115 } 116 117 if dto.IsTheft { 118 claim.Status = ClaimStatusTheftConfirmed 119 } else { 120 claim.Status = ClaimStatusRejected // by authorities 121 } 122 claim.FileReference = dto.FileReference 123 124 claimAsBytes, err = json.Marshal(claim) 125 if err != nil { 126 return shim.Error(err.Error()) 127 } 128 129 err = stub.PutState(key, claimAsBytes) 130 if err != nil { 131 return shim.Error(err.Error()) 132 } 133 return shim.Success(nil) 134 }