-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepairs.go
More file actions
81 lines (67 loc) · 2.02 KB
/
Copy pathrepairs.go
File metadata and controls
81 lines (67 loc) · 2.02 KB
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
package main
import (
"encoding/json"
"errors"
//"myproject/models" // Import the data package
"gorm.io/gorm"
)
func listRepairOrders(db *gorm.DB) ([]map[string]interface{}, error) {
var repairOrders []RepairOrder
// Query all repair orders where Ready is false
err := db.Where("ready = ?", false).Find(&repairOrders).Error
if err != nil {
return nil, errors.New("failed to fetch repair orders: " + err.Error())
}
// Prepare results
results := []map[string]interface{}{}
for _, ro := range repairOrders {
derivedUUID := ro.ClaimUUID
result := map[string]interface{}{
"uuid": derivedUUID,
"claim_uuid": ro.ClaimUUID,
"contract_uuid": ro.ContractUUID,
"item": ro.Item,
}
results = append(results, result)
}
return results, nil
}
func completeRepairOrder(db *gorm.DB, args string) error {
// Parse input JSON
input := struct {
UUID string `json:"uuid"`
}{}
err := json.Unmarshal([]byte(args), &input)
if err != nil {
return errors.New("invalid input: " + err.Error())
}
// Fetch the repair order
var repairOrder RepairOrder
err = db.Where("uuid = ?", input.UUID).First(&repairOrder).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("repair order not found")
} else if err != nil {
return errors.New("failed to fetch repair order: " + err.Error())
}
// Mark the repair order as ready
repairOrder.Ready = true
err = db.Save(&repairOrder).Error
if err != nil {
return errors.New("failed to update repair order: " + err.Error())
}
// Update the associated claim
var claim Claim
err = db.Where("contract_uuid = ? AND uuid = ?", repairOrder.ContractUUID, repairOrder.ClaimUUID).First(&claim).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
// No claim found; skip updating claim
return nil
} else if err != nil {
return errors.New("failed to fetch associated claim: " + err.Error())
}
claim.Repaired = true
err = db.Save(&claim).Error
if err != nil {
return errors.New("failed to update associated claim: " + err.Error())
}
return nil
}