MSP-1756 get new method for token repository
This commit is contained in:
1 parent
1bbe8c71ce
commit
92021b8a0e
1 file changed
+29
@@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/integration-system/isp-kit/db"
|
||||
"github.com/integration-system/isp-kit/db/query"
|
||||
"github.com/integration-system/isp-kit/metrics/sql_metrics"
|
||||
@@ -159,3 +160,31 @@ func (r Token) LastAccessNotBlockedUsers(ctx context.Context) (map[int64]time.Ti
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r Token) LastAccessByUserIds(ctx context.Context, userIds []int) (map[int64]*time.Time, error) {
|
||||
sql_metrics.OperationLabelToContext(ctx, "Token.LastAccessNotBlockedUsers")
|
||||
|
||||
q, args, err := query.New().
|
||||
Select("user_id", "max(created_at) as created_at").
|
||||
From("tokens").
|
||||
Where(squirrel.Eq{"user_id": userIds}).
|
||||
GroupBy("user_id").
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "build query")
|
||||
}
|
||||
|
||||
tokens := make([]entity.Token, 0)
|
||||
err = r.db.Select(ctx, &tokens, q, args...)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "select query: %s", q)
|
||||
}
|
||||
|
||||
result := make(map[int64]*time.Time, 0)
|
||||
for _, token := range tokens {
|
||||
createdAt := token.CreatedAt
|
||||
result[token.UserId] = &createdAt
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in new issue
Block a user