Class: AgentCode::InvitationsController
- Inherits:
-
ActionController::API
- Object
- ActionController::API
- AgentCode::InvitationsController
- Includes:
- Pundit::Authorization
- Defined in:
- lib/agentcode/controllers/invitations_controller.rb
Overview
Invitation management controller — mirrors Laravel InvitationController exactly.
Endpoints: GET /api/org/invitations POST /api/org/invitations POST /api/org/invitations/:id/resend DELETE /api/org/invitations/:id POST /api/invitations/accept (public)
Instance Method Summary collapse
-
#accept ⇒ Object
POST /api/invitations/accept (public route).
-
#cancel ⇒ Object
DELETE /api/org/invitations/:id.
-
#create ⇒ Object
POST /api/org/invitations.
-
#index ⇒ Object
GET /api/org/invitations.
-
#resend ⇒ Object
POST /api/org/invitations/:id/resend.
Instance Method Details
#accept ⇒ Object
POST /api/invitations/accept (public route)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/agentcode/controllers/invitations_controller.rb', line 135 def accept if params[:token].blank? return render json: { errors: { token: ["The token field is required."] } }, status: :unprocessable_entity end invitation = OrganizationInvitation.find_by(token: params[:token], status: "pending") unless invitation return render json: { message: "Invalid or expired invitation token" }, status: :not_found end if invitation.expired? invitation.update!(status: "expired") return render json: { message: "This invitation has expired" }, status: :unprocessable_entity end # Check if user is authenticated user = resolve_current_user unless user return render json: { invitation: invitation.as_json(include: { organization: {}, role: {} }), requires_registration: true, message: "Please register or login to accept this invitation" }, status: :ok end # User is authenticated, accept invitation if invitation.accept!(user) render json: { message: "Invitation accepted successfully", invitation: invitation.as_json(include: { organization: {}, role: {} }), organization: invitation.organization }, status: :ok else render json: { message: "Failed to accept invitation" }, status: :internal_server_error end end |
#cancel ⇒ Object
DELETE /api/org/invitations/:id
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/agentcode/controllers/invitations_controller.rb', line 118 def cancel invitation = OrganizationInvitation .where(id: params[:id], organization_id: current_organization.id) .first! invitation, :destroy?, policy_class: InvitationPolicy unless invitation.status == "pending" return render json: { message: "Only pending invitations can be cancelled" }, status: :unprocessable_entity end invitation.update!(status: "cancelled") render json: { message: "Invitation cancelled successfully" } end |
#create ⇒ Object
POST /api/org/invitations
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/agentcode/controllers/invitations_controller.rb', line 43 def create OrganizationInvitation, :create?, policy_class: InvitationPolicy errors = {} errors[:email] = ["The email field is required."] if params[:email].blank? errors[:role_id] = ["The role_id field is required."] if params[:role_id].blank? unless errors.empty? return render json: { errors: errors }, status: :unprocessable_entity end email = params[:email].to_s.strip role_id = params[:role_id] # Check if user already exists and is in organization user_class = "User".safe_constantize if user_class existing_user = user_class.find_by(email: email) if existing_user&.respond_to?(:organizations) if existing_user.organizations.exists?(id: current_organization.id) return render json: { message: "User is already a member of this organization" }, status: :unprocessable_entity end end end # Check for existing pending invitation existing_invitation = OrganizationInvitation .where(email: email, organization_id: current_organization.id, status: "pending") .where("expires_at IS NULL OR expires_at > ?", Time.current) .first if existing_invitation return render json: { message: "A pending invitation already exists for this email" }, status: :unprocessable_entity end # Create invitation invitation = OrganizationInvitation.create!( organization_id: current_organization.id, email: email, role_id: role_id, invited_by: current_user.id ) # Send notification email send_invitation_email(invitation) render json: invitation.as_json(include: { organization: {}, role: {}, inviter: {} }), status: :created end |
#index ⇒ Object
GET /api/org/invitations
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/agentcode/controllers/invitations_controller.rb', line 19 def index OrganizationInvitation, :index?, policy_class: InvitationPolicy status = params[:status] || "all" query = OrganizationInvitation .where(organization_id: current_organization.id) .includes(:organization, :role, :inviter) case status when "pending" query = query.pending when "expired" query = query.expired when "all" # no filter else query = query.where(status: status) end render json: query.order(created_at: :desc) end |
#resend ⇒ Object
POST /api/org/invitations/:id/resend
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/agentcode/controllers/invitations_controller.rb', line 93 def resend invitation = OrganizationInvitation .where(id: params[:id], organization_id: current_organization.id) .first! invitation, :update?, policy_class: InvitationPolicy unless invitation.status == "pending" return render json: { message: "Only pending invitations can be resent" }, status: :unprocessable_entity end # Update expiration days = AgentCode.config.invitations[:expires_days] || 7 invitation.update!(expires_at: days.days.from_now) # Resend notification email send_invitation_email(invitation) render json: { message: "Invitation resent successfully", invitation: invitation.as_json(include: { organization: {}, role: {}, inviter: {} }) } end |