Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ type Tickets struct {
Dependency []int `gorm:"type:integer[]" json:"dependency"`
Description string `gorm:"type:text" json:"description"`
Status TicketStatus `gorm:"type:varchar(50);default:'DRAFT'" json:"status"`
Assignee string `gorm:"type:varchar(255);default:''" json:"assignee"`
Version int `gorm:"type:integer;default:0" json:"version"`
Author *Author `gorm:"type:varchar(50)" json:"author,omitempty"`
AuthorID *string `gorm:"type:varchar(255)" json:"author_id,omitempty"`
Expand Down
17 changes: 17 additions & 0 deletions handlers/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) {
Status: updateRequest.Ticket.Status,
Amount: updateRequest.Ticket.Amount,
Category: updateRequest.Ticket.Category,
Assignee: updateRequest.Ticket.Assignee,
Version: 1,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Expand All @@ -215,6 +216,7 @@ func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) {
Status: updateRequest.Ticket.Status,
Amount: updateRequest.Ticket.Amount,
Category: updateRequest.Ticket.Category,
Assignee: updateRequest.Ticket.Assignee,
Version: existingTicket.Version + 1,
Author: updateRequest.Ticket.Author,
AuthorID: updateRequest.Ticket.AuthorID,
Expand All @@ -235,6 +237,21 @@ func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) {
return
}

if newTicket.Assignee != "" {
var previousAssignee string
if existingTicket.UUID != uuid.Nil {
previousAssignee = existingTicket.Assignee
}

if newTicket.Assignee != previousAssignee {
person := th.db.GetPersonByPubkey(newTicket.Assignee)
if person.OwnerPubKey != "" {
msg := fmt.Sprintf("You have been assigned a new ticket: %s. %s/bounty/%s", newTicket.Name, os.Getenv("HOST"), newTicket.UUID.String())
processNotification(newTicket.Assignee, "ticket_assigned", msg, person.OwnerAlias, person.OwnerRouteHint)
}
}
}

if updateRequest.Metadata.Source == "websocket" && updateRequest.Metadata.ID != "" {
ticketMsg := websocket.TicketMessage{
BroadcastType: "direct",
Expand Down
124 changes: 124 additions & 0 deletions handlers/ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1840,3 +1840,127 @@ func TestTicketsToBounties(t *testing.T) {
})
}
}

func TestUpdateTicketAssignmentNotification(t *testing.T) {
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

tHandler := NewTicketHandler(&http.Client{}, db.TestDB)

person := db.Person{
Uuid: uuid.New().String(),
OwnerAlias: "assignee-alias",
UniqueName: "assignee-unique",
OwnerPubKey: "assignee-pubkey",
PriceToMeet: 0,
Description: "assignee description",
OwnerRouteHint: "route_hint",
}
db.TestDB.CreateOrEditPerson(person)

workspace := db.Workspace{
Uuid: uuid.New().String(),
Name: "test-workspace-" + uuid.New().String(),
OwnerPubKey: "owner-pubkey",
}
db.TestDB.CreateOrEditWorkspace(workspace)

feature := db.WorkspaceFeatures{
Uuid: uuid.New().String(),
WorkspaceUuid: workspace.Uuid,
Name: "test-feature",
}
db.TestDB.CreateOrEditFeature(feature)

featurePhase := db.FeaturePhase{
Uuid: uuid.New().String(),
FeatureUuid: feature.Uuid,
WorkspaceId: workspace.Uuid,
Name: "test-phase",
}
db.TestDB.CreateOrEditFeaturePhase(featurePhase)

t.Run("should trigger notification when ticket is assigned", func(t *testing.T) {
ticketUUID := uuid.New()
ticket := &db.Tickets{
UUID: ticketUUID,
FeatureUUID: feature.Uuid,
PhaseUUID: featurePhase.Uuid,
Name: "test ticket",
Status: db.DraftTicket,
Assignee: "",
}
db.TestDB.CreateOrEditTicket(ticket)

updateRequest := UpdateTicketRequest{
Ticket: &db.Tickets{
UUID: ticketUUID,
FeatureUUID: feature.Uuid,
PhaseUUID: featurePhase.Uuid,
Name: "test ticket",
Status: db.ActiveTicket,
Assignee: "assignee-pubkey",
},
}
bodyBytes, _ := json.Marshal(updateRequest)

rr := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPut, "/bounties/ticket/"+ticketUUID.String(), bytes.NewReader(bodyBytes))
req = req.WithContext(context.WithValue(req.Context(), auth.ContextKey, "owner-pubkey"))

rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", ticketUUID.String())
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

tHandler.UpdateTicket(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)

var response db.Tickets
err := json.NewDecoder(rr.Body).Decode(&response)
require.NoError(t, err)
assert.Equal(t, "assignee-pubkey", response.Assignee)
})

t.Run("should not trigger notification when assignee is unchanged", func(t *testing.T) {
ticketUUID := uuid.New()
ticket := &db.Tickets{
UUID: ticketUUID,
FeatureUUID: feature.Uuid,
PhaseUUID: featurePhase.Uuid,
Name: "test ticket unchanged",
Status: db.DraftTicket,
Assignee: "assignee-pubkey",
}
db.TestDB.CreateOrEditTicket(ticket)

updateRequest := UpdateTicketRequest{
Ticket: &db.Tickets{
UUID: ticketUUID,
FeatureUUID: feature.Uuid,
PhaseUUID: featurePhase.Uuid,
Name: "test ticket unchanged",
Status: db.ActiveTicket,
Assignee: "assignee-pubkey",
},
}
bodyBytes, _ := json.Marshal(updateRequest)

rr := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPut, "/bounties/ticket/"+ticketUUID.String(), bytes.NewReader(bodyBytes))
req = req.WithContext(context.WithValue(req.Context(), auth.ContextKey, "owner-pubkey"))

rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", ticketUUID.String())
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

tHandler.UpdateTicket(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)

var response db.Tickets
err := json.NewDecoder(rr.Body).Decode(&response)
require.NoError(t, err)
assert.Equal(t, "assignee-pubkey", response.Assignee)
})
}