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