Skip to content

Commit

Permalink
Just use id, avoid stripe_id
Browse files Browse the repository at this point in the history
  • Loading branch information
nkezhaya committed Oct 18, 2019
1 parent ff45d4a commit f6471f6
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 41 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 1 addition & 13 deletions lib/stripe_mock/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions lib/stripe_mock/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
12 changes: 5 additions & 7 deletions lib/stripe_mock_web/controllers/customer_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion lib/stripe_mock_web/view_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions priv/repo/migrations/20191015221147_create_tables.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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: %{})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -46,19 +46,19 @@ 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))

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)
Expand All @@ -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)
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/stripe_mock_web/controllers/source_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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

0 comments on commit f6471f6

Please sign in to comment.