Skip to content
Merged
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
18 changes: 18 additions & 0 deletions lib/cadet_web/admin_controllers/admin_goals_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ defmodule CadetWeb.AdminGoalsController do
use PhoenixSwagger

alias Cadet.Incentives.Goals
alias Cadet.Accounts.CourseRegistration

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

def index_goals_with_progress(conn, %{"course_reg_id" => course_reg_id}) do
course_reg = %CourseRegistration{id: String.to_integer(course_reg_id)}

render(conn, "index_goals_with_progress.json", goals: Goals.get_with_progress(course_reg))
end

def bulk_update(conn, %{"goals" => goals}) do
course_reg = conn.assigns.course_reg

Expand Down Expand Up @@ -87,6 +94,17 @@ defmodule CadetWeb.AdminGoalsController do
response(403, "Forbidden")
end

swagger_path :index_goals_with_progress do
get("/admin/goals/:userid")

summary("Gets goals and goal progress of a user")
security([%{JWT: []}])

response(200, "OK", Schema.array(:GoalWithProgress))
response(401, "Unauthorised")
response(403, "Forbidden")
end

swagger_path :update do
put("/admin/goals/{uuid}")

Expand Down
24 changes: 24 additions & 0 deletions lib/cadet_web/admin_views/admin_goals_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ defmodule CadetWeb.AdminGoalsView do
render_many(goals, CadetWeb.AdminGoalsView, "goal.json", as: :goal)
end

def render("index_goals_with_progress.json", %{goals: goals}) do
render_many(goals, CadetWeb.AdminGoalsView, "goal_with_progress.json", as: :goal)
end

def render("goal.json", %{goal: goal}) do
transform_map_for_view(goal, %{
uuid: :uuid,
Expand All @@ -15,4 +19,24 @@ defmodule CadetWeb.AdminGoalsView do
meta: :meta
})
end

def render("goal_with_progress.json", %{goal: goal}) do
transform_map_for_view(goal, %{
uuid: :uuid,
text: :text,
count: fn
%{progress: [%{count: count}]} -> count
_ -> 0
end,
completed: fn
%{progress: [%{completed: completed}]} -> completed
_ -> false
end,
targetCount: :target_count,
type: :type,
meta: :meta,
achievementUuids:
&Enum.map(&1.achievements, fn achievement -> achievement.achievement_uuid end)
})
end
end
1 change: 1 addition & 0 deletions lib/cadet_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ defmodule CadetWeb.Router do
put("/users", AdminUserController, :upsert_users_and_groups)
put("/users/:course_reg_id/role", AdminUserController, :update_role)
delete("/users/:course_reg_id", AdminUserController, :delete_user)
get("/users/:course_reg_id/goals", AdminGoalsController, :index_goals_with_progress)
post("/users/:course_reg_id/goals/:uuid/progress", AdminGoalsController, :update_progress)

put("/achievements", AdminAchievementsController, :bulk_update)
Expand Down
56 changes: 55 additions & 1 deletion test/cadet_web/admin_controllers/admin_goals_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CadetWeb.AdminGoalsControllerTest do
import Cadet.TestEntityHelper

alias Cadet.Repo
alias Cadet.Incentives.{Goal, Goals}
alias Cadet.Incentives.{Goal, Goals, GoalProgress}
alias CadetWeb.AdminGoalsController
alias Ecto.UUID

Expand Down Expand Up @@ -54,6 +54,56 @@ defmodule CadetWeb.AdminGoalsControllerTest do
end
end

describe "GET v2/courses/:course_id/admin/users/:course_reg_id/goals" do
@tag authenticate: :staff
test "succeeds for staff", %{conn: conn} do
course = conn.assigns.test_cr.course

{:ok, g} =
%Goal{course_id: course.id, uuid: UUID.generate()}
|> Map.merge(goal_literal(5))
|> Repo.insert()

course_reg = insert(:course_registration, %{course: course, role: :student})

{:ok, p} =
%GoalProgress{
goal_uuid: g.uuid,
course_reg_id: course_reg.id,
count: 123,
completed: true
}
|> Repo.insert()

[resp_goal] =
conn
|> get(build_user_goals_path(course.id, course_reg.id))
|> json_response(200)

assert goal_json_literal(5) = resp_goal
assert resp_goal["uuid"] == g.uuid
assert resp_goal["count"] == p.count
assert resp_goal["completed"] == p.completed
end

@tag authenticate: :student
test "403 for student", %{conn: conn} do
course_id = conn.assigns.course_id

conn
|> get(build_path(course_id))
|> response(403)
end

test "401 if unauthenticated", %{conn: conn} do
course = insert(:course)

conn
|> get(build_path(course.id))
|> response(401)
end
end

describe "PUT v2/courses/:course_id/admin/goals/:uuid" do
@tag authenticate: :staff
test "succeeds for staff", %{conn: conn} do
Expand Down Expand Up @@ -265,4 +315,8 @@ defmodule CadetWeb.AdminGoalsControllerTest do
defp build_path(course_id, uuid, course_reg_id) do
"/v2/courses/#{course_id}/admin/users/#{course_reg_id}/goals/#{uuid}/progress/"
end

defp build_user_goals_path(course_id, course_reg_id) do
"/v2/courses/#{course_id}/admin/users/#{course_reg_id}/goals"
end
end