43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package state
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type AddApproverFlowState struct {
|
|
CategoryID uint64 `json:"category_id"`
|
|
ApprovalID uint64 `json:"approval_id"`
|
|
Stage string `json:"stage"` // "select_category", "select_approval", "select_user"
|
|
}
|
|
|
|
func addApproverKey(telegramID int64) string {
|
|
return fmt.Sprintf("add_approver_flow:%d", telegramID)
|
|
}
|
|
|
|
func (s *StateManager) SetAddApprover(ctx context.Context, telegramID int64, state *AddApproverFlowState) error {
|
|
data, err := json.Marshal(state)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.redis.Set(ctx, addApproverKey(telegramID), string(data), 30*time.Minute)
|
|
}
|
|
|
|
func (s *StateManager) GetAddApprover(ctx context.Context, telegramID int64) (*AddApproverFlowState, error) {
|
|
data, err := s.redis.Get(ctx, addApproverKey(telegramID))
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
var state AddApproverFlowState
|
|
if err := json.Unmarshal([]byte(data), &state); err != nil {
|
|
return nil, err
|
|
}
|
|
return &state, nil
|
|
}
|
|
|
|
func (s *StateManager) ClearAddApprover(ctx context.Context, telegramID int64) error {
|
|
return s.redis.Del(ctx, addApproverKey(telegramID))
|
|
}
|