-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement service for inviting to a team (#4948)
* Implement service for inviting to a team * Test and improve implementation of new service * Extend tests fro RegisterForm
- Loading branch information
Showing
11 changed files
with
497 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
defmodule Plausible.Teams.Invitations.InviteToTeam do | ||
@moduledoc """ | ||
Service for inviting new or existing users to team. | ||
""" | ||
|
||
alias Plausible.Teams | ||
alias Plausible.Repo | ||
|
||
@valid_roles Plausible.Teams.Invitation.roles() -- [:guest] | ||
@valid_roles @valid_roles ++ Enum.map(@valid_roles, &to_string/1) | ||
|
||
def invite(team, inviter, invitee_email, role, opts \\ []) | ||
|
||
def invite(team, inviter, invitee_email, role, opts) when role in @valid_roles do | ||
with team <- Repo.preload(team, [:owner]), | ||
:ok <- | ||
Teams.Invitations.check_invitation_permissions( | ||
team, | ||
inviter, | ||
role, | ||
opts | ||
), | ||
:ok <- | ||
Teams.Invitations.check_team_member_limit( | ||
team, | ||
role, | ||
invitee_email | ||
), | ||
invitee = Plausible.Auth.find_user_by(email: invitee_email), | ||
:ok <- | ||
Teams.Invitations.ensure_new_membership( | ||
team, | ||
invitee, | ||
role | ||
), | ||
{:ok, invitation} <- | ||
Teams.Invitations.invite(team, invitee_email, role, inviter) do | ||
send_invitation_email(invitation, invitee) | ||
|
||
{:ok, invitation} | ||
end | ||
end | ||
|
||
def invite(_team, _inviter, _invitee_email, role, _opts) do | ||
raise "Invalid role passed: #{inspect(role)}" | ||
end | ||
|
||
defp send_invitation_email(invitation, invitee) do | ||
invitation | ||
|> Repo.preload([:team, :inviter]) | ||
|> Teams.Invitations.send_invitation_email(invitee) | ||
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
3 changes: 3 additions & 0 deletions
3
lib/plausible_web/templates/email/existing_user_team_invitation.html.heex
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,3 @@ | ||
<%= @inviter.email %> has invited you to the "<%= @team.name %>" team on <%= Plausible.product_name() %>. | ||
<a href={Routes.site_url(PlausibleWeb.Endpoint, :index)}>Click here</a> to view and respond to the invitation. The invitation | ||
will expire 48 hours after this email is sent. |
10 changes: 10 additions & 0 deletions
10
lib/plausible_web/templates/email/new_user_team_invitation.html.heex
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,10 @@ | ||
<%= @inviter.email %> has invited you to join the "<%= @team.name %>" team on <%= Plausible.product_name() %>. | ||
<a href={ | ||
Routes.auth_url( | ||
PlausibleWeb.Endpoint, | ||
:register_from_invitation_form, | ||
@invitation_id | ||
) | ||
}>Click here</a> to create your account. The link is valid for 48 hours after this email is sent. | ||
<br /><br /> | ||
Plausible is a lightweight and open-source website analytics tool. We hope you like our simple and ethical approach to tracking website visitors. |
Oops, something went wrong.