Skip to content

Commit

Permalink
feat(kno-3746): add subscription endpoints support (#22)
Browse files Browse the repository at this point in the history
* feat(kno-3746): add missing listing endpoints

* feat(kno-3746): add subscription endpoints support

* chore: remove incorrect comments

* chore: code review tweaks
  • Loading branch information
juanazam authored Jun 6, 2023
1 parent 07682dd commit 46be50a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
75 changes: 75 additions & 0 deletions lib/knock/resources/objects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ defmodule Knock.Objects do
"""
@type ref :: %{id: :string, collection: :string}

@doc """
Returns paginated list of objects for a collection
# Available optional parameters:
#
# - page_size: specify size of the page to be returned by the api. (max limit: 50)
# - after: after cursor for pagination
# - before: before cursor for pagination
"""
@spec list(Client.t(), String.t(), Keyword.t()) :: Api.response()
def list(client, collection, options \\ []) do
Api.get(client, "/objects/#{collection}", query: options)
end

@doc """
Builds an object reference, which can be used in workflow trigger calls.
"""
Expand Down Expand Up @@ -138,6 +152,67 @@ defmodule Knock.Objects do
Api.get(client, "/objects/#{collection}/#{id}/schedules", query: options)
end

##
# Subscriptions
##

@doc """
Returns paginated subscriptions for the given object
# Available optional parameters:
#
# - page_size: specify size of the page to be returned by the api. (max limit: 50)
# - after: after cursor for pagination
# - before: before cursor for pagination
"""
@spec list_subscriptions(Client.t(), String.t(), String.t(), Keyword.t()) :: Api.response()
def list_subscriptions(client, collection, id, options \\ []) do
Api.get(client, "/objects/#{collection}/#{id}/subscriptions", query: options)
end

@doc """
Returns paginated subscriptions for the given object as recipient
# Available optional parameters:
#
# - page_size: specify size of the page to be returned by the api. (max limit: 50)
# - after: after cursor for pagination
# - before: before cursor for pagination
"""
@spec get_subscriptions(Client.t(), String.t(), String.t(), Keyword.t()) :: Api.response()
def get_subscriptions(client, collection, id, options \\ []) do
options = Keyword.put(options, :mode, "recipient")
Api.get(client, "/objects/#{collection}/#{id}/subscriptions", query: options)
end

@doc """
Adds subscriptions for all recipients passed as arguments
Expected properties:
- recipients: list of recipients to create subscriptions for
- properties: data to be stored at the subscription level for each recipient
"""
@spec add_subscriptions(Client.t(), String.t(), String.t(), map()) :: Api.response()
def add_subscriptions(client, collection, id, params) do
Api.post(client, "/objects/#{collection}/#{id}/subscriptions", params)
end

@doc """
Delete subscriptions for recipients passed as arguments
Expected properties:
- recipients: list of recipients to create subscriptions for
"""
@spec delete_subscriptions(Client.t(), String.t(), String.t(), [String.t() | map()]) ::
Api.response()
def delete_subscriptions(client, collection, id, params) do
recipients = Map.get(params, :recipients)

Api.delete(client, "/objects/#{collection}/#{id}/subscriptions",
body: %{recipients: recipients}
)
end

##
# Preferences
##
Expand Down
32 changes: 32 additions & 0 deletions lib/knock/resources/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ defmodule Knock.Users do

@default_preference_set_id "default"

@doc """
Returns paginated list of users
# Available optional parameters:
#
# - page_size: specify size of the page to be returned by the api. (max limit: 50)
# - after: after cursor for pagination
# - before: before cursor for pagination
"""
@spec list(Client.t(), Keyword.t()) :: Api.response()
def list(client, options \\ []) do
Api.get(client, "/users", query: options)
end

@doc """
Returns information about the user from the `user_id` given.
"""
Expand Down Expand Up @@ -310,6 +324,24 @@ defmodule Knock.Users do
Api.get(client, "/users/#{id}/schedules", query: options)
end

##
# Subscriptions
##

@doc """
Returns paginated subscriptions for the given user
# Available optional parameters:
#
# - page_size: specify size of the page to be returned by the api. (max limit: 50)
# - after: after cursor for pagination
# - before: before cursor for pagination
"""
@spec get_subscriptions(Client.t(), String.t(), Keyword.t()) :: Api.response()
def get_subscriptions(client, id, options \\ []) do
Api.get(client, "/users/#{id}/subscriptions", query: options)
end

defp build_setting_param(setting) when is_map(setting), do: setting
defp build_setting_param(setting), do: %{subscribed: setting}
end

0 comments on commit 46be50a

Please sign in to comment.