-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
473 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
defmodule Chargebeex.Usage do | ||
use TypedStruct | ||
|
||
@resource "usage" | ||
use Chargebeex.Resource, resource: @resource, only: [:list] | ||
|
||
@moduledoc """ | ||
Struct that represent a Chargebee's API usage resource. | ||
""" | ||
|
||
@typedoc """ | ||
"admin_console" | "api" | "bulk_operation" | ||
""" | ||
@type source :: String.t() | ||
|
||
typedstruct do | ||
field :id, String.t() | ||
field :usage_date, String.t() | ||
field :subscription_id, String.t() | ||
field :item_price_id, String.t() | ||
field :invoice_id, String.t() | ||
field :line_item_id, String.t() | ||
field :quantity, String.t() | ||
field :source, source() | ||
field :note, String.t() | ||
field :resource_version, String.t() | ||
field :updated_at, non_neg_integer() | ||
field :created_at, non_neg_integer() | ||
end | ||
|
||
use ExConstructor, :build | ||
|
||
@doc """ | ||
Allows to create a Usage | ||
## Examples | ||
iex> Chargebeex.Usage.create(%{item_price_id: "item_eur_monthly", quantity: 42, usage_date: 1599817250}) | ||
{:ok, %Chargebeex.Usage{}} | ||
""" | ||
def create(subscription_id, params, opts \\ []) do | ||
nested_generic_action_without_id( | ||
:post, | ||
[subscription: subscription_id], | ||
@resource, | ||
"usages", | ||
params, | ||
opts | ||
) | ||
end | ||
|
||
@doc """ | ||
Allows to retrieve a Usage | ||
## Examples | ||
iex> Chargebeex.Usage.retrieve("e49e39cf-a406-4cc9-b14a-85476b3c3ebf", %{id: "a065d78c-a5a8-458b-85b6-e9295118d3bf"}) | ||
{:ok, %Chargebeex.Usage{}} | ||
""" | ||
def retrieve(subscription_id, params, opts \\ []) do | ||
nested_generic_action_without_id( | ||
:get, | ||
[subscription: subscription_id], | ||
@resource, | ||
"usages", | ||
params, | ||
opts | ||
) | ||
end | ||
|
||
@doc """ | ||
Allows to delete a Usage | ||
## Examples | ||
iex> Chargebeex.Usage.delete("e49e39cf-a406-4cc9-b14a-85476b3c3ebf", %{id: "a065d78c-a5a8-458b-85b6-e9295118d3bf"}) | ||
{:ok, %Chargebeex.Usage{}} | ||
""" | ||
def delete(subscription_id, params, opts \\ []) do | ||
nested_generic_action_without_id( | ||
:post, | ||
[subscription: subscription_id], | ||
@resource, | ||
"delete_usage", | ||
params, | ||
opts | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule Chargebeex.Builder.UsageTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Chargebeex.Builder | ||
alias Chargebeex.Fixtures.Usage, as: UsageFixture | ||
alias Chargebeex.Usage | ||
|
||
describe "build/1" do | ||
test "should build an usage" do | ||
builded = | ||
UsageFixture.retrieve() | ||
|> Jason.decode!() | ||
|> Builder.build() | ||
|
||
assert %{"usage" => %Usage{}} = builded | ||
end | ||
|
||
test "should have the usage params" do | ||
usage = | ||
UsageFixture.retrieve() | ||
|> Jason.decode!() | ||
|> Builder.build() | ||
|> Map.get("usage") | ||
|
||
params = UsageFixture.usage_params() |> Jason.decode!() | ||
|
||
assert usage.id == Map.get(params, "id") | ||
assert usage.updated_at == Map.get(params, "updated_at") | ||
assert usage.usage_date == Map.get(params, "usage_date") | ||
assert usage.subscription_id == Map.get(params, "subscription_id") | ||
assert usage.item_price_id == Map.get(params, "item_price_id") | ||
assert usage.invoice_id == Map.get(params, "invoice_id") | ||
assert usage.line_item_id == Map.get(params, "line_item_id") | ||
assert usage.quantity == Map.get(params, "quantity") | ||
assert usage.source == Map.get(params, "source") | ||
assert usage.resource_version == Map.get(params, "resource_version") | ||
assert usage.created_at == Map.get(params, "created_at") | ||
end | ||
end | ||
end |
Oops, something went wrong.