Skip to content

Commit 3edb93a

Browse files
committed
fix: prevent duplicate org creation in onboarding flow
1 parent fd31b24 commit 3edb93a

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/algora/organizations/organizations.ex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@ defmodule Algora.Organizations do
3030
def onboard_organization(params) do
3131
user = Repo.get_by(User, email: params.user.email)
3232

33+
domain = params.user.email |> String.split("@") |> List.last()
34+
3335
org =
3436
case user do
3537
nil ->
36-
nil
38+
Repo.one(from o in User, where: o.domain == ^domain, limit: 1)
3739

3840
user ->
3941
Repo.one(
4042
from o in User,
41-
join: m in assoc(o, :members),
42-
join: u in assoc(m, :user),
43-
where: o.handle in ^generate_unique_org_handle_candidates(params.organization.handle),
44-
where: u.id == ^user.id,
43+
left_join: m in assoc(o, :members),
44+
left_join: u in assoc(m, :user),
45+
where:
46+
o.domain == ^domain or
47+
(o.handle in ^generate_unique_org_handle_candidates(params.organization.handle) and u.id == ^user.id),
4548
limit: 1
4649
)
4750
end

0 commit comments

Comments
 (0)