Skip to content

Commit

Permalink
Merge pull request #2184 from MahtabBukhari/require_new_logic_to_crea…
Browse files Browse the repository at this point in the history
…te_a_version_and_associate_ticket_group

Update Ticket Endpoint will require new logic to create a version and associate ticket group
  • Loading branch information
humansinstitute authored Dec 15, 2024
2 parents de4f6f0 + 818c0c4 commit fa07842
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
58 changes: 41 additions & 17 deletions handlers/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,9 @@ func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) {

var updateRequest UpdateTicketRequest
if err := json.Unmarshal(body, &updateRequest); err != nil {

var ticket db.Tickets
if err := json.Unmarshal(body, &ticket); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Error parsing request body"})
return
}

updateRequest.Ticket = &ticket
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Error parsing request body"})
return
}

if updateRequest.Ticket == nil {
Expand All @@ -128,15 +122,45 @@ func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) {
return
}

updateRequest.Ticket.UUID = ticketUUID
existingTicket, err := th.db.GetTicket(ticketUUID.String())
var newTicket db.Tickets

if updateRequest.Ticket.Status != "" && !db.IsValidTicketStatus(updateRequest.Ticket.Status) {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid ticket status"})
return
if err != nil {
newTicket = db.Tickets{
UUID: updateRequest.Ticket.UUID,
TicketGroup: func() *uuid.UUID { id := uuid.New(); return &id }(),
FeatureUUID: updateRequest.Ticket.FeatureUUID,
PhaseUUID: updateRequest.Ticket.PhaseUUID,
Name: updateRequest.Ticket.Name,
Sequence: updateRequest.Ticket.Sequence,
Dependency: updateRequest.Ticket.Dependency,
Description: updateRequest.Ticket.Description,
Status: updateRequest.Ticket.Status,
Version: 1,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
} else {

newTicket = db.Tickets{
UUID: uuid.New(),
TicketGroup: existingTicket.TicketGroup,
FeatureUUID: updateRequest.Ticket.FeatureUUID,
PhaseUUID: updateRequest.Ticket.PhaseUUID,
Name: updateRequest.Ticket.Name,
Sequence: updateRequest.Ticket.Sequence,
Dependency: updateRequest.Ticket.Dependency,
Description: updateRequest.Ticket.Description,
Status: updateRequest.Ticket.Status,
Version: existingTicket.Version + 1,
Author: updateRequest.Ticket.Author,
AuthorID: updateRequest.Ticket.AuthorID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
}

updatedTicket, err := th.db.CreateOrEditTicket(updateRequest.Ticket)
createdTicket, err := th.db.CreateOrEditTicket(&newTicket)
if err != nil {
if err.Error() == "feature_uuid, phase_uuid, and name are required" {
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -166,15 +190,15 @@ func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) {
log.Printf("Failed to send websocket message: %v", err)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"ticket": updatedTicket,
"ticket": createdTicket,
"websocket_error": err.Error(),
})
return
}
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(updatedTicket)
json.NewEncoder(w).Encode(createdTicket)
}

func (th *ticketHandler) DeleteTicket(w http.ResponseWriter, r *http.Request) {
Expand Down
8 changes: 3 additions & 5 deletions handlers/ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,9 @@ func TestUpdateTicket(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(tHandler.UpdateTicket)

updateTicket := db.Tickets{
UUID: createdTicket.UUID,
Description: "Updated description", // Optional field
Status: db.ReadyTicket, // Optional field
}
updateTicket := createdTicket
updateTicket.Description = "Updated Description"
updateTicket.Status = db.ReadyTicket

updateRequest := UpdateTicketRequest{
Metadata: struct {
Expand Down

0 comments on commit fa07842

Please sign in to comment.