@@ -10,128 +10,72 @@ defmodule AlgoraWeb.InstallationCallbackController do
1010
1111 def new ( conn , params ) do
1212 case validate_query_params ( params ) do
13- { :ok , % { setup_action: " install" , installation_id: installation_id } } ->
14- handle_installation ( conn , installation_id )
13+ { :ok , % { setup_action: : install, installation_id: installation_id } } ->
14+ handle_installation ( conn , :install , installation_id )
1515
16- # TODO: Implement update
17- { :ok , % { setup_action: "update" } } ->
18- redirect ( conn , to: "/user/installations" )
16+ { :ok , % { setup_action: :update , installation_id: installation_id } } ->
17+ handle_installation ( conn , :update , installation_id )
18+
19+ # TODO: Implement request
20+ { :ok , % { setup_action: :request } } ->
21+ conn
22+ |> put_flash (
23+ :info ,
24+ "Installation request submitted! The Algora app will be activated upon approval from your organization administrator."
25+ )
26+ |> redirect ( to: redirect_url ( conn ) )
1927
2028 { :error , _reason } ->
21- redirect ( conn , to: "/user/installations" )
29+ redirect ( conn , to: redirect_url ( conn ) )
2230 end
2331 end
2432
2533 defp validate_query_params ( params ) do
2634 case params do
2735 % { "setup_action" => "install" , "installation_id" => installation_id } ->
28- { :ok , % { setup_action: " install" , installation_id: String . to_integer ( installation_id ) } }
36+ { :ok , % { setup_action: : install, installation_id: String . to_integer ( installation_id ) } }
2937
3038 % { "setup_action" => "update" , "installation_id" => installation_id } ->
31- { :ok , % { setup_action: " update" , installation_id: String . to_integer ( installation_id ) } }
39+ { :ok , % { setup_action: : update, installation_id: String . to_integer ( installation_id ) } }
3240
3341 % { "setup_action" => "request" } ->
34- { :ok , % { setup_action: " request" } }
42+ { :ok , % { setup_action: : request} }
3543
3644 _ ->
3745 { :error , :invalid_params }
3846 end
3947 end
4048
41- defp handle_installation ( conn , installation_id ) do
49+ defp handle_installation ( conn , setup_action , installation_id ) do
4250 user = conn . assigns . current_user
4351
44- case do_handle_installation ( conn , user , installation_id ) do
45- { :ok , org } ->
46- # TODO: Trigger org joined event
47- # trigger_org_joined(org)
48-
49- put_flash ( conn , :info , "Organization created successfully: #{ org . handle } " )
50-
51- # TODO: Redirect to the org dashboard and set the session context
52- redirect_url = determine_redirect_url ( conn , org , user )
53- redirect ( conn , to: redirect_url )
52+ case do_handle_installation ( user , installation_id ) do
53+ { :ok , _org } ->
54+ conn
55+ |> put_flash ( :info , if ( setup_action == :install , do: "Installation successful!" , else: "Installation updated!" ) )
56+ |> redirect ( to: redirect_url ( conn ) )
5457
5558 { :error , error } ->
5659 Logger . error ( "❌ Installation callback failed: #{ inspect ( error ) } " )
5760
58- put_flash ( conn , :error , "#{ inspect ( error ) } " )
59- redirect ( conn , to: "/user/installations" )
61+ conn
62+ |> put_flash ( :error , "#{ inspect ( error ) } " )
63+ |> redirect ( to: redirect_url ( conn ) )
6064 end
6165 end
6266
63- defp do_handle_installation ( conn , user , installation_id ) do
67+ defp do_handle_installation ( user , installation_id ) do
68+ # TODO: replace :last_context with a new :last_installation_target field
69+ # TODO: handle nil user
70+ # TODO: handle nil last_context
6471 with { :ok , access_token } <- Accounts . get_access_token ( user ) ,
6572 { :ok , installation } <- Github . find_installation ( access_token , installation_id ) ,
66- { :ok , github_handle } <- extract_github_handle ( installation ) ,
67- { :ok , account } <- Github . get_user_by_username ( access_token , github_handle ) ,
68- { :ok , org } <- upsert_org ( conn , user , installation , account ) ,
69- { :ok , _ } <- upsert_installation ( user , org , installation ) do
70- { :ok , org }
71- end
72- end
73-
74- defp extract_github_handle ( % { "account" => % { "login" => login } } ) , do: { :ok , login }
75- defp extract_github_handle ( _ ) , do: { :error , 404 }
76-
77- defp upsert_installation ( user , org , installation ) do
78- case Workspace . get_installation_by_provider_id ( "github" , installation [ "id" ] ) do
79- nil ->
80- Workspace . create_installation ( :github , user , org , installation )
81-
82- existing_installation ->
83- Workspace . update_installation ( :github , user , org , existing_installation , installation )
84- end
85- end
86-
87- defp upsert_org ( conn , user , installation , account ) do
88- attrs = % {
89- provider: "github" ,
90- provider_id: account [ "id" ] ,
91- provider_login: account [ "login" ] ,
92- provider_meta: account ,
93- handle: account [ "login" ] ,
94- name: account [ "name" ] ,
95- description: account [ "bio" ] ,
96- website_url: account [ "blog" ] ,
97- twitter_url: get_twitter_url ( account ) ,
98- avatar_url: account [ "avatar_url" ] ,
99- # TODO:
100- active: true ,
101- featured: account [ "type" ] != "User" ,
102- github_handle: account [ "login" ]
103- }
104-
105- case Organizations . get_org_by_handle ( account [ "login" ] ) do
106- nil -> create_org ( conn , user , attrs , installation )
107- existing_org -> update_org ( conn , user , existing_org , attrs , installation )
108- end
109- end
110-
111- # TODO: handle conflicting handles
112- defp create_org ( _conn , user , attrs , _installation ) do
113- # TODO: trigger org joined event
114- # trigger_org_joined(org)
115- with { :ok , org } <- Organizations . create_organization ( attrs ) ,
116- { :ok , _ } <- Organizations . create_member ( org , user , :admin ) do
73+ { :ok , provider_user } <- Workspace . ensure_user ( access_token , installation [ "account" ] [ "login" ] ) ,
74+ { :ok , org } <- Organizations . fetch_org_by ( handle: user . last_context ) ,
75+ { :ok , _ } <- Workspace . upsert_installation ( installation , user , org , provider_user ) do
11776 { :ok , org }
11877 end
11978 end
12079
121- defp update_org ( _conn , _user , existing_org , attrs , _installation ) do
122- with { :ok , _ } <- Organizations . update_organization ( existing_org , attrs ) do
123- { :ok , existing_org }
124- end
125- end
126-
127- defp determine_redirect_url ( _conn , _org , _user ) do
128- # TODO: Implement
129- "/user/installations"
130- end
131-
132- defp get_twitter_url ( % { twitter_username: username } ) when is_binary ( username ) do
133- "https://twitter.com/#{ username } "
134- end
135-
136- defp get_twitter_url ( _ ) , do: nil
80+ defp redirect_url ( conn ) , do: ~p" /org/#{ conn . assigns . current_user . last_context } /settings"
13781end
0 commit comments