Skip to content

Commit 2bc0689

Browse files
committed
add support to ominichannel
1 parent d9cf3b2 commit 2bc0689

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## v3.0.6
4+
5+
Adding support to the agent availabilities API endpoints
6+
37
## v3.0.5
48

59
Adding CBP support to more endpoints. please see `cbp_support_spec.rb` for the complete list.

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,37 @@ ticket.requester # => #<ZendeskAPI::User id=...>
271271
Currently, this feature is limited to only a few resources and their associations.
272272
They are documented on [developer.zendesk.com](https://developer.zendesk.com/rest_api/docs/support/side_loading#supported-endpoints).
273273

274+
#### Recommended Approach
275+
276+
For better control over your data and to avoid large response sizes, consider fetching related resources explicitly. This approach can help you manage data loading more precisely and can lead to optimized performance for complex applications.
277+
278+
```ruby
279+
ticket = ZendeskAPI::Ticket.find(id: 1)
280+
requester = ZendeskAPI::User.find(id: ticket.requester_id)
281+
```
282+
283+
By explicitly fetching associated resources, you can ensure that your application only processes the data it needs, improving overall efficiency.
284+
285+
### Omnichannel
286+
287+
Support for the [Agent Availability API](https://developer.zendesk.com/api-reference/agent-availability/agent-availability-api/introduction/)
288+
289+
An agent’s availability includes their state (such as online) for each channel (such as messaging), and their unified state across channels. It also includes the work items assigned to them.
290+
291+
```ruby
292+
# All agent availabilities
293+
client.agent_availabilities.fetch
294+
295+
# fetch availability for one agent, their channels and work items
296+
agent_availability = ZendeskAPI::AgentAvailability.find(client, 386390041152)
297+
agent_availability.channels
298+
agent_availability.channels.first.work_items
299+
300+
# Using the agent availability filter
301+
ZendeskAPI::AgentAvailability.search(client, { select_channel: 'support' })
302+
ZendeskAPI::AgentAvailability.search(client, { channel_status: 'support:online' })
303+
```
304+
274305
### Search
275306

276307
Searching is done through the client. Returned is an instance of `ZendeskAPI::Collection`:

lib/zendesk_api/resources.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ class Locale < ReadResource; end
1616

1717
class CustomRole < DataResource; end
1818

19+
class WorkItem < Resource; end
20+
21+
class Channel < Resource
22+
def work_items
23+
@work_items ||= attributes.fetch('relationships', {}).fetch('work_items', {}).fetch('data', []).map do |work_item_attributes|
24+
WorkItem.new(@client, work_item_attributes)
25+
end
26+
end
27+
end
28+
1929
# client.agent_availabilities.fetch
2030
# client.agent_availabilities.find 20401208368
2131
# both return consistently - ZendeskAPI::AgentAvailability
@@ -24,10 +34,38 @@ def self.model_key
2434
"data"
2535
end
2636

27-
def self.find(client, id, *args)
37+
def initialize(client, attributes = {})
38+
nested_attributes = attributes.delete('attributes')
39+
super(client, attributes.merge(nested_attributes))
40+
end
41+
42+
def self.find(client, id)
2843
attributes = client.connection.get("#{resource_path}/#{id}").body.fetch(model_key, {})
2944
new(client, attributes)
3045
end
46+
47+
# Examples:
48+
# ZendeskAPI::AgentAvailability.search(client, { channel_status: 'support:online' })
49+
# ZendeskAPI::AgentAvailability.search(client, { agent_status_id: 1 })
50+
# Just pass a hash that includes the key and value you want to search for, it gets turned into a query string
51+
# on the format of filter[key]=value
52+
# Returns a collection of AgentAvailability objects
53+
def self.search(client, args_hash)
54+
query_string = args_hash.map { |k, v| "filter[#{k}]=#{v}" }.join("&")
55+
client.connection.get("#{resource_path}?#{query_string}").body.fetch(model_key, []).map do |attributes|
56+
new(client, attributes)
57+
end
58+
end
59+
60+
def channels
61+
@channels ||= begin
62+
channel_attributes_array = @client.connection.get(attributes['links']['self']).body.fetch('included')
63+
channel_attributes_array.map do |channel_attributes|
64+
nested_attributes = channel_attributes.delete('attributes')
65+
Channel.new(@client, channel_attributes.merge(nested_attributes))
66+
end
67+
end
68+
end
3169
end
3270

3371
class Role < DataResource

spec/live/cbp_support_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,27 @@
222222
end
223223
end
224224
end
225+
226+
describe ZendeskAPI::AgentAvailability do
227+
describe '/agent_availabilities' do
228+
let(:collection_fetched) do
229+
VCR.use_cassette("cbp_#{described_class}_collection") do
230+
client.agent_availabilities.fetch
231+
client.agent_availabilities
232+
end
233+
end
234+
235+
let(:response_body) { collection_fetched.response.body }
236+
let(:collection_fetched_results) { collection_fetched.to_a }
237+
it 'returns a CBP response with all the correct keys' do
238+
expect(response_body).to have_key('meta')
239+
expect(response_body).to have_key('links')
240+
expect(response_body['meta'].keys).to include('has_more')
241+
end
242+
243+
it 'returns a list of AgentAvailability objects' do
244+
expect(collection_fetched_results).to all(be_a(described_class))
245+
end
246+
end
247+
end
225248
end

0 commit comments

Comments
 (0)