BulkAction handles POST /alerts/bulk-action.
(w http.ResponseWriter, r *http.Request)
| 296 | |
| 297 | // BulkAction handles POST /alerts/bulk-action. |
| 298 | func (h *AlertsHandler) BulkAction(w http.ResponseWriter, r *http.Request) { |
| 299 | var req struct { |
| 300 | AlertIDs []string `json:"alertIds"` |
| 301 | Action string `json:"action"` |
| 302 | } |
| 303 | if err := decodeJSON(r, &req); err != nil { |
| 304 | Error(w, http.StatusBadRequest, "Invalid request body") |
| 305 | return |
| 306 | } |
| 307 | if len(req.AlertIDs) == 0 { |
| 308 | Error(w, http.StatusBadRequest, "alertIds required") |
| 309 | return |
| 310 | } |
| 311 | if req.Action == "" { |
| 312 | Error(w, http.StatusBadRequest, "action required") |
| 313 | return |
| 314 | } |
| 315 | |
| 316 | userID, _ := r.Context().Value(middleware.UserIDKey).(string) |
| 317 | d := h.db.DB(r.Context()) |
| 318 | |
| 319 | action, err := d.Queries.GetAlertActionByName(r.Context(), req.Action) |
| 320 | if err != nil { |
| 321 | Error(w, http.StatusBadRequest, "Invalid action") |
| 322 | return |
| 323 | } |
| 324 | |
| 325 | var uid *string |
| 326 | if userID != "" { |
| 327 | uid = &userID |
| 328 | } |
| 329 | |
| 330 | for _, id := range req.AlertIDs { |
| 331 | if action.IsStateAction { |
| 332 | if err := h.alerts.UpdateResolved(r.Context(), id, uid); err != nil { |
| 333 | slog.Error("alerts: bulk-action resolve failed", "alert_id", id, "error", err) |
| 334 | continue |
| 335 | } |
| 336 | } else { |
| 337 | if err := h.alerts.UpdateUnresolve(r.Context(), id); err != nil { |
| 338 | slog.Error("alerts: bulk-action unresolve failed", "alert_id", id, "error", err) |
| 339 | continue |
| 340 | } |
| 341 | } |
| 342 | if err := h.alerts.RecordHistory(r.Context(), id, uid, req.Action, map[string]interface{}{}); err != nil { |
| 343 | slog.Error("alerts: bulk-action record history failed", "alert_id", id, "error", err) |
| 344 | } |
| 345 | if err := d.Queries.UpdateAlert(r.Context(), id); err != nil { |
| 346 | slog.Error("alerts: bulk-action update alert timestamp failed", "alert_id", id, "error", err) |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | successData(w, map[string]interface{}{"processed": len(req.AlertIDs)}) |
| 351 | } |
nothing calls this directly
no test coverage detected