feat: add DingTalk answer feedback workflow
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package feedback
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Service handles answer feedback domain logic. It validates incoming
|
||||
// commands, normalizes fields, generates cryptographically random IDs,
|
||||
// and delegates persistence to a Repository.
|
||||
type Service struct {
|
||||
tenantKey string
|
||||
channel string
|
||||
repo Repository
|
||||
gen IDGenerator
|
||||
}
|
||||
|
||||
// NewService creates a feedback Service. It rejects empty tenant, empty
|
||||
// channel, and nil repo. An optional IDGenerator can be provided for
|
||||
// deterministic testing; if nil, a crypto/rand UUIDv4 generator is used.
|
||||
func NewService(tenantKey, channel string, repo Repository, gen ...IDGenerator) (*Service, error) {
|
||||
if strings.TrimSpace(tenantKey) == "" {
|
||||
return nil, errors.New("tenant_key must not be empty")
|
||||
}
|
||||
if strings.TrimSpace(channel) == "" {
|
||||
return nil, errors.New("channel must not be empty")
|
||||
}
|
||||
if repo == nil {
|
||||
return nil, errors.New("repository must not be nil")
|
||||
}
|
||||
if len(gen) > 1 {
|
||||
return nil, errors.New("at most one IDGenerator allowed")
|
||||
}
|
||||
var idGen IDGenerator
|
||||
if len(gen) == 1 {
|
||||
idGen = gen[0]
|
||||
} else {
|
||||
idGen = &defaultIDGenerator{}
|
||||
}
|
||||
if idGen == nil {
|
||||
return nil, errors.New("IDGenerator must not be nil")
|
||||
}
|
||||
return &Service{
|
||||
tenantKey: strings.TrimSpace(tenantKey),
|
||||
channel: strings.TrimSpace(channel),
|
||||
repo: repo,
|
||||
gen: idGen,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// defaultIDGenerator produces UUIDv4-style 128-bit random IDs.
|
||||
type defaultIDGenerator struct{}
|
||||
|
||||
func (g *defaultIDGenerator) Generate() (string, error) {
|
||||
b := make([]byte, 16)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
b[6] = (b[6] & 0x0f) | 0x40 // version 4
|
||||
b[8] = (b[8] & 0x3f) | 0x80 // variant 8-9-a-b
|
||||
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
|
||||
b[0:4], b[4:6], b[6:8], b[8:10], b[10:16]), nil
|
||||
}
|
||||
|
||||
// Apply validates and processes a feedback Command. It trims inputs,
|
||||
// validates allowed values, normalizes reason codes, generates IDs,
|
||||
// and delegates to the Repository.
|
||||
func (s *Service) Apply(ctx context.Context, cmd Command) (Result, error) {
|
||||
cmd.ExternalActorID = strings.TrimSpace(cmd.ExternalActorID)
|
||||
cmd.OutTrackID = strings.TrimSpace(cmd.OutTrackID)
|
||||
cmd.Value = strings.TrimSpace(cmd.Value)
|
||||
cmd.ReasonCode = strings.TrimSpace(cmd.ReasonCode)
|
||||
cmd.Source = strings.TrimSpace(cmd.Source)
|
||||
|
||||
if cmd.ExternalActorID == "" || len(cmd.ExternalActorID) > 256 {
|
||||
return Result{}, fmt.Errorf("%w: external actor ID", ErrInvalidCommand)
|
||||
}
|
||||
if cmd.OutTrackID == "" || len(cmd.OutTrackID) > 256 {
|
||||
return Result{}, fmt.Errorf("%w: outTrackID", ErrInvalidCommand)
|
||||
}
|
||||
if cmd.Value != ValueUp && cmd.Value != ValueDown {
|
||||
return Result{}, fmt.Errorf("%w: value", ErrInvalidCommand)
|
||||
}
|
||||
if cmd.Source != SourceDingTalkCard {
|
||||
return Result{}, fmt.Errorf("%w: source", ErrInvalidCommand)
|
||||
}
|
||||
if cmd.Value == ValueUp {
|
||||
cmd.ReasonCode = ReasonEmpty
|
||||
} else if !validReason(cmd.ReasonCode) {
|
||||
return Result{}, fmt.Errorf("%w: reason code", ErrInvalidCommand)
|
||||
}
|
||||
|
||||
feedbackID, err := s.gen.Generate()
|
||||
if err != nil {
|
||||
return Result{}, fmt.Errorf("generate feedback ID: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(feedbackID) == "" {
|
||||
return Result{}, errors.New("generate feedback ID: empty ID")
|
||||
}
|
||||
actorID, err := s.gen.Generate()
|
||||
if err != nil {
|
||||
return Result{}, fmt.Errorf("generate actor ID: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(actorID) == "" {
|
||||
return Result{}, errors.New("generate actor ID: empty ID")
|
||||
}
|
||||
|
||||
return s.repo.RecordFeedback(ctx, Record{
|
||||
ID: feedbackID, TenantKey: s.tenantKey, Channel: s.channel, ActorID: actorID,
|
||||
ExternalActorID: cmd.ExternalActorID, OutTrackID: cmd.OutTrackID,
|
||||
Value: cmd.Value, ReasonCode: cmd.ReasonCode, Source: cmd.Source,
|
||||
})
|
||||
}
|
||||
|
||||
func validReason(reason string) bool {
|
||||
switch reason {
|
||||
case ReasonEmpty, ReasonIncorrect, ReasonNotResolved, ReasonIrrelevant, ReasonUnclear, ReasonOutdated, ReasonOther:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user