Skip to content

Commit

Permalink
Associate existing comments during user github connection process
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Sep 19, 2017
1 parent f442ba4 commit c6a504d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
24 changes: 21 additions & 3 deletions lib/code_corps/github/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule CodeCorps.GitHub.User do
Used to perform user actions on the github API
"""

alias CodeCorps.{GitHub, GithubAppInstallation, Repo, Task, User}
alias CodeCorps.{Comment, GitHub, GithubAppInstallation, Repo, Task, User}
alias CodeCorps.GitHub.Adapters.User, as: UserAdapter
alias Ecto.{Changeset, Multi}

Expand All @@ -18,8 +18,8 @@ defmodule CodeCorps.GitHub.User do
Also associates any orphaned `GithubAppInstallation` records matching their
`sender_github_id` field with the user's `github_id`
Also associates any prexisting tasks connected to a premade user with the same
user id, to the newly connected user.
Also associates any existing tasks and comments to the newly connected user,
based on the user's `github_id`
Returns one of the following:
Expand Down Expand Up @@ -54,6 +54,7 @@ defmodule CodeCorps.GitHub.User do
|> Multi.update(:user, changeset)
|> Multi.run(:installations, fn %{user: %User{} = user} -> user |> associate_installations() end)
|> Multi.run(:tasks, fn %{user: %User{} = user} -> user |> associate_tasks() end)
|> Multi.run(:comments, fn %{user: %User{} = user} -> user |> associate_comments() end)

case Repo.transaction(multi) do
{:ok, %{user: %User{} = user, installations: installations}} ->
Expand Down Expand Up @@ -93,6 +94,23 @@ defmodule CodeCorps.GitHub.User do
|> (fn {_count, tasks} -> {:ok, tasks} end).()
end

@spec associate_comments(User.t) :: {:ok, list(Comment.t)}
defp associate_comments(%User{id: user_id, github_id: github_id}) do
updates = [set: [user_id: user_id]]
update_options = [returning: true]

existing_user_ids =
User
|> where(github_id: ^github_id)
|> select([u], u.id)
|> Repo.all

Comment
|> where([c], c.user_id in ^existing_user_ids)
|> Repo.update_all(updates, update_options)
|> (fn {_count, comments} -> {:ok, comments} end).()
end

@doc ~S"""
Requests the currently authenticated user payload from github
"""
Expand Down
21 changes: 20 additions & 1 deletion test/lib/code_corps/github/user_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CodeCorps.GitHub.UserTest do
use CodeCorps.DbAccessCase
import CodeCorps.GitHub.TestHelpers

alias CodeCorps.{GitHub, GithubAppInstallation, Task, User}
alias CodeCorps.{Comment, GitHub, GithubAppInstallation, Task, User}

describe "connect/2" do
test "posts to github, returns updated user" do
Expand Down Expand Up @@ -66,6 +66,25 @@ defmodule CodeCorps.GitHub.UserTest do
refute Repo.get(Task, task_2.id).user_id == returned_user.id
end

test "posts to github, associates user and comments" do
user = insert(:user)
%{"id" => github_id} = load_endpoint_fixture("user")
premade_user = insert(:user, github_id: github_id)

# 2 test comments
# this one should associate,
# because the associated user has the same github id
comment_1 = insert(:comment, user: premade_user)
# this one should not associate, because the associated user has a
# different (or no) github id
comment_2 = insert(:comment)

{:ok, %User{} = returned_user} = GitHub.User.connect(user, "foo_code", "foo_state")

assert Repo.get(Comment, comment_1.id).user_id == returned_user.id
refute Repo.get(Comment, comment_2.id).user_id == returned_user.id
end

defmodule NotFoundRequest do
@moduledoc false

Expand Down

0 comments on commit c6a504d

Please sign in to comment.