update obervers selection in code #32
|
|
@ -37,6 +37,7 @@ type UserDBI interface {
|
|||
IsExecutorDB(ctx context.Context, userID uint64) (bool, error)
|
||||
GetObserversForCategoryDB(ctx context.Context, categoryCode string) ([]*typescore.User, error)
|
||||
GetUsersByRoleCodeDB(ctx context.Context, roleCode string) ([]*typescore.User, error)
|
||||
GetObserversForApprovalDB(ctx context.Context, categoryCode string, topicID *uint64) ([]*typescore.User, error)
|
||||
}
|
||||
|
||||
func (u *UserDB) GetUsersListDB(ctx context.Context, opts ...typescore.ListDbOptions) ([]*typescore.User, uint64, error) {
|
||||
|
|
@ -358,6 +359,64 @@ func (u *UserDB) GetObserversForCategoryDB(ctx context.Context, categoryCode str
|
|||
return result, nil
|
||||
}
|
||||
|
||||
// GetObserversForApprovalDB — возвращает наблюдателей для конкретной заявки.
|
||||
// Учитывает права как на категорию темы, так и на саму тему (topic_id).
|
||||
// Дубли устраняются через DISTINCT.
|
||||
func (u *UserDB) GetObserversForApprovalDB(
|
||||
ctx context.Context,
|
||||
categoryCode string,
|
||||
topicID *uint64,
|
||||
) ([]*typescore.User, error) {
|
||||
q := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).
|
||||
Select("DISTINCT u.*").
|
||||
From(dbcoretablenames.TableNameUsers.ToString() + " u").
|
||||
Join(dbcoretablenames.TableNameUserRoles.ToString() + " ur ON ur.user_id = u.id").
|
||||
Join(dbcoretablenames.TableNameRoles.ToString() + " r ON r.id = ur.role_id").
|
||||
Join(dbcoretablenames.TableNamePermissions.ToString() + " p ON p.role_id = r.id").
|
||||
Where(squirrel.Eq{"r.is_observer": true}).
|
||||
Where(squirrel.Eq{"p.action": "observe"}).
|
||||
Where(squirrel.Eq{"u.is_blocked": false})
|
||||
|
||||
// Условие: право на категорию ИЛИ право на конкретную тему
|
||||
if topicID != nil {
|
||||
q = q.Where(squirrel.Or{
|
||||
squirrel.Expr(
|
||||
"p.topic_category_id IN (SELECT id FROM approval_topic_categories WHERE code = ?)",
|
||||
categoryCode,
|
||||
),
|
||||
squirrel.Eq{"p.topic_id": *topicID},
|
||||
})
|
||||
} else {
|
||||
// Если topicID не передан — fallback на старую логику
|
||||
q = q.Join(
|
||||
dbcoretablenames.TableNameApprovalTopicCategories.ToString() +
|
||||
" c ON c.id = p.topic_category_id",
|
||||
).Where(squirrel.Eq{"c.code": categoryCode})
|
||||
}
|
||||
|
||||
sql, args, err := q.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, err := u.pool.Query(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []*typescore.User
|
||||
for rows.Next() {
|
||||
usr := &typescore.User{}
|
||||
if err := dbutils.ScanRowsToStructRows(rows, usr, nil); err != nil {
|
||||
logrus.Warn("GetObserversForApprovalDB:ScanRowsToStruct", err)
|
||||
continue
|
||||
}
|
||||
result = append(result, usr)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (u *UserDB) GetUsersByRoleCodeDB(ctx context.Context, roleCode string) ([]*typescore.User, error) {
|
||||
sql, args, err := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).
|
||||
Select("DISTINCT u.*").
|
||||
|
|
|
|||
|
|
@ -207,7 +207,7 @@ func BuildExcelApprovalsReport(ctx context.Context, db *database.ModuleDB, appro
|
|||
var observerParts []string
|
||||
categoryCode := GetTopicCategoryCodeByApproval(ctx, db, a)
|
||||
if categoryCode != "" {
|
||||
observers, _ := db.User.GetObserversForCategoryDB(ctx, categoryCode)
|
||||
observers, _ := db.User.GetObserversForApprovalDB(ctx, categoryCode, a.TopicID)
|
||||
for _, obs := range observers {
|
||||
if obs.FullName != nil {
|
||||
observerParts = append(observerParts, *obs.FullName)
|
||||
|
|
|
|||
|
|
@ -424,7 +424,7 @@ func (h *Handler) HandleRevokeWithComment(bot *tgbotapi.BotAPI, chatID int64, te
|
|||
|
||||
if *approval.Status == "revoked" {
|
||||
categoryCode := h.getTopicCodeByApproval(ctx, approval)
|
||||
observers, _ := h.ipc.Database.User.GetObserversForCategoryDB(ctx, categoryCode)
|
||||
observers, _ := h.ipc.Database.User.GetObserversForApprovalDB(ctx, categoryCode, approval.TopicID)
|
||||
for _, obs := range observers {
|
||||
if obs.TelegramID == nil {
|
||||
continue
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ func (h *Handler) canUserCommentApproval(ctx context.Context, userID uint64, app
|
|||
isObserver, _ := h.ipc.Database.User.IsObserverDB(ctx, userID)
|
||||
if isObserver {
|
||||
// Проверяем права наблюдателя на эту категорию
|
||||
observers, _ := h.ipc.Database.User.GetObserversForCategoryDB(ctx, *topic.Code)
|
||||
observers, _ := h.ipc.Database.User.GetObserversForApprovalDB(ctx, *topic.Code, topic.ID)
|
||||
if observers != nil {
|
||||
for _, obs := range observers {
|
||||
if obs.ID != nil && *obs.ID == userID {
|
||||
|
|
@ -328,7 +328,7 @@ func (h *Handler) notifyAllParticipantsAboutComment(
|
|||
if isFinalStatus && approval.TopicID != nil {
|
||||
categoryCode := h.getTopicCodeByApproval(ctx, approval)
|
||||
if categoryCode != "" {
|
||||
observers, _ := h.ipc.Database.User.GetObserversForCategoryDB(ctx, categoryCode)
|
||||
observers, _ := h.ipc.Database.User.GetObserversForApprovalDB(ctx, categoryCode, approval.TopicID)
|
||||
for _, obs := range observers {
|
||||
if obs.TelegramID != nil && *obs.TelegramID != skipTelegramID {
|
||||
recipients[*obs.TelegramID] = true
|
||||
|
|
|
|||
|
|
@ -250,7 +250,7 @@ func (h *Handler) processNextStepOrComplete(bot *tgbotapi.BotAPI, ctx context.Co
|
|||
}
|
||||
}
|
||||
|
||||
if observers, err := h.ipc.Database.User.GetObserversForCategoryDB(ctx, categoryCode); err == nil && len(observers) > 0 {
|
||||
if observers, err := h.ipc.Database.User.GetObserversForApprovalDB(ctx, categoryCode, approval.TopicID); err == nil && len(observers) > 0 {
|
||||
stepApprover, _ := h.ipc.Database.User.GetUserByIDDB(ctx, *currentStep.ApproverID)
|
||||
notifier.NotifyObserversDecision(bot, observers, initiator, stepApprover, approval, collected)
|
||||
}
|
||||
|
|
@ -406,7 +406,7 @@ func (h *Handler) handleDecision(
|
|||
}
|
||||
notifier.NotifyInitiatorRejected(bot, initiator, stepApprover, approval, collected, comment)
|
||||
}
|
||||
if observers, err := h.ipc.Database.User.GetObserversForCategoryDB(ctx, categoryCode); err == nil && len(observers) > 0 {
|
||||
if observers, err := h.ipc.Database.User.GetObserversForApprovalDB(ctx, categoryCode, approval.TopicID); err == nil && len(observers) > 0 {
|
||||
var stepApprover *typescore.User
|
||||
if stepApproverID != nil {
|
||||
stepApprover, _ = h.ipc.Database.User.GetUserByIDDB(ctx, *stepApproverID)
|
||||
|
|
|
|||
|
|
@ -568,7 +568,7 @@ func (h *Handler) notifyObserversAboutNewFile(
|
|||
return
|
||||
}
|
||||
|
||||
observers, err := h.ipc.Database.User.GetObserversForCategoryDB(ctx, *topic.Code)
|
||||
observers, err := h.ipc.Database.User.GetObserversForApprovalDB(ctx, *topic.Code, topic.ID)
|
||||
if err != nil {
|
||||
logrus.Warnf("notifyObserversAboutNewFile: GetObserversForCategoryDB err=%v", err)
|
||||
return
|
||||
|
|
@ -666,7 +666,7 @@ func (h *Handler) notifyAllParticipantsAboutFile(
|
|||
}
|
||||
|
||||
categoryCode := h.getTopicCodeByApproval(ctx, approval)
|
||||
observers, _ := h.ipc.Database.User.GetObserversForCategoryDB(ctx, categoryCode)
|
||||
observers, _ := h.ipc.Database.User.GetObserversForApprovalDB(ctx, categoryCode, approval.TopicID)
|
||||
recipients = append(recipients, observers...)
|
||||
|
||||
// Получаем нотифайер для sendFilesToRecipient
|
||||
|
|
|
|||
Loading…
Reference in New Issue