Skip to content

Commit 6e04a69

Browse files
jonas-chowangelsl
authored andcommitted
Add endpoint for admin to get user goal progress
1 parent 907dbb3 commit 6e04a69

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

lib/cadet_web/admin_controllers/admin_goals_controller.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ defmodule CadetWeb.AdminGoalsController do
44
use PhoenixSwagger
55

66
alias Cadet.Incentives.Goals
7+
alias Cadet.Accounts.CourseRegistration
78

89
def index(conn, _) do
910
course_id = conn.assigns.course_reg.course_id
1011
render(conn, "index.json", goals: Goals.get(course_id))
1112
end
1213

14+
def index_goals_with_progress(conn, %{"course_reg_id" => course_reg_id}) do
15+
course_reg = %CourseRegistration{id: String.to_integer(course_reg_id)}
16+
17+
render(conn, "index_goals_with_progress.json", goals: Goals.get_with_progress(course_reg))
18+
end
19+
1320
def bulk_update(conn, %{"goals" => goals}) do
1421
course_reg = conn.assigns.course_reg
1522

@@ -87,6 +94,17 @@ defmodule CadetWeb.AdminGoalsController do
8794
response(403, "Forbidden")
8895
end
8996

97+
swagger_path :index_goals_with_progress do
98+
get("/admin/goals/:userid")
99+
100+
summary("Gets goals and goal progress of a user")
101+
security([%{JWT: []}])
102+
103+
response(200, "OK", Schema.array(:GoalWithProgress))
104+
response(401, "Unauthorised")
105+
response(403, "Forbidden")
106+
end
107+
90108
swagger_path :update do
91109
put("/admin/goals/{uuid}")
92110

lib/cadet_web/admin_views/admin_goals_view.ex

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ defmodule CadetWeb.AdminGoalsView do
66
render_many(goals, CadetWeb.AdminGoalsView, "goal.json", as: :goal)
77
end
88

9+
def render("index_goals_with_progress.json", %{goals: goals}) do
10+
render_many(goals, CadetWeb.AdminGoalsView, "goal_with_progress.json", as: :goal)
11+
end
12+
913
def render("goal.json", %{goal: goal}) do
1014
transform_map_for_view(goal, %{
1115
uuid: :uuid,
@@ -15,4 +19,24 @@ defmodule CadetWeb.AdminGoalsView do
1519
meta: :meta
1620
})
1721
end
22+
23+
def render("goal_with_progress.json", %{goal: goal}) do
24+
transform_map_for_view(goal, %{
25+
uuid: :uuid,
26+
text: :text,
27+
count: fn
28+
%{progress: [%{count: count}]} -> count
29+
_ -> 0
30+
end,
31+
completed: fn
32+
%{progress: [%{completed: completed}]} -> completed
33+
_ -> false
34+
end,
35+
targetCount: :target_count,
36+
type: :type,
37+
meta: :meta,
38+
achievementUuids:
39+
&Enum.map(&1.achievements, fn achievement -> achievement.achievement_uuid end)
40+
})
41+
end
1842
end

lib/cadet_web/router.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ defmodule CadetWeb.Router do
140140

141141
get("/goals", AdminGoalsController, :index)
142142
put("/goals", AdminGoalsController, :bulk_update)
143+
get("/goals/:course_reg_id", AdminGoalsController, :index_goals_with_progress)
143144
put("/goals/:uuid", AdminGoalsController, :update)
144145
delete("/goals/:uuid", AdminGoalsController, :delete)
145146

test/cadet_web/admin_controllers/admin_goals_controller_test.exs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule CadetWeb.AdminGoalsControllerTest do
44
import Cadet.TestEntityHelper
55

66
alias Cadet.Repo
7-
alias Cadet.Incentives.{Goal, Goals}
7+
alias Cadet.Incentives.{Goal, Goals, GoalProgress}
88
alias CadetWeb.AdminGoalsController
99
alias Ecto.UUID
1010

@@ -54,6 +54,56 @@ defmodule CadetWeb.AdminGoalsControllerTest do
5454
end
5555
end
5656

57+
describe "GET v2/courses/:course_id/admin/goals/:userid" do
58+
@tag authenticate: :staff
59+
test "succeeds for staff", %{conn: conn} do
60+
course = conn.assigns.test_cr.course
61+
62+
{:ok, g} =
63+
%Goal{course_id: course.id, uuid: UUID.generate()}
64+
|> Map.merge(goal_literal(5))
65+
|> Repo.insert()
66+
67+
course_reg = insert(:course_registration, %{course: course, role: :student})
68+
69+
{:ok, p} =
70+
%GoalProgress{
71+
goal_uuid: g.uuid,
72+
course_reg_id: course_reg.id,
73+
count: 123,
74+
completed: true
75+
}
76+
|> Repo.insert()
77+
78+
[resp_goal] =
79+
conn
80+
|> get(build_path(course.id, course_reg.id))
81+
|> json_response(200)
82+
83+
assert goal_json_literal(5) = resp_goal
84+
assert resp_goal["uuid"] == g.uuid
85+
assert resp_goal["count"] == p.count
86+
assert resp_goal["completed"] == p.completed
87+
end
88+
89+
@tag authenticate: :student
90+
test "403 for student", %{conn: conn} do
91+
course_id = conn.assigns.course_id
92+
93+
conn
94+
|> get(build_path(course_id))
95+
|> response(403)
96+
end
97+
98+
test "401 if unauthenticated", %{conn: conn} do
99+
course = insert(:course)
100+
101+
conn
102+
|> get(build_path(course.id))
103+
|> response(401)
104+
end
105+
end
106+
57107
describe "PUT v2/courses/:course_id/admin/goals/:uuid" do
58108
@tag authenticate: :staff
59109
test "succeeds for staff", %{conn: conn} do

0 commit comments

Comments
 (0)