Merge pull request 'kanban dashboard refactor' (#21) from kanban into develop
Reviewed-on: https://repo.ravanertebat.com/TM/tm_back/pulls/21 Reviewed-by: Hadi Barzegar <barzagarhadi@gmail.com>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package gorules
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
"tm/pkg/logger"
|
||||
|
||||
"repo.ravanertebat.com/k.khodayari/gorulessdk"
|
||||
)
|
||||
|
||||
const (
|
||||
showAvailableStatusAction = gorulessdk.ShowAvailableStatus
|
||||
)
|
||||
|
||||
// Config contains GoRules client configuration.
|
||||
type Config struct {
|
||||
BaseURL string
|
||||
Token string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// StatusWithCategory contains status and category values returned by GoRules.
|
||||
type StatusWithCategory struct {
|
||||
Status string `json:"status"`
|
||||
StatusCategory string `json:"status_category"`
|
||||
}
|
||||
|
||||
// Client evaluates available statuses against GoRules.
|
||||
type Client interface {
|
||||
EvaluateAvailableStatuses(ctx context.Context, currentStatus string, ruleID string) ([]StatusWithCategory, error)
|
||||
}
|
||||
|
||||
type sdkClient struct {
|
||||
client *gorulessdk.Client
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
// New creates a new GoRules client.
|
||||
func New(cfg Config, log logger.Logger) (Client, error) {
|
||||
if strings.TrimSpace(cfg.BaseURL) == "" {
|
||||
return nil, errors.New("gorules base url is required")
|
||||
}
|
||||
if strings.TrimSpace(cfg.Token) == "" {
|
||||
return nil, errors.New("gorules token is required")
|
||||
}
|
||||
|
||||
timeout := cfg.Timeout
|
||||
if timeout <= 0 {
|
||||
timeout = 10 * time.Second
|
||||
}
|
||||
|
||||
client, err := gorulessdk.NewClient(cfg.BaseURL, cfg.Token, gorulessdk.WithTimeout(timeout))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &sdkClient{
|
||||
client: client,
|
||||
logger: log,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sdkClient) EvaluateAvailableStatuses(ctx context.Context, currentStatus string, ruleID string) ([]StatusWithCategory, error) {
|
||||
input := gorulessdk.Input{
|
||||
Status: currentStatus,
|
||||
Action: showAvailableStatusAction,
|
||||
}
|
||||
|
||||
resp, err := s.client.Evaluate(ctx, ruleID, &input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
available := resp.AvailableStatuses()
|
||||
statuses := make([]StatusWithCategory, 0, len(available))
|
||||
for _, item := range available {
|
||||
statuses = append(statuses, StatusWithCategory{
|
||||
Status: item.Status,
|
||||
StatusCategory: item.Category,
|
||||
})
|
||||
}
|
||||
return statuses, nil
|
||||
}
|
||||
Reference in New Issue
Block a user