Skip to content

Commit

Permalink
Merge pull request stakwork#2028 from MahtabBukhari/Route_Conflict_on…
Browse files Browse the repository at this point in the history
…_Get_Tickets_for_Phase

[BUG] Route Conflict on Get Tickets for Phase
  • Loading branch information
humansinstitute authored Dec 2, 2024
2 parents 195840c + e04e2c4 commit ea4c0c7
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 468 deletions.
15 changes: 0 additions & 15 deletions db/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,21 +328,6 @@ func (db database) GetBountiesByPhaseUuid(phaseUuid string) []Bounty {
return bounties
}

func (db database) GetTicketsByPhaseUUID(featureUUID string, phaseUUID string) ([]Tickets, error) {
var tickets []Tickets

result := db.db.
Where("feature_uuid = ? AND phase_uuid = ?", featureUUID, phaseUUID).
Order("sequence ASC").
Find(&tickets)

if result.Error != nil {
return nil, fmt.Errorf("failed to fetch tickets: %w", result.Error)
}

return tickets, nil
}

func (db database) GetProductBrief(workspaceUuid string) (string, error) {
workspace := Workspace{}
result := db.db.Model(&Workspace{}).Where("uuid = ?", workspaceUuid).First(&workspace)
Expand Down
1 change: 0 additions & 1 deletion db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,4 @@ type Database interface {
GetProductBrief(workspaceUuid string) (string, error)
GetFeatureBrief(featureUuid string) (string, error)
GetTicketsByPhaseUUID(featureUUID string, phaseUUID string) ([]Tickets, error)
GetTicketsByPhase(phaseUUID string) ([]Tickets, error)
}
8 changes: 4 additions & 4 deletions db/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,18 @@ func (db database) DeleteTicket(uuid string) error {
return nil
}

func (db database) GetTicketsByPhase(phaseUUID string) ([]Tickets, error) {
func (db database) GetTicketsByPhaseUUID(featureUUID string, phaseUUID string) ([]Tickets, error) {
var tickets []Tickets

result := db.db.Where("phase_uuid = ?", phaseUUID).
result := db.db.
Where("feature_uuid = ? AND phase_uuid = ?", featureUUID, phaseUUID).
Order("sequence ASC").
Find(&tickets)

if result.Error != nil {
return nil, fmt.Errorf("failed to fetch tickets for phase: %w", result.Error)
return nil, fmt.Errorf("failed to fetch tickets: %w", result.Error)
}

// Return empty slice if no tickets found
if result.RowsAffected == 0 {
return []Tickets{}, nil
}
Expand Down
108 changes: 0 additions & 108 deletions db/tickets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,111 +223,3 @@ func TestGetTicket(t *testing.T) {
}
})
}

func TestGetTicketsByPhase(t *testing.T) { // create person
InitTestDB()

// create person
now := time.Now()

person := Person{
Uuid: uuid.New().String(),
OwnerPubKey: "testfeaturepubkey",
OwnerAlias: "testfeaturealias",
Description: "testfeaturedescription",
Created: &now,
Updated: &now,
Deleted: false,
}

// create person
TestDB.CreateOrEditPerson(person)

workspace := Workspace{
Uuid: uuid.New().String(),
Name: "Test tickets space",
Created: &now,
Updated: &now,
}

// create workspace
TestDB.CreateOrEditWorkspace(workspace)

workspaceFeatures := WorkspaceFeatures{
Uuid: uuid.New().String(),
WorkspaceUuid: workspace.Uuid,
Name: "test",
Brief: "test get brief",
Requirements: "Test get requirements",
Architecture: "Test get architecture",
Url: "Test get url",
Priority: 1,
Created: &now,
Updated: &now,
CreatedBy: "test",
UpdatedBy: "test",
}

// create WorkspaceFeatures
TestDB.CreateOrEditFeature(workspaceFeatures)

featurePhase := FeaturePhase{
Uuid: uuid.New().String(),
FeatureUuid: workspaceFeatures.Uuid,
Name: "test get feature phase",
Priority: 1,
Created: &now,
Updated: &now,
}

// create FeaturePhase
TestDB.CreateOrEditFeaturePhase(featurePhase)

ticket := Tickets{
UUID: uuid.New(),
FeatureUUID: workspaceFeatures.Uuid,
PhaseUUID: featurePhase.Uuid,
Name: "test get tickets 1",
CreatedAt: now,
UpdatedAt: now,
}

// create ticket
TestDB.CreateOrEditTicket(&ticket)

ticket2 := Tickets{
UUID: uuid.New(),
FeatureUUID: workspaceFeatures.Uuid,
PhaseUUID: featurePhase.Uuid,
Name: "test get tickets 2",
CreatedAt: now,
UpdatedAt: now,
}

// create ticket
TestDB.CreateOrEditTicket(&ticket2)

// should return an empty slice if no tickets are found for the phase
t.Run("should return an empty slice if no tickets are found for the phase", func(t *testing.T) {
result, err := TestDB.GetTicketsByPhase(uuid.New().String())
if err != nil {
t.Errorf("expected no error but got %v", err)
}

if len(result) != 0 {
t.Errorf("expected an empty slice but got %v", result)
}
})

// should return a slice of tickets if the phase exists
t.Run("should return a slice of tickets if the phase exists", func(t *testing.T) {
result, err := TestDB.GetTicketsByPhase(featurePhase.Uuid)
if err != nil {
t.Errorf("expected no error but got %v", err)
}

if len(result) != 2 {
t.Errorf("expected 2 tickets but got %v", len(result))
}
})
}
49 changes: 0 additions & 49 deletions handlers/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"os"

"github.com/go-chi/chi"
"github.com/google/uuid"
"github.com/rs/xid"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
Expand Down Expand Up @@ -698,51 +697,3 @@ func (oh *featureHandler) BriefSend(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(resp.StatusCode)
w.Write(respBody)
}

func (oh *featureHandler) GetTicketsByPhaseUUID(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)
if pubKeyFromAuth == "" {
fmt.Println("no pubkey from auth")
w.WriteHeader(http.StatusUnauthorized)
return
}

featureUUID := chi.URLParam(r, "feature_uuid")
phaseUUID := chi.URLParam(r, "phase_uuid")

if _, err := uuid.Parse(featureUUID); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "invalid feature UUID format"})
return
}
if _, err := uuid.Parse(phaseUUID); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "invalid phase UUID format"})
return
}

feature := oh.db.GetFeatureByUuid(featureUUID)
if feature.Uuid == "" {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"error": "feature not found"})
return
}

_, err := oh.db.GetFeaturePhaseByUuid(featureUUID, phaseUUID)
if err != nil {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"error": "Phase not found"})
return
}

tickets, err := oh.db.GetTicketsByPhaseUUID(featureUUID, phaseUUID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tickets)
}
Loading

0 comments on commit ea4c0c7

Please sign in to comment.