MCPcopy Index your code
hub / github.com/PatchMon/PatchMon / BulkAction

Method BulkAction

server-source-code/internal/handler/alerts.go:298–351  ·  view source on GitHub ↗

BulkAction handles POST /alerts/bulk-action.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

296
297// BulkAction handles POST /alerts/bulk-action.
298func (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}

Callers

nothing calls this directly

Calls 11

decodeJSONFunction · 0.85
ErrorFunction · 0.85
successDataFunction · 0.85
UpdateResolvedMethod · 0.80
ErrorMethod · 0.80
UpdateUnresolveMethod · 0.80
RecordHistoryMethod · 0.80
DBMethod · 0.65
GetAlertActionByNameMethod · 0.65
UpdateAlertMethod · 0.65
ValueMethod · 0.45

Tested by

no test coverage detected