Skip to content

Commit

Permalink
add ticket review webhook endpoint and ProcessTicketReview handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Nov 28, 2024
1 parent 3d51ab7 commit 73f7e97
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
59 changes: 52 additions & 7 deletions handlers/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"

"github.com/go-chi/chi"
"github.com/google/uuid"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/utils"
)

type ticketHandler struct {
Expand All @@ -21,6 +23,13 @@ func NewTicketHandler(database db.Database) *ticketHandler {
}
}

type TicketReviewRequest struct {
FeatureUUID string `json:"featureUUID" validate:"required"`
PhaseUUID string `json:"phaseUUID" validate:"required"`
TicketUUID string `json:"ticketUUID" validate:"required"`
TicketDescription string `json:"ticketDescription" validate:"required"`
}

func (th *ticketHandler) GetTicket(w http.ResponseWriter, r *http.Request) {
uuid := chi.URLParam(r, "uuid")
if uuid == "" {
Expand All @@ -38,7 +47,7 @@ func (th *ticketHandler) GetTicket(w http.ResponseWriter, r *http.Request) {
return
}

respondWithJSON(w, http.StatusOK, ticket)
utils.RespondWithJSON(w, http.StatusOK, ticket)
}

func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -78,7 +87,7 @@ func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) {
return
}

respondWithJSON(w, http.StatusOK, updatedTicket)
utils.RespondWithJSON(w, http.StatusOK, updatedTicket)
}

func (th *ticketHandler) DeleteTicket(w http.ResponseWriter, r *http.Request) {
Expand All @@ -98,11 +107,47 @@ func (th *ticketHandler) DeleteTicket(w http.ResponseWriter, r *http.Request) {
return
}

respondWithJSON(w, http.StatusOK, map[string]string{"status": "success"})
utils.RespondWithJSON(w, http.StatusOK, map[string]string{"status": "success"})
}

func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
json.NewEncoder(w).Encode(payload)
func (th *ticketHandler) ProcessTicketReview(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading request body: %v", err)
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}

var reviewReq utils.TicketReviewRequest
if err := json.Unmarshal(body, &reviewReq); err != nil {
log.Printf("Error parsing request JSON: %v", err)
http.Error(w, "Error parsing request body", http.StatusBadRequest)
return
}

if err := utils.ValidateTicketReviewRequest(&reviewReq); err != nil {
log.Printf("Invalid request data: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

ticket, err := th.db.GetTicket(reviewReq.TicketUUID)
if err != nil {
log.Printf("Error fetching ticket: %v", err)
http.Error(w, "Ticket not found", http.StatusNotFound)
return
}

ticket.Description = reviewReq.TicketDescription

updatedTicket, err := th.db.UpdateTicket(ticket)
if err != nil {
log.Printf("Error updating ticket: %v", err)
http.Error(w, "Failed to update ticket", http.StatusInternalServerError)
return
}

log.Printf("Successfully updated ticket %s", reviewReq.TicketUUID)

utils.RespondWithJSON(w, http.StatusOK, updatedTicket)
}
1 change: 1 addition & 0 deletions routes/ticket_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TicketRoutes() chi.Router {
r.Get("/{uuid}", ticketHandler.GetTicket)
r.Put("/{uuid}", ticketHandler.UpdateTicket)
r.Delete("/{uuid}", ticketHandler.DeleteTicket)
r.Post("/review", ticketHandler.ProcessTicketReview)
})

return r
Expand Down
36 changes: 36 additions & 0 deletions utils/ticket_processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package utils

import (
"encoding/json"
"errors"
"net/http"
)

type TicketReviewRequest struct {
FeatureUUID string `json:"featureUUID" validate:"required"`
PhaseUUID string `json:"phaseUUID" validate:"required"`
TicketUUID string `json:"ticketUUID" validate:"required"`
TicketDescription string `json:"ticketDescription" validate:"required"`
}

func ValidateTicketReviewRequest(req *TicketReviewRequest) error {
if req.FeatureUUID == "" {
return errors.New("featureUUID is required")
}
if req.PhaseUUID == "" {
return errors.New("phaseUUID is required")
}
if req.TicketUUID == "" {
return errors.New("ticketUUID is required")
}
if req.TicketDescription == "" {
return errors.New("ticketDescription is required")
}
return nil
}

func RespondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
json.NewEncoder(w).Encode(payload)
}

0 comments on commit 73f7e97

Please sign in to comment.