-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfields.go
More file actions
91 lines (80 loc) · 2.26 KB
/
fields.go
File metadata and controls
91 lines (80 loc) · 2.26 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
90
91
package flashduty
import (
"context"
"fmt"
"net/http"
)
// ListFieldsInput contains parameters for listing custom fields
type ListFieldsInput struct {
FieldIDs []string // Filter by field IDs
FieldName string // Filter by exact field name
}
// ListFieldsOutput contains the result of listing fields
type ListFieldsOutput struct {
Fields []FieldInfo `json:"fields"`
Total int `json:"total"`
}
// ListFields queries custom field definitions
func (c *Client) ListFields(ctx context.Context, input *ListFieldsInput) (*ListFieldsOutput, error) {
resp, err := c.makeRequest(ctx, "POST", "/field/list", map[string]any{})
if err != nil {
return nil, fmt.Errorf("failed to list fields: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, handleAPIError(c.logger, resp)
}
var result struct {
Error *DutyError `json:"error,omitempty"`
Data *struct {
Items []struct {
FieldID string `json:"field_id"`
FieldName string `json:"field_name"`
DisplayName string `json:"display_name"`
FieldType string `json:"field_type"`
ValueType string `json:"value_type"`
Options []string `json:"options,omitempty"`
DefaultValue any `json:"default_value,omitempty"`
} `json:"items"`
} `json:"data,omitempty"`
}
if err := parseResponse(c.logger, resp, &result); err != nil {
return nil, err
}
if result.Error != nil {
return nil, result.Error
}
// Build filter ID set
filterIDSet := make(map[string]struct{})
for _, id := range input.FieldIDs {
filterIDSet[id] = struct{}{}
}
fields := []FieldInfo{}
if result.Data != nil {
for _, f := range result.Data.Items {
// Filter by ID if provided
if len(filterIDSet) > 0 {
if _, ok := filterIDSet[f.FieldID]; !ok {
continue
}
}
// Filter by name if provided
if input.FieldName != "" && f.FieldName != input.FieldName {
continue
}
fields = append(fields, FieldInfo{
FieldID: f.FieldID,
FieldName: f.FieldName,
DisplayName: f.DisplayName,
FieldType: f.FieldType,
ValueType: f.ValueType,
Options: f.Options,
DefaultValue: f.DefaultValue,
})
}
}
return &ListFieldsOutput{
Fields: fields,
Total: len(fields),
}, nil
}