-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: views for accepting or declining team invitations
- Loading branch information
Showing
17 changed files
with
173 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class Users::InvitationsController < ApplicationController | ||
skip_before_action :authenticate_user!, only: [:show] | ||
|
||
def show | ||
@invitation = AccountInvitation.find_puid!(params[:id]) | ||
|
||
if @invitation.accepted_at? | ||
flash[:alert] = "Invitation has already been accepted by you or someone else." | ||
return redirect_to dashboard_path | ||
elsif current_user&.accounts&.include? @invitation.account | ||
flash[:notice] = "You are already a member of this team." | ||
return redirect_to dashboard_path(account_id: @invitation.account) | ||
end | ||
|
||
@team = @invitation.account | ||
session[:after_sign_in_path] = user_invitation_path(@invitation) | ||
end | ||
|
||
def update | ||
return redirect_back(fallback_location: dashboard_path) unless params[:user_invitation][:status] == "accepted" | ||
return head :forbidden if AccountInvitation.find_puid!(params[:id]).accepted_at? | ||
|
||
@invitation = AccountInvitation.find_puid!(params[:id]) | ||
@account = @invitation.account | ||
@account.account_users.new(user: current_user) | ||
|
||
if @account.save | ||
@invitation.update(accepted_at: Time.now) | ||
flash[:notice] = "You have successfully joined the team" | ||
redirect_to dashboard_path(account_id: @account) | ||
else | ||
redirect_to user_invitation_path(@invitation), alert: "Failed to accept invitation. Please try again later" | ||
end | ||
end | ||
|
||
def destroy | ||
@invitation = AccountInvitation.find_puid!(params[:id]) | ||
@invitation.destroy | ||
|
||
flash[:notice] = "You have declined the invitation" | ||
redirect_to dashboard_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<% title "You've been invited to join #{@team.name}" %> | ||
|
||
<%= fullscreen do |screen| %> | ||
<%= screen.with_form( | ||
title: t(".title"), | ||
description: t(".description", team_name: @team.name) | ||
) do %> | ||
<main class="flex flex-col gap-8 w-full max-w-xl"> | ||
|
||
<% if user_signed_in? && onboarded? %> | ||
<p><%= t(".instructions", team_name: @team.name) %></p> | ||
<div class="flex justify-center gap-x-4"> | ||
<%= button_to t(".buttons.accept"), "#", class: "button", method: :patch, params: { | ||
user_invitation: { status: "accepted" } | ||
} %> | ||
<%= button_to t(".buttons.decline"), "#", class: "button danger", method: :patch, params: { | ||
user_invitation: { status: "declined" } | ||
} %> | ||
</div> | ||
<% else %> | ||
<p><%= t(".guest_instructions", team_name: @team.name) %></p> | ||
<div class="flex justify-center items-center gap-x-4"> | ||
<%= link_to "Sign in", new_user_session_path, class: "button" %> | ||
or | ||
<%= link_to "Create account", new_user_session_path, class: "button alt" %> | ||
</div> | ||
<% end %> | ||
</main> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
db/migrate/20240408105555_add_unique_index_to_account_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddUniqueIndexToAccountUsers < ActiveRecord::Migration[7.1] | ||
def change | ||
add_index :account_users, [:account_id, :user_id], unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require "test_helper" | ||
|
||
class Users::InvitationsControllerTest < ActionDispatch::IntegrationTest | ||
test "can view page without beeing signed in" do | ||
invitation = create(:account_invitation) | ||
get user_invitation_path(invitation) | ||
assert_response :success | ||
end | ||
|
||
test "can accept invitation" do | ||
user = create(:user) | ||
invitation = create(:account_invitation) | ||
sign_in user | ||
|
||
assert_difference -> { user.accounts.count } do | ||
patch user_invitation_path(invitation), params: {user_invitation: {status: "accepted"}} | ||
end | ||
|
||
assert_redirected_to dashboard_path(account_id: invitation.account) | ||
assert_not_nil invitation.reload.accepted_at | ||
end | ||
|
||
test "cant be accepted twice" do | ||
user = create(:user) | ||
invitation = create(:account_invitation, accepted_at: Time.now) | ||
sign_in user | ||
|
||
get user_invitation_path(invitation) | ||
assert_redirected_to dashboard_path | ||
|
||
assert_no_difference -> { user.accounts.count } do | ||
patch user_invitation_path(invitation), params: {user_invitation: {status: "accepted"}} | ||
end | ||
|
||
assert_response :forbidden | ||
end | ||
|
||
test "can decline invitation" do | ||
user = create(:user) | ||
invitation = create(:account_invitation) | ||
sign_in user | ||
|
||
assert_no_difference -> { user.accounts.count } do | ||
delete user_invitation_path(invitation) | ||
end | ||
|
||
assert_redirected_to dashboard_path | ||
assert_nil AccountInvitation.find_by(id: invitation.id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
FactoryBot.define do | ||
factory :account_invitation do | ||
account { nil } | ||
account { create(:account) } | ||
email { Faker::Internet.email } | ||
end | ||
end |