MSP-7036 refactor
This commit is contained in:
1 parent
c036063534
commit
d19bb44bb6
13 files changed
+40
-48
No files matched your search
@@ -69,6 +69,7 @@ definitions:
|
||||
limit:
|
||||
type: integer
|
||||
offset:
|
||||
format: int64
|
||||
type: integer
|
||||
order:
|
||||
$ref: '#/definitions/domain.OrderParams'
|
||||
@@ -311,6 +312,7 @@ definitions:
|
||||
limit:
|
||||
type: integer
|
||||
offset:
|
||||
format: int64
|
||||
type: integer
|
||||
order:
|
||||
$ref: '#/definitions/domain.OrderParams'
|
||||
@@ -432,6 +434,7 @@ definitions:
|
||||
limit:
|
||||
type: integer
|
||||
offset:
|
||||
format: int64
|
||||
type: integer
|
||||
order:
|
||||
$ref: '#/definitions/domain.OrderParams'
|
||||
|
||||
+2
-2
@@ -8,8 +8,8 @@ const (
|
||||
)
|
||||
|
||||
type LimitOffestParams struct {
|
||||
Limit int `validate:"required"`
|
||||
Offset int
|
||||
Limit uint64 `validate:"required"`
|
||||
Offset uint64
|
||||
}
|
||||
|
||||
type OrderParams struct {
|
||||
|
||||
@@ -9,7 +9,7 @@ require (
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/stretchr/testify v1.11.1
|
||||
github.com/txix-open/bgjob v1.6.0
|
||||
github.com/txix-open/isp-kit v1.64.6
|
||||
github.com/txix-open/isp-kit v1.64.7
|
||||
github.com/txix-open/jsonschema v1.3.0
|
||||
golang.org/x/crypto v0.45.0
|
||||
golang.org/x/sync v0.18.0
|
||||
|
||||
@@ -154,8 +154,8 @@ github.com/txix-open/etp/v4 v4.0.1 h1:5VSYgGsvnMz0tvUHsjVWFETI0qWBTCF1O3ARCHaCIg
|
||||
github.com/txix-open/etp/v4 v4.0.1/go.mod h1:FIu7TUDdwcgmtmF6tFTV1bGA2fRY/iVaPQdsGzvqYc8=
|
||||
github.com/txix-open/grmq v1.9.0 h1:ArDbJJcFX9zXKEcPf49rp9nYiRyGMJxGKJAxdbSsfaM=
|
||||
github.com/txix-open/grmq v1.9.0/go.mod h1:EyFOP17KrwEFOUjR+PH8iH3LCtiN9TSQ8t0ysyk62po=
|
||||
github.com/txix-open/isp-kit v1.64.6 h1:AXQ8/HQGG1VoJyiFvPqgPimPp5WJ4MrlWzPBxuxqhRs=
|
||||
github.com/txix-open/isp-kit v1.64.6/go.mod h1:GvBsX5p2U3/ugK6PuWANYzSy/4YgawROm3eAKb35G6o=
|
||||
github.com/txix-open/isp-kit v1.64.7 h1:gPSVDtmmpNqKJumZxEvnHYY1K1B54zFwHyBIaKo3y5A=
|
||||
github.com/txix-open/isp-kit v1.64.7/go.mod h1:GvBsX5p2U3/ugK6PuWANYzSy/4YgawROm3eAKb35G6o=
|
||||
github.com/txix-open/jsonschema v1.3.0 h1:XGuOTg22G7PdqfKypjajM20/i5FyGfFYsJKmheebKi4=
|
||||
github.com/txix-open/jsonschema v1.3.0/go.mod h1:l8YDZ1nvJrw6uxWowSVOxCV/ebiMJyapffW87ZEqH00=
|
||||
github.com/txix-open/validator/v10 v10.0.0-20250506161033-f8ce404fffdb h1:UJgT4u/QMv5QHKOQeJ7igShHa36c2/vIRqJiRLdDlf0=
|
||||
|
||||
+4
-5
@@ -47,16 +47,15 @@ func (r Audit) Insert(ctx context.Context, log entity.Audit) (int, error) {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
//nolint:dupl,gosec
|
||||
func (r Audit) All(ctx context.Context, req domain.AuditPageRequest) ([]entity.Audit, error) {
|
||||
func (r Audit) AllByRequest(ctx context.Context, req domain.AuditPageRequest) ([]entity.Audit, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Audit.All")
|
||||
|
||||
q := query.New().
|
||||
Select("*").
|
||||
From("audit").
|
||||
OrderBy(strcase.ToSnake(req.Order.Field) + " " + req.Order.Type).
|
||||
Offset(uint64(req.Offset)).
|
||||
Limit(uint64(req.Limit))
|
||||
Offset(req.Offset).
|
||||
Limit(req.Limit)
|
||||
|
||||
query, args, err := reqAuditQuery(q, req.Query).ToSql()
|
||||
if err != nil {
|
||||
@@ -130,7 +129,7 @@ func reqAuditQuery(q squirrel.SelectBuilder, reqQuery *domain.AuditQuery) squirr
|
||||
|
||||
if reqQuery.CreatedAt != nil {
|
||||
q = q.Where(squirrel.GtOrEq{"created_at": reqQuery.CreatedAt.From}).
|
||||
Where(squirrel.LtOrEq{"created_at": reqQuery.CreatedAt.To})
|
||||
Where(squirrel.Lt{"created_at": reqQuery.CreatedAt.To})
|
||||
}
|
||||
|
||||
return q
|
||||
|
||||
+11
-20
@@ -97,7 +97,6 @@ func (r Token) All(ctx context.Context) ([]entity.Token, error) {
|
||||
return tokens, nil
|
||||
}
|
||||
|
||||
//nolint:dupl,gosec
|
||||
func (r Token) AllByRequest(ctx context.Context, req domain.SessionPageRequest) ([]entity.Token, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Token.AllByRequest")
|
||||
|
||||
@@ -105,8 +104,8 @@ func (r Token) AllByRequest(ctx context.Context, req domain.SessionPageRequest)
|
||||
Select("*").
|
||||
From("tokens").
|
||||
OrderBy(strcase.ToSnake(req.Order.Field) + " " + req.Order.Type).
|
||||
Offset(uint64(req.Offset)).
|
||||
Limit(uint64(req.Limit))
|
||||
Offset(req.Offset).
|
||||
Limit(req.Limit)
|
||||
|
||||
query, args, err := reqTokenQuery(q, req.Query).ToSql()
|
||||
if err != nil {
|
||||
@@ -136,8 +135,8 @@ func (r Token) UpdateStatus(ctx context.Context, id int, status string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r Token) SetExpiredStatusById(ctx context.Context, ids []int) error {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Token.SetExpiredStatusById")
|
||||
func (r Token) SetExpiredStatusByIds(ctx context.Context, ids []int) error {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Token.SetExpiredStatusByIds")
|
||||
|
||||
query, args, err := query.New().
|
||||
Update("tokens").
|
||||
@@ -190,23 +189,15 @@ func (r Token) UpdateStatusByUserId(ctx context.Context, userId int, status stri
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r Token) LastAccessByUserIds(ctx context.Context, userIds []int, reqQuery *domain.UserQuery) (map[int64]*time.Time, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Token.LastAccessNotBlockedUsers")
|
||||
func (r Token) LastAccessByUserIds(ctx context.Context, userIds []int) (map[int64]*time.Time, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Token.LastAccessByUserIds")
|
||||
|
||||
q := query.New().
|
||||
query, args, err := query.New().
|
||||
Select("user_id", "max(created_at) as created_at").
|
||||
From("tokens").
|
||||
Where(squirrel.Eq{"user_id": userIds}).
|
||||
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()
|
||||
GroupBy("user_id").
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "build query")
|
||||
}
|
||||
@@ -245,12 +236,12 @@ func reqTokenQuery(q squirrel.SelectBuilder, reqQuery *domain.SessionQuery) squi
|
||||
|
||||
if reqQuery.CreatedAt != nil {
|
||||
q = q.Where(squirrel.GtOrEq{"created_at": reqQuery.CreatedAt.From}).
|
||||
Where(squirrel.LtOrEq{"created_at": reqQuery.CreatedAt.To})
|
||||
Where(squirrel.Lt{"created_at": reqQuery.CreatedAt.To})
|
||||
}
|
||||
|
||||
if reqQuery.ExpiredAt != nil {
|
||||
q = q.Where(squirrel.GtOrEq{"expired_at": reqQuery.ExpiredAt.From}).
|
||||
Where(squirrel.LtOrEq{"expired_at": reqQuery.ExpiredAt.To})
|
||||
Where(squirrel.Lt{"expired_at": reqQuery.ExpiredAt.To})
|
||||
}
|
||||
|
||||
return q
|
||||
|
||||
+2
-3
@@ -134,15 +134,14 @@ func (u User) UpsertBySudirUserId(ctx context.Context, user entity.User) (*entit
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
//nolint: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("*", "(SELECT max(created_at) FROM tokens WHERE tokens.user_id = users.id) as last_session_created_at").
|
||||
From("users").
|
||||
Offset(uint64(req.Offset)).
|
||||
Limit(uint64(req.Limit))
|
||||
Offset(req.Offset).
|
||||
Limit(req.Limit)
|
||||
|
||||
if req.Order.Field == "userId" {
|
||||
q = q.OrderBy("last_name "+req.Order.Type, "first_name "+req.Order.Type)
|
||||
|
||||
+2
-2
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
type AuditRepository interface {
|
||||
Insert(ctx context.Context, log entity.Audit) (int, error)
|
||||
All(ctx context.Context, req domain.AuditPageRequest) ([]entity.Audit, error)
|
||||
AllByRequest(ctx context.Context, req domain.AuditPageRequest) ([]entity.Audit, error)
|
||||
Count(ctx context.Context, reqQuery *domain.AuditQuery) (int64, error)
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ func (s Audit) All(ctx context.Context, req domain.AuditPageRequest) (*domain.Au
|
||||
group, ctx := errgroup.WithContext(ctx)
|
||||
group.Go(func() error {
|
||||
var err error
|
||||
logs, err = s.auditRep.All(ctx, req)
|
||||
logs, err = s.auditRep.AllByRequest(ctx, req)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get all audit")
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ type TokenTransactionRunner interface {
|
||||
|
||||
type TokenTransaction interface {
|
||||
All(ctx context.Context) ([]entity.Token, error)
|
||||
SetExpiredStatusById(ctx context.Context, ids []int) error
|
||||
SetExpiredStatusByIds(ctx context.Context, ids []int) error
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
@@ -73,7 +73,7 @@ func (w Service) do(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = tx.SetExpiredStatusById(ctx, expiredTokenIds)
|
||||
err = tx.SetExpiredStatusByIds(ctx, expiredTokenIds)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "set expired status by id")
|
||||
}
|
||||
|
||||
+4
-4
@@ -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, reqQuery *domain.UserQuery) (map[int64]*time.Time, error)
|
||||
LastAccessByUserIds(ctx context.Context, userIds []int) (map[int64]*time.Time, error)
|
||||
}
|
||||
|
||||
type UserRoleRepo interface {
|
||||
@@ -293,7 +293,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)}, nil)
|
||||
userLastSession, err := tx.LastAccessByUserIds(ctx, []int{int(user.Id)})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get last user session")
|
||||
}
|
||||
@@ -357,7 +357,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}, nil)
|
||||
lastSessions, err := u.tokenRepo.LastAccessByUserIds(ctx, []int{userId})
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get last sessions by user id")
|
||||
}
|
||||
@@ -558,7 +558,7 @@ func filteredLastSession(reqQuery *domain.UserQuery, lastSessionCreatedAt *time.
|
||||
}
|
||||
|
||||
if reqQuery.LastSessionCreatedAt.From.Compare(*lastSessionCreatedAt) <= 0 &&
|
||||
reqQuery.LastSessionCreatedAt.To.Compare(*lastSessionCreatedAt) >= 0 {
|
||||
reqQuery.LastSessionCreatedAt.To.Compare(*lastSessionCreatedAt) > 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -200,7 +200,7 @@ func (s *AuthTestSuite) Test_Logout_NotFound() {
|
||||
|
||||
time.Sleep(time.Second)
|
||||
audit := repository.NewAudit(s.db)
|
||||
auditList, err := audit.All(context.Background(), domain.AuditPageRequest{
|
||||
auditList, err := audit.AllByRequest(context.Background(), domain.AuditPageRequest{
|
||||
LimitOffestParams: domain.LimitOffestParams{
|
||||
Limit: 10,
|
||||
Offset: 0,
|
||||
@@ -211,7 +211,7 @@ func (s *AuthTestSuite) Test_Logout_NotFound() {
|
||||
},
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(auditList, 1) //nolint:mnd
|
||||
s.Require().Len(auditList, 1)
|
||||
s.Require().Equal(entity.EventSuccessLogout, auditList[0].Event)
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestInactiveWorker(t *testing.T) {
|
||||
require.NoError(err)
|
||||
require.True(user.Blocked)
|
||||
|
||||
list, err := repository.NewAudit(db).All(t.Context(), domain.AuditPageRequest{
|
||||
list, err := repository.NewAudit(db).AllByRequest(t.Context(), domain.AuditPageRequest{
|
||||
LimitOffestParams: domain.LimitOffestParams{
|
||||
Limit: 10,
|
||||
Offset: 0,
|
||||
|
||||
+4
-4
@@ -199,7 +199,7 @@ func (s *UserTestSuite) TestGetUsers() {
|
||||
|
||||
roles := []int{int(roleId1), int(roleId2)}
|
||||
|
||||
s.Require().Len(response.Items, 2) //nolint:mnd
|
||||
s.Require().Len(response.Items, 2)
|
||||
s.Require().EqualValues("a1@b.ru", response.Items[0].Email)
|
||||
s.Require().EqualValues(roles, response.Items[0].Roles)
|
||||
s.Require().EqualValues("a1@c.ru", response.Items[1].Email)
|
||||
@@ -279,8 +279,8 @@ func (s *UserTestSuite) TestGetUsersFilterByLastActiveAt() {
|
||||
},
|
||||
Query: &domain.UserQuery{
|
||||
LastSessionCreatedAt: &domain.DateFromToParams{
|
||||
From: userTime1.Add(-24 * time.Hour),
|
||||
To: userTime1.Add(24 * time.Hour),
|
||||
From: userTime1.Add(-48 * time.Hour),
|
||||
To: userTime1.Add(48 * time.Hour),
|
||||
},
|
||||
},
|
||||
}).
|
||||
@@ -288,7 +288,7 @@ func (s *UserTestSuite) TestGetUsersFilterByLastActiveAt() {
|
||||
Do(context.Background())
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.Require().Len(response.Items, 2) //nolint:mnd
|
||||
s.Require().Len(response.Items, 2)
|
||||
s.Require().EqualValues(userId2, response.Items[0].Id)
|
||||
s.Require().EqualValues(userId1, response.Items[1].Id)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user