Skip to content

Commit

Permalink
added tickets group update to default, and wrote test
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Dec 11, 2024
1 parent 0f7ee06 commit 48a6122
Show file tree
Hide file tree
Showing 6 changed files with 476 additions and 145 deletions.
18 changes: 18 additions & 0 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"fmt"
"log"
"os"

"github.com/rs/xid"
Expand Down Expand Up @@ -456,3 +457,20 @@ func (db database) UserHasManageBountyRoles(pubKeyFromAuth string, uuid string)
}
return true
}

func (db database) ProcessUpdateTicketsWithoutGroup() {
// get all tickets without group
tickets, err := DB.GetTicketsWithoutGroup()
if err != nil {
log.Printf("Error getting tickets without group: %v", err)
return
}

// update each ticket with group uuid
for _, ticket := range tickets {
err := DB.UpdateTicketsWithoutGroup(ticket)
if err != nil {
log.Printf("Error updating ticket: %v", err)
}
}
}
101 changes: 101 additions & 0 deletions db/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -152,3 +153,103 @@ func TestUserHasManageBountyRoles(t *testing.T) {
assert.False(t, result, "Expected UserHasManageBountyRoles to return false for user without all bounty roles")
})
}

func TestProcessUpdateTicketsWithoutGroup(t *testing.T) {
InitTestDB()

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

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

// create person
TestDB.CreateOrEditPerson(person)

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

// create workspace
TestDB.CreateOrEditWorkspace(workspace)

workspaceFeatures := WorkspaceFeatures{
Uuid: uuid.New().String(),
WorkspaceUuid: workspace.Uuid,
Name: "test process feature",
Brief: "test get process brief",
Requirements: "Test get process requirements",
Architecture: "Test get process architecture",
Url: "Test get process 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 process 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 process ticket",
CreatedAt: now,
UpdatedAt: now,
}

// create ticket
TestDB.CreateOrEditTicket(&ticket)

// get tickets without group and assert that there is 1
tickets, err := TestDB.GetTicketsWithoutGroup()
assert.NoError(t, err)
assert.Equal(t, 1, len(tickets))

// process update tickets without group
TestDB.ProcessUpdateTicketsWithoutGroup()

// get tickets without group and assert that there is 0
tickets, err = TestDB.GetTicketsWithoutGroup()
assert.NoError(t, err)
assert.Equal(t, 0, len(tickets))

// get ticket and assert that the ticket group is the same as the ticket uuid
ticket, err = TestDB.GetTicket(ticket.UUID.String())
assert.NoError(t, err)
assert.Equal(t, ticket.TicketGroup, ticket.UUID)

// get ticket and assert that the author id is 12345
ticket, err = TestDB.GetTicket(ticket.UUID.String())
assert.NoError(t, err)
assert.Equal(t, ticket.AuthorID, "12345")

// get ticket and assert that the author is HUMAN
ticket, err = TestDB.GetTicket(ticket.UUID.String())
assert.NoError(t, err)
assert.Equal(t, ticket.Author, "HUMAN")
}
3 changes: 3 additions & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,7 @@ type Database interface {
GetProductBrief(workspaceUuid string) (string, error)
GetFeatureBrief(featureUuid string) (string, error)
GetTicketsByPhaseUUID(featureUUID string, phaseUUID string) ([]Tickets, error)
GetTicketsWithoutGroup() ([]Tickets, error)
UpdateTicketsWithoutGroup(ticket Tickets) error
ProcessUpdateTicketsWithoutGroup()
}
45 changes: 45 additions & 0 deletions db/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,48 @@ func (db database) GetTicketsByPhaseUUID(featureUUID string, phaseUUID string) (

return tickets, nil
}

func (db database) GetTicketsWithoutGroup() ([]Tickets, error) {
var tickets []Tickets

result := db.db.
Where("ticket_group IS NULL OR ticket_group = ?", uuid.Nil).Find(&tickets)

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

if result.RowsAffected == 0 {
return []Tickets{}, nil
}

return tickets, nil
}

func (db database) UpdateTicketsWithoutGroup(ticket Tickets) error {
data := []map[string]interface{}{}

data = append(data, map[string]interface{}{
"ticket_group": ticket.UUID,
})

if ticket.AuthorID == nil {
data = append(data, map[string]interface{}{
"author_id": "12345",
})
}

if ticket.Author == nil {
data = append(data, map[string]interface{}{
"author": "HUMAN",
})
}

result := db.db.Model(&Tickets{}).Where("uuid = ?", ticket.UUID).Updates(data)

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

return nil
}
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ import (
)

func main() {
var err error

err = godotenv.Load()
if err != nil {
if err := godotenv.Load(); err != nil {
fmt.Println("no .env file")
}

db.InitDB()
db.InitRedis()
db.InitCache()
db.InitRoles()
db.DB.ProcessUpdateTicketsWithoutGroup()

// Config has to be inited before JWT, if not it will lead to NO JWT error
config.InitConfig()
auth.InitJwt()
Expand Down
Loading

0 comments on commit 48a6122

Please sign in to comment.