-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: added support to student cycle info logging
- created `StudentsCycleInfoLog` context - created `StudentCycleInfoLog` schema in `StudentsCycleInfoLog` context
- Loading branch information
Showing
10 changed files
with
304 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
defmodule Lanttern.StudentsCycleInfoLog do | ||
@moduledoc """ | ||
The StudentsCycleInfoLog context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias Lanttern.Repo | ||
|
||
alias Lanttern.StudentsCycleInfo.StudentCycleInfo | ||
alias Lanttern.StudentsCycleInfoLog.StudentCycleInfoLog | ||
|
||
@doc """ | ||
Creates a student_cycle_info_log. | ||
## Examples | ||
iex> create_student_cycle_info_log(%{field: value}) | ||
{:ok, %StudentCycleInfoLog{}} | ||
iex> create_student_cycle_info_log(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_student_cycle_info_log(attrs \\ %{}) do | ||
%StudentCycleInfoLog{} | ||
|> StudentCycleInfoLog.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Util for create a student cycle info log. | ||
Accepts `{:ok, %StudentCycleInfo{}}` or `{:error, %Ecto.Changeset{}}` tuple as first arg. | ||
Always returns the note or tuple as is. The logging process is handled in an async task. | ||
### Options: | ||
- `:log_profile_id` – the profile id used to log the operation. if not present, logging will be skipped | ||
""" | ||
@spec maybe_create_student_cycle_info_log( | ||
{:ok, StudentCycleInfo.t()} | {:error, Ecto.Changeset.t()}, | ||
operation :: String.t(), | ||
opts :: Keyword.t() | ||
) :: | ||
{:ok, StudentCycleInfo.t()} | {:error, Ecto.Changeset.t()} | ||
def maybe_create_student_cycle_info_log(operation_tuple, operation, opts \\ []) | ||
|
||
def maybe_create_student_cycle_info_log({:error, _} = operation_tuple, _, _), | ||
do: operation_tuple | ||
|
||
def maybe_create_student_cycle_info_log( | ||
{:ok, %StudentCycleInfo{} = student_cycle_info} = operation_tuple, | ||
operation, | ||
opts | ||
) do | ||
case Keyword.get(opts, :log_profile_id) do | ||
profile_id when not is_nil(profile_id) -> | ||
do_create_student_cycle_info_log(student_cycle_info, operation, profile_id) | ||
operation_tuple | ||
|
||
_ -> | ||
operation_tuple | ||
end | ||
end | ||
|
||
defp do_create_student_cycle_info_log(student_cycle_info, operation, profile_id) do | ||
attrs = | ||
student_cycle_info | ||
|> Map.from_struct() | ||
|> Map.put(:student_cycle_info_id, student_cycle_info.id) | ||
|> Map.put(:profile_id, profile_id) | ||
|> Map.put(:operation, operation) | ||
|
||
# create the log in a async task (fire and forget) | ||
Task.Supervisor.start_child(Lanttern.TaskSupervisor, fn -> | ||
create_student_cycle_info_log(attrs) | ||
end) | ||
end | ||
end |
48 changes: 48 additions & 0 deletions
48
lib/lanttern/students_cycle_info_log/student_cycle_info_log.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Lanttern.StudentsCycleInfoLog.StudentCycleInfoLog do | ||
@moduledoc """ | ||
The `StudentCycleInfoLog` schema | ||
""" | ||
|
||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@schema_prefix "log" | ||
schema "students_cycle_info" do | ||
field :student_cycle_info_id, :id | ||
field :profile_id, :id | ||
field :operation, :string | ||
|
||
field :student_id, :id | ||
field :cycle_id, :id | ||
field :school_id, :id | ||
field :school_info, :string | ||
field :family_info, :string | ||
field :profile_picture_url, :string | ||
|
||
timestamps(updated_at: false) | ||
end | ||
|
||
@doc false | ||
def changeset(student_cycle_info_log, attrs) do | ||
student_cycle_info_log | ||
|> cast(attrs, [ | ||
:student_cycle_info_id, | ||
:profile_id, | ||
:operation, | ||
:student_id, | ||
:cycle_id, | ||
:school_id, | ||
:school_info, | ||
:family_info, | ||
:profile_picture_url | ||
]) | ||
|> validate_required([ | ||
:student_cycle_info_id, | ||
:profile_id, | ||
:operation, | ||
:student_id, | ||
:cycle_id, | ||
:school_id | ||
]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
priv/repo/migrations/20250106191205_create_students_cycle_info_logs.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule Lanttern.Repo.Migrations.CreateStudentsCycleInfoLogs do | ||
use Ecto.Migration | ||
|
||
@prefix "log" | ||
|
||
def change do | ||
create table(:students_cycle_info, prefix: @prefix) do | ||
add :student_cycle_info_id, :bigint, null: false | ||
add :profile_id, :bigint, null: false | ||
add :operation, :text, null: false | ||
|
||
add :student_id, :bigint, null: false | ||
add :cycle_id, :bigint, null: false | ||
add :school_id, :bigint, null: false | ||
|
||
add :school_info, :text | ||
add :family_info, :text | ||
add :profile_picture_url, :text | ||
|
||
timestamps(updated_at: false) | ||
end | ||
|
||
create constraint( | ||
:students_cycle_info, | ||
:valid_operations, | ||
prefix: @prefix, | ||
check: "operation IN ('CREATE', 'UPDATE', 'DELETE')" | ||
) | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
priv/repo/migrations/20250106191746_fix_students_cycle_info_null_constraints.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule Lanttern.Repo.Migrations.FixStudentsCycleInfoNullConstraints do | ||
use Ecto.Migration | ||
|
||
def change do | ||
execute "ALTER TABLE students_cycle_info ALTER COLUMN student_id SET NOT NULL", | ||
"ALTER TABLE students_cycle_info ALTER COLUMN student_id DROP NOT NULL" | ||
|
||
execute "ALTER TABLE students_cycle_info ALTER COLUMN cycle_id SET NOT NULL", | ||
"ALTER TABLE students_cycle_info ALTER COLUMN cycle_id DROP NOT NULL" | ||
|
||
execute "ALTER TABLE students_cycle_info ALTER COLUMN school_id SET NOT NULL", | ||
"ALTER TABLE students_cycle_info ALTER COLUMN school_id DROP NOT NULL" | ||
end | ||
end |
Oops, something went wrong.