@@ -23,10 +23,12 @@ defmodule CodeCorps.Task.Service do
2323 :user
2424 ]
2525
26+ @ type result :: { :ok , Task . t } | { :error , Changeset . t } | { :error , :github } | { :error , :task_not_found }
27+
2628 @ doc ~S"""
2729 Performs all actions involved in creating a task on a project
2830 """
29- @ spec create ( map ) :: { :ok , Task . t } | { :error , Changeset . t } | { :error , :github }
31+ @ spec create ( map ) :: result
3032 def create ( % { } = attributes ) do
3133 Multi . new
3234 |> Multi . insert ( :task , % Task { } |> Task . create_changeset ( attributes ) )
@@ -38,22 +40,23 @@ defmodule CodeCorps.Task.Service do
3840 |> marshall_result ( )
3941 end
4042
41- @ spec update ( Task . t , map ) :: { :ok , Task . t } | { :error , Changeset . t } | { :error , :github }
43+ @ spec update ( Task . t , map ) :: result
4244 def update ( % Task { } = task , % { } = attributes ) do
4345 Multi . new
4446 |> Multi . update ( :task , task |> Task . update_changeset ( attributes ) )
4547 |> Multi . run ( :preload , fn % { task: % Task { } = task } ->
4648 { :ok , task |> Repo . preload ( @ preloads ) }
4749 end )
4850 |> Multi . run ( :github , ( fn % { preload: % Task { } = task } -> task |> update_on_github ( ) end ) )
49- |> Repo . transaction
51+ |> Repo . transaction ( )
5052 |> marshall_result ( )
5153 end
5254
53- @ spec marshall_result ( tuple ) :: { :ok , Task . t } | { :error , Changeset . t } | { :error , :github }
55+ @ spec marshall_result ( tuple ) :: result
5456 defp marshall_result ( { :ok , % { github: % Task { } = task } } ) , do: { :ok , task }
5557 defp marshall_result ( { :ok , % { task: % Task { } = task } } ) , do: { :ok , task }
5658 defp marshall_result ( { :error , :task , % Changeset { } = changeset , _steps } ) , do: { :error , changeset }
59+ defp marshall_result ( { :error , :github , { :error , :task_not_found } , _steps } ) , do: { :error , :task_not_found }
5760 defp marshall_result ( { :error , :github , result , _steps } ) do
5861 Logger . info "An error occurred when creating/updating the task with the GitHub API"
5962 Logger . info "#{ inspect result } "
@@ -70,7 +73,7 @@ defmodule CodeCorps.Task.Service do
7073 { :ok , % GithubIssue { } = github_issue } <-
7174 payload
7275 |> Sync.Issue.GithubIssue . create_or_update_issue ( github_repo ) do
73- task |> link_with_github_changeset ( github_issue ) |> Repo . update
76+ task |> link_with_github_changeset ( github_issue ) |> Repo . update ( )
7477 else
7578 { :error , error } -> { :error , error }
7679 end
@@ -81,16 +84,16 @@ defmodule CodeCorps.Task.Service do
8184 task |> Changeset . change ( % { github_issue: github_issue } )
8285 end
8386
84- @ spec update_on_github ( Task . t ) :: { :ok , Task . t } | { :error , Changeset . t } | { :error , GitHub . api_error_struct }
87+ @ spec update_on_github ( Task . t ) :: { :ok , Task . t } | { :error , Changeset . t } | { :error , GitHub . api_error_struct } | { :error , :task_not_found }
8588 defp update_on_github ( % Task { github_repo_id: nil , github_issue_id: nil } = task ) , do: { :ok , task }
8689 defp update_on_github ( % Task { github_repo_id: _ , github_issue_id: nil } = task ) , do: task |> create_on_github ( )
8790 defp update_on_github ( % Task { github_repo: github_repo } = task ) do
8891 with { :ok , payload } <- GitHub.API.Issue . update ( task ) ,
89- { :ok , % GithubIssue { } } <-
90- payload
91- |> Sync.Issue.GithubIssue . create_or_update_issue ( github_repo ) do
92- { :ok , Task |> Repo . get ( task . id ) }
92+ { :ok , % GithubIssue { } } <- payload |> Sync.Issue.GithubIssue . create_or_update_issue ( github_repo ) ,
93+ % Task { } = task <- Repo . get ( Task , task . id ) do
94+ { :ok , task }
9395 else
96+ nil -> { :error , :task_not_found }
9497 { :error , error } -> { :error , error }
9598 end
9699 end
0 commit comments