diff --git a/config/config.exs b/config/config.exs index 3ad0083..c651015 100644 --- a/config/config.exs +++ b/config/config.exs @@ -15,7 +15,7 @@ config :stripe_mock, StripeMockWeb.Endpoint, render_errors: [view: StripeMockWeb.ErrorView, accepts: ~w(json)] config :stripe_mock, ecto_repos: [StripeMock.Repo] -config :stripe_mock, StripeMock.Repo, migration_primary_key: [type: :binary_id] +config :stripe_mock, StripeMock.Repo, migration_primary_key: [type: :string] # Configures Elixir's Logger config :logger, :console, diff --git a/lib/stripe_mock/repo.ex b/lib/stripe_mock/repo.ex index 791cbf0..65f74dc 100644 --- a/lib/stripe_mock/repo.ex +++ b/lib/stripe_mock/repo.ex @@ -8,24 +8,12 @@ defmodule StripeMock.Repo do end def fetch(schema, id) do - if valid_uuid?(id) do - get(schema, id) - else - get_by(schema, stripe_id: id) - end - |> case do + case get(schema, id) do nil -> {:error, :not_found} object -> {:ok, object} end end - defp valid_uuid?(id) do - case Ecto.UUID.cast(id) do - {:ok, _} -> true - _ -> false - end - end - defoverridable delete: 1, delete: 2 def delete(struct) do diff --git a/lib/stripe_mock/schema.ex b/lib/stripe_mock/schema.ex index 9160edc..3ee30e6 100644 --- a/lib/stripe_mock/schema.ex +++ b/lib/stripe_mock/schema.ex @@ -10,15 +10,14 @@ defmodule StripeMock.Schema do import unquote(__MODULE__) alias StripeMock.{API, Repo} - Module.put_attribute(__MODULE__, :primary_key, {:id, :binary_id, autogenerate: false}) - Module.put_attribute(__MODULE__, :foreign_key_type, :binary_id) + Module.put_attribute(__MODULE__, :primary_key, {:id, :string, autogenerate: false}) + Module.put_attribute(__MODULE__, :foreign_key_type, :string) Module.put_attribute(__MODULE__, :timestamps_opts, inserted_at: :created, updated_at: false) end end defmacro common_fields() do quote do - field :stripe_id, :string field :description, :string field :metadata, :map, default: %{} end @@ -36,16 +35,16 @@ defmodule StripeMock.Schema do defp put_ids(changeset) do case get_field(changeset, :id) do - nil -> do_put_stripe_id(changeset) + nil -> do_put_id(changeset) _ -> changeset end end - defp do_put_stripe_id(changeset) do + defp do_put_id(changeset) do uuid = Ecto.UUID.generate() prefix = ID.prefix(changeset.data) - stripe_id = prefix <> "_" <> StripeMock.ID.from_uuid(uuid) + id = prefix <> "_" <> StripeMock.ID.from_uuid(uuid) - change(changeset, %{id: uuid, stripe_id: stripe_id}) + change(changeset, %{id: id}) end end diff --git a/lib/stripe_mock_web/controllers/customer_controller.ex b/lib/stripe_mock_web/controllers/customer_controller.ex index 794506d..8a10807 100644 --- a/lib/stripe_mock_web/controllers/customer_controller.ex +++ b/lib/stripe_mock_web/controllers/customer_controller.ex @@ -27,18 +27,16 @@ defmodule StripeMockWeb.CustomerController do end def update(conn, %{"id" => id} = customer_params) do - customer = API.get_customer!(id) - - with {:ok, %Customer{} = customer} <- API.update_customer(customer, customer_params) do + with {:ok, customer} <- API.get_customer(id), + {:ok, customer} <- API.update_customer(customer, customer_params) do render(conn, "show.json", customer: customer) end end def delete(conn, %{"id" => id}) do - customer = API.get_customer!(id) - - with {:ok, %Customer{}} <- API.delete_customer(customer) do - send_resp(conn, :no_content, "") + with {:ok, customer} <- API.get_customer(id), + {:ok, customer} <- API.delete_customer(customer) do + render(conn, "show.json", customer: customer) end end end diff --git a/lib/stripe_mock_web/view_helpers.ex b/lib/stripe_mock_web/view_helpers.ex index f15a4bc..41fc5bb 100644 --- a/lib/stripe_mock_web/view_helpers.ex +++ b/lib/stripe_mock_web/view_helpers.ex @@ -8,7 +8,6 @@ defmodule StripeMockWeb.ViewHelpers do |> update_created() |> Map.delete(:__struct__) |> Map.delete(:__meta__) - |> Map.put(:id, struct.stripe_id) end defp update_created(%{created: %{} = timestamp} = struct) do diff --git a/priv/repo/migrations/20191015221147_create_tables.exs b/priv/repo/migrations/20191015221147_create_tables.exs index 1b7297a..1634f07 100644 --- a/priv/repo/migrations/20191015221147_create_tables.exs +++ b/priv/repo/migrations/20191015221147_create_tables.exs @@ -3,7 +3,7 @@ defmodule StripeMock.Repo.Migrations.CreateTables do defmacro common_fields() do quote do - add(:stripe_id, :string, null: false) + add(:id, :string, null: false, primary_key: true) add(:deleted, :boolean, null: false, default: false) add(:description, :string, null: true) add(:metadata, :map, null: false, default: %{}) @@ -12,7 +12,7 @@ defmodule StripeMock.Repo.Migrations.CreateTables do end def change do - create table(:customers) do + create table(:customers, primary_key: false) do add(:currency, :string, default: "usd") add(:email, :string) add(:name, :string) @@ -21,7 +21,7 @@ defmodule StripeMock.Repo.Migrations.CreateTables do common_fields() end - create table(:cards) do + create table(:cards, primary_key: false) do add(:brand, :string) add(:last4, :string) add(:source, :string) @@ -36,7 +36,7 @@ defmodule StripeMock.Repo.Migrations.CreateTables do common_fields() end - create table(:tokens) do + create table(:tokens, primary_key: false) do add(:client_ip, :string) add(:type, :string) add(:used, :boolean, default: false) @@ -46,11 +46,11 @@ defmodule StripeMock.Repo.Migrations.CreateTables do common_fields() end - create table(:sources) do + create table(:sources, primary_key: false) do common_fields() end - create table(:payment_methods) do + create table(:payment_methods, primary_key: false) do add(:card_id, references(:cards)) add(:token_id, references(:tokens)) add(:source_id, references(:sources)) @@ -58,7 +58,7 @@ defmodule StripeMock.Repo.Migrations.CreateTables do common_fields() end - create table(:payment_intents) do + create table(:payment_intents, primary_key: false) do add(:amount, :integer) add(:capture_method, :string) add(:confirmation_method, :string) @@ -75,7 +75,7 @@ defmodule StripeMock.Repo.Migrations.CreateTables do common_fields() end - create table(:charges) do + create table(:charges, primary_key: false) do add(:amount, :integer) add(:capture, :boolean, default: false) add(:currency, :string) @@ -90,7 +90,7 @@ defmodule StripeMock.Repo.Migrations.CreateTables do common_fields() end - create table(:refunds) do + create table(:refunds, primary_key: false) do add(:amount, :integer) add(:reason, :string) diff --git a/test/stripe_mock_web/controllers/customer_controller_test.exs b/test/stripe_mock_web/controllers/customer_controller_test.exs index ad72dae..006112e 100644 --- a/test/stripe_mock_web/controllers/customer_controller_test.exs +++ b/test/stripe_mock_web/controllers/customer_controller_test.exs @@ -80,7 +80,7 @@ defmodule StripeMockWeb.CustomerControllerTest do customer: %Customer{id: id} = customer } do conn = delete(conn, Routes.customer_path(conn, :delete, customer)) - assert response(conn, 204) + assert %{"deleted" => true} = json_response(conn, :ok) assert %{"deleted" => true} = conn diff --git a/test/stripe_mock_web/controllers/source_controller_test.exs b/test/stripe_mock_web/controllers/source_controller_test.exs index 45b5553..fabf751 100644 --- a/test/stripe_mock_web/controllers/source_controller_test.exs +++ b/test/stripe_mock_web/controllers/source_controller_test.exs @@ -90,7 +90,7 @@ defmodule StripeMockWeb.SourceControllerTest do |> get(Routes.customer_source_path(conn, :index, card.customer_id, object: "card")) |> json_response(200) - assert Enum.find(data, &(&1["id"] == card.stripe_id)) + assert Enum.find(data, &(&1["id"] == card.id)) delete(conn, Routes.customer_source_path(conn, :delete, card.customer_id, card)) @@ -99,7 +99,7 @@ defmodule StripeMockWeb.SourceControllerTest do |> get(Routes.customer_source_path(conn, :index, card.customer_id, object: "card")) |> json_response(200) - refute Enum.find(data, &(&1["id"] == card.stripe_id)) + refute Enum.find(data, &(&1["id"] == card.id)) end end end