Merge pull request 'Update task delegation table migration' (#185) from fix-permissions into main
Deploy / deploy (push) Successful in 20s
Details
Deploy / deploy (push) Successful in 20s
Details
Reviewed-on: https://72.56.235.246.nip.io/gitea/romanskijdev/ruspet_portal_back/pulls/185
This commit is contained in:
commit
177cdca824
|
|
@ -10,6 +10,7 @@ import (
|
|||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
|
|
@ -295,31 +296,125 @@ func (h *Handler) createPendingDelegation(ctx context.Context, initiator *typesc
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) activatePendingDelegation(ctx context.Context, approval *typescore.Approval) {
|
||||
func (h *Handler) activatePendingDelegation(bot *tgbotapi.BotAPI, ctx context.Context, approval *typescore.Approval) {
|
||||
if approval == nil || approval.ID == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Получаем имя инициатора (кто уходит в отпуск)
|
||||
initiatorName := "Сотрудник"
|
||||
if approval.InitiatorID != nil {
|
||||
if u, _ := h.ipc.Database.User.GetUserByIDDB(ctx, *approval.InitiatorID); u != nil && u.FullName != nil {
|
||||
initiatorName = *u.FullName
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
for _, d := range apprDelegations {
|
||||
d.IsActive = &active
|
||||
h.ipc.Database.ApprovalDelegation.UpdateApprovalDelegationDB(ctx, nil, d) //nolint:errcheck
|
||||
|
||||
if d.DelegateID != nil {
|
||||
if delegateCategories[*d.DelegateID] == nil {
|
||||
delegateCategories[*d.DelegateID] = make(map[string]struct{})
|
||||
}
|
||||
|
||||
scopeName := "Неизвестная категория"
|
||||
if d.TopicID != nil {
|
||||
topic, _ := h.ipc.Database.ApprovalTopic.GetApprovalTopicByIDDB(ctx, *d.TopicID)
|
||||
if topic != nil && topic.Name != nil {
|
||||
scopeName = *topic.Name
|
||||
}
|
||||
} else if d.CategoryID != nil {
|
||||
category, _ := h.ipc.Database.ApprovalTopicCategory.GetApprovalTopicCategoryByIDDB(ctx, *d.CategoryID)
|
||||
if category != nil && category.Name != nil {
|
||||
scopeName = *category.Name
|
||||
}
|
||||
}
|
||||
|
||||
delegateCategories[*d.DelegateID][scopeName] = struct{}{}
|
||||
|
||||
if d.ValidUntil != nil {
|
||||
delegateValidUntil[*d.DelegateID] = d.ValidUntil.Format("02.01.2006")
|
||||
} else if delegateValidUntil[*d.DelegateID] == "" {
|
||||
delegateValidUntil[*d.DelegateID] = "—"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Активируем задачи
|
||||
// Рассылаем уведомления по заявкам
|
||||
for delegateID, categoriesMap := range delegateCategories {
|
||||
delegateUser, _ := h.ipc.Database.User.GetUserByIDDB(ctx, delegateID)
|
||||
if delegateUser != nil && delegateUser.TelegramID != nil {
|
||||
var catNames []string
|
||||
for catName := range categoriesMap {
|
||||
catNames = append(catNames, "• "+catName)
|
||||
}
|
||||
|
||||
catListStr := strings.Join(catNames, "\n")
|
||||
validStr := delegateValidUntil[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, catListStr,
|
||||
))
|
||||
msg.ParseMode = "HTML"
|
||||
bot.Send(msg) //nolint:errcheck
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// 2. Активируем задачи
|
||||
// ==========================================
|
||||
taskDelegations, _, _ := h.ipc.Database.TaskDelegation.GetTaskDelegationsListDB(ctx, typescore.ListDbOptions{
|
||||
Filtering: &typescore.TaskDelegation{ApprovalID: approval.ID},
|
||||
})
|
||||
|
||||
notifiedTaskExecutors := make(map[uint64]bool)
|
||||
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 delegateUser != nil && delegateUser.TelegramID != nil {
|
||||
validStr := "—"
|
||||
if td.ValidUntil != nil {
|
||||
validStr = td.ValidUntil.Format("02.01.2006")
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) deactivatePendingDelegation(ctx context.Context, approval *typescore.Approval) {
|
||||
if approval == nil || approval.ID == nil {
|
||||
return
|
||||
|
|
|
|||
|
|
@ -256,7 +256,6 @@ func (h *Handler) HandleCompleteTask(bot *tgbotapi.BotAPI, cq *tgbotapi.Callback
|
|||
}
|
||||
}
|
||||
|
||||
// activateNextTask активирует следующую задачу в очереди после завершения текущей
|
||||
// activateNextTask активирует следующую задачу в очереди после завершения текущей
|
||||
func (h *Handler) activateNextTask(bot *tgbotapi.BotAPI, ctx context.Context, approval *typescore.Approval, completedTask *typescore.ApprovalTask) {
|
||||
if approval == nil || approval.ID == nil || completedTask.StepOrder == nil {
|
||||
|
|
@ -288,7 +287,7 @@ func (h *Handler) activateNextTask(bot *tgbotapi.BotAPI, ctx context.Context, ap
|
|||
var actualExecutor *typescore.User
|
||||
|
||||
if originalExecutor != nil {
|
||||
actual, isDelegated, orig := h.getActualTaskExecutor(ctx, originalExecutor)
|
||||
actual, isDelegated, orig := h.getActualTaskExecutor(ctx, originalExecutor, approval.TopicID)
|
||||
actualExecutor = actual
|
||||
|
||||
// Если за время ожидания задачи человек ушел в отпуск/делегировал права,
|
||||
|
|
|
|||
Loading…
Reference in New Issue