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 authored and georgepadayatti committed Dec 18, 2023
1 parent 0030101 commit 1c08ef5
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions internal/handler/v2/audit/audit_admin_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net/http"
"strconv"
"strings"

"github.com/bb-consent/api/internal/actionlog"
"github.com/bb-consent/api/internal/common"
Expand Down Expand Up @@ -34,20 +35,26 @@ 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 {
params := strings.Split(param, ",") // Split by comma
for _, p := range params {
if oInt, err := strconv.Atoi(p); 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 +84,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 1c08ef5

Please sign in to comment.