Skip to content

Commit 2107f97

Browse files
committed
fix: update teammate onboarding to preserve existing org details
1 parent 7ef2706 commit 2107f97

File tree

3 files changed

+70
-29
lines changed

3 files changed

+70
-29
lines changed

lib/algora/organizations/organizations.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,15 @@ defmodule Algora.Organizations do
8282
|> Repo.insert()
8383

8484
existing_org ->
85+
updated_params =
86+
params.organization
87+
|> Map.take([:hiring, :categories, :tech_stack])
88+
|> Map.update(:hiring, existing_org.hiring, &(existing_org.hiring || &1))
89+
|> Map.update(:categories, existing_org.categories, &Enum.uniq(existing_org.categories ++ &1))
90+
|> Map.update(:tech_stack, existing_org.tech_stack, &Enum.uniq(existing_org.tech_stack ++ &1))
91+
8592
existing_org
86-
|> Org.changeset(Map.delete(params.organization, :handle))
93+
|> Org.changeset(updated_params)
8794
|> Repo.update()
8895
end
8996

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule Algora.Repo.Migrations.UpdateUserCategories do
2+
use Ecto.Migration
3+
4+
def change do
5+
execute "UPDATE users SET categories = '{}' WHERE categories IS NULL"
6+
7+
alter table(:users) do
8+
modify :categories, {:array, :string}, null: false, default: "{}"
9+
end
10+
end
11+
end

test/algora/organizations_test.exs

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,57 @@ defmodule Algora.OrganizationsTest do
8787
assert user.avatar_url == "https://avatars.githubusercontent.com/u/17045339?v=4"
8888
end
8989

90-
test "onboard updates existing records when params change" do
91-
assert {:ok, first_result} = Algora.Organizations.onboard_organization(@params)
92-
93-
updated_params =
94-
@params
95-
|> put_in([:user, :display_name], "Updated User")
96-
|> put_in([:user, :tech_stack], ["Haskell"])
97-
|> put_in([:organization, :display_name], "Updated Org")
98-
|> put_in([:organization, :tech_stack], ["Rust"])
99-
|> put_in([:organization, :categories], ["nonprofit"])
100-
|> put_in([:organization, :hiring], false)
101-
|> put_in([:member, :role], :mod)
102-
103-
assert {:ok, second_result} = Algora.Organizations.onboard_organization(updated_params)
104-
105-
assert first_result.user.last_context == first_result.org.handle
106-
assert first_result.user.id == second_result.user.id
107-
assert first_result.org.id == second_result.org.id
108-
assert first_result.member.id == second_result.member.id
109-
110-
assert second_result.user.last_context == second_result.org.handle
111-
assert second_result.user.display_name == "Updated User"
112-
assert second_result.user.tech_stack == ["Haskell"]
113-
assert second_result.org.display_name == "Updated Org"
114-
assert second_result.org.tech_stack == ["Rust"]
115-
assert second_result.org.categories == ["nonprofit"]
116-
assert second_result.org.hiring == false
117-
assert second_result.member.role == :mod
90+
test "onboard preserves original org details when subsequent teammates join unless autogenerated" do
91+
assert {:ok, result0} =
92+
@params_crawler
93+
|> put_in([:organization, :hiring], nil)
94+
|> put_in([:organization, :categories], [])
95+
|> put_in([:organization, :tech_stack], [])
96+
|> put_in([:organization, :og_title], "Title 0")
97+
|> put_in([:user, :email], "[email protected]")
98+
|> Algora.Organizations.onboard_organization()
99+
100+
assert {:ok, result1} =
101+
@params_crawler
102+
|> put_in([:organization, :hiring], false)
103+
|> put_in([:organization, :categories], ["open_source"])
104+
|> put_in([:organization, :tech_stack], ["Elixir"])
105+
|> put_in([:organization, :og_title], "Title 1")
106+
|> put_in([:user, :email], "[email protected]")
107+
|> Algora.Organizations.onboard_organization()
108+
109+
assert {:ok, result2} =
110+
@params_crawler
111+
|> put_in([:organization, :hiring], true)
112+
|> put_in([:organization, :categories], ["agency"])
113+
|> put_in([:organization, :tech_stack], ["Phoenix"])
114+
|> put_in([:organization, :og_title], "Title 2")
115+
|> put_in([:user, :email], "[email protected]")
116+
|> Algora.Organizations.onboard_organization()
117+
118+
assert {:ok, result3} =
119+
@params_crawler
120+
|> put_in([:organization, :hiring], false)
121+
|> put_in([:organization, :categories], ["nonprofit"])
122+
|> put_in([:organization, :tech_stack], ["PostgreSQL"])
123+
|> put_in([:organization, :og_title], "Title 3")
124+
|> put_in([:user, :email], "[email protected]")
125+
|> Algora.Organizations.onboard_organization()
126+
127+
assert Enum.all?([result0, result1, result2, result3], &(&1.org.id == result0.org.id))
128+
assert Enum.all?([result0, result1, result2, result3], &(&1.org.og_title == "Title 0"))
129+
assert is_nil(result0.org.hiring)
130+
assert result1.org.hiring == false
131+
assert result2.org.hiring == true
132+
assert result3.org.hiring == true
133+
assert result0.org.categories == []
134+
assert result1.org.categories == ["open_source"]
135+
assert result2.org.categories == ["open_source", "agency"]
136+
assert result3.org.categories == ["open_source", "agency", "nonprofit"]
137+
assert result0.org.tech_stack == []
138+
assert result1.org.tech_stack == ["Elixir"]
139+
assert result2.org.tech_stack == ["Elixir", "Phoenix"]
140+
assert result3.org.tech_stack == ["Elixir", "Phoenix", "PostgreSQL"]
118141
end
119142

120143
test "onboard handles user handle collision by generating alternative handles" do

0 commit comments

Comments
 (0)