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/relatorios #133

Merged
merged 10 commits into from
Nov 10, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ defmodule ModuloPesquisa.Adapters.RelatorioAdapter do
import Timex.Format.DateTime.Formatter, only: [lformat!: 3]

alias Identidades.Handlers.UsuarioHandler

alias ModuloPesquisa.Models.RelatorioAnualPesquisa, as: Anual
alias ModuloPesquisa.Models.RelatorioMensalPesquisa, as: Mensal
alias ModuloPesquisa.Models.RelatorioTrimestralPesquisa, as: Trimestral
alias ModuloPesquisa.Models.RelatorioPesquisa, as: RelatorioPesquisaModel
alias ModuloPesquisa.Schemas.RelatorioPesquisa
Comment on lines +5 to 6
Copy link
Member

Choose a reason for hiding this comment

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

por questão de padronização, acho melhor deixar o alias do model como ele é e só renomear o do schema para Schema, tipo:

alias Modulo.Models.X
alias Modulo.Schemas.X, as: Schema


@locale Application.compile_env(:pescarte, :locale, "pt_BR")
Copy link
Member

Choose a reason for hiding this comment

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

boa! tava querendo fazer isso msm! mandou bem


@type relatorio :: Anual.t() | Mensal.t() | Trimestral.t()
@typep changeset :: Ecto.Changeset.t()

@spec internal_to_external(relatorio) :: {:ok, RelatorioPesquisa.t()} | {:error, changeset}
@spec internal_to_external(RelatorioPesquisaModel.t()) ::
{:ok, RelatorioPesquisa.t()} | {:error, changeset}
def internal_to_external(%{pesquisador: pesquisador} = relatorio) do
attrs = %{
status: relatorio.status,
Expand All @@ -26,9 +23,9 @@ defmodule ModuloPesquisa.Adapters.RelatorioAdapter do
RelatorioPesquisa.parse!(attrs)
end

defp get_relatorio_tipo(%Anual{}), do: :anual
defp get_relatorio_tipo(%Mensal{}), do: :mensal
defp get_relatorio_tipo(%Trimestral{}), do: :trimestral
defp get_relatorio_tipo(%RelatorioPesquisa{tipo: "anual"}), do: :anual
defp get_relatorio_tipo(%RelatorioPesquisa{tipo: "mensal"}), do: :mensal
defp get_relatorio_tipo(%RelatorioPesquisa{tipo: "trimestral"}), do: :trimestral
Comment on lines +26 to +28
Copy link
Member

Choose a reason for hiding this comment

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

achei que o tipo do relatório era um Ecto.Enum, deveria vir como átomo já


defp get_relatorio_periodo!(relatorio) do
relatorio.ano
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
defmodule ModuloPesquisa.Handlers.RelatoriosHandler do
alias ModuloPesquisa.Adapters.RelatorioAdapter
alias ModuloPesquisa.Handlers.IManageRelatoriosHandler
alias ModuloPesquisa.Models.RelatorioPesquisa
alias ModuloPesquisa.Repository

@behaviour IManageRelatoriosHandler

@impl true
def change_relatorio_pesquisa(%RelatorioPesquisa{} = relatorio, attrs \\ %{}) do
RelatorioPesquisa.changeset(relatorio, attrs)
end

@impl true
def list_relatorios_from_pesquisador(id, sorter \\ &sort_by_periodo/1) do
id
Expand Down
13 changes: 4 additions & 9 deletions apps/modulo_pesquisa/lib/modulo_pesquisa/i_manage_repository.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ defmodule ModuloPesquisa.IManageRepository do
alias ModuloPesquisa.Models.Midia.Tag
alias ModuloPesquisa.Models.NucleoPesquisa
alias ModuloPesquisa.Models.Pesquisador
alias ModuloPesquisa.Models.RelatorioAnualPesquisa
alias ModuloPesquisa.Models.RelatorioMensalPesquisa
alias ModuloPesquisa.Models.RelatorioTrimestralPesquisa
alias ModuloPesquisa.Models.RelatorioPesquisa

@opaque changeset :: Ecto.Changeset.t()

Expand All @@ -29,6 +27,7 @@ defmodule ModuloPesquisa.IManageRepository do
@callback list_pesquisador :: list(Pesquisador.t())
@callback list_relatorios_pesquisa :: list(struct)
@callback list_relatorios_pesquisa_from_pesquisador(Database.id()) :: list(struct)
@callback fetch_relatorio_pesquisa_by_id(Database.id()) :: struct
@callback list_tag :: list(Tag.t())
@callback list_tags_from_categoria(Database.id()) :: list(Tag.t())
@callback list_tags_from_midia(Database.id()) :: list(Tag.t())
Expand All @@ -42,11 +41,7 @@ defmodule ModuloPesquisa.IManageRepository do
{:ok, NucleoPesquisa.t()} | {:error, changeset}
@callback upsert_pesquisador(Pesquisador.t(), map) ::
{:ok, Pesquisador.t()} | {:error, changeset}
@callback upsert_relatorio_anual(RelatorioAnualPesquisa.t(), map) ::
{:ok, RelatorioAnualPesquisa.t()} | {:error, changeset}
@callback upsert_relatorio_mensal(RelatorioMensalPesquisa.t(), map) ::
{:ok, RelatorioMensalPesquisa.t()} | {:error, changeset}
@callback upsert_relatorio_trimestral(RelatorioTrimestralPesquisa.t(), map) ::
{:ok, RelatorioTrimestralPesquisa.t()} | {:error, changeset}
@callback upsert_relatorio_pesquisa(RelatorioPesquisa.t(), map) ::
{:ok, RelatorioPesquisa.t()} | {:error, changeset}
@callback upsert_tag(Tag.t(), map) :: {:ok, Tag.t()} | {:error, changeset}
end
18 changes: 3 additions & 15 deletions apps/modulo_pesquisa/lib/modulo_pesquisa/models/pesquisador.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ defmodule ModuloPesquisa.Models.Pesquisador do
alias ModuloPesquisa.Models.Campus
alias ModuloPesquisa.Models.LinhaPesquisa
alias ModuloPesquisa.Models.Midia
alias ModuloPesquisa.Models.RelatorioAnualPesquisa
alias ModuloPesquisa.Models.RelatorioMensalPesquisa
alias ModuloPesquisa.Models.RelatorioTrimestralPesquisa
alias ModuloPesquisa.Models.RelatorioPesquisa

@type t :: %Pesquisador{
minibio: binary,
Expand All @@ -26,9 +24,7 @@ defmodule ModuloPesquisa.Models.Pesquisador do
orientandos: list(Pesquisador.t()),
orientador: Pesquisador.t() | nil,
midias: list(Midia.t()),
relatorios_anuais: list(RelatorioAnualPesquisa.t()),
relatorios_mensais: list(RelatorioMensalPesquisa.t()),
relatorios_trimestrais: list(RelatorioTrimestralPesquisa.t()),
relatorios_pesquisa: list(RelatorioPesquisa.t()),
campus: Campus.t(),
usuario: User.t()
}
Expand Down Expand Up @@ -63,7 +59,7 @@ defmodule ModuloPesquisa.Models.Pesquisador do
foreign_key: :responsavel_lp_id,
references: :id_publico

has_many :relatorios_anuais, RelatorioAnualPesquisa,
has_many :relatorios_pesquisa, RelatorioPesquisa,
references: :id_publico,
foreign_key: :pesquisador_id

Expand All @@ -74,14 +70,6 @@ defmodule ModuloPesquisa.Models.Pesquisador do
references: :id_publico,
foreign_key: :pesquisador_id

has_many :relatorios_mensais, RelatorioMensalPesquisa,
references: :id_publico,
foreign_key: :pesquisador_id

has_many :relatorios_trimestrais, RelatorioTrimestralPesquisa,
references: :id_publico,
foreign_key: :pesquisador_id

belongs_to :usuario, Usuario,
on_replace: :update,
references: :id_publico,
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
defmodule ModuloPesquisa.Models.RelatorioPesquisa do
use Database, :model

alias ModuloPesquisa.Models.Pesquisador
alias ModuloPesquisa.Schemas.ConteudoAnual
alias ModuloPesquisa.Schemas.ConteudoMensal
alias ModuloPesquisa.Schemas.ConteudoTrimestral

@type t :: %RelatorioPesquisa{
tipo: atom,
data_inicio: Date.t(),
data_fim: Date.t(),
data_entrega: Date.t(),
data_limite: Date.t(),
link: binary,
status: atom,
pesquisador: Pesquisador.t(),
id_publico: binary
}

@tipo ~w(mensal bimestral trimestral anual)a
@status ~w(entregue atrasado pendente)a

@required_fields ~w(tipo data_inicio data_fim status pesquisador_id)a
@optional_fields ~w(data_entrega data_limite link)a

@primary_key false
schema "relatorio_pesquisa" do
field(:link, :string)
field(:data_inicio, :date, primary_key: true)
field(:data_fim, :date, primary_key: true)
field(:data_entrega, :date)
field(:data_limite, :date)
field(:tipo, Ecto.Enum, values: @tipo)
field(:status, Ecto.Enum, values: @status)
field(:id_publico, Database.Types.PublicId, autogenerate: true)

embeds_one(:conteudo_anual, ConteudoAnual, source: :conteudo, on_replace: :update)
embeds_one(:conteudo_mensal, ConteudoMensal, source: :conteudo, on_replace: :update)
embeds_one(:conteudo_trimestral, ConteudoTrimestral, source: :conteudo, on_replace: :update)

belongs_to(:pesquisador, Pesquisador,
on_replace: :update,
references: :id_publico,
type: :string,
primary_key: true
)

timestamps()
end

@spec changeset(RelatorioPesquisa.t(), map) :: changeset
def changeset(%RelatorioPesquisa{} = relatorio, attrs) do
relatorio
|> cast(attrs, @required_fields ++ @optional_fields)
|> cast_embed(:conteudo_anual)
|> cast_embed(:conteudo_mensal)
|> cast_embed(:conteudo_trimestral)
Comment on lines +56 to +58
Copy link
Member

Choose a reason for hiding this comment

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

talvez seja melhor depois criar 3 changeset/2 diferentes, pra cada tipo, assim a gente consegue forças que o conteúdo seja required, tipo:

def mensal_changeset(relatorio, attrs) do
  relatorio
  |> changeset()
  |> cast_embed(:conteudo_mensal, required: true)
end

|> validate_required(@required_fields)
|> validate_inclusion(:tipo, @tipo)
|> validate_inclusion(:status, @status)
|> foreign_key_constraint(:pesquisador_id)
|> validate_period()
end

defp validate_period(changeset) do
start_date = get_field(changeset, :data_inicio)
end_date = get_field(changeset, :data_fim)

case {start_date, end_date} do
{start_date, end_date} when is_nil(start_date) or is_nil(end_date) ->
changeset

{_, _} ->
if Date.compare(start_date, end_date) == :gt do
add_error(changeset, :data_inicio, "A data de início deve ser anterior à data de fim")
else
changeset
end
end
end
end
Loading
Loading