Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci-waydowntown-full-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
api-level: 24
arch: x86_64
profile: Nexus 6
script: flutter test integration_test/token_refresh_test.dart integration_test/string_collector_test.dart integration_test/fill_in_the_blank_test.dart --flavor local --dart-define=API_BASE_URL=http://10.0.2.2:4001 --timeout none --file-reporter json:test-results.json
script: flutter test integration_test/token_refresh_test.dart integration_test/string_collector_test.dart integration_test/fill_in_the_blank_test.dart integration_test/team_game_test.dart --flavor local --dart-define=API_BASE_URL=http://10.0.2.2:4001 --timeout none --file-reporter json:test-results.json

- name: Publish test results
uses: EnricoMi/publish-unit-test-result-action@v2
Expand Down
8 changes: 7 additions & 1 deletion registrations/lib/registrations/waydowntown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ defmodule Registrations.Waydowntown do
|> Repo.update()
end

def update_user_details(user, attrs) do
user
|> User.details_changeset(attrs)
|> Repo.update()
end

defp concepts_yaml do
ConCache.get_or_store(:registrations_cache, :concepts_yaml, fn ->
YamlElixir.read_from_file!(Path.join(:code.priv_dir(:registrations), "concepts.yaml"))
Expand Down Expand Up @@ -366,7 +372,7 @@ defmodule Registrations.Waydowntown do
run = get_run!(run_id)

case result do
{:ok, _} when not is_nil(run.winner_submission_id) ->
{:ok, _} ->
broadcast_run_update(run, conn)

_ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ defmodule RegistrationsWeb.ApiUserController do

action_fallback(RegistrationsWeb.FallbackController)

@team_fields ~w(team_emails proposed_team_name risk_aversion)

def update(conn, params) do
user = Pow.Plug.current_user(conn)

case Waydowntown.update_user(user, params) do
result =
if has_team_fields?(params) do
Waydowntown.update_user_details(user, params)
else
Waydowntown.update_user(user, params)
end

case result do
{:ok, updated_user} ->
conn
|> put_view(UserView)
Expand All @@ -31,4 +40,8 @@ defmodule RegistrationsWeb.ApiUserController do
})
end
end

defp has_team_fields?(params) do
Enum.any?(@team_fields, fn field -> Map.has_key?(params, field) end)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule RegistrationsWeb.TeamNegotiationController do
use RegistrationsWeb, :controller

alias Registrations.Repo
alias RegistrationsWeb.TeamFinder
alias RegistrationsWeb.User
alias RegistrationsWeb.JSONAPI.TeamNegotiationView

action_fallback(RegistrationsWeb.FallbackController)

def show(conn, _params) do
current_user = Pow.Plug.current_user(conn)
users = Repo.all(User)
current_user_with_team = Repo.preload(current_user, team: [:users])
relationships = TeamFinder.relationships(current_user, users)

conn
|> put_view(TeamNegotiationView)
|> render("show.json",
data: current_user_with_team,
relationships: relationships,
conn: conn
)
end
end
56 changes: 46 additions & 10 deletions registrations/lib/registrations_web/controllers/test_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ defmodule RegistrationsWeb.TestController do
game_data = create_orientation_memory_game()
Map.merge(base_response, game_data)

"string_collector_team" ->
game_data = create_string_collector_team_game()
Map.merge(base_response, game_data)

_ ->
base_response
end
Expand Down Expand Up @@ -129,31 +133,63 @@ defmodule RegistrationsWeb.TestController do
}
end

defp create_string_collector_team_game do
region = Repo.insert!(%Region{name: "Test Region"})

specification =
Repo.insert!(%Specification{
concept: "string_collector",
task_description: "Find all the hidden words",
start_description: "Look around for words",
region: region,
duration: 300
})

answer1 = Repo.insert!(%Answer{answer: "apple", specification_id: specification.id})
answer2 = Repo.insert!(%Answer{answer: "banana", specification_id: specification.id})
answer3 = Repo.insert!(%Answer{answer: "cherry", specification_id: specification.id})

# Create second test user
user2 = create_or_reset_user("test2@example.com", @test_password, "Test User 2")

%{
specification_id: specification.id,
correct_answers: ["apple", "banana", "cherry"],
total_answers: 3,
answer_ids: [answer1.id, answer2.id, answer3.id],
user2_email: "test2@example.com",
user2_password: @test_password
}
end

defp create_or_reset_test_user do
case Repo.get_by(RegistrationsWeb.User, email: @test_email) do
create_or_reset_user(@test_email, @test_password, "Test User")
end

defp create_or_reset_user(email, password, name) do
case Repo.get_by(RegistrationsWeb.User, email: email) do
nil ->
%RegistrationsWeb.User{}
|> RegistrationsWeb.User.changeset(%{
email: @test_email,
password: @test_password,
password_confirmation: @test_password
email: email,
password: password,
password_confirmation: password
})
|> Repo.insert!()
|> Ecto.Changeset.change(%{name: "Test User"})
|> Ecto.Changeset.change(%{name: name})
|> Repo.update!()

existing_user ->
# Delete and recreate to ensure clean state
Repo.delete!(existing_user)

%RegistrationsWeb.User{}
|> RegistrationsWeb.User.changeset(%{
email: @test_email,
password: @test_password,
password_confirmation: @test_password
email: email,
password: password,
password_confirmation: password
})
|> Repo.insert!()
|> Ecto.Changeset.change(%{name: "Test User"})
|> Ecto.Changeset.change(%{name: name})
|> Repo.update!()
end
end
Expand Down
2 changes: 2 additions & 0 deletions registrations/lib/registrations_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ defmodule RegistrationsWeb.Router do
scope "/waydowntown", RegistrationsWeb do
pipe_through([:pow_json_api_protected])

get("/team-negotiation", TeamNegotiationController, :show)

resources "/participations", ParticipationController, only: [:create, :update]
resources "/reveals", RevealController, only: [:create]

Expand Down
110 changes: 110 additions & 0 deletions registrations/lib/registrations_web/views/team_negotiation_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
defmodule RegistrationsWeb.JSONAPI.TeamNegotiationView do
def render("show.json", %{data: user, relationships: relationships, conn: _conn}) do
included = build_included(user, relationships)

%{
data: %{
id: user.id,
type: "team-negotiations",
attributes: %{
team_emails: user.team_emails,
proposed_team_name: user.proposed_team_name,
risk_aversion: user.risk_aversion,
empty: relationships[:empty?],
only_mutuals: relationships[:only_mutuals?]
},
relationships: build_relationships(user, relationships)
},
included: included
}
end

defp build_relationships(user, relationships) do
base = %{
mutuals: %{data: Enum.map(relationships[:mutuals], &member_ref/1)},
proposers: %{data: Enum.map(relationships[:proposers], &member_ref/1)},
proposees: %{data: Enum.map(relationships[:proposees], &proposee_ref/1)},
invalids: %{data: Enum.map(relationships[:invalids], fn email -> %{type: "invalids", id: email} end)}
}

if user.team do
Map.put(base, :team, %{data: %{type: "teams", id: user.team.id}})
else
base
end
end

defp build_included(user, relationships) do
team_included =
if user.team do
[build_team(user.team)]
else
[]
end

mutuals_included = Enum.map(relationships[:mutuals], &build_member/1)
proposers_included = Enum.map(relationships[:proposers], &build_member/1)
proposees_included = Enum.map(relationships[:proposees], &build_proposee/1)
invalids_included = Enum.map(relationships[:invalids], &build_invalid/1)

team_included ++ mutuals_included ++ proposers_included ++ proposees_included ++ invalids_included
end

defp member_ref(user) do
%{type: "team-members", id: user.id}
end

defp proposee_ref(proposee) do
%{type: "proposees", id: proposee.email}
end

defp build_team(team) do
%{
id: team.id,
type: "teams",
attributes: %{
name: team.name,
risk_aversion: team.risk_aversion,
notes: team.notes
},
relationships: %{
members: %{data: Enum.map(team.users, &member_ref/1)}
}
}
end

defp build_member(user) do
%{
id: user.id,
type: "team-members",
attributes: %{
email: user.email,
name: user.name,
risk_aversion: user.risk_aversion,
proposed_team_name: user.proposed_team_name
}
}
end

defp build_proposee(proposee) do
%{
id: proposee.email,
type: "proposees",
attributes: %{
email: proposee.email,
invited: proposee.invited,
registered: Map.get(proposee, :registered, false)
}
}
end

defp build_invalid(email) do
%{
id: email,
type: "invalids",
attributes: %{
value: email
}
}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ defmodule RegistrationsWeb.JSONAPI.UserView do
use JSONAPI.View, type: "users"

def fields do
[:admin, :email, :name]
[:admin, :email, :name, :team_emails, :proposed_team_name, :risk_aversion, :team_id]
end
end
Loading
Loading