-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
151 lines (119 loc) · 3.31 KB
/
rest.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
package httpapi
import (
"database/sql"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/korylprince/bisd-device-checkin-server/v2/db"
"github.com/korylprince/bisd-device-checkin-server/v2/session"
)
type errResponse struct {
Err string `json:"error"`
}
func (e *errResponse) Error() string {
return e.Err
}
func (s *Server) readDevice(r *http.Request, tx *sql.Tx) (int, interface{}) {
bagTag := mux.Vars(r)["id"]
device, err := s.db.ReadDevice(tx, bagTag)
if err != nil {
return http.StatusInternalServerError, err
}
if device == nil {
return http.StatusNotFound, errors.New("Could not find device")
}
if device.Status != "Checked Out" {
return http.StatusBadRequest, &errResponse{Err: "Device not checked out"}
}
if strings.TrimSpace(device.User) == "" {
return http.StatusBadRequest, &errResponse{Err: "Device has no assigned user"}
}
return http.StatusOK, device
}
func (s *Server) checkinDevice(r *http.Request, tx *sql.Tx) (int, interface{}) {
type request struct {
Charges db.Charges `json:"charges"`
Missing bool `json:"missing"`
Notes string `json:"notes"`
}
type response struct {
ChargeID int64 `json:"charge_id"`
}
bagTag := mux.Vars(r)["id"]
//read charges
req := new(request)
if err := jsonRequest(r, req); err != nil {
return http.StatusBadRequest, err
}
//read device
device, err := s.db.ReadDevice(tx, bagTag)
if err != nil {
return http.StatusInternalServerError, err
}
if device == nil {
return http.StatusNotFound, errors.New("Could not find device")
}
//validate
if device.User == "" {
return http.StatusBadRequest, fmt.Errorf("There is no user for bag tag: %s", bagTag)
}
if device.Status != "Checked Out" {
return http.StatusBadRequest, fmt.Errorf("Device is not Checked Out for bag tag: %s", bagTag)
}
//get inventory user
user := r.Context().Value(contextKeyUser).(*session.Session)
//update notes
var fmtStr string
if req.Missing {
fmtStr = "\n%s %s: Chromebook (Bag Tag %s) checked out to %s is Missing\n"
} else {
fmtStr = "\n%s %s: Checked in Chromebook (Bag Tag %s) from %s\n"
}
device.Notes += fmt.Sprintf(fmtStr,
time.Now().Format("01/02/06"),
user.DisplayName,
bagTag,
device.User,
)
//if charges add note
if len(req.Charges) > 0 {
text := req.Charges.Marshal()
device.Notes += fmt.Sprintf("\tCharges: %s\n",
//pretty format
strings.Replace(strings.Replace(text[1:len(text)-1], "|", ", ", -1), ":", ": $", -1),
)
}
//add extra notes
if len(req.Notes) > 0 {
device.Notes += "\tNotes:\n"
for _, line := range strings.Split(req.Notes, "\n") {
device.Notes += fmt.Sprintf("\t\t%s\n", line)
}
}
//update fields
devUser := device.User
device.User = ""
device.Status = "Storage"
if req.Missing {
device.Status = "Missing"
}
//update device
err = s.db.UpdateDevice(tx, device, user.Username)
if err != nil {
return http.StatusInternalServerError, err
}
//create charge
if req.Charges.Total() > 0 {
id, err := s.db.CreateCharge(tx, req.Charges, device.InventoryNumber, devUser,
fmt.Sprintf("Charges created %s by %s.\n%s", time.Now().Format("01/02/06"), user.DisplayName, req.Notes),
)
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, &response{ChargeID: id}
}
return http.StatusOK, &response{ChargeID: 0}
}