From f7e662bd869e7373904c24f77b57365d40d008e6 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Thu, 20 Oct 2016 16:56:38 +0200 Subject: [PATCH] Add StripeAccount model --- lib/code_corps/map_utils.ex | 7 ++++ .../stripe/adapters/stripe_account.ex | 7 ++++ .../20161020144622_create_stripe_account.exs | 29 +++++++++++++ .../stripe/adapters/stripe_account_test.exs | 14 +++++++ test/models/stripe_account_test.exs | 41 +++++++++++++++++++ web/models/stripe_account.ex | 35 ++++++++++++++++ 6 files changed, 133 insertions(+) create mode 100644 lib/code_corps/map_utils.ex create mode 100644 lib/code_corps/stripe/adapters/stripe_account.ex create mode 100644 priv/repo/migrations/20161020144622_create_stripe_account.exs create mode 100644 test/lib/code_corps/stripe/adapters/stripe_account_test.exs create mode 100644 test/models/stripe_account_test.exs create mode 100644 web/models/stripe_account.ex diff --git a/lib/code_corps/map_utils.ex b/lib/code_corps/map_utils.ex new file mode 100644 index 000000000..14e305f7d --- /dev/null +++ b/lib/code_corps/map_utils.ex @@ -0,0 +1,7 @@ +defmodule CodeCorps.MapUtils do + def rename(map, old_key, new_key) do + map + |> Map.put(new_key, map |> Map.get(old_key)) + |> Map.delete(old_key) + end +end diff --git a/lib/code_corps/stripe/adapters/stripe_account.ex b/lib/code_corps/stripe/adapters/stripe_account.ex new file mode 100644 index 000000000..94e2148b9 --- /dev/null +++ b/lib/code_corps/stripe/adapters/stripe_account.ex @@ -0,0 +1,7 @@ +defmodule CodeCorps.Stripe.Adapters.StripeAccount do + import CodeCorps.MapUtils, only: [rename: 3] + + def params_from_stripe(%{} = stripe_map) do + stripe_map |> rename("id", "id_from_stripe") + end +end diff --git a/priv/repo/migrations/20161020144622_create_stripe_account.exs b/priv/repo/migrations/20161020144622_create_stripe_account.exs new file mode 100644 index 000000000..a02e98409 --- /dev/null +++ b/priv/repo/migrations/20161020144622_create_stripe_account.exs @@ -0,0 +1,29 @@ +defmodule CodeCorps.Repo.Migrations.CreateStripeAccount do + use Ecto.Migration + + def change do + create table(:stripe_accounts) do + add :business_name, :string + add :business_url, :string + add :charges_enabled, :boolean + add :country, :string + add :default_currency, :string + add :details_submitted, :boolean + add :display_name, :string + add :email, :string + add :id_from_stripe, :string, null: false + add :managed, :boolean + add :support_email, :string + add :support_phone, :string + add :support_url, :string + add :transfers_enabled, :boolean + + add :organization_id, references(:organizations), null: false + + timestamps() + end + + create unique_index(:stripe_accounts, [:id_from_stripe]) + create unique_index(:stripe_accounts, [:organization_id]) + end +end diff --git a/test/lib/code_corps/stripe/adapters/stripe_account_test.exs b/test/lib/code_corps/stripe/adapters/stripe_account_test.exs new file mode 100644 index 000000000..3243ddbb0 --- /dev/null +++ b/test/lib/code_corps/stripe/adapters/stripe_account_test.exs @@ -0,0 +1,14 @@ +defmodule CodeCorps.Stripe.Adapters.StripeAccountTest do + use ExUnit.Case, async: true + + import CodeCorps.Stripe.Adapters.StripeAccount, only: [params_from_stripe: 1] + + @stripe_map %{"id" => "str_123", "foo" => "bar"} + @local_map %{"id_from_stripe" => "str_123", "foo" => "bar"} + + describe "params_from_stripe/1" do + test "converts from stripe map to local properly" do + assert @stripe_map |> params_from_stripe == @local_map + end + end +end diff --git a/test/models/stripe_account_test.exs b/test/models/stripe_account_test.exs new file mode 100644 index 000000000..2ee96f921 --- /dev/null +++ b/test/models/stripe_account_test.exs @@ -0,0 +1,41 @@ +defmodule CodeCorps.StripeAccountTest do + use CodeCorps.ModelCase + + alias CodeCorps.StripeAccount + + @valid_attrs %{ + id_from_stripe: "abc123" + } + + @invalid_attrs %{} + + describe "create_changeset/2" do + test "reports as valid when attributes are valid" do + organization_id = insert(:organization).id + + changes = Map.merge(@valid_attrs, %{organization_id: organization_id}) + changeset = StripeAccount.create_changeset(%StripeAccount{}, changes) + assert changeset.valid? + end + + test "reports as invalid when attributes are invalid" do + changeset = StripeAccount.create_changeset(%StripeAccount{}, @invalid_attrs) + refute changeset.valid? + + assert changeset.errors[:id_from_stripe] == {"can't be blank", []} + assert changeset.errors[:organization_id] == {"can't be blank", []} + end + + test "ensures associations link to records that exist" do + attrs = @valid_attrs |> Map.merge(%{organization_id: -1}) + + { result, changeset } = + StripeAccount.create_changeset(%StripeAccount{}, attrs) + |> Repo.insert + + assert result == :error + refute changeset.valid? + assert changeset.errors[:organization] == {"does not exist", []} + end + end +end diff --git a/web/models/stripe_account.ex b/web/models/stripe_account.ex new file mode 100644 index 000000000..934f74845 --- /dev/null +++ b/web/models/stripe_account.ex @@ -0,0 +1,35 @@ +defmodule CodeCorps.StripeAccount do + @moduledoc """ + Represents a StripeAccount stored on Code Corps. + """ + + use CodeCorps.Web, :model + + schema "stripe_accounts" do + field :business_name, :string + field :business_url, :string + field :charges_enabled, :boolean + field :country, :string + field :default_currency, :string + field :details_submitted, :boolean + field :display_name, :string + field :email, :string + field :id_from_stripe, :string, null: false + field :managed, :boolean + field :support_email, :string + field :support_phone, :string + field :support_url, :string + field :transfers_enabled, :boolean + + belongs_to :organization, CodeCorps.Organization + + timestamps() + end + + def create_changeset(struct, params \\ %{}) do + struct + |> cast(params, [:id_from_stripe, :organization_id]) + |> validate_required([:id_from_stripe, :organization_id]) + |> assoc_constraint(:organization) + end +end