Hello guys, noobish question here.
I am trying to build a system where users will invite other users to their organizations. So for that purpose i have the following schemas:
Users - self explaning
Organizations - the actual objects.
Organisation Admins - the original admin is added when the Organization object is created. Additional will come with invitation
System Messages - the schema for the invitations.
The workflow i am trying to achieve is the following
The page has a form with single text field and “Invite“ button. I am collecting the email of the user. My “save” event need to perform several checks before the record in the invitation is actually inserted. So i need to check
- Is there an active invitation at the moment? i.e. invitation which is not complete.
- Is there an admin with the same email or user-id already?
- is the user present in the DB? (if not i do display the same message after the email submission but it doesn’t do anything)
- If all of the above are not true - perform the invitation insert in the database.
So i have tried multiple consecutive conditions with “if..else“ and also nested but i cannot make the invitation check to trigger for some reason.
Here is sample of the code:
the liveview save event:
def handle_event("save", %{"organization_admin" => organization_admin_params}, socket) do
%{"email" => email} = organization_admin_params
user_data = UserChecks.get_user_by_email(email)
if (UserChecks.check_invitation_status(socket.assigns.org_id, user_data.id) != nil) do
socket =
socket
|>put_flash(:info, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|> push_navigate(to: ~p"/org/#{socket.assigns.org_id}/org-admins")
{:noreply, socket}
end
user_to_invite = UserCreates.invite_admin_to_org(socket.assigns.org_id, user_data.id)
UserCreates.send_org_admin_invitation(socket.assigns.org_id, user_to_invite.id, socket.assigns.current_scope.user.id)
socket =
socket
|>put_flash(:info, "If the user exists in our database they will receive your invitation.")
|> push_navigate(to: ~p"/org/#{socket.assigns.org_id}/org-admins")
{:noreply, socket}
end
and the database query:
def check_invitation_status(orgid, userid) do
Repo.all_by(SysMessage, recipient: userid)
end
The DB query is just an example. It should list the messages for that particular user and then redirect the user. I have more detailed query which reacts the same.
The question here is what i am missing? I suspect that if..else does not work the way i am understanding it. I have checked the documentation but can’t figure out how to make it work. Also read one of the posts here where NoobZ explains it with consecutive def lines and maybe i can do as well but i need help to understand where i am doing it wrong.
I hope that makes sense.






















