Merge pull request 'Update task delegation info message' (#186) from fix-permissions into main
Deploy / deploy (push) Successful in 17s Details

Reviewed-on: https://72.56.235.246.nip.io/gitea/romanskijdev/ruspet_portal_back/pulls/186
This commit is contained in:
romanskijdev 2026-10-13 16:16:25 +03:00
commit e3a299acd4
2 changed files with 70 additions and 48 deletions

View File

@ -256,28 +256,28 @@ func (h *Handler) createPendingDelegation(ctx context.Context, initiator *typesc
inactive := false
notifiedFalse := false
// 1. Создаем отложенное делегирование ЗАЯВОК
// 1. Создаем отложенное делегирование ЗАЯВОК (только по тем категориям, где инициатор — согласующий)
if flowState.SelectedDelegateID != 0 {
categories, _, _ := h.ipc.Database.ApprovalTopicCategory.GetApprovalTopicCategoryListDB(ctx, typescore.ListDbOptions{
Filtering: &typescore.ApprovalTopicCategory{IsActive: utilscore.PointerToBool(true)},
})
for _, cat := range categories {
if cat.ID == nil {
continue
// Получаем реальные категории, где этот пользователь является согласующим
approverCategories, err := h.getApproverCategories(ctx, *initiator.ID)
if err == nil {
for _, cat := range approverCategories {
if cat == nil || cat.ID == nil {
continue
}
catID := *cat.ID
delegation := &typescore.ApprovalDelegation{
DelegatorID: initiator.ID,
DelegateID: &flowState.SelectedDelegateID,
CategoryID: &catID,
ApprovalID: approval.ID,
ValidUntil: &validUntil,
IsActive: &inactive,
NotifiedDay: &notifiedFalse,
Notified2h: &notifiedFalse,
}
h.ipc.Database.ApprovalDelegation.CreateApprovalDelegationDB(ctx, nil, delegation) //nolint:errcheck
}
catID := *cat.ID
delegation := &typescore.ApprovalDelegation{
DelegatorID: initiator.ID,
DelegateID: &flowState.SelectedDelegateID,
CategoryID: &catID,
ApprovalID: approval.ID,
ValidUntil: &validUntil,
IsActive: &inactive,
NotifiedDay: &notifiedFalse,
Notified2h: &notifiedFalse,
}
h.ipc.Database.ApprovalDelegation.CreateApprovalDelegationDB(ctx, nil, delegation) //nolint:errcheck
}
}
@ -301,7 +301,6 @@ func (h *Handler) activatePendingDelegation(bot *tgbotapi.BotAPI, ctx context.Co
return
}
// Получаем имя инициатора (кто уходит в отпуск)
initiatorName := "Сотрудник"
if approval.InitiatorID != nil {
if u, _ := h.ipc.Database.User.GetUserByIDDB(ctx, *approval.InitiatorID); u != nil && u.FullName != nil {
@ -311,14 +310,10 @@ func (h *Handler) activatePendingDelegation(bot *tgbotapi.BotAPI, ctx context.Co
active := true
// ==========================================
// 1. Активируем заявки и собираем категории
// ==========================================
apprDelegations, _, _ := h.ipc.Database.ApprovalDelegation.GetApprovalDelegationsListDB(ctx, typescore.ListDbOptions{
Filtering: &typescore.ApprovalDelegation{ApprovalID: approval.ID},
})
// Словари для группировки категорий по делегатам
delegateCategories := make(map[uint64]map[string]struct{})
delegateValidUntil := make(map[uint64]string)
@ -354,7 +349,6 @@ func (h *Handler) activatePendingDelegation(bot *tgbotapi.BotAPI, ctx context.Co
}
}
// Рассылаем уведомления по заявкам
for delegateID, categoriesMap := range delegateCategories {
delegateUser, _ := h.ipc.Database.User.GetUserByIDDB(ctx, delegateID)
if delegateUser != nil && delegateUser.TelegramID != nil {
@ -380,41 +374,71 @@ func (h *Handler) activatePendingDelegation(bot *tgbotapi.BotAPI, ctx context.Co
}
// ==========================================
// 2. Активируем задачи
// 2. Активируем задачи и собираем их области (Категории/Темы)
// ==========================================
taskDelegations, _, _ := h.ipc.Database.TaskDelegation.GetTaskDelegationsListDB(ctx, typescore.ListDbOptions{
Filtering: &typescore.TaskDelegation{ApprovalID: approval.ID},
})
notifiedTaskExecutors := make(map[uint64]bool)
taskDelegateScopes := make(map[uint64]map[string]struct{})
taskDelegateValidUntil := make(map[uint64]string)
for _, td := range taskDelegations {
td.IsActive = &active
h.ipc.Database.TaskDelegation.UpdateTaskDelegationDB(ctx, nil, td) //nolint:errcheck
// Уведомляем делегата задач (один раз)
if td.DelegateID != nil && !notifiedTaskExecutors[*td.DelegateID] {
notifiedTaskExecutors[*td.DelegateID] = true
delegateUser, _ := h.ipc.Database.User.GetUserByIDDB(ctx, *td.DelegateID)
if td.DelegateID != nil {
if taskDelegateScopes[*td.DelegateID] == nil {
taskDelegateScopes[*td.DelegateID] = make(map[string]struct{})
}
if delegateUser != nil && delegateUser.TelegramID != nil {
validStr := "—"
if td.ValidUntil != nil {
validStr = td.ValidUntil.Format("02.01.2006")
scopeName := "🌍 Все задачи (Глобально)"
if td.TopicID != nil {
if topic, _ := h.ipc.Database.ApprovalTopic.GetApprovalTopicByIDDB(ctx, *td.TopicID); topic != nil && topic.Name != nil {
scopeName = "Тема: " + *topic.Name
}
msg := tgbotapi.NewMessage(*delegateUser.TelegramID, fmt.Sprintf(
"🔄 <b>Вам делегировано исполнение задач</b>\n\n"+
"👤 <b>От кого:</b> %s\n"+
"📅 <b>Действует до:</b> %s\n"+
"📋 <b>Объем:</b> Все задачи\n\n"+
"<i>Заявка на отсутствие сотрудника согласована, делегирование вступило в силу. Теперь все новые задачи этого сотрудника будут поступать вам.</i>",
initiatorName, validStr,
))
msg.ParseMode = "HTML"
bot.Send(msg) //nolint:errcheck
} else if td.CategoryID != nil {
if category, _ := h.ipc.Database.ApprovalTopicCategory.GetApprovalTopicCategoryByIDDB(ctx, *td.CategoryID); category != nil && category.Name != nil {
scopeName = "Категория: " + *category.Name
}
}
taskDelegateScopes[*td.DelegateID][scopeName] = struct{}{}
if td.ValidUntil != nil {
taskDelegateValidUntil[*td.DelegateID] = td.ValidUntil.Format("02.01.2006")
} else if taskDelegateValidUntil[*td.DelegateID] == "" {
taskDelegateValidUntil[*td.DelegateID] = "—"
}
}
}
// Рассылаем детальные уведомления по задачам
for delegateID, scopesMap := range taskDelegateScopes {
delegateUser, _ := h.ipc.Database.User.GetUserByIDDB(ctx, delegateID)
if delegateUser != nil && delegateUser.TelegramID != nil {
var scopeNames []string
for scopeName := range scopesMap {
scopeNames = append(scopeNames, "• "+scopeName)
}
scopeListStr := strings.Join(scopeNames, "\n")
validStr := taskDelegateValidUntil[delegateID]
msg := tgbotapi.NewMessage(*delegateUser.TelegramID, fmt.Sprintf(
"🔄 <b>Вам делегировано исполнение задач</b>\n\n"+
"👤 <b>От кого:</b> %s\n"+
"📅 <b>Действует до:</b> %s\n\n"+
"📋 <b>Переданные области задач:</b>\n%s\n\n"+
"<i>Заявка на отсутствие сотрудника согласована, делегирование вступило в силу.</i>",
initiatorName, validStr, scopeListStr,
))
msg.ParseMode = "HTML"
bot.Send(msg) //nolint:errcheck
}
}
}
func (h *Handler) deactivatePendingDelegation(ctx context.Context, approval *typescore.Approval) {
if approval == nil || approval.ID == nil {
return

View File

@ -578,8 +578,6 @@ func (h *Handler) createTasksForApproval(
taskCounter := 0
// ... (начало метода createTasksForApproval остается без изменений) ...
for _, system := range systemsToProcess {
for _, topicExec := range topicExecutors {
if topicExec.UserID == nil {