Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sorting for completed and scheduled maintenance entries #384

Merged
Merged
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
22 changes: 19 additions & 3 deletions 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 @@ -130,21 +131,36 @@ func (r *MaintenanceEntryRepository) GetMaintenanceByItemID(ctx context.Context,
item.HasGroupWith(group.IDEQ(groupID)),
),
)
if filters.Status == MaintenanceFilterStatusScheduled {
switch filters.Status {
case MaintenanceFilterStatusScheduled:
query = query.Where(maintenanceentry.Or(
maintenanceentry.DateIsNil(),
maintenanceentry.DateEQ(time.Time{}),
maintenanceentry.DateGT(time.Now()),
))
} else if filters.Status == MaintenanceFilterStatusCompleted {
// Sort scheduled entries by ascending scheduled date
query = query.Order(
maintenanceentry.ByScheduledDate(sql.OrderAsc()),
)
case 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()),
)
default:
// 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
Loading