Skip to content

Commit ef94efd

Browse files
committed
Add user_even_creation for ext users
1 parent 6963f5e commit ef94efd

5 files changed

Lines changed: 126 additions & 16 deletions

File tree

services/app/apps/codebattle/lib/codebattle/event.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ defmodule Codebattle.Event do
6868
Repo.get_by!(__MODULE__, slug: String.downcase(slug))
6969
end
7070

71+
@spec get_by_slug(String.t()) :: t() | nil
72+
def get_by_slug(slug) do
73+
Repo.get_by(__MODULE__, slug: String.downcase(slug))
74+
end
75+
7176
@spec create(map()) :: {:ok, t()} | {:error, term()}
7277
def create(params) do
7378
%__MODULE__{}

services/app/apps/codebattle/lib/codebattle_web/controllers/ext_api/user_controller.ex

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,29 @@ defmodule CodebattleWeb.ExtApi.UserController do
2020
@adjectives ~w(Swift Agile Robust Stealthy Precise Nimble Efficient Reliable Versatile Dynamic Resilient Tenacious Sharp Astute Ingenious Crafty Clever Adaptable Resourceful Dexterous Brilliant Smart Intuitive Systematic Logical Strategic Tactical Flexible Creative Innovative Analytical Precise Methodical Persistent Perceptive Diligent Meticulous Inventive Quick-witted Cunning Problem-solving Insightful Versatile Ambitious Tenacious Robust Adaptive Ingenious Efficient Collaborative)
2121

2222
def create(conn, %{"UID" => external_oauth_id} = params) do
23-
User
24-
|> Repo.get_by(external_oauth_id: external_oauth_id)
25-
|> case do
26-
nil -> create_user(external_oauth_id, params)
27-
user -> update_user(user, params)
23+
user_result =
24+
User
25+
|> Repo.get_by(external_oauth_id: external_oauth_id)
26+
|> case do
27+
nil -> create_user(external_oauth_id, params)
28+
user -> update_user(user, params)
29+
end
30+
31+
with {:ok, user} <- user_result,
32+
event_slug = Application.get_env(:codebattle, :main_event_slug),
33+
true <- !is_nil(event_slug),
34+
event = Codebattle.Event.get_by_slug(event_slug),
35+
true <- !is_nil(event),
36+
user_event = Codebattle.UserEvent.get_by_user_id_and_event_id(user.id, event.id),
37+
true <- is_nil(user_event) do
38+
Codebattle.UserEvent.create(%{
39+
user_id: user.id,
40+
event_id: event.id,
41+
state: %{}
42+
})
2843
end
29-
|> case do
44+
45+
case user_result do
3046
{:ok, user} ->
3147
conn
3248
|> put_status(200)
@@ -134,9 +150,11 @@ defmodule CodebattleWeb.ExtApi.UserController do
134150

135151
# Attributes casting helpers
136152
defp cast_attribute(attrs, key, value), do: Map.put(attrs, key, value)
153+
137154
defp cast_changeset_attribute(changeset, key, value), do: Changeset.put_change(changeset, key, value)
138155

139156
defp cast_category(attrs, %{"category" => category}), do: cast_attribute(attrs, :category, category)
157+
140158
defp cast_category(attrs, _params), do: attrs
141159

142160
defp cast_name(attrs, params) do

services/app/apps/codebattle/lib/codebattle_web/controllers/root_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule CodebattleWeb.RootController do
1111
conn = put_meta_tags(conn, Application.get_all_env(:phoenix_meta_tags))
1212

1313
current_user = conn.assigns.current_user
14-
event_slug = Application.get_env(:codebattle, :lobby_event_slug)
14+
event_slug = Application.get_env(:codebattle, :main_event_slug)
1515
is_guest? = current_user.is_guest
1616
is_event? = event_slug not in [nil, ""]
1717

services/app/apps/codebattle/test/codebattle_web/controllers/ext_api/user_controller_test.exs

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
defmodule CodebattleWeb.ExtApi.UserControllerTest do
2-
use CodebattleWeb.ConnCase, async: true
2+
use CodebattleWeb.ConnCase, async: false
33

44
alias Codebattle.Clan
55
alias Codebattle.Repo
66
alias Codebattle.User
7+
alias Codebattle.UserEvent
78

89
describe "create/2" do
910
test "checks auth", %{conn: conn} do
@@ -15,7 +16,14 @@ defmodule CodebattleWeb.ExtApi.UserControllerTest do
1516
test "creates user with clan and auth token", %{conn: conn} do
1617
conn
1718
|> put_req_header("x-auth-key", "x-key")
18-
|> post(Routes.ext_api_user_path(conn, :create, %{name: "lol", clan: "S2xhbg==", UID: "asdf", category: "cat"}))
19+
|> post(
20+
Routes.ext_api_user_path(conn, :create, %{
21+
name: "lol",
22+
clan: "S2xhbg==",
23+
UID: "asdf",
24+
category: "cat"
25+
})
26+
)
1927
|> json_response(200)
2028

2129
user = Repo.get_by(User, name: "lol")
@@ -97,11 +105,25 @@ defmodule CodebattleWeb.ExtApi.UserControllerTest do
97105

98106
test "updates user by UID", %{conn: conn} do
99107
clan = insert(:clan, name: "Kek", long_name: "lOl_kEk")
100-
user = insert(:user, name: "whatever", clan_id: nil, subscription_type: :free, external_oauth_id: "asdf")
108+
109+
user =
110+
insert(:user,
111+
name: "whatever",
112+
clan_id: nil,
113+
subscription_type: :free,
114+
external_oauth_id: "asdf"
115+
)
101116

102117
conn
103118
|> put_req_header("x-auth-key", "x-key")
104-
|> post(Routes.ext_api_user_path(conn, :create, %{category: "lol", name: "oiblz", clan: "Kek ", UID: "asdf"}))
119+
|> post(
120+
Routes.ext_api_user_path(conn, :create, %{
121+
category: "lol",
122+
name: "oiblz",
123+
clan: "Kek ",
124+
UID: "asdf"
125+
})
126+
)
105127
|> json_response(200)
106128

107129
user = Repo.get(User, user.id)
@@ -113,17 +135,39 @@ defmodule CodebattleWeb.ExtApi.UserControllerTest do
113135
category: "lol",
114136
external_oauth_id: "asdf",
115137
subscription_type: :premium
116-
} == Map.take(user, [:id, :name, :clan_id, :external_oauth_id, :subscription_type, :category])
138+
} ==
139+
Map.take(user, [
140+
:id,
141+
:name,
142+
:clan_id,
143+
:external_oauth_id,
144+
:subscription_type,
145+
:category
146+
])
117147
end
118148

119149
test "updates user with duplicated name by UID", %{conn: conn} do
120150
clan = insert(:clan, name: "Kek", long_name: "lOl_kEk")
121151
insert(:user, name: "oiblz")
122-
user = insert(:user, name: "whatever", clan_id: nil, subscription_type: :free, external_oauth_id: "asdf")
152+
153+
user =
154+
insert(:user,
155+
name: "whatever",
156+
clan_id: nil,
157+
subscription_type: :free,
158+
external_oauth_id: "asdf"
159+
)
123160

124161
conn
125162
|> put_req_header("x-auth-key", "x-key")
126-
|> post(Routes.ext_api_user_path(conn, :create, %{category: "lol", name: "oiblz", clan: "Kek ", UID: "asdf"}))
163+
|> post(
164+
Routes.ext_api_user_path(conn, :create, %{
165+
category: "lol",
166+
name: "oiblz",
167+
clan: "Kek ",
168+
UID: "asdf"
169+
})
170+
)
127171
|> json_response(200)
128172

129173
user = Repo.get(User, user.id)
@@ -135,7 +179,50 @@ defmodule CodebattleWeb.ExtApi.UserControllerTest do
135179
external_oauth_id: "asdf",
136180
category: "lol",
137181
subscription_type: :premium
138-
} == Map.take(user, [:id, :clan_id, :external_oauth_id, :subscription_type, :category])
182+
} ==
183+
Map.take(user, [:id, :clan_id, :external_oauth_id, :subscription_type, :category])
184+
end
185+
186+
test "finds or creates user with user_event", %{conn: conn} do
187+
Application.put_env(:codebattle, :main_event_slug, "e")
188+
189+
insert(:event, slug: "e")
190+
191+
conn
192+
|> put_req_header("x-auth-key", "x-key")
193+
|> post(
194+
Routes.ext_api_user_path(conn, :create, %{
195+
name: "lol",
196+
clan: "S2xhbg==",
197+
UID: "asdf",
198+
category: "cat"
199+
})
200+
)
201+
|> json_response(200)
202+
203+
user = Repo.get_by(User, name: "lol")
204+
user_event = Repo.get_by(UserEvent, user_id: user.id)
205+
206+
assert user_event
207+
assert user_event.state == %{}
208+
209+
conn
210+
|> put_req_header("x-auth-key", "x-key")
211+
|> post(
212+
Routes.ext_api_user_path(conn, :create, %{
213+
name: "lol",
214+
clan: "S2xhbg==",
215+
UID: "asdf",
216+
category: "cat"
217+
})
218+
)
219+
|> json_response(200)
220+
221+
user_event = Repo.get_by(UserEvent, user_id: user.id)
222+
223+
assert user_event.state == %{}
224+
225+
Application.delete_env(:codebattle, :main_event_slug)
139226
end
140227
end
141228
end

services/app/config/releases.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ config :codebattle, :firebase,
8080
api_key: System.get_env("FIREBASE_API_KEY"),
8181
firebase_autn_url: "https://identitytoolkit.googleapis.com/v1/accounts"
8282

83-
config :codebattle, :lobby_event_slug, System.get_env("CODEBATTLE_LOBBY_EVENT_SLUG")
8483
config :codebattle, :logo_title, logo_title
84+
config :codebattle, :main_event_slug, System.get_env("CODEBATTLE_MAIN_EVENT_SLUG")
8585

8686
config :codebattle, :oauth,
8787
github_client_id: System.get_env("GITHUB_CLIENT_ID", "ASFD"),

0 commit comments

Comments
 (0)