Added permissions for web panel approvals getting #108
|
|
@ -56,6 +56,27 @@ func (u *ApprovalDB) GetApprovalsListFilteredDB(ctx context.Context, filter type
|
|||
LeftJoin("approval_topic_categories c ON c.id = t.category_id").
|
||||
OrderBy("a.created_at DESC")
|
||||
|
||||
if filter.ObserverUserID != nil {
|
||||
subQ1 := buildApprovalIdSubquery("initiator", filter.ObserverUserID)
|
||||
subQ2 := buildApprovalIdSubquery("approver", filter.ObserverUserID)
|
||||
subQ3 := buildApprovalIdSubquery("observer", filter.ObserverUserID)
|
||||
subQ4 := buildApprovalIdSubquery("executor", filter.ObserverUserID)
|
||||
subQ5 := buildApprovalIdSubquery("additional_approver", filter.ObserverUserID)
|
||||
subQ6 := buildApprovalIdSubquery("additional_observer", filter.ObserverUserID)
|
||||
|
||||
s1, a1, _ := subQ1.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
s2, a2, _ := subQ2.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
s3, a3, _ := subQ3.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
s4, a4, _ := subQ4.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
s5, a5, _ := subQ5.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
s6, a6, _ := subQ6.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
|
||||
unionSQL := fmt.Sprintf("(%s) UNION (%s) UNION (%s) UNION (%s) UNION (%s) UNION (%s)", s1, s2, s3, s4, s5, s6)
|
||||
allArgs := append(append(append(append(append(a1, a2...), a3...), a4...), a5...), a6...)
|
||||
|
||||
q = q.Where(squirrel.Expr("a.id IN ("+unionSQL+")", allArgs...))
|
||||
}
|
||||
|
||||
if filter.Status != nil && *filter.Status != "" {
|
||||
q = q.Where(squirrel.Eq{"a.status": *filter.Status})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,6 +45,28 @@ func (u *ApprovalTaskDB) GetApprovalTasksListDB(ctx context.Context, opts ...typ
|
|||
query := dbutils.BuildSelectQuery(dbcoretablenames.TableNameApprovalTasks.ToString(), selectFields)
|
||||
query = dbutils.SetterLimitAndOffsetQuery(query, options.Offset, options.Limit)
|
||||
query = dbutils.ApplyFilters(query, filter, options.LikeFields, dbcoretablenames.TableNameApprovalTasks.ToString())
|
||||
|
||||
if filter != nil && filter.ObserverUserID != nil {
|
||||
subQ1 := buildApprovalIdSubquery("initiator", filter.ObserverUserID)
|
||||
subQ2 := buildApprovalIdSubquery("approver", filter.ObserverUserID)
|
||||
subQ3 := buildApprovalIdSubquery("observer", filter.ObserverUserID)
|
||||
subQ4 := buildApprovalIdSubquery("executor", filter.ObserverUserID)
|
||||
subQ5 := buildApprovalIdSubquery("additional_approver", filter.ObserverUserID)
|
||||
subQ6 := buildApprovalIdSubquery("additional_observer", filter.ObserverUserID)
|
||||
|
||||
s1, a1, _ := subQ1.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
s2, a2, _ := subQ2.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
s3, a3, _ := subQ3.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
s4, a4, _ := subQ4.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
s5, a5, _ := subQ5.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
s6, a6, _ := subQ6.PlaceholderFormat(squirrel.Question).ToSql()
|
||||
|
||||
unionSQL := fmt.Sprintf("(%s) UNION (%s) UNION (%s) UNION (%s) UNION (%s) UNION (%s)", s1, s2, s3, s4, s5, s6)
|
||||
allArgs := append(append(append(append(append(a1, a2...), a3...), a4...), a5...), a6...)
|
||||
|
||||
query = query.Where(squirrel.Expr(dbcoretablenames.TableNameApprovalTasks.ToString()+".approval_id IN ("+unionSQL+")", allArgs...))
|
||||
}
|
||||
|
||||
query = query.OrderBy("created_at DESC")
|
||||
|
||||
sql, args, err := query.ToSql()
|
||||
|
|
|
|||
|
|
@ -22,15 +22,16 @@ func (status ApprovalStatus) String() string {
|
|||
|
||||
// ApprovalFilter — расширенный фильтр для списка заявок
|
||||
type ApprovalFilter struct {
|
||||
ID *uint64 `json:"id"`
|
||||
Status *string `json:"status,omitempty"`
|
||||
InitiatorID *uint64 `json:"initiator_id,omitempty"`
|
||||
DepartmentID *uint64 `json:"department_id,omitempty"`
|
||||
Content *string `json:"content,omitempty"`
|
||||
DateFrom *time.Time `json:"date_from,omitempty"`
|
||||
DateTo *time.Time `json:"date_to,omitempty"`
|
||||
CategoryIDs []uint64 `json:"category_ids,omitempty"` // множественный выбор категорий
|
||||
TopicIDs []uint64 `json:"topic_ids,omitempty"` // множественный выбор тем
|
||||
ID *uint64 `json:"id"`
|
||||
Status *string `json:"status,omitempty"`
|
||||
InitiatorID *uint64 `json:"initiator_id,omitempty"`
|
||||
DepartmentID *uint64 `json:"department_id,omitempty"`
|
||||
Content *string `json:"content,omitempty"`
|
||||
DateFrom *time.Time `json:"date_from,omitempty"`
|
||||
DateTo *time.Time `json:"date_to,omitempty"`
|
||||
CategoryIDs []uint64 `json:"category_ids,omitempty"` // множественный выбор категорий
|
||||
TopicIDs []uint64 `json:"topic_ids,omitempty"` // множественный выбор тем
|
||||
ObserverUserID *uint64 `json:"observer_user_id"`
|
||||
}
|
||||
|
||||
// Approval — заявка на согласование.
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@ type ApprovalTask struct {
|
|||
CreatedAt *time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP;column:created_at" json:"created_at" db:"created_at"`
|
||||
UpdatedAt *time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP;column:updated_at" json:"updated_at" db:"updated_at"`
|
||||
|
||||
Approval *Approval `gorm:"-" json:"approval,omitempty"`
|
||||
Executor *User `gorm:"-" json:"executor,omitempty"`
|
||||
Files []*ApprovalFile `gorm:"-" json:"files,omitempty"`
|
||||
ObserverUserID *uint64 `json:"observer_user_id,omitempty" db:"-"`
|
||||
Approval *Approval `gorm:"-" json:"approval,omitempty"`
|
||||
Executor *User `gorm:"-" json:"executor,omitempty"`
|
||||
Files []*ApprovalFile `gorm:"-" json:"files,omitempty"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,20 @@ func (s *ApprovalTasksReg) GetApprovalTasksListHandler(w http.ResponseWriter, r
|
|||
return nil, errW
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
if currentAdmin, ok := s.getCurrentAdmin(ctx); ok && currentAdmin.IsObserver != nil && *currentAdmin.IsObserver {
|
||||
if currentAdmin.TelegramID != nil && *currentAdmin.TelegramID > 0 {
|
||||
if tgUser, err := s.ipc.Database.User.GetUserByTelegramIDDB(ctx, *currentAdmin.TelegramID); err == nil && tgUser != nil && tgUser.ID != nil {
|
||||
taskParams.ObserverUserID = tgUser.ID
|
||||
} else {
|
||||
return &typescore.ApiResponseBase{TotalCount: 0, Data: []*typescore.ApprovalTask{}}, nil
|
||||
}
|
||||
} else {
|
||||
return &typescore.ApiResponseBase{TotalCount: 0, Data: []*typescore.ApprovalTask{}}, nil
|
||||
}
|
||||
}
|
||||
|
||||
options := typescore.ListDbOptions{
|
||||
Filtering: taskParams,
|
||||
Offset: offset,
|
||||
|
|
@ -37,7 +51,6 @@ func (s *ApprovalTasksReg) GetApprovalTasksListHandler(w http.ResponseWriter, r
|
|||
LikeFields: likeFields,
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
tasks, total, errW := s.ipc.Database.ApprovalTasks.GetApprovalTasksListDB(ctx, options)
|
||||
if errW != nil {
|
||||
return nil, errW
|
||||
|
|
@ -2,6 +2,7 @@ package approvalshandler
|
|||
|
||||
import (
|
||||
"bitrix_balancer_api/core/typescore"
|
||||
"bitrix_balancer_api/rest_admin_service/handlers"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
|
@ -127,6 +128,18 @@ func (s *ApprovalsReg) GetApprovalsListHandler(w http.ResponseWriter, r *http.Re
|
|||
|
||||
ctx := r.Context()
|
||||
|
||||
if currentAdmin, ok := s.getCurrentAdmin(ctx); ok && currentAdmin.IsObserver != nil && *currentAdmin.IsObserver {
|
||||
if currentAdmin.TelegramID != nil && *currentAdmin.TelegramID > 0 {
|
||||
if tgUser, err := s.ipc.Database.User.GetUserByTelegramIDDB(ctx, *currentAdmin.TelegramID); err == nil && tgUser != nil && tgUser.ID != nil {
|
||||
filter.ObserverUserID = tgUser.ID
|
||||
} else {
|
||||
return &typescore.ApiResponseBase{TotalCount: 0, Data: []*typescore.Approval{}}, nil
|
||||
}
|
||||
} else {
|
||||
return &typescore.ApiResponseBase{TotalCount: 0, Data: []*typescore.Approval{}}, nil
|
||||
}
|
||||
}
|
||||
|
||||
approvalsDbLst, total, errW := s.ipc.Database.Approval.GetApprovalsListFilteredDB(ctx, filter, offset, limit)
|
||||
if errW != nil {
|
||||
logrus.Errorf("❌ GetApprovalsListHandler error: %v", errW)
|
||||
|
|
@ -683,3 +696,23 @@ func (s *ApprovalsReg) enrichApprovalsBatch(ctx context.Context, approvals []*ty
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getCurrentAdmin извлекает ID из контекста и получает полные данные администратора из БД
|
||||
func (s *ApprovalsReg) getCurrentAdmin(ctx context.Context) (*typescore.Admin, bool) {
|
||||
currentUserID, err := handlers.GetIDFromContext(ctx)
|
||||
if err != nil {
|
||||
logrus.Warnf("getCurrentAdmin: GetIDFromContext err=%v", err)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
admins, _, err := s.ipc.Database.Admin.GetAdminsListDB(typescore.ListDbOptions{
|
||||
Filtering: &typescore.Admin{ID: ¤tUserID},
|
||||
})
|
||||
|
||||
if err != nil || len(admins) == 0 || admins[0] == nil || admins[0].ID == nil {
|
||||
logrus.Warnf("getCurrentAdmin: admin not found for userID=%d err=%v", currentUserID, err)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return admins[0], true
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue