From 480c309f5c0c6637dc56fd3f6fd4901bbd4428f3 Mon Sep 17 00:00:00 2001 From: romanskijdev Date: Thu, 25 Jun 2026 13:57:19 +0300 Subject: [PATCH] update obervers selection in code --- core/db/methods/user.go | 59 +++++++++++++++++++ core/reportcore/excel.go | 2 +- .../handlers/approval/handler.go | 2 +- .../handlers/approval/handler_comment.go | 4 +- .../handlers/approval/handler_decision.go | 4 +- .../handlers/approval/notify.go | 4 +- 6 files changed, 67 insertions(+), 8 deletions(-) diff --git a/core/db/methods/user.go b/core/db/methods/user.go index eb8ab34..8f8b874 100644 --- a/core/db/methods/user.go +++ b/core/db/methods/user.go @@ -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.*"). diff --git a/core/reportcore/excel.go b/core/reportcore/excel.go index 4d8b034..d41ce11 100644 --- a/core/reportcore/excel.go +++ b/core/reportcore/excel.go @@ -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) diff --git a/service/telegram_service/handlers/approval/handler.go b/service/telegram_service/handlers/approval/handler.go index 8080329..dfd98e9 100644 --- a/service/telegram_service/handlers/approval/handler.go +++ b/service/telegram_service/handlers/approval/handler.go @@ -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 diff --git a/service/telegram_service/handlers/approval/handler_comment.go b/service/telegram_service/handlers/approval/handler_comment.go index b6d3dc6..e549c7d 100644 --- a/service/telegram_service/handlers/approval/handler_comment.go +++ b/service/telegram_service/handlers/approval/handler_comment.go @@ -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 diff --git a/service/telegram_service/handlers/approval/handler_decision.go b/service/telegram_service/handlers/approval/handler_decision.go index b1606f0..3f16a17 100644 --- a/service/telegram_service/handlers/approval/handler_decision.go +++ b/service/telegram_service/handlers/approval/handler_decision.go @@ -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) diff --git a/service/telegram_service/handlers/approval/notify.go b/service/telegram_service/handlers/approval/notify.go index c558430..51b2f4f 100644 --- a/service/telegram_service/handlers/approval/notify.go +++ b/service/telegram_service/handlers/approval/notify.go @@ -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 -- 2.43.0