From 22ed20e9b97ab5180dc7211f42b6790645823b97 Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Thu, 28 Nov 2024 09:19:58 +0500 Subject: [PATCH] Endpoint for bounty ticket review send --- db/structs.go | 1 + handlers/ticket.go | 63 +++++++++++++++++++++++++++++++++++++++++ routes/ticket_routes.go | 1 + 3 files changed, 65 insertions(+) diff --git a/db/structs.go b/db/structs.go index 300758092..a73000c5e 100644 --- a/db/structs.go +++ b/db/structs.go @@ -963,6 +963,7 @@ type Tickets struct { Dependency []int `gorm:"type:integer[]"` Description string `gorm:"type:text"` Status TicketStatus `gorm:"type:varchar(50);not null;default:'draft'"` + Version int `gorm:"type:integer" json:"version"` CreatedAt time.Time `gorm:"type:timestamp;not null;default:current_timestamp" json:"created_at"` UpdatedAt time.Time `gorm:"type:timestamp;not null;default:current_timestamp" json:"updated_at"` } diff --git a/handlers/ticket.go b/handlers/ticket.go index 64ade8ea2..8d27c6148 100644 --- a/handlers/ticket.go +++ b/handlers/ticket.go @@ -15,6 +15,13 @@ type ticketHandler struct { db db.Database } +type TicketResponse struct { + Success bool `json:"success"` + TicketID string `json:"ticket_id,omitempty"` + Message string `json:"message"` + Errors []string `json:"errors,omitempty"` +} + func NewTicketHandler(database db.Database) *ticketHandler { return &ticketHandler{ db: database, @@ -101,6 +108,62 @@ func (th *ticketHandler) DeleteTicket(w http.ResponseWriter, r *http.Request) { respondWithJSON(w, http.StatusOK, map[string]string{"status": "success"}) } +func (th *ticketHandler) PostTicketDataToStakwork(w http.ResponseWriter, r *http.Request) { + body, err := io.ReadAll(r.Body) + if err != nil { + respondWithJSON(w, http.StatusBadRequest, TicketResponse{ + Success: false, + Message: "Validation failed", + Errors: []string{"Error reading request body"}, + }) + return + } + + var ticket db.Tickets + if err := json.Unmarshal(body, &ticket); err != nil { + respondWithJSON(w, http.StatusBadRequest, TicketResponse{ + Success: false, + Message: "Validation failed", + Errors: []string{"Error parsing request body: " + err.Error()}, + }) + return + } + + var validationErrors []string + if ticket.UUID == uuid.Nil { + validationErrors = append(validationErrors, "UUID is required") + } else { + if _, err := uuid.Parse(ticket.UUID.String()); err != nil { + validationErrors = append(validationErrors, "Invalid UUID format") + } + } + + if ticket.FeatureUUID == "" { + validationErrors = append(validationErrors, "FeatureUUID is required") + } + if ticket.PhaseUUID == "" { + validationErrors = append(validationErrors, "PhaseUUID is required") + } + if ticket.Name == "" { + validationErrors = append(validationErrors, "Name is required") + } + + if len(validationErrors) > 0 { + respondWithJSON(w, http.StatusBadRequest, TicketResponse{ + Success: false, + Message: "Validation failed", + Errors: validationErrors, + }) + return + } + + respondWithJSON(w, http.StatusOK, TicketResponse{ + Success: true, + TicketID: ticket.UUID.String(), + Message: "Ticket submission is valid", + }) +} + func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(code) diff --git a/routes/ticket_routes.go b/routes/ticket_routes.go index 0fee7bef4..c2479414c 100644 --- a/routes/ticket_routes.go +++ b/routes/ticket_routes.go @@ -14,6 +14,7 @@ func TicketRoutes() chi.Router { r.Group(func(r chi.Router) { r.Use(auth.PubKeyContext) + r.Post("/review/send", ticketHandler.PostTicketDataToStakwork) r.Get("/{uuid}", ticketHandler.GetTicket) r.Put("/{uuid}", ticketHandler.UpdateTicket) r.Delete("/{uuid}", ticketHandler.DeleteTicket)