Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias user on create #1357

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Alias user on create
  • Loading branch information
joshsmith committed Dec 30, 2017
commit 84e97f4a61f52bc14d68c8ac2bae2c6767c40aa2
4 changes: 4 additions & 0 deletions lib/code_corps/analytics/segment_api.ex
Original file line number Diff line number Diff line change
@@ -3,6 +3,10 @@ defmodule CodeCorps.Analytics.SegmentAPI do
Interface to the Segment API through the [`analytics-elixir` package](https://github.com/stueccles/analytics-elixir).
"""

def alias(user_id, previous_id) do
Segment.Analytics.alias(user_id, previous_id)
end

def identify(user_id, traits) do
Segment.Analytics.identify(user_id, traits)
end
8 changes: 8 additions & 0 deletions lib/code_corps/analytics/segment_tracker.ex
Original file line number Diff line number Diff line change
@@ -10,6 +10,14 @@ defmodule CodeCorps.Analytics.SegmentTracker do

@api Application.get_env(:code_corps, :analytics)

@doc """
Calls `alias` in the configured API module.
"""
@spec alias(String.t, String.t) :: any
def alias(user_id, previous_id) do
@api.alias(user_id, previous_id)
end

@doc """
Calls `identify` in the configured API module.
"""
5 changes: 5 additions & 0 deletions lib/code_corps/analytics/test_api.ex
Original file line number Diff line number Diff line change
@@ -6,6 +6,11 @@ defmodule CodeCorps.Analytics.TestAPI do
Each function should have the same signature as `CodeCorps.Analytics.SegmentAPI` and simply return `nil`.
"""

def alias(user_id, previous_id) do
send self(), {:alias, user_id, previous_id}
nil
end

def identify(user_id, traits) do
send self(), {:identify, user_id, traits}
nil
7 changes: 7 additions & 0 deletions lib/code_corps_web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ defmodule CodeCorpsWeb.UserController do
with {:ok, %User{} = user} <- %User{} |> User.registration_changeset(params) |> Repo.insert(),
user <- preload(user)
do
maybe_alias(user, params)
conn |> put_status(:created) |> render("show.json-api", data: user)
end
end
@@ -91,4 +92,10 @@ defmodule CodeCorpsWeb.UserController do
def preload(data) do
Repo.preload(data, @preloads)
end

@spec maybe_alias(User.t, map) :: any
defp maybe_alias(%User{id: id}, %{"previous_id" => previous_id}) do
Analytics.SegmentTracker.alias(id, previous_id)
end
defp maybe_alias(_user, _params), do: nil
end
14 changes: 8 additions & 6 deletions test/lib/code_corps_web/controllers/user_controller_test.exs
Original file line number Diff line number Diff line change
@@ -8,20 +8,21 @@ defmodule CodeCorpsWeb.UserControllerTest do
alias CodeCorps.{User, Repo}

@valid_attrs %{
biography: "Just a test user",
email: "test@user.com",
username: "testuser",
first_name: "Test",
last_name: "User",
website: "http://www.example.com",
previous_id: "abc123",
twitter: "testuser",
biography: "Just a test user"
username: "testuser",
website: "http://www.example.com"
}

@invalid_attrs %{
email: "",
twitter: " @ testuser",
username: "",
website: "---_<>-blank.com",
twitter: " @ testuser"
website: "---_<>-blank.com"
}

@relationships %{}
@@ -134,6 +135,8 @@ defmodule CodeCorpsWeb.UserControllerTest do
}
}
id = json_response(conn, 201)["data"]["id"] |> String.to_integer
previous_id = @valid_attrs.previous_id
assert_received {:alias, ^id, ^previous_id}
assert_received {:track, ^id, "Signed Up", %{}}
end

@@ -192,7 +195,6 @@ defmodule CodeCorpsWeb.UserControllerTest do
|> put(path, params)

id = json_response(conn, 200)["data"]["id"] |> String.to_integer
assert_received {:identify, ^id, %{email: "original@mail.com"}}
assert_received {:track, ^id, "Updated Profile", %{}}
end