Repository: nukosuke/go-zendesk
Pull request #277 (2023-06-22 09:03:04)

feat: add organization tickets function
Use the get tickets API but with the organization id parameter.

https://developer.zendesk.com/api-reference/ticketing/tickets/tickets/#list-tickets
(GET /api/v2/organizations/{organization_id}/tickets)


Patch:
diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go
index 565721a..36cfb70 100644
--- a/zendesk/mock/client.go
+++ b/zendesk/mock/client.go
@@ -915,6 +915,22 @@ func (mr *ClientMockRecorder) GetOrganizationTags(arg0, arg1 interface{}) *gomoc
 	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOrganizationTags", reflect.TypeOf((*Client)(nil).GetOrganizationTags), arg0, arg1)
 }
 
+// GetOrganizationTickets mocks base method.
+func (m *Client) GetOrganizationTickets(arg0 context.Context, arg1 int64, arg2 *zendesk.TicketListOptions) ([]zendesk.Ticket, zendesk.Page, error) {
+	m.ctrl.T.Helper()
+	ret := m.ctrl.Call(m, "GetOrganizationTickets", arg0, arg1, arg2)
+	ret0, _ := ret[0].([]zendesk.Ticket)
+	ret1, _ := ret[1].(zendesk.Page)
+	ret2, _ := ret[2].(error)
+	return ret0, ret1, ret2
+}
+
+// GetOrganizationTickets indicates an expected call of GetOrganizationTickets.
+func (mr *ClientMockRecorder) GetOrganizationTickets(arg0, arg1, arg2 interface{}) *gomock.Call {
+	mr.mock.ctrl.T.Helper()
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOrganizationTickets", reflect.TypeOf((*Client)(nil).GetOrganizationTickets), arg0, arg1, arg2)
+}
+
 // GetOrganizationUsers mocks base method.
 func (m *Client) GetOrganizationUsers(arg0 context.Context, arg1 int64, arg2 *zendesk.UserListOptions) ([]zendesk.User, zendesk.Page, error) {
 	m.ctrl.T.Helper()
diff --git a/zendesk/ticket.go b/zendesk/ticket.go
index 836026e..e0a7512 100644
--- a/zendesk/ticket.go
+++ b/zendesk/ticket.go
@@ -143,6 +143,7 @@ type TicketListOptions struct {
 // TicketAPI an interface containing all ticket related methods
 type TicketAPI interface {
 	GetTickets(ctx context.Context, opts *TicketListOptions) ([]Ticket, Page, error)
+	GetOrganizationTickets(ctx context.Context, organizationID int64, ops *TicketListOptions) ([]Ticket, Page, error)
 	GetTicket(ctx context.Context, id int64) (Ticket, error)
 	GetMultipleTickets(ctx context.Context, ticketIDs []int64) ([]Ticket, error)
 	CreateTicket(ctx context.Context, ticket Ticket) (Ticket, error)
@@ -181,6 +182,40 @@ func (z *Client) GetTickets(ctx context.Context, opts *TicketListOptions) ([]Tic
 	return data.Tickets, data.Page, nil
 }
 
+// GetOrganizationTickets get organization ticket list
+//
+// ref: https://developer.zendesk.com/rest_api/docs/support/tickets#list-tickets
+func (z *Client) GetOrganizationTickets(
+	ctx context.Context, organizationID int64, opts *TicketListOptions,
+) ([]Ticket, Page, error) {
+	var data struct {
+		Tickets []Ticket `json:"tickets"`
+		Page
+	}
+
+	tmp := opts
+	if tmp == nil {
+		tmp = &TicketListOptions{}
+	}
+
+	path := fmt.Sprintf("/organizations/%d/tickets.json", organizationID)
+	u, err := addOptions(path, tmp)
+	if err != nil {
+		return nil, Page{}, err
+	}
+
+	body, err := z.get(ctx, u)
+	if err != nil {
+		return nil, Page{}, err
+	}
+
+	err = json.Unmarshal(body, &data)
+	if err != nil {
+		return nil, Page{}, err
+	}
+	return data.Tickets, data.Page, nil
+}
+
 // GetTicket gets a specified ticket
 //
 // ref: https://developer.zendesk.com/rest_api/docs/support/tickets#show-ticket


Test patch:
diff --git a/zendesk/ticket_test.go b/zendesk/ticket_test.go
index eb73b02..31d1e88 100644
--- a/zendesk/ticket_test.go
+++ b/zendesk/ticket_test.go
@@ -34,6 +34,29 @@ func TestGetTickets(t *testing.T) {
 	}
 }
 
+func TestGetOrganizationTickets(t *testing.T) {
+	mockAPI := newMockAPI(http.MethodGet, "tickets.json")
+	client := newTestClient(mockAPI)
+	defer mockAPI.Close()
+
+	tickets, _, err := client.GetOrganizationTickets(ctx, 360363695492, &TicketListOptions{
+		PageOptions: PageOptions{
+			Page:    1,
+			PerPage: 10,
+		},
+		SortBy:    "created_at",
+		SortOrder: "asc",
+	})
+	if err != nil {
+		t.Fatalf("Failed to get tickets: %s", err)
+	}
+
+	expectedLength := 2
+	if len(tickets) != expectedLength {
+		t.Fatalf("Returned tickets does not have the expected length %d. Tickets length is %d", expectedLength, len(tickets))
+	}
+}
+
 func TestGetTicket(t *testing.T) {
 	mockAPI := newMockAPI(http.MethodGet, "ticket.json")
 	client := newTestClient(mockAPI)