github.com/s7techlab/cckit@v0.10.5/examples/insurance/app/data.go (about)

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