11defmodule CodeCorpsWeb.OrganizationInviteController do
22 use CodeCorpsWeb , :controller
3- use JaResource
43
5- import CodeCorps.Helpers.Query , only: [ id_filter: 2 ]
6- alias CodeCorps . { Emails , Mailer , OrganizationInvite }
4+ alias CodeCorps . { Emails , Helpers.Query , Mailer , OrganizationInvite , User }
75
8- plug :load_and_authorize_resource , model: OrganizationInvite , only: [ :create , :update ]
9- plug JaResource
6+ action_fallback CodeCorpsWeb.FallbackController
7+ plug CodeCorpsWeb.Plug.DataToAttributes
8+ plug CodeCorpsWeb.Plug.IdsToIntegers
109
11- @ spec model :: module
12- def model , do: CodeCorps.OrganizationInvite
10+ @ spec index ( Conn . t , map ) :: Conn . t
11+ def index ( % Conn { } = conn , % { } = params ) do
12+ with organization_invites <- OrganizationInvite |> Query . id_filter ( params ) |> Repo . all do
13+ conn |> render ( "index.json-api" , data: organization_invites )
14+ end
15+ end
1316
14- def filter ( _conn , query , "id" , id_list ) do
15- query |> id_filter ( id_list )
17+ @ spec show ( Conn . t , map ) :: Conn . t
18+ def show ( % Conn { } = conn , % { "id" => id } ) do
19+ with % OrganizationInvite { } = organization_invite <- OrganizationInvite |> Repo . get ( id ) do
20+ conn |> render ( "show.json-api" , data: organization_invite )
21+ end
1622 end
1723
18- def handle_create ( _conn , attributes ) do
19- OrganizationInvite . create_changeset ( % OrganizationInvite { } , attributes )
24+ @ spec create ( Plug.Conn . t , map ) :: Conn . t
25+ def create ( % Conn { } = conn , % { } = params ) do
26+ with % User { } = current_user <- conn |> Guardian.Plug . current_resource ,
27+ { :ok , :authorized } <- current_user |> Policy . authorize ( :create , % OrganizationInvite { } , params ) ,
28+ { :ok , % OrganizationInvite { } = organization_invite } <- % OrganizationInvite { } |> OrganizationInvite . create_changeset ( params ) |> Repo . insert do
29+
30+ send_email ( organization_invite )
31+
32+ conn
33+ |> put_status ( :created )
34+ |> render ( "show.json-api" , data: organization_invite )
35+ end
2036 end
2137
22- def render_create ( conn , model ) do
23- send_email ( model )
24- conn
25- |> put_status ( :created )
26- |> Phoenix.Controller . render ( :show , data: model )
38+ @ spec update ( Conn . t , map ) :: Conn . t
39+ def update ( % Conn { } = conn , % { "id" => id } = params ) do
40+ with % OrganizationInvite { } = organization_invite <- OrganizationInvite |> Repo . get ( id ) ,
41+ % User { } = current_user <- conn |> Guardian.Plug . current_resource ,
42+ { :ok , :authorized } <- current_user |> Policy . authorize ( :update , organization_invite ) ,
43+ { :ok , % OrganizationInvite { } = organization_invite } <- organization_invite |> OrganizationInvite . changeset ( params ) |> Repo . update do
44+ conn |> render ( "show.json-api" , data: organization_invite )
45+ end
2746 end
2847
2948 defp send_email ( organization_invite ) do
3049 organization_invite
3150 |> Emails.OrganizationInviteEmail . create ( )
3251 |> Mailer . deliver_later ( )
3352 end
34- end
53+ end
0 commit comments