MSP-7036 update pages sorting & search
This commit is contained in:
1 parent
b1d5aae316
commit
c17970678b
13 files changed
+288
-99
No files matched your search
+6
-5
@@ -3,14 +3,15 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
|
||||
"msp-admin-service/domain"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"msp-admin-service/domain"
|
||||
)
|
||||
|
||||
type AuditService interface {
|
||||
All(ctx context.Context, limit int, offset int) (*domain.AuditResponse, error)
|
||||
All(ctx context.Context, req domain.AuditPageRequest) (*domain.AuditResponse, error)
|
||||
Events(ctx context.Context) ([]domain.AuditEvent, error)
|
||||
SetEvents(ctx context.Context, req []domain.SetAuditEvent) error
|
||||
}
|
||||
@@ -31,13 +32,13 @@ func NewAudit(service AuditService) Audit {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param X-AUTH-ADMIN header string true "Токен администратора"
|
||||
// @Param body body domain.PageRequest true "Тело запроса"
|
||||
// @Param body body domain.AuditPageRequest true "Тело запроса"
|
||||
// @Success 200 {object} domain.AuditResponse
|
||||
// @Failure 400 {object} domain.GrpcError "Невалидное тело запроса"
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /log/all [POST]
|
||||
func (c Audit) All(ctx context.Context, req domain.PageRequest) (*domain.AuditResponse, error) {
|
||||
return c.service.All(ctx, req.Limit, req.Offset)
|
||||
func (c Audit) All(ctx context.Context, req domain.AuditPageRequest) (*domain.AuditResponse, error) {
|
||||
return c.service.All(ctx, req)
|
||||
}
|
||||
|
||||
// Events
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
type SessionService interface {
|
||||
All(ctx context.Context, limit int, offset int) (*domain.SessionResponse, error)
|
||||
All(ctx context.Context, req domain.SessionPageRequest) (*domain.SessionResponse, error)
|
||||
Revoke(ctx context.Context, id int) error
|
||||
}
|
||||
|
||||
@@ -27,13 +27,13 @@ func NewSession(service SessionService) Session {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param X-AUTH-ADMIN header string true "Токен администратора"
|
||||
// @Param body body domain.SessionRequest true "Тело запроса"
|
||||
// @Success 200 {object} domain.PageRequest
|
||||
// @Param body body domain.SessionPageRequest true "Тело запроса"
|
||||
// @Success 200 {object} domain.SessionResponse
|
||||
// @Failure 400 {object} domain.GrpcError "Невалидное тело запроса"
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /session/all [POST]
|
||||
func (c Session) All(ctx context.Context, req domain.PageRequest) (*domain.SessionResponse, error) {
|
||||
return c.service.All(ctx, req.Limit, req.Offset)
|
||||
func (c Session) All(ctx context.Context, req domain.SessionPageRequest) (*domain.SessionResponse, error) {
|
||||
return c.service.All(ctx, req)
|
||||
}
|
||||
|
||||
// Revoke
|
||||
|
||||
+7
-5
@@ -2,18 +2,20 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/txix-open/isp-kit/grpc/apierrors"
|
||||
|
||||
"msp-admin-service/domain"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"msp-admin-service/domain"
|
||||
)
|
||||
|
||||
type userService interface {
|
||||
GetProfileById(ctx context.Context, userId int64) (*domain.AdminUserShort, error)
|
||||
GetUsers(ctx context.Context, identities domain.UsersRequest) (*domain.UsersResponse, error)
|
||||
GetUsers(ctx context.Context, req domain.UsersPageRequest) (*domain.UsersResponse, error)
|
||||
CreateUser(ctx context.Context, req domain.CreateUserRequest, adminId int64) (*domain.User, error)
|
||||
UpdateUser(ctx context.Context, req domain.UpdateUserRequest, adminId int64) (*domain.User, error)
|
||||
DeleteUsers(ctx context.Context, ids []int64, adminId int64) (int, error)
|
||||
@@ -70,13 +72,13 @@ func (u User) GetProfile(ctx context.Context, authData grpc.AuthData) (*domain.A
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param X-AUTH-ADMIN header string true "Токен администратора"
|
||||
// @Param body body domain.UsersRequest true "Тело запроса"
|
||||
// @Param body body domain.UsersPageRequest true "Тело запроса"
|
||||
// @Success 200 {object} domain.UsersResponse
|
||||
// @Failure 400 {object} domain.GrpcError
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /user/get_users [POST]
|
||||
func (u User) GetUsers(ctx context.Context, identities domain.UsersRequest) (*domain.UsersResponse, error) {
|
||||
users, err := u.userService.GetUsers(ctx, identities)
|
||||
func (u User) GetUsers(ctx context.Context, req domain.UsersPageRequest) (*domain.UsersResponse, error) {
|
||||
users, err := u.userService.GetUsers(ctx, req)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get users")
|
||||
}
|
||||
|
||||
+11
-3
@@ -15,10 +15,18 @@ type AuditResponse struct {
|
||||
TotalCount int
|
||||
Items []Audit
|
||||
}
|
||||
type AuditPageRequest struct {
|
||||
LimitOffestParams
|
||||
|
||||
type PageRequest struct {
|
||||
Limit int `validate:"required"`
|
||||
Offset int
|
||||
Order *OrderParams
|
||||
Query *AuditQuery
|
||||
}
|
||||
|
||||
type AuditQuery struct {
|
||||
Id *int
|
||||
UserId *int
|
||||
Message *string
|
||||
CreatedAt *DateFromToParams
|
||||
}
|
||||
|
||||
type SetAuditEvent struct {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type LimitOffestParams struct {
|
||||
Limit int `validate:"required"`
|
||||
Offset int
|
||||
}
|
||||
|
||||
type OrderParams struct {
|
||||
Field string
|
||||
Type string `validate:"oneof=asc desc ASC DESC"`
|
||||
}
|
||||
|
||||
type DateFromToParams struct {
|
||||
From time.Time `validate:"required"`
|
||||
To time.Time `validate:"required"`
|
||||
}
|
||||
+15
-5
@@ -12,16 +12,26 @@ type Session struct {
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type SessionPageRequest struct {
|
||||
LimitOffestParams
|
||||
|
||||
Order *OrderParams
|
||||
Query *SessionQuery
|
||||
}
|
||||
|
||||
type SessionQuery struct {
|
||||
Id *int
|
||||
UserId *int
|
||||
Status *string
|
||||
CreatedAt *DateFromToParams
|
||||
ExpiredAt *DateFromToParams
|
||||
}
|
||||
|
||||
type SessionResponse struct {
|
||||
TotalCount int
|
||||
Items []Session
|
||||
}
|
||||
|
||||
type SessionRequest struct {
|
||||
Limit int
|
||||
Offset int
|
||||
}
|
||||
|
||||
type RevokeRequest struct {
|
||||
Id int `validate:"required"`
|
||||
}
|
||||
+13
-5
@@ -21,11 +21,19 @@ type UsersResponse struct {
|
||||
Items []User
|
||||
}
|
||||
|
||||
type UsersRequest struct {
|
||||
Ids []int64
|
||||
Offset int
|
||||
Limit int
|
||||
Email string
|
||||
type UserQuery struct {
|
||||
Id *int
|
||||
Description *string
|
||||
Email *string
|
||||
Roles []int
|
||||
LastSessionCreatedAt *DateFromToParams
|
||||
}
|
||||
|
||||
type UsersPageRequest struct {
|
||||
LimitOffestParams
|
||||
|
||||
Order *OrderParams
|
||||
Query *UserQuery
|
||||
}
|
||||
|
||||
type AdminUserShort struct {
|
||||
|
||||
+46
-11
@@ -4,11 +4,14 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"msp-admin-service/domain"
|
||||
"msp-admin-service/entity"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/db"
|
||||
"github.com/txix-open/isp-kit/db/query"
|
||||
"github.com/txix-open/isp-kit/metrics/sql_metrics"
|
||||
"msp-admin-service/entity"
|
||||
)
|
||||
|
||||
type Audit struct {
|
||||
@@ -43,28 +46,32 @@ func (r Audit) Insert(ctx context.Context, log entity.Audit) (int, error) {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
//nolint:gosec
|
||||
func (r Audit) All(ctx context.Context, limit int, offset int) ([]entity.Audit, error) {
|
||||
//nolint:dupl,gosec
|
||||
func (r Audit) All(ctx context.Context, req domain.AuditPageRequest) ([]entity.Audit, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Audit.All")
|
||||
|
||||
query, args, err := query.New().
|
||||
q := query.New().
|
||||
Select("*").
|
||||
From("audit").
|
||||
OrderBy("created_at DESC").
|
||||
Offset(uint64(offset)).
|
||||
Limit(uint64(limit)).
|
||||
ToSql()
|
||||
Offset(uint64(req.Offset)).
|
||||
Limit(uint64(req.Limit))
|
||||
|
||||
if req.Order != nil {
|
||||
q = q.OrderBy(req.Order.Field + " " + req.Order.Type)
|
||||
}
|
||||
|
||||
query, args, err := reqAuditQuery(q, req.Query).ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "build query")
|
||||
}
|
||||
|
||||
tokens := make([]entity.Audit, 0)
|
||||
err = r.db.Select(ctx, &tokens, query, args...)
|
||||
logs := make([]entity.Audit, 0)
|
||||
err = r.db.Select(ctx, &logs, query, args...)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "select query: %s", query)
|
||||
}
|
||||
|
||||
return tokens, nil
|
||||
return logs, nil
|
||||
}
|
||||
|
||||
func (r Audit) Count(ctx context.Context) (int64, error) {
|
||||
@@ -96,3 +103,31 @@ func (r Audit) DeleteUpToCreatedAt(ctx context.Context, createdAt time.Time) err
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func reqAuditQuery(q squirrel.SelectBuilder, reqQuery *domain.AuditQuery) squirrel.SelectBuilder {
|
||||
if reqQuery == nil {
|
||||
return q
|
||||
}
|
||||
|
||||
switch {
|
||||
case reqQuery.Id != nil:
|
||||
{
|
||||
q = q.Where("id = ?", *reqQuery.Id)
|
||||
}
|
||||
case reqQuery.UserId != nil:
|
||||
{
|
||||
q = q.Where("user_id = ?", *reqQuery.UserId)
|
||||
}
|
||||
case reqQuery.Message != nil:
|
||||
{
|
||||
q = q.Where("message LIKE ?", "%"+*reqQuery.Message+"%")
|
||||
}
|
||||
case reqQuery.CreatedAt != nil:
|
||||
{
|
||||
q = q.Where(squirrel.GtOrEq{"created_at": reqQuery.CreatedAt.From},
|
||||
squirrel.LtOrEq{"created_at": reqQuery.CreatedAt.To})
|
||||
}
|
||||
}
|
||||
|
||||
return q
|
||||
}
|
||||
+64
-17
@@ -3,15 +3,17 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/Masterminds/squirrel"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
|
||||
"msp-admin-service/domain"
|
||||
"msp-admin-service/entity"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/db"
|
||||
"github.com/txix-open/isp-kit/db/query"
|
||||
"github.com/txix-open/isp-kit/metrics/sql_metrics"
|
||||
"msp-admin-service/domain"
|
||||
"msp-admin-service/entity"
|
||||
)
|
||||
|
||||
type Token struct {
|
||||
@@ -76,17 +78,21 @@ func (r Token) RevokeByUserId(ctx context.Context, userId int64, updatedAt time.
|
||||
return nil
|
||||
}
|
||||
|
||||
//nolint:gosec
|
||||
func (r Token) All(ctx context.Context, limit int, offset int) ([]entity.Token, error) {
|
||||
//nolint:dupl,gosec
|
||||
func (r Token) All(ctx context.Context, req domain.SessionPageRequest) ([]entity.Token, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Token.All")
|
||||
|
||||
query, args, err := query.New().
|
||||
q := query.New().
|
||||
Select("*").
|
||||
From("tokens").
|
||||
OrderBy("created_at DESC").
|
||||
Offset(uint64(offset)).
|
||||
Limit(uint64(limit)).
|
||||
ToSql()
|
||||
Offset(uint64(req.Offset)).
|
||||
Limit(uint64(req.Limit))
|
||||
|
||||
if req.Order != nil {
|
||||
q = q.OrderBy(req.Order.Field + " " + req.Order.Type)
|
||||
}
|
||||
|
||||
query, args, err := reqTokenQuery(q, req.Query).ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "build query")
|
||||
}
|
||||
@@ -139,26 +145,34 @@ func (r Token) UpdateStatusByUserId(ctx context.Context, userId int, status stri
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r Token) LastAccessByUserIds(ctx context.Context, userIds []int) (map[int64]*time.Time, error) {
|
||||
func (r Token) LastAccessByUserIds(ctx context.Context, userIds []int, reqQuery *domain.UserQuery) (map[int64]*time.Time, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Token.LastAccessNotBlockedUsers")
|
||||
|
||||
q, args, err := query.New().
|
||||
q := query.New().
|
||||
Select("user_id", "max(created_at) as created_at").
|
||||
From("tokens").
|
||||
Where(squirrel.Eq{"user_id": userIds}).
|
||||
GroupBy("user_id").
|
||||
ToSql()
|
||||
GroupBy("user_id")
|
||||
|
||||
if reqQuery != nil {
|
||||
if reqQuery.LastSessionCreatedAt != nil {
|
||||
q = q.Where(squirrel.GtOrEq{"created_at": reqQuery.LastSessionCreatedAt.From}).
|
||||
Where(squirrel.LtOrEq{"created_at": reqQuery.LastSessionCreatedAt.To})
|
||||
}
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "build query")
|
||||
}
|
||||
|
||||
tokens := make([]entity.Token, 0)
|
||||
err = r.db.Select(ctx, &tokens, q, args...)
|
||||
err = r.db.Select(ctx, &tokens, query, args...)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "select query: %s", q)
|
||||
return nil, errors.WithMessagef(err, "select query: %s", query)
|
||||
}
|
||||
|
||||
result := make(map[int64]*time.Time, 0)
|
||||
result := make(map[int64]*time.Time)
|
||||
for _, token := range tokens {
|
||||
createdAt := token.CreatedAt
|
||||
result[token.UserId] = &createdAt
|
||||
@@ -166,3 +180,36 @@ func (r Token) LastAccessByUserIds(ctx context.Context, userIds []int) (map[int6
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func reqTokenQuery(q squirrel.SelectBuilder, reqQuery *domain.SessionQuery) squirrel.SelectBuilder {
|
||||
if reqQuery == nil {
|
||||
return q
|
||||
}
|
||||
|
||||
switch {
|
||||
case reqQuery.Id != nil:
|
||||
{
|
||||
q = q.Where("id = ?", *reqQuery.Id)
|
||||
}
|
||||
case reqQuery.UserId != nil:
|
||||
{
|
||||
q = q.Where("user_id = ?", *reqQuery.UserId)
|
||||
}
|
||||
case reqQuery.Status != nil:
|
||||
{
|
||||
q = q.Where("status = ?", *reqQuery.Status)
|
||||
}
|
||||
case reqQuery.CreatedAt != nil:
|
||||
{
|
||||
q = q.Where(squirrel.GtOrEq{"created_at": reqQuery.CreatedAt.From}).
|
||||
Where(squirrel.LtOrEq{"created_at": reqQuery.CreatedAt.To})
|
||||
}
|
||||
case reqQuery.ExpiredAt != nil:
|
||||
{
|
||||
q = q.Where(squirrel.GtOrEq{"expired_at": reqQuery.ExpiredAt.From}).
|
||||
Where(squirrel.LtOrEq{"expired_at": reqQuery.ExpiredAt.To})
|
||||
}
|
||||
}
|
||||
|
||||
return q
|
||||
}
|
||||
+36
-17
@@ -73,12 +73,12 @@ func (u User) GetUserById(ctx context.Context, identity int64) (*entity.User, er
|
||||
}
|
||||
}
|
||||
|
||||
func (u User) GetUserByEmailAndSudirId(ctx context.Context, email string, sudir_user_id string) (*entity.User, error) {
|
||||
func (u User) GetUserByEmailAndSudirId(ctx context.Context, email string, sudirUserId string) (*entity.User, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "User.GetUserByEmail")
|
||||
|
||||
equalClause := squirrel.Eq{"email": email, "sudir_user_id": nil}
|
||||
if sudir_user_id != "" {
|
||||
equalClause["sudir_user_id"] = sudir_user_id
|
||||
if sudirUserId != "" {
|
||||
equalClause["sudir_user_id"] = sudirUserId
|
||||
}
|
||||
|
||||
q, args, err := query.New().
|
||||
@@ -133,31 +133,27 @@ func (u User) UpsertBySudirUserId(ctx context.Context, user entity.User) (*entit
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
//nolint:gosec
|
||||
func (u User) GetUsers(ctx context.Context, ids []int64, offset, limit int, email string) ([]entity.User, error) {
|
||||
//nolint:dupl,gosec
|
||||
func (u User) GetUsers(ctx context.Context, req domain.UsersPageRequest) ([]entity.User, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "User.GetUsers")
|
||||
|
||||
q := query.New().
|
||||
Select("*").
|
||||
From("users")
|
||||
From("users").
|
||||
Offset(uint64(req.Offset)).
|
||||
Limit(uint64(req.Limit))
|
||||
|
||||
if len(ids) > 0 {
|
||||
q = q.Where(squirrel.Eq{"id": ids})
|
||||
if req.Order != nil {
|
||||
q = q.OrderBy(req.Order.Field + " " + req.Order.Type)
|
||||
}
|
||||
if email != "" {
|
||||
q = q.Where("email LIKE ?", "%"+email+"%")
|
||||
}
|
||||
qstring, args, err := q.
|
||||
Limit(uint64(limit)).
|
||||
Offset(uint64(offset)).
|
||||
OrderBy("created_at DESC").
|
||||
ToSql()
|
||||
|
||||
query, args, err := reqUsersQuery(q, req.Query).ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "build query")
|
||||
}
|
||||
|
||||
users := make([]entity.User, 0)
|
||||
err = u.db.Select(ctx, &users, qstring, args...)
|
||||
err = u.db.Select(ctx, &users, query, args...)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "db select")
|
||||
}
|
||||
@@ -346,3 +342,26 @@ func (u User) UpdateLastActiveAt(ctx context.Context, userId int64, lastActiveAt
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func reqUsersQuery(q squirrel.SelectBuilder, reqQuery *domain.UserQuery) squirrel.SelectBuilder {
|
||||
if reqQuery == nil {
|
||||
return q
|
||||
}
|
||||
|
||||
switch {
|
||||
case reqQuery.Id != nil:
|
||||
{
|
||||
q = q.Where("id = ?", *reqQuery.Id)
|
||||
}
|
||||
case reqQuery.Description != nil:
|
||||
{
|
||||
q = q.Where("description = ?", *reqQuery.Description)
|
||||
}
|
||||
case reqQuery.Email != nil:
|
||||
{
|
||||
q = q.Where("email LIKE ?", "%"+*reqQuery.Email+"%")
|
||||
}
|
||||
}
|
||||
|
||||
return q
|
||||
}
|
||||
+9
-9
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
type AuditRepository interface {
|
||||
Insert(ctx context.Context, log entity.Audit) (int, error)
|
||||
All(ctx context.Context, limit int, offset int) ([]entity.Audit, error)
|
||||
All(ctx context.Context, req domain.AuditPageRequest) ([]entity.Audit, error)
|
||||
Count(ctx context.Context) (int64, error)
|
||||
}
|
||||
|
||||
@@ -94,13 +94,13 @@ func (s Audit) SaveAuditAsync(ctx context.Context, userId int64, message string,
|
||||
}()
|
||||
}
|
||||
|
||||
func (s Audit) All(ctx context.Context, limit int, offset int) (*domain.AuditResponse, error) {
|
||||
func (s Audit) All(ctx context.Context, req domain.AuditPageRequest) (*domain.AuditResponse, error) {
|
||||
group, ctx := errgroup.WithContext(ctx)
|
||||
var tokens []entity.Audit
|
||||
var logs []entity.Audit
|
||||
var total int64
|
||||
var err error
|
||||
group.Go(func() error {
|
||||
tokens, err = s.auditRep.All(ctx, limit, offset)
|
||||
logs, err = s.auditRep.All(ctx, req)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get all audit")
|
||||
}
|
||||
@@ -119,12 +119,12 @@ func (s Audit) All(ctx context.Context, limit int, offset int) (*domain.AuditRes
|
||||
}
|
||||
|
||||
items := make([]domain.Audit, 0)
|
||||
for _, token := range tokens {
|
||||
for _, log := range logs {
|
||||
items = append(items, domain.Audit{
|
||||
Id: token.Id,
|
||||
UserId: token.UserId,
|
||||
Message: token.Message,
|
||||
CreatedAt: token.CreatedAt,
|
||||
Id: log.Id,
|
||||
UserId: log.UserId,
|
||||
Message: log.Message,
|
||||
CreatedAt: log.CreatedAt,
|
||||
})
|
||||
}
|
||||
result := domain.AuditResponse{
|
||||
|
||||
+6
-5
@@ -6,17 +6,18 @@ import (
|
||||
"encoding/hex"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"msp-admin-service/domain"
|
||||
"msp-admin-service/entity"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
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, limit int, offset int) ([]entity.Token, error)
|
||||
All(ctx context.Context, req domain.SessionPageRequest) ([]entity.Token, error)
|
||||
Count(ctx context.Context) (int64, error)
|
||||
UpdateStatus(ctx context.Context, id int, status string) error
|
||||
}
|
||||
@@ -73,13 +74,13 @@ func (s Token) RevokeAllByUserId(ctx context.Context, userId int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Token) All(ctx context.Context, limit int, offset int) (*domain.SessionResponse, error) {
|
||||
func (s Token) All(ctx context.Context, req domain.SessionPageRequest) (*domain.SessionResponse, error) {
|
||||
group, ctx := errgroup.WithContext(ctx)
|
||||
var tokens []entity.Token
|
||||
var total int64
|
||||
var err error
|
||||
group.Go(func() error {
|
||||
tokens, err = s.tokenRep.All(ctx, limit, offset)
|
||||
tokens, err = s.tokenRep.All(ctx, req)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get all tokens")
|
||||
}
|
||||
|
||||
+52
-12
@@ -28,8 +28,8 @@ type UserTransactionRunner interface {
|
||||
|
||||
type UserRepo interface {
|
||||
GetUserById(ctx context.Context, identity int64) (*entity.User, error)
|
||||
GetUsers(ctx context.Context, ids []int64, offset, limit int, email string) ([]entity.User, error)
|
||||
GetUserByEmailAndSudirId(ctx context.Context, email string, sudir_user_id string) (*entity.User, error)
|
||||
GetUsers(ctx context.Context, req domain.UsersPageRequest) ([]entity.User, error)
|
||||
GetUserByEmailAndSudirId(ctx context.Context, email string, sudirUserId string) (*entity.User, error)
|
||||
GetUsersByEmail(ctx context.Context, email string) ([]entity.User, error)
|
||||
UpdateUser(ctx context.Context, id int64, user entity.UpdateUser) (*entity.User, error)
|
||||
DeleteUser(ctx context.Context, ids []int64) (int, error)
|
||||
@@ -41,7 +41,7 @@ type UserRepo interface {
|
||||
|
||||
type TokenRepo interface {
|
||||
UpdateStatusByUserId(ctx context.Context, userId int, status string) error
|
||||
LastAccessByUserIds(ctx context.Context, userIds []int) (map[int64]*time.Time, error)
|
||||
LastAccessByUserIds(ctx context.Context, userIds []int, reqQuery *domain.UserQuery) (map[int64]*time.Time, error)
|
||||
}
|
||||
|
||||
type UserRoleRepo interface {
|
||||
@@ -134,8 +134,8 @@ func (u User) GetProfileById(ctx context.Context, userId int64) (*domain.AdminUs
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (u User) GetUsers(ctx context.Context, req domain.UsersRequest) (*domain.UsersResponse, error) {
|
||||
users, err := u.userRepo.GetUsers(ctx, req.Ids, req.Offset, req.Limit, req.Email)
|
||||
func (u User) GetUsers(ctx context.Context, req domain.UsersPageRequest) (*domain.UsersResponse, error) {
|
||||
users, err := u.userRepo.GetUsers(ctx, req)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get users from repo")
|
||||
}
|
||||
@@ -145,12 +145,12 @@ func (u User) GetUsers(ctx context.Context, req domain.UsersRequest) (*domain.Us
|
||||
userIds = append(userIds, int(user.Id))
|
||||
}
|
||||
|
||||
rolesByUsers, err := u.userRoleRepo.GetRolesByUserIds(ctx, userIds)
|
||||
userRoles, err := u.userRoleRepo.GetRolesByUserIds(ctx, userIds)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get roles by user ids")
|
||||
return nil, errors.WithMessage(err, "get roles by user ids and roles id")
|
||||
}
|
||||
|
||||
lastSessionsByUsers, err := u.tokenRepo.LastAccessByUserIds(ctx, userIds)
|
||||
lastSessionsByUsers, err := u.tokenRepo.LastAccessByUserIds(ctx, userIds, req.Query)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get last sessions by user ids")
|
||||
}
|
||||
@@ -159,13 +159,15 @@ func (u User) GetUsers(ctx context.Context, req domain.UsersRequest) (*domain.Us
|
||||
for _, user := range users {
|
||||
roles := make([]int, 0)
|
||||
|
||||
for _, role := range rolesByUsers {
|
||||
for _, role := range userRoles {
|
||||
if role.UserId == int(user.Id) {
|
||||
roles = append(roles, role.RoleId)
|
||||
}
|
||||
}
|
||||
|
||||
items = append(items, u.toDomain(user, roles, lastSessionsByUsers[user.Id]))
|
||||
if filteredRoles(req.Query, roles) && filteredLastSession(req.Query, lastSessionsByUsers[user.Id]) {
|
||||
items = append(items, u.toDomain(user, roles, lastSessionsByUsers[user.Id]))
|
||||
}
|
||||
}
|
||||
|
||||
return &domain.UsersResponse{Items: items}, nil
|
||||
@@ -296,7 +298,7 @@ func (u User) UpdateUser(ctx context.Context, req domain.UpdateUserRequest, admi
|
||||
return errors.WithMessage(err, "update user role links")
|
||||
}
|
||||
|
||||
userLastSession, err := tx.LastAccessByUserIds(ctx, []int{int(user.Id)})
|
||||
userLastSession, err := tx.LastAccessByUserIds(ctx, []int{int(user.Id)}, nil)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get last user session")
|
||||
}
|
||||
@@ -360,7 +362,7 @@ func (u User) GetById(ctx context.Context, userId int) (*domain.User, error) {
|
||||
return nil, errors.WithMessage(err, "get roles by user id")
|
||||
}
|
||||
|
||||
lastSessions, err := u.tokenRepo.LastAccessByUserIds(ctx, []int{userId})
|
||||
lastSessions, err := u.tokenRepo.LastAccessByUserIds(ctx, []int{userId}, nil)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get last sessions by user id")
|
||||
}
|
||||
@@ -524,3 +526,41 @@ func (u User) toDomain(user entity.User, roleIds []int, lastSessionCreatedAt *ti
|
||||
LastSessionCreatedAt: lastSessionCreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func filteredRoles(reqQuery *domain.UserQuery, roleIds []int) bool {
|
||||
if reqQuery == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if reqQuery.Roles == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if len(reqQuery.Roles) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, roleId := range roleIds {
|
||||
if slices.Contains(reqQuery.Roles, roleId) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func filteredLastSession(reqQuery *domain.UserQuery, lastSessionCreatedAt *time.Time) bool {
|
||||
if reqQuery == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if reqQuery.LastSessionCreatedAt == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if lastSessionCreatedAt != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user