Update approver, first approver && observer selection methods

This commit is contained in:
romanskijdev 2026-04-27 11:50:02 +03:00
parent 9dd2bb9cc9
commit 9cce8c22b2
5 changed files with 108 additions and 29 deletions

View File

@ -373,28 +373,42 @@ func (h *Handler) continueFlowAfterInput(
return
}
topicCode, _ := topic.TopicCodeFromCollected(flowState.Collected)
approvalTopic, _ := h.ipc.Database.ApprovalTopic.GetApprovalTopicByCodeDB(ctx, topicCode)
topicCode, err := topic.TopicCodeFromCollected(flowState.Collected)
if err != nil {
logrus.Warnf("continueFlowAfterInput: TopicCodeFromCollected err=%v collected=%v", err, flowState.Collected)
h.States.Clear(ctx, telegramID) //nolint:errcheck
return
}
approvalTopic, err := h.ipc.Database.ApprovalTopic.GetApprovalTopicByCodeDB(ctx, topicCode)
if err != nil {
logrus.Errorf("continueFlowAfterInput: GetApprovalTopicByCodeDB topicCode=%s err=%v", topicCode, err)
}
// Выбор первого согласующего по роли
if approvalTopic != nil &&
approvalTopic.FirstApproverRoleCode != nil &&
*approvalTopic.FirstApproverRoleCode != "" &&
flowState.SelectedFirstApproverID == 0 {
flowState.SelectedFirstApproverID == 0 &&
!flowState.AwaitFirstApproverSelection {
flowState.AwaitFirstApproverSelection = true
h.States.Set(ctx, telegramID, flowState) //nolint:errcheck
_, roleCode, roleLabel := h.getFirstApproverRoleForFlow(ctx, flowState)
roleCode := *approvalTopic.FirstApproverRoleCode
roleLabel := roleCode
role := h.getRoleLabelByCode(ctx, roleCode)
if role != nil && role.Name != nil {
roleLabel = *role.Name
}
h.showFirstApproverSelection(bot, ctx, chatID, telegramID, roleCode, roleLabel)
return
}
// Выбор основного согласующего
if approvalTopic != nil &&
approvalTopic.AllowApproverSelection != nil &&
*approvalTopic.AllowApproverSelection &&
flowState.SelectedApproverID == 0 {
flowState.SelectedApproverID == 0 &&
!flowState.AwaitApproverSelection {
flowState.AwaitApproverSelection = true
h.States.Set(ctx, telegramID, flowState) //nolint:errcheck
@ -402,19 +416,17 @@ func (h *Handler) continueFlowAfterInput(
return
}
// Выбор наблюдателей
if approvalTopic != nil &&
approvalTopic.AllowObserverSelection != nil &&
*approvalTopic.AllowObserverSelection &&
!flowState.AwaitObserverSelection {
!flowState.ObserverSelectionDone {
flowState.AwaitObserverSelection = true
flowState.ObserverSelectionDone = true
h.States.Set(ctx, telegramID, flowState) //nolint:errcheck
h.showObserverSelection(bot, ctx, chatID, telegramID, flowState)
return
}
// Все шаги пройдены — отправляем заявку
h.States.Clear(ctx, telegramID) //nolint:errcheck
h.finalizeAndSubmit(bot, ctx, chatID, telegramID, flowState)
}

View File

@ -51,10 +51,28 @@ func (h *Handler) HandleApproverSelectionCallback(bot *tgbotapi.BotAPI, cq *tgbo
return
}
if !flowState.AwaitApproverSelection {
logrus.Warnf("HandleApproverSelectionCallback: not awaiting approver selection, userID=%d", telegramID)
return
}
selected, err := h.ipc.Database.User.GetUserByIDDB(ctx, approverID)
if err != nil || selected == nil {
sendText(bot, chatID, "⚠️ Согласующий не найден.")
return
}
selectedName := "выбранный согласующий"
if selected.FullName != nil {
selectedName = *selected.FullName
}
flowState.SelectedApproverID = approverID
flowState.AwaitApproverSelection = false
h.States.Set(ctx, telegramID, flowState) //nolint:errcheck
sendText(bot, chatID, fmt.Sprintf("✅ Выбран согласующий: <b>%s</b>", selectedName))
h.continueFlowAfterInput(bot, ctx, chatID, telegramID, flowState)
}

View File

@ -13,23 +13,38 @@ import (
)
// showFirstApproverSelection показывает список пользователей с нужной ролью
func (h *Handler) showFirstApproverSelection(bot *tgbotapi.BotAPI, ctx context.Context, chatID int64, telegramID int64, roleCode string, roleLabel string) {
func (h *Handler) showFirstApproverSelection(
bot *tgbotapi.BotAPI,
ctx context.Context,
chatID int64,
telegramID int64,
roleCode string,
roleLabel string,
) {
users, err := h.ipc.Database.User.GetUsersByRoleCodeDB(ctx, roleCode)
if err != nil {
logrus.Errorf("showFirstApproverSelection: GetUsersByRoleCodeDB err=%v", err)
logrus.Errorf("showFirstApproverSelection: GetUsersByRoleCodeDB roleCode=%s err=%v", roleCode, err)
sendText(bot, chatID, "Ошибка при получении списка пользователей.")
return
}
if len(users) == 0 {
sendText(bot, chatID, fmt.Sprintf("⚠️ Нет пользователей с ролью «%s».", roleLabel))
h.States.Clear(ctx, telegramID) //nolint:errcheck
return
}
if len(users) == 0 {
// Пользователей с такой ролью нет — пропускаем шаг
logrus.Warnf("showFirstApproverSelection: no users with role=%s, skipping", roleCode)
flowState, err := h.States.Get(ctx, telegramID)
if err != nil || flowState == nil {
return
}
flowState.AwaitFirstApproverSelection = false
h.States.Set(ctx, telegramID, flowState) //nolint:errcheck
h.continueFlowAfterInput(bot, ctx, chatID, telegramID, flowState)
return
}
msg := tgbotapi.NewMessage(chatID, fmt.Sprintf(
"👤 <b>Выберите %s</b>\n"+
"<i>Этот пользователь станет первым согласующим в цепочке.</i>",
"👤 <b>Выберите %s</b>\n\n"+
"<i>Этот пользователь согласует заявку первым.</i>",
roleLabel,
))
msg.ParseMode = "HTML"
@ -37,6 +52,19 @@ func (h *Handler) showFirstApproverSelection(bot *tgbotapi.BotAPI, ctx context.C
bot.Send(msg) //nolint:errcheck
}
// getRoleLabelByCode — вспомогательный метод получения названия роли
func (h *Handler) getRoleLabelByCode(ctx context.Context, roleCode string) *typescore.Role {
roleList, _, err := h.ipc.Database.Role.GetRolesListDB(ctx, typescore.ListDbOptions{
Filtering: &typescore.Role{
Code: &roleCode,
},
})
if err != nil || roleList == nil || len(roleList) < 1 || roleList[0] == nil {
return nil
}
return roleList[0]
}
// HandleFirstApproverSelectCallback — пользователь выбрал первого согласующего
func (h *Handler) HandleFirstApproverSelectCallback(bot *tgbotapi.BotAPI, cq *tgbotapi.CallbackQuery, userID uint64) {
ctx := context.Background()
@ -47,25 +75,36 @@ func (h *Handler) HandleFirstApproverSelectCallback(bot *tgbotapi.BotAPI, cq *tg
bot.Request(tgbotapi.NewDeleteMessage(chatID, cq.Message.MessageID)) //nolint:errcheck
flowState, err := h.States.Get(ctx, telegramID)
if err != nil || flowState == nil || !flowState.AwaitFirstApproverSelection {
if err != nil || flowState == nil {
sendText(bot, chatID, "⚠️ Состояние утеряно. Начните заново.")
return
}
if !flowState.AwaitFirstApproverSelection {
logrus.Warnf("HandleFirstApproverSelectCallback: not awaiting first approver, userID=%d", telegramID)
return
}
selected, err := h.ipc.Database.User.GetUserByIDDB(ctx, userID)
if err != nil || selected == nil || selected.FullName == nil {
if err != nil || selected == nil {
sendText(bot, chatID, "⚠️ Пользователь не найден.")
return
}
selectedName := "выбранный пользователь"
if selected.FullName != nil {
selectedName = *selected.FullName
}
flowState.SelectedFirstApproverID = userID
flowState.AwaitFirstApproverSelection = false
h.States.Set(ctx, telegramID, flowState) //nolint:errcheck
sendText(bot, chatID, fmt.Sprintf("✅ Выбран первый согласующий: <b>%s</b>", selectedName))
// Следующий шаг — выбор основного согласующего или наблюдателей
h.continueFlowAfterInput(bot, ctx, chatID, telegramID, flowState)
}
// HandleFirstApproverPageCallback — пагинация списка
func (h *Handler) HandleFirstApproverPageCallback(bot *tgbotapi.BotAPI, cq *tgbotapi.CallbackQuery, page int) {
ctx := context.Background()
chatID := cq.Message.Chat.ID
@ -78,12 +117,22 @@ func (h *Handler) HandleFirstApproverPageCallback(bot *tgbotapi.BotAPI, cq *tgbo
return
}
approvalTopic, roleCode, roleLabel := h.getFirstApproverRoleForFlow(ctx, flowState)
if approvalTopic == nil {
topic, _ := h.registry.Get(flowState.TopicCode)
if topic == nil {
return
}
topicCode, _ := topic.TopicCodeFromCollected(flowState.Collected)
approvalTopic, _ := h.ipc.Database.ApprovalTopic.GetApprovalTopicByCodeDB(ctx, topicCode)
if approvalTopic == nil || approvalTopic.FirstApproverRoleCode == nil {
return
}
_ = approvalTopic
roleCode := *approvalTopic.FirstApproverRoleCode
role := h.getRoleLabelByCode(ctx, roleCode)
roleLabel := roleCode
if role != nil && role.Name != nil {
roleLabel = *role.Name
}
users, err := h.ipc.Database.User.GetUsersByRoleCodeDB(ctx, roleCode)
if err != nil {
return

View File

@ -121,7 +121,6 @@ func (h *Handler) HandleObserverConfirmCallback(bot *tgbotapi.BotAPI, cq *tgbota
return
}
flowState.AwaitObserverSelection = false
h.States.Clear(ctx, telegramID) //nolint:errcheck
h.finalizeAndSubmit(bot, ctx, chatID, telegramID, flowState)
}

View File

@ -39,6 +39,7 @@ type ApprovalFlowState struct {
// Выбор наблюдателей (мультиселект)
AwaitObserverSelection bool `json:"await_observer_selection"`
ObserverSelectionDone bool `json:"observer_selection_done"`
SelectedObserverIDs []uint64 `json:"selected_observer_ids,omitempty"`
}