package api import ( "net/http" "regexp" "strings" "github.com/a-h/templ" "github.com/gin-gonic/gin" "github.com/google/uuid" "git.zb-server.de/ZB-Server/uptimesebi/internal/authz" "git.zb-server.de/ZB-Server/uptimesebi/internal/store" "git.zb-server.de/ZB-Server/uptimesebi/internal/web" ) // An organization's own incident statuses, next to the built-in ones. var hexColor = regexp.MustCompile(`^#[0-9a-f]{6}$`) type incidentStatusRequest struct { Name string `json:"name" binding:"required,max=40"` Color string `json:"color" binding:"required,max=7"` } type incidentStatusView struct { ID uuid.UUID `json:"id"` Name string `json:"name"` Color string `json:"color"` } // validStatusRequest normalizes req and reports whether it is usable. func validStatusRequest(c *gin.Context, req *incidentStatusRequest) bool { req.Name, req.Color = strings.TrimSpace(req.Name), strings.ToLower(strings.TrimSpace(req.Color)) if req.Name == "" || !hexColor.MatchString(req.Color) { fail(c, http.StatusUnprocessableEntity, "invalid_input") return false } return true } func (s *Server) listIncidentStatuses(c *gin.Context) { ctx := c.Request.Context() var rows []store.IncidentStatus if err := s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error { var err error rows, err = q.ListIncidentStatuses(ctx) return err }); err != nil { internalError(c, err) return } out := make([]incidentStatusView, len(rows)) for i, r := range rows { out[i] = incidentStatusView{ID: r.ID, Name: r.Name, Color: r.Color} } c.JSON(http.StatusOK, gin.H{"statuses": out}) } func (s *Server) createIncidentStatus(c *gin.Context) { var req incidentStatusRequest if !bind(c, &req) || !validStatusRequest(c, &req) { return } p := current(c) ctx := c.Request.Context() var row store.IncidentStatus err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error { var err error row, err = q.CreateIncidentStatus(ctx, store.CreateIncidentStatusParams{OrgID: p.Org.ID, Name: req.Name, Color: req.Color}) return err }) if isUniqueViolation(err, "incident_statuses_org_id_name_key") { fail(c, http.StatusConflict, "status_name_taken") return } if err != nil { internalError(c, err) return } auditDetail(c, "name", req.Name) c.JSON(http.StatusCreated, incidentStatusView{ID: row.ID, Name: row.Name, Color: row.Color}) } // updateIncidentStatus renames or recolors a status, also in the history // of incidents that used it. func (s *Server) updateIncidentStatus(c *gin.Context) { id, err := uuid.Parse(c.Param("id")) if err != nil { fail(c, http.StatusNotFound, "not_found") return } var req incidentStatusRequest if !bind(c, &req) || !validStatusRequest(c, &req) { return } ctx := c.Request.Context() var n int64 err = s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error { n, err = q.UpdateIncidentStatus(ctx, store.UpdateIncidentStatusParams{ID: id, Name: req.Name, Color: req.Color}) return err }) switch { case isUniqueViolation(err, "incident_statuses_org_id_name_key"): fail(c, http.StatusConflict, "status_name_taken") case err != nil: internalError(c, err) case n == 0: fail(c, http.StatusNotFound, "not_found") default: auditDetail(c, "name", req.Name) c.Status(http.StatusNoContent) } } // deleteIncidentStatus removes a status nobody used yet; history keeps // the ones in use. func (s *Server) deleteIncidentStatus(c *gin.Context) { id, err := uuid.Parse(c.Param("id")) if err != nil { fail(c, http.StatusNotFound, "not_found") return } ctx := c.Request.Context() var n int64 err = s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error { n, err = q.DeleteIncidentStatus(ctx, id) return err }) switch { case isForeignKeyViolation(err): fail(c, http.StatusConflict, "status_in_use") case err != nil: internalError(c, err) case n == 0: fail(c, http.StatusNotFound, "not_found") default: c.Status(http.StatusNoContent) } } func (s *Server) incidentStatusesView(c *gin.Context, p *principal, page web.Page, me web.Me) (templ.Component, error) { ctx := c.Request.Context() var rows []store.IncidentStatus if err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error { var err error rows, err = q.ListIncidentStatuses(ctx) return err }); err != nil { return nil, err } v := make([]web.CustomIncidentStatus, len(rows)) for i, r := range rows { v[i] = web.CustomIncidentStatus{ID: r.ID.String(), Name: r.Name, Color: r.Color} } return web.IncidentStatusesPage(page, me, v, p.Grants.Can(authz.OrgManage)), nil }