Skip to content

Commit ad51ebd

Browse files
committed
feat: add count tickets in views
Signed-off-by: Paolo Romolini <[email protected]>
1 parent 23c7571 commit ad51ebd

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

fixture/GET/views_ticket_count.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"view_counts": [
3+
{
4+
"fresh": true,
5+
"pretty": "~700",
6+
"url": "https://company.zendesk.com/api/v2/views/25/count.json",
7+
"value": 719,
8+
"view_id": 25
9+
},
10+
{
11+
"fresh": false,
12+
"pretty": "...",
13+
"url": "https://company.zendesk.com/api/v2/views/78/count.json",
14+
"value": null,
15+
"view_id": 78
16+
}
17+
]
18+
}

zendesk/mock/client.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zendesk/view.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"strings"
78
"time"
89
)
910

@@ -24,11 +25,20 @@ type (
2425
// Restriction Restriction
2526
}
2627

28+
ViewCount struct {
29+
ViewID int64 `json:"view_id"`
30+
URL string `json:"url"`
31+
Value int64 `json:"value"`
32+
Pretty string `json:"pretty"`
33+
Fresh bool `json:"fresh"`
34+
}
35+
2736
// ViewAPI encapsulates methods on view
2837
ViewAPI interface {
2938
GetView(context.Context, int64) (View, error)
3039
GetViews(context.Context) ([]View, Page, error)
3140
GetTicketsFromView(context.Context, int64, *TicketListOptions) ([]Ticket, Page, error)
41+
GetCountTicketsInViews(ctx context.Context, ids []string) ([]ViewCount, error)
3242
}
3343
)
3444

@@ -104,3 +114,22 @@ func (z *Client) GetTicketsFromView(ctx context.Context, viewID int64, opts *Tic
104114

105115
return result.Tickets, result.Page, nil
106116
}
117+
118+
// GetCountTicketsInViews count tickets in views using views ids
119+
// ref https://developer.zendesk.com/api-reference/ticketing/business-rules/views/#count-tickets-in-views
120+
func (z *Client) GetCountTicketsInViews(ctx context.Context, ids []string) ([]ViewCount, error) {
121+
var result struct {
122+
ViewCounts []ViewCount `json:"view_counts"`
123+
}
124+
idsURLParameter := strings.Join(ids, ",")
125+
body, err := z.get(ctx, fmt.Sprintf("/views/count_many?ids=%s", idsURLParameter))
126+
127+
if err != nil {
128+
return []ViewCount{}, err
129+
}
130+
131+
if err := json.Unmarshal(body, &result); err != nil {
132+
return []ViewCount{}, err
133+
}
134+
return result.ViewCounts, nil
135+
}

zendesk/view_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,18 @@ func TestGetViews(t *testing.T) {
3535
t.Fatalf("expected length of views is 2, but got %d", len(views))
3636
}
3737
}
38+
39+
func TestGetCountTicketsInViewsTestGetViews(t *testing.T) {
40+
mockAPI := newMockAPI(http.MethodGet, "views_ticket_count.json")
41+
client := newTestClient(mockAPI)
42+
defer mockAPI.Close()
43+
ids := []string{"25", "78"}
44+
viewsCount, err := client.GetCountTicketsInViews(ctx, ids)
45+
if err != nil {
46+
t.Fatalf("Failed to get views tickets count: %s", err)
47+
}
48+
49+
if len(viewsCount) != 2 {
50+
t.Fatalf("expected length of views ticket counts is 2, but got %d", len(viewsCount))
51+
}
52+
}

0 commit comments

Comments
 (0)