feat: implement tenant-side creator audit feature and update related tests and documentation
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go.ipao.vip/gen/field"
|
||||
"go.ipao.vip/gen/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -717,6 +718,158 @@ func (s *creator) ListOrders(
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s *creator) ListAuditLogs(
|
||||
ctx context.Context,
|
||||
tenantID int64,
|
||||
userID int64,
|
||||
filter *creator_dto.CreatorAuditLogListFilter,
|
||||
) (*requests.Pager, error) {
|
||||
if filter == nil {
|
||||
filter = &creator_dto.CreatorAuditLogListFilter{}
|
||||
}
|
||||
if tenantID == 0 {
|
||||
return nil, errorx.ErrRecordNotFound.WithMsg("租户不存在")
|
||||
}
|
||||
|
||||
if _, err := Tenant.ensureTenantAdmin(ctx, tenantID, userID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tbl, q := models.AuditLogQuery.QueryContext(ctx)
|
||||
q = q.Where(tbl.TenantID.Eq(tenantID))
|
||||
|
||||
if filter.OperatorID != nil && *filter.OperatorID > 0 {
|
||||
q = q.Where(tbl.OperatorID.Eq(*filter.OperatorID))
|
||||
}
|
||||
if filter.Action != nil && strings.TrimSpace(*filter.Action) != "" {
|
||||
q = q.Where(tbl.Action.Eq(strings.TrimSpace(*filter.Action)))
|
||||
}
|
||||
if filter.TargetID != nil && strings.TrimSpace(*filter.TargetID) != "" {
|
||||
q = q.Where(tbl.TargetID.Eq(strings.TrimSpace(*filter.TargetID)))
|
||||
}
|
||||
if filter.Keyword != nil && strings.TrimSpace(*filter.Keyword) != "" {
|
||||
keyword := "%" + strings.TrimSpace(*filter.Keyword) + "%"
|
||||
q = q.Where(field.Or(tbl.Detail.Like(keyword), tbl.Action.Like(keyword), tbl.TargetID.Like(keyword)))
|
||||
}
|
||||
|
||||
operatorIDs, operatorFilter, err := Tenant.lookupUserIDs(ctx, filter.OperatorName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if operatorFilter {
|
||||
if len(operatorIDs) == 0 {
|
||||
q = q.Where(tbl.ID.Eq(-1))
|
||||
} else {
|
||||
q = q.Where(tbl.OperatorID.In(operatorIDs...))
|
||||
}
|
||||
}
|
||||
|
||||
if filter.CreatedAtFrom != nil {
|
||||
from, err := Super.parseFilterTime(filter.CreatedAtFrom)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if from != nil {
|
||||
q = q.Where(tbl.CreatedAt.Gte(*from))
|
||||
}
|
||||
}
|
||||
if filter.CreatedAtTo != nil {
|
||||
to, err := Super.parseFilterTime(filter.CreatedAtTo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if to != nil {
|
||||
q = q.Where(tbl.CreatedAt.Lte(*to))
|
||||
}
|
||||
}
|
||||
|
||||
orderApplied := false
|
||||
if filter.Desc != nil && strings.TrimSpace(*filter.Desc) != "" {
|
||||
switch strings.TrimSpace(*filter.Desc) {
|
||||
case "id":
|
||||
q = q.Order(tbl.ID.Desc())
|
||||
case "created_at":
|
||||
q = q.Order(tbl.CreatedAt.Desc())
|
||||
}
|
||||
orderApplied = true
|
||||
} else if filter.Asc != nil && strings.TrimSpace(*filter.Asc) != "" {
|
||||
switch strings.TrimSpace(*filter.Asc) {
|
||||
case "id":
|
||||
q = q.Order(tbl.ID)
|
||||
case "created_at":
|
||||
q = q.Order(tbl.CreatedAt)
|
||||
}
|
||||
orderApplied = true
|
||||
}
|
||||
if !orderApplied {
|
||||
q = q.Order(tbl.CreatedAt.Desc())
|
||||
}
|
||||
|
||||
filter.Pagination.Format()
|
||||
total, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
list, err := q.Offset(int(filter.Pagination.Offset())).Limit(int(filter.Pagination.Limit)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
if len(list) == 0 {
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: []creator_dto.CreatorAuditLogItem{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
operatorSet := make(map[int64]struct{}, len(list))
|
||||
for _, log := range list {
|
||||
if log.OperatorID > 0 {
|
||||
operatorSet[log.OperatorID] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
operatorMap := make(map[int64]*models.User, len(operatorSet))
|
||||
if len(operatorSet) > 0 {
|
||||
ids := make([]int64, 0, len(operatorSet))
|
||||
for id := range operatorSet {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
userTbl, userQuery := models.UserQuery.QueryContext(ctx)
|
||||
users, err := userQuery.Where(userTbl.ID.In(ids...)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
for _, user := range users {
|
||||
operatorMap[user.ID] = user
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]creator_dto.CreatorAuditLogItem, 0, len(list))
|
||||
for _, log := range list {
|
||||
item := creator_dto.CreatorAuditLogItem{
|
||||
ID: log.ID,
|
||||
OperatorID: log.OperatorID,
|
||||
Action: log.Action,
|
||||
TargetID: log.TargetID,
|
||||
Detail: log.Detail,
|
||||
CreatedAt: s.formatTime(log.CreatedAt),
|
||||
}
|
||||
if operator := operatorMap[log.OperatorID]; operator != nil {
|
||||
item.OperatorName = operator.Username
|
||||
} else if log.OperatorID > 0 {
|
||||
item.OperatorName = "ID:" + strconv.FormatInt(log.OperatorID, 10)
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *creator) ProcessRefund(ctx context.Context, tenantID, userID, id int64, form *creator_dto.RefundForm) error {
|
||||
tid, err := s.getTenantID(ctx, tenantID, userID)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user