MSP-7036 fix search & sort
This commit is contained in:
1 parent
71e0768231
commit
ec327424c9
19 files changed
+447
-126
No files matched your search
+2
-2
@@ -16,7 +16,7 @@ import (
|
||||
type AuditRepository interface {
|
||||
Insert(ctx context.Context, log entity.Audit) (int, error)
|
||||
All(ctx context.Context, req domain.AuditPageRequest) ([]entity.Audit, error)
|
||||
Count(ctx context.Context) (int64, error)
|
||||
Count(ctx context.Context, reqQuery *domain.AuditQuery) (int64, error)
|
||||
}
|
||||
|
||||
type AuditEventRepository interface {
|
||||
@@ -109,7 +109,7 @@ func (s Audit) All(ctx context.Context, req domain.AuditPageRequest) (*domain.Au
|
||||
})
|
||||
group.Go(func() error {
|
||||
var err error
|
||||
total, err = s.auditRep.Count(ctx)
|
||||
total, err = s.auditRep.Count(ctx, req.Query)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "count all audit")
|
||||
}
|
||||
|
||||
+36
-6
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"msp-admin-service/domain"
|
||||
@@ -17,8 +19,8 @@ type TokenRep interface {
|
||||
TokenSaver
|
||||
Get(ctx context.Context, token string) (*entity.Token, error)
|
||||
RevokeByUserId(ctx context.Context, userId int64, updatedAt time.Time) error
|
||||
All(ctx context.Context, req domain.SessionPageRequest) ([]entity.Token, error)
|
||||
Count(ctx context.Context) (int64, error)
|
||||
All(ctx context.Context, req domain.SessionPageRequest, timeNowUTC time.Time) ([]entity.Token, error)
|
||||
Count(ctx context.Context, reqQuery *domain.SessionQuery, timeNowUTC time.Time) (int64, error)
|
||||
UpdateStatus(ctx context.Context, id int, status string) error
|
||||
}
|
||||
|
||||
@@ -78,10 +80,12 @@ func (s Token) All(ctx context.Context, req domain.SessionPageRequest) (*domain.
|
||||
var tokens []entity.Token
|
||||
var total int64
|
||||
|
||||
timeNowUTC := time.Now().UTC()
|
||||
|
||||
group, ctx := errgroup.WithContext(ctx)
|
||||
group.Go(func() error {
|
||||
var err error
|
||||
tokens, err = s.tokenRep.All(ctx, req)
|
||||
tokens, err = s.tokenRep.All(ctx, req, timeNowUTC)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get all tokens")
|
||||
}
|
||||
@@ -89,7 +93,7 @@ func (s Token) All(ctx context.Context, req domain.SessionPageRequest) (*domain.
|
||||
})
|
||||
group.Go(func() error {
|
||||
var err error
|
||||
total, err = s.tokenRep.Count(ctx)
|
||||
total, err = s.tokenRep.Count(ctx, req.Query, timeNowUTC)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "count all tokens")
|
||||
}
|
||||
@@ -103,7 +107,7 @@ func (s Token) All(ctx context.Context, req domain.SessionPageRequest) (*domain.
|
||||
items := make([]domain.Session, 0)
|
||||
for _, token := range tokens {
|
||||
status := token.Status
|
||||
if time.Now().UTC().After(token.ExpiredAt) {
|
||||
if timeNowUTC.After(token.ExpiredAt) {
|
||||
status = entity.TokenStatusExpired
|
||||
}
|
||||
items = append(items, domain.Session{
|
||||
@@ -114,9 +118,22 @@ func (s Token) All(ctx context.Context, req domain.SessionPageRequest) (*domain.
|
||||
CreatedAt: token.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
if req.Order.Field == "status" {
|
||||
if strings.ToUpper(req.Order.Type) == "ASC" {
|
||||
slices.SortFunc(items, func(a, b domain.Session) int {
|
||||
return strings.Compare(a.Status, b.Status)
|
||||
})
|
||||
} else {
|
||||
slices.SortFunc(items, func(a, b domain.Session) int {
|
||||
return strings.Compare(b.Status, a.Status)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
result := domain.SessionResponse{
|
||||
TotalCount: int(total),
|
||||
Items: items,
|
||||
Items: applyLimitOffset(items, req.Limit, req.Offset),
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
@@ -129,3 +146,16 @@ func (s Token) Revoke(ctx context.Context, id int) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func applyLimitOffset(items []domain.Session, limit int, offset int) []domain.Session {
|
||||
result := make([]domain.Session, 0)
|
||||
for i := offset; i < offset+limit; i++ {
|
||||
if i == len(items) {
|
||||
break
|
||||
}
|
||||
|
||||
result = append(result, items[i])
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
+8
-8
@@ -150,11 +150,6 @@ func (u User) GetUsers(ctx context.Context, req domain.UsersPageRequest) (*domai
|
||||
return nil, errors.WithMessage(err, "get roles by user ids and roles id")
|
||||
}
|
||||
|
||||
lastSessionsByUsers, err := u.tokenRepo.LastAccessByUserIds(ctx, userIds, req.Query)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get last sessions by user ids")
|
||||
}
|
||||
|
||||
items := make([]domain.User, 0, len(users))
|
||||
for _, user := range users {
|
||||
roles := make([]int, 0)
|
||||
@@ -165,8 +160,8 @@ func (u User) GetUsers(ctx context.Context, req domain.UsersPageRequest) (*domai
|
||||
}
|
||||
}
|
||||
|
||||
if filteredRoles(req.Query, roles) && filteredLastSession(req.Query, lastSessionsByUsers[user.Id]) {
|
||||
items = append(items, u.toDomain(user, roles, lastSessionsByUsers[user.Id]))
|
||||
if filteredRoles(req.Query, roles) && filteredLastSession(req.Query, user.LastSessionCreatedAt) {
|
||||
items = append(items, u.toDomain(user, roles, user.LastSessionCreatedAt))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -558,7 +553,12 @@ func filteredLastSession(reqQuery *domain.UserQuery, lastSessionCreatedAt *time.
|
||||
return true
|
||||
}
|
||||
|
||||
if lastSessionCreatedAt != nil {
|
||||
if lastSessionCreatedAt == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if reqQuery.LastSessionCreatedAt.From.Compare(*lastSessionCreatedAt) <= 0 &&
|
||||
reqQuery.LastSessionCreatedAt.To.Compare(*lastSessionCreatedAt) >= 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user