Repository: nukosuke/go-zendesk
Pull request #290 (2023-10-03 14:23:05)

Count tickets in views
Ref: https://developer.zendesk.com/api-reference/ticketing/business-rules/views/#count-tickets-in-views


Patch:
diff --git a/fixture/GET/views_ticket_count.json b/fixture/GET/views_ticket_count.json
new file mode 100644
index 0000000..270c9e7
--- /dev/null
+++ b/fixture/GET/views_ticket_count.json
@@ -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
+    }
+  ]
+}
\ No newline at end of file
diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go
index c664718..fe8720f 100644
--- a/zendesk/mock/client.go
+++ b/zendesk/mock/client.go
@@ -748,6 +748,21 @@ func (mr *ClientMockRecorder) GetBrand(ctx, brandID any) *gomock.Call {
 	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBrand", reflect.TypeOf((*Client)(nil).GetBrand), ctx, brandID)
 }
 
+// GetCountTicketsInViews mocks base method.
+func (m *Client) GetCountTicketsInViews(arg0 context.Context, arg1 []string) ([]zendesk.ViewCount, error) {
+	m.ctrl.T.Helper()
+	ret := m.ctrl.Call(m, "GetCountTicketsInViews", arg0, arg1)
+	ret0, _ := ret[0].([]zendesk.ViewCount)
+	ret1, _ := ret[1].(error)
+	return ret0, ret1
+}
+
+// GetCountTicketsInViews indicates an expected call of GetCountTicketsInViews.
+func (mr *ClientMockRecorder) GetCountTicketsInViews(arg0, arg1 interface{}) *gomock.Call {
+	mr.mock.ctrl.T.Helper()
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCountTicketsInViews", reflect.TypeOf((*Client)(nil).GetCountTicketsInViews), arg0, arg1)
+}
+
 // GetCustomRoles mocks base method.
 func (m *Client) GetCustomRoles(ctx context.Context) ([]zendesk.CustomRole, error) {
 	m.ctrl.T.Helper()
diff --git a/zendesk/view.go b/zendesk/view.go
index e61637d..c78c2d2 100644
--- a/zendesk/view.go
+++ b/zendesk/view.go
@@ -4,6 +4,7 @@ import (
 	"context"
 	"encoding/json"
 	"fmt"
+	"strings"
 	"time"
 )
 
@@ -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)
@@ -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
+}


Test patch:
diff --git a/zendesk/view_test.go b/zendesk/view_test.go
index 359904e..466ff3f 100644
--- a/zendesk/view_test.go
+++ b/zendesk/view_test.go
@@ -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))
+	}
+}