62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
package governance
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/modules/governance/internal/store"
|
|
)
|
|
|
|
// PathStep is one stop on the route an unanswered approval request takes: who
|
|
// it is passed to, and how long it waits there. The first step is the default
|
|
// owner, who may be nobody, and anyone with authority.
|
|
type PathStep struct {
|
|
// Person is the id of the person asked at this step, or empty.
|
|
Person string `json:"person"`
|
|
Name string `json:"name"`
|
|
DeadlineMS int64 `json:"deadline_ms"`
|
|
}
|
|
|
|
// Deadline is how long the request waits at this step.
|
|
func (p PathStep) Deadline() time.Duration { return time.Duration(p.DeadlineMS) * time.Millisecond }
|
|
|
|
// EscalationPath returns the route an approval request takes, with its first
|
|
// step the default owner and every later step a person on the escalation path.
|
|
// When the action needs authority, a step held by someone without it is left
|
|
// out, so the request never moves to a person who could not answer it.
|
|
func (m *Module) EscalationPath(ctx context.Context, needAuthority bool) ([]PathStep, error) {
|
|
set, err := m.Settings(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
people, err := m.store.People(ctx, m.orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return set.path(people, needAuthority), nil
|
|
}
|
|
|
|
func (s Settings) path(people []store.Person, needAuthority bool) []PathStep {
|
|
find := func(id string) (store.Person, bool) {
|
|
i := slices.IndexFunc(people, func(p store.Person) bool { return p.ID.String() == id })
|
|
if i < 0 {
|
|
return store.Person{}, false
|
|
}
|
|
return people[i], true
|
|
}
|
|
first := PathStep{Person: s.DefaultOwner, DeadlineMS: s.ApprovalDeadline.Milliseconds()}
|
|
if p, ok := find(s.DefaultOwner); ok {
|
|
first.Name = p.Name
|
|
}
|
|
out := []PathStep{first}
|
|
for _, st := range s.Escalation {
|
|
p, ok := find(st.Person)
|
|
if !ok || (needAuthority && !p.Authority) {
|
|
continue
|
|
}
|
|
out = append(out, PathStep{Person: st.Person, Name: p.Name, DeadlineMS: st.Deadline.Milliseconds()})
|
|
}
|
|
return out
|
|
}
|