Skip to content

Commit

Permalink
fix: sorting for completed and scheduled maintenance entries
Browse files Browse the repository at this point in the history
* Completed entries are sorted by descending completion date
* Scheduled entries are sorted by ascending scheduling date
* Selecting both entries shall sort them by scheduling and then completion date in descending order
  • Loading branch information
zebrapurring committed Dec 12, 2024
1 parent 90adeb0 commit 2e20832
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion backend/internal/data/repo/repo_maintenance_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"time"

"entgo.io/ent/dialect/sql"
"github.com/google/uuid"
"github.com/sysadminsmedia/homebox/backend/internal/data/ent"
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/group"
Expand Down Expand Up @@ -136,15 +137,29 @@ func (r *MaintenanceEntryRepository) GetMaintenanceByItemID(ctx context.Context,
maintenanceentry.DateEQ(time.Time{}),
maintenanceentry.DateGT(time.Now()),
))
// Sort scheduled entries by ascending scheduled date
query = query.Order(
maintenanceentry.ByScheduledDate(sql.OrderAsc()),
)
} else if filters.Status == MaintenanceFilterStatusCompleted {
query = query.Where(
maintenanceentry.Not(maintenanceentry.Or(
maintenanceentry.DateIsNil(),
maintenanceentry.DateEQ(time.Time{}),
maintenanceentry.DateGT(time.Now()),
)))
// Sort completed entries by descending completion date
query = query.Order(
maintenanceentry.ByDate(sql.OrderDesc()),
)
} else {
// Sort entries by default by scheduled and maintenance date in descending order
query = query.Order(
maintenanceentry.ByScheduledDate(sql.OrderDesc()),
maintenanceentry.ByDate(sql.OrderDesc()),
)
}
entries, err := query.WithItem().Order(maintenanceentry.ByScheduledDate()).All(ctx)
entries, err := query.WithItem().All(ctx)

if err != nil {
return []MaintenanceEntryWithDetails{}, err
Expand Down

0 comments on commit 2e20832

Please sign in to comment.