Skip to content

Commit

Permalink
Merge pull request #290 from paoloromolini/count_tickets_in_views
Browse files Browse the repository at this point in the history
Count tickets in views
  • Loading branch information
nukosuke authored Oct 13, 2023
2 parents 76a5fe4 + b971c8b commit 56bff48
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
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)
GetTicketsFromViewIterator(ctx context.Context, opts *PaginationOptions) *Iterator[Ticket]
GetTicketsFromViewOBP(ctx context.Context, opts *OBPOptions) ([]Ticket, Page, error)
GetTicketsFromViewCBP(ctx context.Context, opts *CBPOptions) ([]Ticket, CursorPaginationMeta, error)
Expand Down Expand Up @@ -110,3 +120,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))
}
}

0 comments on commit 56bff48

Please sign in to comment.