-
Notifications
You must be signed in to change notification settings - Fork 12
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
Feat/relatorios #133
Changes from all commits
3a1e58e
cfc41ae
d34f638
d2310b8
213f58a
13e9253
6319362
f229a5f
29cb5f2
5c39359
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
@locale Application.compile_env(:pescarte, :locale, "pt_BR") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. achei que o tipo do relatório era um |
||
|
||
defp get_relatorio_periodo!(relatorio) do | ||
relatorio.ano | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. talvez seja melhor depois criar 3 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 |
There was a problem hiding this comment.
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: