Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count tickets in views #290

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions fixture/GET/views_ticket_count.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"view_counts": [
{
"fresh": true,
"pretty": "~700",
"url": "https://company.zendesk.com/api/v2/views/25/count.json",
"value": 719,
"view_id": 25
},
{
"fresh": false,
"pretty": "...",
"url": "https://company.zendesk.com/api/v2/views/78/count.json",
"value": null,
"view_id": 78
}
]
}
15 changes: 15 additions & 0 deletions zendesk/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions zendesk/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
)

Expand All @@ -24,11 +25,20 @@ type (
// Restriction Restriction
}

ViewCount struct {
ViewID int64 `json:"view_id"`
URL string `json:"url"`
Value int64 `json:"value"`
Pretty string `json:"pretty"`
Fresh bool `json:"fresh"`
}

// ViewAPI encapsulates methods on view
ViewAPI interface {
GetView(context.Context, int64) (View, error)
GetViews(context.Context) ([]View, Page, error)
GetTicketsFromView(context.Context, int64, *TicketListOptions) ([]Ticket, Page, error)
GetCountTicketsInViews(ctx context.Context, ids []string) ([]ViewCount, error)
}
)

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

return result.Tickets, result.Page, nil
}

// GetCountTicketsInViews count tickets in views using views ids
// ref https://developer.zendesk.com/api-reference/ticketing/business-rules/views/#count-tickets-in-views
func (z *Client) GetCountTicketsInViews(ctx context.Context, ids []string) ([]ViewCount, error) {
var result struct {
ViewCounts []ViewCount `json:"view_counts"`
}
idsURLParameter := strings.Join(ids, ",")
body, err := z.get(ctx, fmt.Sprintf("/views/count_many?ids=%s", idsURLParameter))

if err != nil {
return []ViewCount{}, err
}

if err := json.Unmarshal(body, &result); err != nil {
return []ViewCount{}, err
}
return result.ViewCounts, nil
}
15 changes: 15 additions & 0 deletions zendesk/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ func TestGetViews(t *testing.T) {
t.Fatalf("expected length of views is 2, but got %d", len(views))
}
}

func TestGetCountTicketsInViewsTestGetViews(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "views_ticket_count.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()
ids := []string{"25", "78"}
viewsCount, err := client.GetCountTicketsInViews(ctx, ids)
if err != nil {
t.Fatalf("Failed to get views tickets count: %s", err)
}

if len(viewsCount) != 2 {
t.Fatalf("expected length of views ticket counts is 2, but got %d", len(viewsCount))
}
}
Loading