From eb0a632ece8a2f72591511b2884230e29ed49f8c Mon Sep 17 00:00:00 2001 From: akash-sachan Date: Thu, 11 May 2023 19:52:17 +0530 Subject: [PATCH] order --- handlers/incident.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/handlers/incident.go b/handlers/incident.go index b79ee21b..294a3711 100644 --- a/handlers/incident.go +++ b/handlers/incident.go @@ -72,7 +72,7 @@ func getVisibleIncidentsOfService(service *services.Service) []incidents.Inciden visibleIncidentIds = append(visibleIncidentIds, incident.Id) } else if checkResolvedVisibility(incident.Updates) { incidentVar := *incident - sortUpdates(incidentVar.Updates) + sortUpdates(incidentVar.Updates, "DESC") visibleIncidents = append(visibleIncidents, incidentVar) visibleIncidentIds = append(visibleIncidentIds, incident.Id) } @@ -81,9 +81,15 @@ func getVisibleIncidentsOfService(service *services.Service) []incidents.Inciden return visibleIncidents } -func sortUpdates(updates []*incidents.IncidentUpdate) { +func sortUpdates(updates []*incidents.IncidentUpdate, order string) { sort.Slice(updates, func(i, j int) bool { - return updates[i].CreatedAt.Unix() >= updates[j].CreatedAt.Unix() + switch order { + case "DESC": + return updates[i].CreatedAt.Unix() >= updates[j].CreatedAt.Unix() + case "ASC": + return updates[i].CreatedAt.Unix() <= updates[j].CreatedAt.Unix() + } + return false }) } @@ -113,7 +119,7 @@ func apiIncidentUpdatesHandler(w http.ResponseWriter, r *http.Request) { sendErrorJson(err, w, r) return } - sortUpdates(incid.Updates) + sortUpdates(incid.Updates, "ASC") returnJson(incid.Updates, w, r) }