Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: API to update student's status #250

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions lib/dbservice/statuses.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ defmodule Dbservice.Statuses do
"""
def get_status!(id), do: Repo.get!(Status, id)

@doc """
Gets a status by title.
Raises `Ecto.NoResultsError` if the Status does not exist.
## Examples
iex> get_status_by_title(abc)
%Status{}
iex> get_status_by_title(1234)
** (Ecto.NoResultsError)
"""
def get_status_by_title(title) do
Repo.get_by(Status, title: title)
end

@doc """
Creates a status.
## Examples
Expand Down
110 changes: 109 additions & 1 deletion lib/dbservice_web/controllers/student_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,10 @@ defmodule DbserviceWeb.StudentController do
end
end

def update_student_status(conn, %{"student_id" => student_id}) do
@doc """
This function removes the dropout status from both the enrollment records and the student table.
"""
def remove_dropout_status(conn, %{"student_id" => student_id}) do
with {:ok, student} <- get_student(student_id),
enrollment_records <-
EnrollmentRecords.get_enrollment_records_by_user_id(student.user_id),
Expand Down Expand Up @@ -842,4 +845,109 @@ defmodule DbserviceWeb.StudentController do
module.__schema__(:fields)
|> Enum.map(&Atom.to_string/1)
end

swagger_path :update_student_status do
post("/api/student/{student_id}/status")

parameters do
student_id(:path, :string, "The student_id of the student", required: true)
status(:query, :string, "The status to be updated", required: true)
academic_year(:query, :string, "The academic year", required: true)
start_date(:query, :string, "The start date for the status", required: true)
end

response(200, "OK", Schema.ref(:Student))
response(400, "Bad Request")
response(404, "Not Found")
end

def update_student_status(conn, params) do
with {:ok, status} <- get_status_by_title(params["status"]),
{:ok, student} <- get_student_by_student_id(params["student_id"]),
:ok <- check_existing_status(student, status.title),
{:ok, %Student{} = updated_student} <- update_student_status_field(student, status),
{:ok, _enrollment_record} <- create_status_enrollment_record(student, status, params) do
conn
|> put_status(:ok)
|> render("show.json", student: updated_student)
else
{:error, :status_not_found} ->
conn
|> put_status(:not_found)
|> json(%{error: "Status not found"})

{:error, :student_not_found} ->
conn
|> put_status(:not_found)
|> json(%{error: "Student not found"})

{:error, :status_already_assigned} ->
conn
|> put_status(:ok)
|> json(%{message: "Student already has the requested status"})

{:error, changeset} ->
conn
|> put_status(:bad_request)
|> json(%{error: "Failed to update status", details: changeset.errors})
end
end

# Helper function to check if student already has the status
defp check_existing_status(student, status_title) do
case student.status == Atom.to_string(status_title) do
true -> {:error, :status_already_assigned}
false -> :ok
end
end

# Helper function to get status by title
defp get_status_by_title(status_title) do
case Statuses.get_status_by_title(status_title) do
nil -> {:error, :status_not_found}
status -> {:ok, status}
end
end

# Helper function to get student by student_id
defp get_student_by_student_id(student_id) do
case Users.get_student_by_student_id(student_id) do
nil -> {:error, :student_not_found}
student -> {:ok, student}
end
end

# Helper function to update student status
defp update_student_status_field(student, status) do
Users.update_student(student, %{status: Atom.to_string(status.title)})
end

# Helper function to create new enrollment record
defp create_status_enrollment_record(student, status, params) do
# First, update any existing status enrollment records to is_current: false
update_existing_status_enrollments(student.user_id, params["start_date"])

enrollment_attrs = %{
user_id: student.user_id,
group_id: status.id,
group_type: "status",
grade_id: student.grade_id,
academic_year: params["academic_year"],
start_date: params["start_date"],
is_current: true
}

EnrollmentRecords.create_enrollment_record(enrollment_attrs)
end

# Helper function to update existing status enrollment records
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function name is a bit weird.. it closes a particular status (is current false and enddate is set).. not merely updates. maybe adding a comment will help

defp update_existing_status_enrollments(user_id, end_date) do
from(e in EnrollmentRecord,
where: e.user_id == ^user_id and e.group_type == "status" and e.is_current == true,
update: [set: [is_current: false, end_date: ^end_date]]
)
|> Repo.update_all([])

{:ok, :updated}
end
end
5 changes: 4 additions & 1 deletion lib/dbservice_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ defmodule DbserviceWeb.Router do
patch("/update-user-enrollment-records", StudentController, :update_user_enrollment_records)
post("/student/batch-process", StudentController, :batch_process)
post("/group-user/batch-process", GroupUserController, :batch_process)
patch("/student/update-status/:student_id", StudentController, :update_student_status)

# Some students were incorrectly marked as "dropouts" in our system. This endpoint was introduced to reverse this mistake by removing the dropout status from both the enrollment records and the student table
patch("/student/remove-dropout-status/:student_id", StudentController, :remove_dropout_status)
delete("/cleanup-student/:student_id", UserSessionController, :cleanup_student)
post("/student/:student_id/status", StudentController, :update_student_status)

def swagger_info do
source(["config/.env", "config/.env"])
Expand Down
Loading