Skip to content

Commit

Permalink
sort response if public is true
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Vitanov committed Jul 30, 2024
1 parent e1284ee commit ae5c4a8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
5 changes: 3 additions & 2 deletions driver/web/apis_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ func (h ClientAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *toke
return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true)
}

resData := getSurveysResData(surveys, surverysRsponse, completed)
list := getSurveysResData(surveys, surverysRsponse, completed)
respData := sortIfpublicIsTrue(list, public)

rdata, err := json.Marshal(resData)
rdata, err := json.Marshal(respData)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionMarshal, logutils.TypeResponseBody, nil, err, http.StatusInternalServerError, false)
}
Expand Down
54 changes: 54 additions & 0 deletions driver/web/convertions_surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,57 @@ func getSurveysResData(items []model.Survey, surveyResponses []model.SurveyRespo

return list
}

func sortIfpublicIsTrue(list []model.SurveysResponseData, public *bool) []model.SurveysResponseData {

if public == nil || !*public {
// If public is nil or false, just return the list as-is
return list
}

var incompleteSurveys, noEndDateSurveys, completedSurveys []model.SurveysResponseData

// Split surveys into categories based on completion status and end date
for _, survey := range list {
if survey.Completed != nil && *survey.Completed {
completedSurveys = append(completedSurveys, survey)
} else if survey.EndDate != nil {
incompleteSurveys = append(incompleteSurveys, survey)
} else {
noEndDateSurveys = append(noEndDateSurveys, survey)
}
}

// Sort incomplete surveys by end date (ascending)
sort.Slice(incompleteSurveys, func(i, j int) bool {
return incompleteSurveys[i].EndDate.Before(*incompleteSurveys[j].EndDate)
})

// Sort no-end-date surveys first by start date (descending) or by creation date if start date is missing
sort.Slice(noEndDateSurveys, func(i, j int) bool {
if noEndDateSurveys[i].StartDate != nil && noEndDateSurveys[j].StartDate != nil {
return noEndDateSurveys[i].StartDate.After(*noEndDateSurveys[j].StartDate)
}
if noEndDateSurveys[i].StartDate != nil {
return true
}
if noEndDateSurveys[j].StartDate != nil {
return false
}
return noEndDateSurveys[i].DateCreated.After(noEndDateSurveys[j].DateCreated)
})

// Sort completed surveys by estimated completion time (descending)
sort.Slice(completedSurveys, func(i, j int) bool {
if completedSurveys[i].EstimatedCompletionTime != nil && completedSurveys[j].EstimatedCompletionTime != nil {
return *completedSurveys[i].EstimatedCompletionTime > *completedSurveys[j].EstimatedCompletionTime
}
return completedSurveys[i].DateCreated.After(completedSurveys[j].DateCreated)
})

// Combine all sorted slices
result := append(incompleteSurveys, noEndDateSurveys...)
result = append(result, completedSurveys...)

return result
}

0 comments on commit ae5c4a8

Please sign in to comment.