fix(ticket): guard stale responses and stale hours

This commit is contained in:
mlogclub
2026-05-02 20:20:30 +08:00
parent 8a8b92a307
commit 8102f89308
5 changed files with 61 additions and 21 deletions
+22 -7
View File
@@ -55,6 +55,15 @@ type TicketListAggregate struct {
type ticketService struct {
}
func normalizeTicketStaleHours(staleHours int) int {
switch staleHours {
case 24, 48, 168:
return staleHours
default:
return 24
}
}
func (s *ticketService) Get(id int64) *models.Ticket {
return repositories.TicketRepository.Get(sqls.DB(), id)
}
@@ -84,6 +93,16 @@ func (s *ticketService) FindPageAggregateByCnd(cnd *sqls.Cnd, _ int64) (*TicketL
return s.buildTicketListAggregate(sqls.DB(), list, paging), nil
}
func (s *ticketService) ApplyStaleFilter(cnd *sqls.Cnd, staleHours int) *sqls.Cnd {
if cnd == nil {
cnd = sqls.NewCnd()
}
staleHour := normalizeTicketStaleHours(staleHours)
return cnd.
NotEq("status", enums.TicketStatusDone).
Where("updated_at < ?", time.Now().Add(-time.Duration(staleHour)*time.Hour))
}
func (s *ticketService) Count(cnd *sqls.Cnd) int64 {
return repositories.TicketRepository.Count(sqls.DB(), cnd)
}
@@ -445,8 +464,8 @@ func (s *ticketService) GetDetail(id int64) (*TicketDetailAggregate, error) {
}
func (s *ticketService) GetSummary(operator *dto.AuthPrincipal, staleHours ...int) *TicketSummaryAggregate {
staleHour := 24
if len(staleHours) > 0 && staleHours[0] > 0 {
staleHour := 0
if len(staleHours) > 0 {
staleHour = staleHours[0]
}
summary := &TicketSummaryAggregate{
@@ -455,11 +474,7 @@ func (s *ticketService) GetSummary(operator *dto.AuthPrincipal, staleHours ...in
InProgress: s.Count(sqls.NewCnd().Eq("status", enums.TicketStatusInProgress)),
Done: s.Count(sqls.NewCnd().Eq("status", enums.TicketStatusDone)),
Unassigned: s.Count(sqls.NewCnd().Eq("current_assignee_id", 0)),
Stale: s.Count(
sqls.NewCnd().
NotEq("status", enums.TicketStatusDone).
Where("updated_at < ?", time.Now().Add(-time.Duration(staleHour)*time.Hour)),
),
Stale: s.Count(s.ApplyStaleFilter(sqls.NewCnd(), staleHour)),
}
if operator != nil {
summary.Mine = s.Count(sqls.NewCnd().Eq("current_assignee_id", operator.UserID))
+11 -5
View File
@@ -259,7 +259,7 @@ func TestTicketServiceSummaryCountsStaleTickets(t *testing.T) {
if _, err := services.TicketService.CreateTicket(createTestTicketRequest("unassigned ticket"), operator); err != nil {
t.Fatalf("CreateTicket() unassigned error = %v", err)
}
staleUpdatedAt := time.Now().Add(-48 * time.Hour)
staleUpdatedAt := time.Now().Add(-36 * time.Hour)
if err := repositories.TicketRepository.Updates(sqls.DB(), mine.ID, map[string]any{
"updated_at": staleUpdatedAt,
}); err != nil {
@@ -282,6 +282,15 @@ func TestTicketServiceSummaryCountsStaleTickets(t *testing.T) {
if summary.Stale != 1 {
t.Fatalf("expected stale count 1, got %d", summary.Stale)
}
summary48 := services.TicketService.GetSummary(operator, 48)
if summary48.Stale != 0 {
t.Fatalf("expected stale count 0 for 48 hour threshold, got %d", summary48.Stale)
}
summaryInvalid := services.TicketService.GetSummary(operator, 1<<30)
if summaryInvalid.Stale != 1 {
t.Fatalf("expected invalid stale threshold to use 24 hours, got %d", summaryInvalid.Stale)
}
}
func TestTicketServiceFindPageAggregateFiltersStaleTickets(t *testing.T) {
@@ -316,10 +325,7 @@ func TestTicketServiceFindPageAggregateFiltersStaleTickets(t *testing.T) {
}
aggregate, err := services.TicketService.FindPageAggregateByCnd(
sqls.NewCnd().
NotEq("status", enums.TicketStatusDone).
Where("updated_at < ?", time.Now().Add(-24*time.Hour)).
Page(1, 10),
services.TicketService.ApplyStaleFilter(sqls.NewCnd(), 24).Page(1, 10),
operator.UserID,
)
if err != nil {