-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
11 changed files
with
175 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Canary.Accounts.Membership do | ||
use Ash.Type.Enum, values: [:free, :starter, :admin] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
defmodule Canary.Accounts.MembershipCalculation do | ||
use Ash.Resource.Calculation | ||
|
||
@impl true | ||
def init(opts) do | ||
if [ | ||
:stripe_subscription_attribute | ||
] | ||
|> Enum.any?(&is_nil(opts[&1])) do | ||
{:error, :invalid_opts} | ||
else | ||
{:ok, opts} | ||
end | ||
end | ||
|
||
@impl true | ||
def load(_query, opts, _context) do | ||
[ | ||
opts[:stripe_subscription_attribute] | ||
] | ||
end | ||
|
||
@impl true | ||
def calculate(records, opts, _args) do | ||
records | ||
|> Enum.map(fn record -> | ||
sub = record |> Map.get(opts[:stripe_subscription_attribute]) | ||
|
||
%{ | ||
tier: tier(sub), | ||
trial: trial?(sub), | ||
trial_end: trial_end(sub), | ||
will_renew: will_renew?(sub), | ||
current_period_end: current_period_end(sub), | ||
status: status(sub) | ||
} | ||
end) | ||
end | ||
|
||
defp tier(subscription) do | ||
starter_price_id = Application.fetch_env!(:canary, :stripe_starter_price_id) | ||
|
||
cond do | ||
is_nil(subscription) -> | ||
:free | ||
|
||
true -> | ||
found = | ||
subscription["items"]["data"] |> Enum.find(&(&1["price"]["id"] == starter_price_id)) | ||
|
||
case found do | ||
nil -> :free | ||
%{"plan" => %{"active" => false}} -> :free | ||
%{"plan" => %{"active" => true}} -> :starter | ||
_ -> :free | ||
end | ||
end | ||
end | ||
|
||
defp trial?(nil), do: false | ||
defp trial?(%{"trial_end" => v}), do: not is_nil(v) | ||
|
||
defp will_renew?(nil), do: false | ||
defp will_renew?(%{"cancel_at_period_end" => v}), do: not v | ||
|
||
defp current_period_end(nil), do: nil | ||
defp current_period_end(%{"current_period_end" => v}), do: DateTime.from_unix!(v) | ||
|
||
defp trial_end(nil), do: nil | ||
|
||
defp trial_end(%{ | ||
"trial_end" => trial_end, | ||
"trial_settings" => %{"end_behavior" => %{"missing_payment_method" => _create_invoice}} | ||
}) do | ||
case trial_end do | ||
nil -> nil | ||
t -> DateTime.from_unix!(t) | ||
end | ||
end | ||
|
||
defp status(nil), do: nil | ||
defp status(%{"status" => status}), do: status | ||
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
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
21 changes: 21 additions & 0 deletions
21
core/priv/repo/migrations/20241020054720_add_super_user.exs
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,21 @@ | ||
defmodule Canary.Repo.Migrations.AddSuperUser do | ||
@moduledoc """ | ||
Updates resources based on their most recent snapshots. | ||
This file was autogenerated with `mix ash_postgres.generate_migrations` | ||
""" | ||
|
||
use Ecto.Migration | ||
|
||
def up do | ||
alter table(:accounts) do | ||
add :super_user, :boolean, default: false | ||
end | ||
end | ||
|
||
def down do | ||
alter table(:accounts) do | ||
remove :super_user | ||
end | ||
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