Skip to content

Commit

Permalink
Add #595 Admin Logs filter: It shall be possible to filter using mult…
Browse files Browse the repository at this point in the history
…iple choices as in ED
  • Loading branch information
albinpa committed Dec 14, 2023
1 parent 4f4fa0d commit 6f386e4
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions internal/handler/v2/audit/audit_admin_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,23 @@ func (e ListActionLogsError) Error() string {
}
}

// ParseListActionLogQueryParams parses query params for listing action logs.
func ParseListActionLogQueryParams(r *http.Request) (int, error) {
func ParseListActionLogQueryParams(r *http.Request) ([]int, error) {
query := r.URL.Query()
var logType int
var logTypes []int

// Check if logType query param is provided.
if r, ok := query["logType"]; ok && len(r) > 0 {
if oInt, err := strconv.Atoi(r[0]); err == nil && oInt >= 1 {
logType = oInt
return logType, nil
if logTypeParams, ok := query["logType"]; ok && len(logTypeParams) > 0 {
for _, param := range logTypeParams {
if oInt, err := strconv.Atoi(param); err == nil && oInt >= 1 {
logTypes = append(logTypes, oInt)
}
}
if len(logTypes) > 0 {
return logTypes, nil
}
}

return logType, ActionLogTypeIsMissingError
return logTypes, ActionLogTypeIsMissingError
}

type listActionLogsResp struct {
Expand Down Expand Up @@ -77,11 +80,11 @@ func AuditGetOrgLogs(w http.ResponseWriter, r *http.Request) {

var pipeline []primitive.M

logType, err := ParseListActionLogQueryParams(r)
logTypes, err := ParseListActionLogQueryParams(r)
if err != nil && errors.Is(err, ActionLogTypeIsMissingError) {
pipeline = []bson.M{{"$sort": bson.M{"timestamp": -1}}}
} else {
pipeline = []bson.M{{"$match": bson.M{"type": logType}}, {"$sort": bson.M{"timestamp": -1}}}
pipeline = []bson.M{{"$match": bson.M{"type": bson.M{"$in": logTypes}}}, {"$sort": bson.M{"timestamp": -1}}}
}
// Return all action logs
var actionLogs []actionlog.ActionLog
Expand Down

0 comments on commit 6f386e4

Please sign in to comment.