Skip to content

Commit 0282d68

Browse files
author
nukosuke
authored
Merge pull request #277 from paoloromolini/organization_tickets
feat: add organization tickets function
2 parents 0154b9b + 52bc7fe commit 0282d68

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

zendesk/mock/client.go

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

zendesk/ticket.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ type TicketListOptions struct {
143143
// TicketAPI an interface containing all ticket related methods
144144
type TicketAPI interface {
145145
GetTickets(ctx context.Context, opts *TicketListOptions) ([]Ticket, Page, error)
146+
GetOrganizationTickets(ctx context.Context, organizationID int64, ops *TicketListOptions) ([]Ticket, Page, error)
146147
GetTicket(ctx context.Context, id int64) (Ticket, error)
147148
GetMultipleTickets(ctx context.Context, ticketIDs []int64) ([]Ticket, error)
148149
CreateTicket(ctx context.Context, ticket Ticket) (Ticket, error)
@@ -181,6 +182,40 @@ func (z *Client) GetTickets(ctx context.Context, opts *TicketListOptions) ([]Tic
181182
return data.Tickets, data.Page, nil
182183
}
183184

185+
// GetOrganizationTickets get organization ticket list
186+
//
187+
// ref: https://developer.zendesk.com/rest_api/docs/support/tickets#list-tickets
188+
func (z *Client) GetOrganizationTickets(
189+
ctx context.Context, organizationID int64, opts *TicketListOptions,
190+
) ([]Ticket, Page, error) {
191+
var data struct {
192+
Tickets []Ticket `json:"tickets"`
193+
Page
194+
}
195+
196+
tmp := opts
197+
if tmp == nil {
198+
tmp = &TicketListOptions{}
199+
}
200+
201+
path := fmt.Sprintf("/organizations/%d/tickets.json", organizationID)
202+
u, err := addOptions(path, tmp)
203+
if err != nil {
204+
return nil, Page{}, err
205+
}
206+
207+
body, err := z.get(ctx, u)
208+
if err != nil {
209+
return nil, Page{}, err
210+
}
211+
212+
err = json.Unmarshal(body, &data)
213+
if err != nil {
214+
return nil, Page{}, err
215+
}
216+
return data.Tickets, data.Page, nil
217+
}
218+
184219
// GetTicket gets a specified ticket
185220
//
186221
// ref: https://developer.zendesk.com/rest_api/docs/support/tickets#show-ticket

zendesk/ticket_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ func TestGetTickets(t *testing.T) {
3434
}
3535
}
3636

37+
func TestGetOrganizationTickets(t *testing.T) {
38+
mockAPI := newMockAPI(http.MethodGet, "tickets.json")
39+
client := newTestClient(mockAPI)
40+
defer mockAPI.Close()
41+
42+
tickets, _, err := client.GetOrganizationTickets(ctx, 360363695492, &TicketListOptions{
43+
PageOptions: PageOptions{
44+
Page: 1,
45+
PerPage: 10,
46+
},
47+
SortBy: "created_at",
48+
SortOrder: "asc",
49+
})
50+
if err != nil {
51+
t.Fatalf("Failed to get tickets: %s", err)
52+
}
53+
54+
expectedLength := 2
55+
if len(tickets) != expectedLength {
56+
t.Fatalf("Returned tickets does not have the expected length %d. Tickets length is %d", expectedLength, len(tickets))
57+
}
58+
}
59+
3760
func TestGetTicket(t *testing.T) {
3861
mockAPI := newMockAPI(http.MethodGet, "ticket.json")
3962
client := newTestClient(mockAPI)

0 commit comments

Comments
 (0)