diff --git a/internal/pkg/apiclient/data.go b/internal/pkg/apiclient/data.go index 0eb909e..75b9d22 100644 --- a/internal/pkg/apiclient/data.go +++ b/internal/pkg/apiclient/data.go @@ -1,8 +1,10 @@ package apiclient import ( + "bytes" "encoding/json" "fmt" + "io" "net/http" "github.com/AFASystems/presence/internal/pkg/config" @@ -30,8 +32,14 @@ func GetGateways(token string, client *http.Client, cfg *config.Config) ([]model return []model.Gateway{}, err } + body, err := io.ReadAll(res.Body) + if err != nil { + return []model.Gateway{}, err + } + fmt.Printf("Gateways raw response: %s\n", body) + var i []model.Gateway - err = json.NewDecoder(res.Body).Decode(&i) + err = json.NewDecoder(bytes.NewReader(body)).Decode(&i) if err != nil { return []model.Gateway{}, err } diff --git a/internal/pkg/controller/alerts_controller.go b/internal/pkg/controller/alerts_controller.go index c47a095..68fee68 100644 --- a/internal/pkg/controller/alerts_controller.go +++ b/internal/pkg/controller/alerts_controller.go @@ -81,9 +81,7 @@ func AlertDeleteController(db *gorm.DB, ctx context.Context) http.HandlerFunc { func AlertUpdateStatusController(db *gorm.DB, ctx context.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["id"] - var body struct { - Status string `json:"status"` - } + var body service.AlertPatchBody if err := json.NewDecoder(r.Body).Decode(&body); err != nil { response.BadRequest(w, "invalid request body") return @@ -92,7 +90,13 @@ func AlertUpdateStatusController(db *gorm.DB, ctx context.Context) http.HandlerF response.BadRequest(w, err.Error()) return } - if err := service.UpdateAlertStatus(id, body.Status, db, ctx); err != nil { + + if err := validation.Var(body.Operator, "required"); err != nil { + response.BadRequest(w, err.Error()) + return + } + + if err := service.UpdateAlertStatus(id, body, db, ctx); err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { response.NotFound(w, "alert not found") return diff --git a/internal/pkg/model/alerts.go b/internal/pkg/model/alerts.go index d9df5f1..4be748a 100644 --- a/internal/pkg/model/alerts.go +++ b/internal/pkg/model/alerts.go @@ -9,4 +9,6 @@ type Alert struct { Status string `json:"status"` Timestamp time.Time `json:"timestamp"` ResolutionTimestamp time.Time `json:"resolution_timestamp"` + Operator string `json:"operator"` + Zone string `json:"zone"` } diff --git a/internal/pkg/model/gateway.go b/internal/pkg/model/gateway.go index abb7fee..31ab928 100644 --- a/internal/pkg/model/gateway.go +++ b/internal/pkg/model/gateway.go @@ -13,4 +13,5 @@ type Gateway struct { Notes string `json:"notes"` Floor string `json:"floor"` Building string `json:"building"` + Zone string `json:"zone"` } diff --git a/internal/pkg/service/alert_service.go b/internal/pkg/service/alert_service.go index dd12274..4ad8dd3 100644 --- a/internal/pkg/service/alert_service.go +++ b/internal/pkg/service/alert_service.go @@ -2,6 +2,7 @@ package service import ( "context" + "time" "github.com/AFASystems/presence/internal/pkg/model" "gorm.io/gorm" @@ -38,9 +39,14 @@ func GetAlertById(id string, db *gorm.DB, ctx context.Context) (model.Alert, err return alert, nil } +type AlertPatchBody struct { + Status string `json:"status"` + Operator string `json:"operator"` +} + // UpdateAlertStatus updates the status of an alert by id. Returns gorm.ErrRecordNotFound if the alert does not exist. -func UpdateAlertStatus(id string, status string, db *gorm.DB, ctx context.Context) error { - result := db.WithContext(ctx).Model(&model.Alert{}).Where("id = ?", id).Update("status", status) +func UpdateAlertStatus(id string, body AlertPatchBody, db *gorm.DB, ctx context.Context) error { + result := db.WithContext(ctx).Model(&model.Alert{}).Where("id = ?", id).Updates(model.Alert{Status: body.Status, Operator: body.Operator, ResolutionTimestamp: time.Now()}) if result.Error != nil { return result.Error }