Created
August 11, 2019 15:52
-
-
Save Harrisonl/dfb14038fdb04c360274970a5135a252 to your computer and use it in GitHub Desktop.
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
def register_user(%{organisation_id: org_id, account_type: type}} = user_params) do | |
case Enum.all?(["name", "email", "password", "organisation_id"], fn(field) -> user_params[field] end) do | |
true -> | |
case Repo.get(Organisation, org_id) do | |
nil -> {:error, :not_found} | |
org -> | |
valid = | |
case type do | |
"basic" -> org.used + 1 > org.capacity | |
"pro" -> org.used + 2 > org.capacity | |
"elite" -> | |
if org.grandfathered do | |
org.used + 1 > org.capacity | |
else | |
org.used + 3 > org.capacity | |
end | |
end | |
if valid do | |
%User | |
|> User.changeset(user_params, organisation) | |
|> Repo.insert() | |
|> case do | |
{:ok, %User{} = user} -> | |
case EmailLibary.send("Welcome to OurCompany", user.to, "hello@xyz.com") do | |
:ok -> {:ok, user} | |
error -> error | |
end | |
error -> error | |
end | |
else | |
{:error, :over_capacity} | |
end | |
end | |
false -> :error | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment