Skip to content

Commit

Permalink
Merge branch 'main' of github.com:peapescarte/pescarte
Browse files Browse the repository at this point in the history
  • Loading branch information
Annabell committed Nov 25, 2023
2 parents fee413d + 4338e7b commit 8e747a9
Show file tree
Hide file tree
Showing 34 changed files with 831 additions and 755 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
erlang 25.2.2
elixir 1.14.2
erlang 26.1.2
elixir 1.15.7
postgres 14.6
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

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

@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

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,88 @@
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{status: :entregue} = relatorio, attrs) do
relatorio
|> cast(attrs, @required_fields ++ @optional_fields)
|> add_error(:status, "O relatório já foi entregue")
end

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)
|> 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

0 comments on commit 8e747a9

Please sign in to comment.