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
/
invoke_insurance.go
547 lines (472 loc) · 12.8 KB
/
invoke_insurance.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package app
import (
"encoding/json"
"strings"
"time"
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
)
func listContractTypes(stub shim.ChaincodeStubInterface, args []string) pb.Response {
callingAsMerchant := len(args) == 1
input := struct {
ShopType string `json:"shop_type"`
}{}
if callingAsMerchant {
err := json.Unmarshal([]byte(args[0]), &input)
if err != nil {
return shim.Error(err.Error())
}
}
resultsIterator, err := stub.GetStateByPartialCompositeKey(prefixContractType, []string{})
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
results := []interface{}{}
for resultsIterator.HasNext() {
kvResult, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
ct := struct {
UUID string `json:"uuid"`
*ContractType
}{}
err = json.Unmarshal(kvResult.Value, &ct)
if err != nil {
return shim.Error(err.Error())
}
prefix, keyParts, err := stub.SplitCompositeKey(kvResult.Key)
if err != nil {
return shim.Error(err.Error())
}
if len(keyParts) > 0 {
ct.UUID = keyParts[0]
} else {
ct.UUID = prefix
}
// Apply proper filtering, merchants should only see active contracts
if !callingAsMerchant ||
(strings.Contains(strings.ToTitle(ct.ShopType), strings.ToTitle(input.ShopType)) && ct.Active) {
results = append(results, ct)
}
}
returnBytes, err := json.Marshal(results)
return shim.Success(returnBytes)
}
func createContractType(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Invalid argument count.")
}
partial := struct {
UUID string `json:"uuid"`
}{}
ct := ContractType{}
err := json.Unmarshal([]byte(args[0]), &partial)
if err != nil {
return shim.Error(err.Error())
}
err = json.Unmarshal([]byte(args[0]), &ct)
if err != nil {
return shim.Error(err.Error())
}
key, err := stub.CreateCompositeKey(prefixContractType, []string{partial.UUID})
if err != nil {
return shim.Error(err.Error())
}
value, err := json.Marshal(ct)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(key, value)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func setActiveContractType(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Invalid argument count.")
}
req := struct {
UUID string `json:"uuid"`
Active bool `json:"active"`
}{}
ct := ContractType{}
err := json.Unmarshal([]byte(args[0]), &req)
if err != nil {
return shim.Error(err.Error())
}
key, err := stub.CreateCompositeKey(prefixContractType, []string{req.UUID})
if err != nil {
return shim.Error(err.Error())
}
valAsBytes, err := stub.GetState(key)
if err != nil {
return shim.Error(err.Error())
}
if len(valAsBytes) == 0 {
return shim.Error("Contract Type could not be found")
}
err = json.Unmarshal(valAsBytes, &ct)
if err != nil {
return shim.Error(err.Error())
}
ct.Active = req.Active
valAsBytes, err = json.Marshal(ct)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(key, valAsBytes)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func listContracts(stub shim.ChaincodeStubInterface, args []string) pb.Response {
input := struct {
Username string `json:"username"`
}{}
if len(args) == 1 {
err := json.Unmarshal([]byte(args[0]), &input)
if err != nil {
return shim.Error(err.Error())
}
}
filterByUsername := len(input.Username) > 0
var resultsIterator shim.StateQueryIteratorInterface
var err error
// Filtering by username if required
if filterByUsername {
resultsIterator, err = stub.GetStateByPartialCompositeKey(prefixContract, []string{input.Username})
} else {
resultsIterator, err = stub.GetStateByPartialCompositeKey(prefixContract, []string{})
}
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
results := []interface{}{}
// Iterate over the results
for resultsIterator.HasNext() {
kvResult, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
// Construct response struct
result := struct {
UUID string `json:"uuid"`
*Contract
Claims []Claim `json:"claims,omitempty"`
}{}
err = json.Unmarshal(kvResult.Value, &result)
if err != nil {
return shim.Error(err.Error())
}
// Fetch key
prefix, keyParts, err := stub.SplitCompositeKey(kvResult.Key)
if len(keyParts) == 2 {
result.UUID = keyParts[1]
} else {
result.UUID = prefix
}
// Fetch the claims, if the the username parameter is specified
if len(input.Username) > 0 {
result.Claims, err = result.Contract.Claims(stub)
if err != nil {
return shim.Error(err.Error())
}
}
result.ClaimIndex = []string{} // Remove internal data
results = append(results, result)
}
resultsAsBytes, err := json.Marshal(results)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(resultsAsBytes)
}
func listClaims(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var status ClaimStatus
if len(args) > 0 {
input := struct {
Status ClaimStatus `json:"status"`
}{}
err := json.Unmarshal([]byte(args[0]), &input)
if err != nil {
return shim.Error(err.Error())
}
status = input.Status
}
results := []interface{}{}
resultsIterator, err := stub.GetStateByPartialCompositeKey(prefixClaim, []string{})
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
for resultsIterator.HasNext() {
kvResult, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
result := struct {
UUID string `json:"uuid"`
*Claim
}{}
err = json.Unmarshal(kvResult.Value, &result)
if err != nil {
return shim.Error(err.Error())
}
// Skip the processing of the result, if the status
// does not equal the query status; list all, if unknown
if result.Status != status && status != ClaimStatusUnknown {
continue
}
// Fetch key
prefix, keyParts, err := stub.SplitCompositeKey(kvResult.Key)
if len(keyParts) < 2 {
result.UUID = prefix
} else {
result.UUID = keyParts[1]
}
results = append(results, result)
}
claimsAsBytes, err := json.Marshal(results)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(claimsAsBytes)
}
func fileClaim(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Invalid argument count.")
}
dto := struct {
UUID string `json:"uuid"`
ContractUUID string `json:"contract_uuid"`
Date time.Time `json:"date"`
Description string `json:"description"`
IsTheft bool `json:"is_theft"`
}{}
err := json.Unmarshal([]byte(args[0]), &dto)
if err != nil {
return shim.Error(err.Error())
}
claim := Claim{
ContractUUID: dto.ContractUUID,
Date: dto.Date,
Description: dto.Description,
IsTheft: dto.IsTheft,
Status: ClaimStatusNew,
}
// Check if the contract exists
contract, err := claim.Contract(stub)
if err != nil {
return shim.Error(err.Error())
}
if contract == nil {
return shim.Error("Contract could not be found.")
}
// Persist the claim
claimKey, err := stub.CreateCompositeKey(prefixClaim,
[]string{dto.ContractUUID, dto.UUID})
if err != nil {
return shim.Error(err.Error())
}
claimBytes, err := json.Marshal(claim)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(claimKey, claimBytes)
if err != nil {
return shim.Error(err.Error())
}
// Update the claim index in the contract
contract.ClaimIndex = append(contract.ClaimIndex, claimKey)
contractKey, err := stub.CreateCompositeKey(prefixContract,
[]string{contract.Username, claim.ContractUUID})
if err != nil {
return shim.Error(err.Error())
}
contractBytes, err := json.Marshal(contract)
if err != nil {
return shim.Error(err.Error())
}
stub.PutState(contractKey, contractBytes)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func processClaim(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Invalid argument count.")
}
input := struct {
UUID string `json:"uuid"`
ContractUUID string `json:"contract_uuid"`
Status ClaimStatus `json:"status"`
Reimbursable float32 `json:"reimbursable"`
}{}
err := json.Unmarshal([]byte(args[0]), &input)
if err != nil {
return shim.Error(err.Error())
}
claimKey, err := stub.CreateCompositeKey(prefixClaim, []string{input.ContractUUID, input.UUID})
if err != nil {
return shim.Error(err.Error())
}
claimBytes, _ := stub.GetState(claimKey)
if len(claimBytes) == 0 {
return shim.Error("Claim cannot be found.")
}
claim := Claim{}
err = json.Unmarshal(claimBytes, &claim)
if err != nil {
return shim.Error(err.Error())
}
if !claim.IsTheft && claim.Status != ClaimStatusNew {
// Check if altering claim is allowed
return shim.Error("Cannot change the status of a non-new claim.")
}
if claim.IsTheft && claim.Status == ClaimStatusNew {
return shim.Error("Theft must first be confirmed by authorities.")
}
claim.Status = input.Status // Assigning requested status
switch input.Status {
case ClaimStatusRepair:
// Approve and create a repair order
if claim.IsTheft {
return shim.Error("Cannot repair stolen items.")
}
claim.Reimbursable = 0
contract, err := claim.Contract(stub)
if err != nil {
return shim.Error(err.Error())
}
// Create new repair order
repairOrder := RepairOrder{
Item: contract.Item,
ClaimUUID: input.UUID,
ContractUUID: input.ContractUUID,
Ready: false,
}
repairOrderKey, err := stub.CreateCompositeKey(prefixRepairOrder, []string{input.UUID})
if err != nil {
return shim.Error(err.Error())
}
repairOrderBytes, err := json.Marshal(repairOrder)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(repairOrderKey, repairOrderBytes)
if err != nil {
return shim.Error(err.Error())
}
case ClaimStatusReimbursement:
// Approve reimbursement of item, and add the sum
claim.Reimbursable = input.Reimbursable
// If theft was involved, mark the contract as void
if claim.IsTheft {
contract, err := claim.Contract(stub)
if err != nil {
return shim.Error(err.Error())
}
contract.Void = true
// Persist contract
contractKey, err := stub.CreateCompositeKey(
prefixContract, []string{contract.Username, claim.ContractUUID})
if err != nil {
return shim.Error(err.Error())
}
contractBytes, err := json.Marshal(contract)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(contractKey, contractBytes)
if err != nil {
return shim.Error(err.Error())
}
}
case ClaimStatusRejected:
// Mark as rejected
claim.Reimbursable = 0
default:
return shim.Error("Unknown status change.")
}
// Persist claim
claimBytes, err = json.Marshal(claim)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(claimKey, claimBytes)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func authUser(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Invalid argument count.")
}
input := struct {
Username string `json:"username"`
Password string `json:"password"`
}{}
authenticated := false
err := json.Unmarshal([]byte(args[0]), &input)
if err != nil {
return shim.Error(err.Error())
}
userKey, err := stub.CreateCompositeKey(prefixUser, []string{input.Username})
if err != nil {
return shim.Error(err.Error())
}
userBytes, _ := stub.GetState(userKey)
if len(userBytes) == 0 {
authenticated = false
} else {
user := User{}
err := json.Unmarshal(userBytes, &user)
if err != nil {
return shim.Error(err.Error())
}
authenticated = user.Password == input.Password
}
authBytes, _ := json.Marshal(authenticated)
return shim.Success(authBytes)
}
func getUser(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Invalid argument count.")
}
input := struct {
Username string `json:"username"`
}{}
err := json.Unmarshal([]byte(args[0]), &input)
if err != nil {
return shim.Error(err.Error())
}
userKey, err := stub.CreateCompositeKey(prefixUser, []string{input.Username})
if err != nil {
return shim.Error(err.Error())
}
userBytes, _ := stub.GetState(userKey)
if len(userBytes) == 0 {
return shim.Success(nil)
}
response := struct {
Username string `json:"username"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}{}
err = json.Unmarshal(userBytes, &response)
if err != nil {
return shim.Error(err.Error())
}
responseBytes, err := json.Marshal(response)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(responseBytes)
}