github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/build-blockchain-insurance-app-master/web/chaincode/src/bcins/data.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "time" 6 7 "errors" 8 "github.com/hyperledger/fabric/core/chaincode/shim" 9 "strings" 10 ) 11 12 // Key consists of prefix + UUID of the contract type 13 type contractType struct { 14 ShopType string `json:"shop_type"` 15 FormulaPerDay string `json:"formula_per_day"` 16 MaxSumInsured float32 `json:"max_sum_insured"` 17 TheftInsured bool `json:"theft_insured"` 18 Description string `json:"description"` 19 Conditions string `json:"conditions"` 20 Active bool `json:"active"` 21 MinDurationDays int32 `json:"min_duration_days"` 22 MaxDurationDays int32 `json:"max_duration_days"` 23 } 24 25 // Key consists of prefix + username + UUID of the contract 26 type contract struct { 27 Username string `json:"username"` 28 Item item `json:"item"` 29 StartDate time.Time `json:"start_date"` 30 EndDate time.Time `json:"end_date"` 31 Void bool `json:"void"` 32 ContractTypeUUID string `json:"contract_type_uuid"` 33 ClaimIndex []string `json:"claim_index,omitempty"` 34 } 35 36 // Entity not persisted on its own 37 type item struct { 38 ID int32 `json:"id"` 39 Brand string `json:"brand"` 40 Model string `json:"model"` 41 Price float32 `json:"price"` 42 Description string `json:"description"` 43 SerialNo string `json:"serial_no"` 44 } 45 46 // Key consists of prefix + UUID of the contract + UUID of the claim 47 type claim struct { 48 ContractUUID string `json:"contract_uuid"` 49 Date time.Time `json:"date"` 50 Description string `json:"description"` 51 IsTheft bool `json:"is_theft"` 52 Status ClaimStatus `json:"status"` 53 Reimbursable float32 `json:"reimbursable"` 54 Repaired bool `json:"repaired"` 55 FileReference string `json:"file_reference"` 56 } 57 58 // The claim status indicates how the claim should be treated 59 type ClaimStatus int8 60 61 const ( 62 // The claims status is unknown 63 ClaimStatusUnknown ClaimStatus = iota 64 // The claim is new 65 ClaimStatusNew 66 // The claim has been rejected (either by the insurer, or by authorities 67 ClaimStatusRejected 68 // The item is up for repairs, or has been repaired 69 ClaimStatusRepair 70 // The customer should be reimbursed, or has already been 71 ClaimStatusReimbursement 72 // The theft of the item has been confirmed by authorities 73 ClaimStatusTheftConfirmed 74 ) 75 76 func (s *ClaimStatus) UnmarshalJSON(b []byte) error { 77 var value string 78 if err := json.Unmarshal(b, &value); err != nil { 79 return err 80 } 81 82 switch strings.ToUpper(value) { 83 default: 84 *s = ClaimStatusUnknown 85 case "N": 86 *s = ClaimStatusNew 87 case "J": 88 *s = ClaimStatusRejected 89 case "R": 90 *s = ClaimStatusRepair 91 case "F": 92 *s = ClaimStatusReimbursement 93 case "P": 94 *s = ClaimStatusTheftConfirmed 95 } 96 97 return nil 98 } 99 100 func (s ClaimStatus) MarshalJSON() ([]byte, error) { 101 var value string 102 103 switch s { 104 default: 105 fallthrough 106 case ClaimStatusUnknown: 107 value = "" 108 case ClaimStatusNew: 109 value = "N" 110 case ClaimStatusRejected: 111 value = "J" 112 case ClaimStatusRepair: 113 value = "R" 114 case ClaimStatusReimbursement: 115 value = "F" 116 case ClaimStatusTheftConfirmed: 117 value = "P" 118 } 119 120 return json.Marshal(value) 121 } 122 123 // Key consists of prefix + username 124 type user struct { 125 Username string `json:"username"` 126 Password string `json:"password"` 127 FirstName string `json:"first_name"` 128 LastName string `json:"last_name"` 129 ContractIndex []string `json:"contracts"` 130 } 131 132 // Key consists of prefix + UUID fo the repair order 133 type repairOrder struct { 134 ClaimUUID string `json:"claim_uuid"` 135 ContractUUID string `json:"contract_uuid"` 136 Item item `json:"item"` 137 Ready bool `json:"ready"` 138 } 139 140 func (u *user) Contacts(stub shim.ChaincodeStubInterface) []contract { 141 contracts := make([]contract, 0) 142 143 // for each contractID in user.ContractIndex 144 for _, contractID := range u.ContractIndex { 145 146 c := &contract{} 147 148 // get contract 149 contractAsBytes, err := stub.GetState(contractID) 150 if err != nil { 151 //res := "Failed to get state for " + contractID 152 return nil 153 } 154 155 // parse contract 156 err = json.Unmarshal(contractAsBytes, c) 157 if err != nil { 158 //res := "Failed to parse contract" 159 return nil 160 } 161 162 // append to the contracts array 163 contracts = append(contracts, *c) 164 } 165 166 return contracts 167 } 168 169 func (c *contract) Claims(stub shim.ChaincodeStubInterface) ([]claim, error) { 170 claims := []claim{} 171 172 for _, claimKey := range c.ClaimIndex { 173 claim := claim{} 174 175 claimAsBytes, err := stub.GetState(claimKey) 176 if err != nil { 177 return nil, err 178 } 179 180 err = json.Unmarshal(claimAsBytes, &claim) 181 if err != nil { 182 return nil, err 183 } 184 185 claims = append(claims, claim) 186 } 187 188 return claims, nil 189 } 190 191 func (c *contract) User(stub shim.ChaincodeStubInterface) (*user, error) { 192 user := &user{} 193 194 if len(c.Username) == 0 { 195 return nil, errors.New("Invalid user name in contract.") 196 } 197 198 userKey, err := stub.CreateCompositeKey(prefixUser, []string{c.Username}) 199 if err != nil { 200 return nil, err 201 } 202 203 userAsBytes, err := stub.GetState(userKey) 204 if err != nil { 205 return nil, err 206 } 207 err = json.Unmarshal(userAsBytes, user) 208 if err != nil { 209 return nil, err 210 } 211 212 return user, nil 213 } 214 215 func (c *claim) Contract(stub shim.ChaincodeStubInterface) (*contract, error) { 216 if len(c.ContractUUID) == 0 { 217 return nil, nil 218 } 219 220 resultsIterator, err := stub.GetStateByPartialCompositeKey(prefixContract, []string{}) 221 if err != nil { 222 return nil, err 223 } 224 defer resultsIterator.Close() 225 226 for resultsIterator.HasNext() { 227 kvResult, err := resultsIterator.Next() 228 if err != nil { 229 return nil, err 230 } 231 232 _, keyParams, err := stub.SplitCompositeKey(kvResult.Key) 233 if len(keyParams) != 2 { 234 continue 235 } 236 237 if keyParams[1] == c.ContractUUID { 238 contract := &contract{} 239 err := json.Unmarshal(kvResult.Value, contract) 240 if err != nil { 241 return nil, err 242 } 243 return contract, nil 244 } 245 } 246 return nil, nil 247 }