Skip to content

Commit 6dff9ef

Browse files
authored
Merge pull request #139 from shore-gmbh/FEAT-add-subscription-entitlement
feat: Add `SubscriptionEntitlement`
2 parents fb0e65d + 971b616 commit 6dff9ef

File tree

6 files changed

+216
-0
lines changed

6 files changed

+216
-0
lines changed

lib/chargebeex/builder.ex

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule Chargebeex.Builder do
1212
PaymentSource,
1313
PortalSession,
1414
Subscription,
15+
SubscriptionEntitlement,
1516
Item,
1617
ItemPrice,
1718
Quote,
@@ -50,6 +51,9 @@ defmodule Chargebeex.Builder do
5051
def build_resource(%{"usage" => params}), do: Usage.build(params)
5152
def build_resource(%{"attached_item" => params}), do: AttachedItem.build(params)
5253

54+
def build_resource(%{"subscription_entitlement" => params}),
55+
do: SubscriptionEntitlement.build(params)
56+
5357
def build_resource("subscription", params), do: Subscription.build(params)
5458
def build_resource("customer", params), do: Customer.build(params)
5559
def build_resource("portal_session", params), do: PortalSession.build(params)
@@ -67,5 +71,8 @@ defmodule Chargebeex.Builder do
6771
def build_resource("usage", params), do: Usage.build(params)
6872
def build_resource("attached_item", params), do: AttachedItem.build(params)
6973

74+
def build_resource("subscription_entitlement", params),
75+
do: SubscriptionEntitlement.build(params)
76+
7077
def build_resource(_resource, params), do: params
7178
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
defmodule Chargebeex.SubscriptionEntitlement do
2+
use TypedStruct
3+
4+
@resource "subscription_entitlement"
5+
use Chargebeex.Resource, resource: @resource, only: []
6+
7+
@moduledoc """
8+
Struct that represent a Chargebee's API subscription entitlement resource.
9+
"""
10+
11+
@typedoc """
12+
"switch" | "custom" | "quantity" | "range"
13+
"""
14+
@type feature_type :: String.t()
15+
16+
typedstruct do
17+
field :subscription_id, String.t()
18+
field :feature_id, String.t()
19+
field :feature_name, String.t()
20+
field :feature_type, feature_type()
21+
field :value, String.t()
22+
field :name, String.t()
23+
field :is_overridden, boolean()
24+
field :is_enabled, boolean()
25+
field :object, String.t()
26+
end
27+
28+
use ExConstructor, :build
29+
30+
@doc """
31+
Allows to list Subscription Entitlements
32+
33+
Available filters can be found here: https://apidocs.chargebee.com/docs/api/subscription_entitlements#list_subscription_entitlements
34+
35+
## Examples
36+
37+
iex> filters = %{limit: 2}
38+
iex(2)> Chargebeex.SubscriptionEntitlement.list(filters)
39+
{:ok, [%Chargebeex.SubscriptionEntitlement{...}, %Chargebeex.SubscriptionEntitlement{...}], %{"next_offset" => nil}}
40+
"""
41+
def list(subscription_id, params \\ %{}, opts \\ []) do
42+
nested_generic_action_without_id(
43+
:get,
44+
[subscription: subscription_id],
45+
@resource,
46+
"subscription_entitlements",
47+
params,
48+
opts
49+
)
50+
end
51+
end

mix.exs

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ defmodule Chargebeex.MixProject do
7878
Chargebeex.Invoice,
7979
Chargebeex.PortalSession,
8080
Chargebeex.Subscription,
81+
Chargebeex.SubscriptionEntitlement,
8182
Chargebeex.Quote,
8283
Chargebeex.HostedPage,
8384
Chargebeex.ItemPrice,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule Chargebeex.Builder.SubscriptionEntitlementTest do
2+
use ExUnit.Case, async: true
3+
4+
alias Chargebeex.Builder
5+
alias Chargebeex.Fixtures.SubscriptionEntitlement, as: SubscriptionEntitlementFixture
6+
alias Chargebeex.SubscriptionEntitlement
7+
8+
describe "build/1" do
9+
test "should build an subscription_entitlement" do
10+
builded =
11+
SubscriptionEntitlementFixture.retrieve()
12+
|> Jason.decode!()
13+
|> Builder.build()
14+
15+
assert %{"subscription_entitlement" => %SubscriptionEntitlement{}} = builded
16+
end
17+
18+
test "should have the subscription_entitlement params" do
19+
subscription_entitlement =
20+
SubscriptionEntitlementFixture.retrieve()
21+
|> Jason.decode!()
22+
|> Builder.build()
23+
|> Map.get("subscription_entitlement")
24+
25+
params = SubscriptionEntitlementFixture.subscription_entitlement_params() |> Jason.decode!()
26+
27+
assert subscription_entitlement.subscription_id == Map.get(params, "subscription_id")
28+
assert subscription_entitlement.feature_id == Map.get(params, "feature_id")
29+
assert subscription_entitlement.feature_name == Map.get(params, "feature_name")
30+
assert subscription_entitlement.feature_type == Map.get(params, "feature_type")
31+
assert subscription_entitlement.value == Map.get(params, "value")
32+
assert subscription_entitlement.name == Map.get(params, "name")
33+
assert subscription_entitlement.is_overridden == Map.get(params, "is_overridden")
34+
assert subscription_entitlement.is_enabled == Map.get(params, "is_enabled")
35+
assert subscription_entitlement.object == Map.get(params, "object")
36+
end
37+
end
38+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
defmodule Chargebeex.SubscriptionEntitlementTest do
2+
use ExUnit.Case, async: true
3+
4+
import Hammox
5+
6+
alias Chargebeex.Fixtures.Common
7+
alias Chargebeex.SubscriptionEntitlement
8+
9+
setup :verify_on_exit!
10+
11+
describe "list" do
12+
test "with bad authentication should fail" do
13+
unauthorized = Common.unauthorized()
14+
15+
expect(
16+
Chargebeex.HTTPClientMock,
17+
:get,
18+
fn url, body, headers ->
19+
assert url ==
20+
"https://test-namespace.chargebee.com/api/v2/subscriptions/subscription_id/subscription_entitlements"
21+
22+
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
23+
assert body == ""
24+
25+
{:ok, 401, [], Jason.encode!(unauthorized)}
26+
end
27+
)
28+
29+
assert {:error, 401, [], ^unauthorized} = SubscriptionEntitlement.list("subscription_id")
30+
end
31+
32+
test "with no param, no offset should succeed" do
33+
expect(
34+
Chargebeex.HTTPClientMock,
35+
:get,
36+
fn url, body, headers ->
37+
assert url ==
38+
"https://test-namespace.chargebee.com/api/v2/subscriptions/subscription_id/subscription_entitlements"
39+
40+
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
41+
assert body == ""
42+
43+
{:ok, 200, [],
44+
Jason.encode!(%{
45+
list: [%{subscription_entitlement: %{}}, %{subscription_entitlement: %{}}]
46+
})}
47+
end
48+
)
49+
50+
assert {:ok, [%SubscriptionEntitlement{}, %SubscriptionEntitlement{}],
51+
%{"next_offset" => nil}} =
52+
SubscriptionEntitlement.list("subscription_id")
53+
end
54+
55+
test "with limit & offset params should succeed" do
56+
expect(
57+
Chargebeex.HTTPClientMock,
58+
:get,
59+
fn url, body, headers ->
60+
assert url ==
61+
"https://test-namespace.chargebee.com/api/v2/subscriptions/subscription_id/subscription_entitlements?limit=1&feature_type%5Bis%5D=switch"
62+
63+
assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}]
64+
assert body == ""
65+
66+
result = %{
67+
list: [%{subscription_entitlement: %{subscription_id: "subscription_id"}}],
68+
next_offset: "bar"
69+
}
70+
71+
{:ok, 200, [], Jason.encode!(result)}
72+
end
73+
)
74+
75+
assert {:ok, [%SubscriptionEntitlement{}], %{"next_offset" => "bar"}} =
76+
SubscriptionEntitlement.list("subscription_id", %{
77+
"feature_type[is]" => "switch",
78+
limit: 1
79+
})
80+
end
81+
end
82+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule Chargebeex.Fixtures.SubscriptionEntitlement do
2+
def subscription_entitlement_params() do
3+
"""
4+
{
5+
"subscription_id": "Azq8g8ULPXqdL6zM",
6+
"feature_id": "xero-integration",
7+
"feature_name": "Xero Integration",
8+
"feature_type": "SWITCH",
9+
"value": "true",
10+
"name": "Available",
11+
"is_overridden": true,
12+
"is_enabled": true,
13+
"object": "subscription_entitlement"
14+
}
15+
"""
16+
end
17+
18+
def retrieve() do
19+
"""
20+
{
21+
"subscription_entitlement": #{subscription_entitlement_params()}
22+
}
23+
"""
24+
end
25+
26+
def list() do
27+
"""
28+
{
29+
"list": [
30+
#{retrieve()},
31+
#{retrieve()}
32+
],
33+
"next_offset": "1612890918000"
34+
}
35+
"""
36+
end
37+
end

0 commit comments

Comments
 (0)