-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapproval_decision.go
More file actions
89 lines (80 loc) · 2.73 KB
/
approval_decision.go
File metadata and controls
89 lines (80 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package agentremote
import (
"errors"
"strings"
)
// Approval decision reason constants.
const (
ApprovalReasonAllowOnce = "allow_once"
ApprovalReasonAllowAlways = "allow_always"
ApprovalReasonAutoApproved = "auto_approved"
ApprovalReasonDeny = "deny"
ApprovalReasonTimeout = "timeout"
ApprovalReasonExpired = "expired"
ApprovalReasonCancelled = "cancelled"
ApprovalReasonDeliveryError = "delivery_error"
)
type ApprovalResolutionOrigin string
const (
ApprovalResolutionOriginUser ApprovalResolutionOrigin = "user"
ApprovalResolutionOriginAgent ApprovalResolutionOrigin = "agent"
)
// ApprovalDecisionPayload is the standardized decision type for all approval flows.
type ApprovalDecisionPayload struct {
ApprovalID string
Approved bool
Always bool
Reason string
ReactionKey string
ResolvedBy ApprovalResolutionOrigin
}
func normalizeApprovalResolutionOrigin(origin ApprovalResolutionOrigin) ApprovalResolutionOrigin {
switch origin {
case ApprovalResolutionOriginUser, ApprovalResolutionOriginAgent:
return origin
default:
return ""
}
}
func ApprovalResolutionOriginFromString(value string) ApprovalResolutionOrigin {
switch strings.ToLower(strings.TrimSpace(value)) {
case string(ApprovalResolutionOriginUser):
return ApprovalResolutionOriginUser
case string(ApprovalResolutionOriginAgent):
return ApprovalResolutionOriginAgent
default:
return ""
}
}
// Shared sentinel errors for approval resolution.
var (
ErrApprovalMissingID = errors.New("missing approval id")
ErrApprovalMissingRoom = errors.New("missing room id")
ErrApprovalOnlyOwner = errors.New("only the owner can approve")
ErrApprovalUnknown = errors.New("unknown or expired approval id")
ErrApprovalWrongRoom = errors.New("approval id does not belong to this room")
ErrApprovalExpired = errors.New("approval expired")
ErrApprovalAlreadyHandled = errors.New("approval already resolved")
)
// ApprovalErrorToastText maps an approval error to a user-facing toast string.
func ApprovalErrorToastText(err error) string {
if err == nil {
return ""
}
switch {
case errors.Is(err, ErrApprovalOnlyOwner):
return "Only the owner can approve."
case errors.Is(err, ErrApprovalWrongRoom):
return "That approval request belongs to a different room."
case errors.Is(err, ErrApprovalExpired), errors.Is(err, ErrApprovalUnknown):
return "That approval request is expired or no longer valid."
case errors.Is(err, ErrApprovalAlreadyHandled):
return "That approval request was already handled."
case errors.Is(err, ErrApprovalMissingID):
return "Missing approval ID."
case errors.Is(err, ErrApprovalMissingRoom):
return "Missing room ID."
default:
return strings.TrimSpace(err.Error())
}
}