Skip to content

Commit

Permalink
Atualizar elixir para 1.18 (#247)
Browse files Browse the repository at this point in the history
* upgrade: elixir nix

* fix: type check errors

* upgrade: elixir in infra
  • Loading branch information
zoedsoupe authored Dec 20, 2024
1 parent ec684be commit 55ca8d5
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 62 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
MIX_ENV: dev
strategy:
matrix:
otp: [26.2.5]
elixir: [1.16.2]
otp: [27.1.2]
elixir: [1.18.0]
steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -71,8 +71,8 @@ jobs:
MIX_ENV: test
strategy:
matrix:
otp: [26.2.5]
elixir: [1.16.2]
otp: [27.1.2]
elixir: [1.18.0]
services:
postgres:
image: postgres:15
Expand Down
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
elixir 1.16.2-otp-26
elixir 1.18.0-otp-27
postgres 14.6
erlang 27.0.1
erlang 27.1.2
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ARG ELIXIR_VERSION=1.16.0
ARG OTP_VERSION=26.1.2
ARG ALPINE_VERSION=3.18.4
ARG ELIXIR_VERSION=1.18.0
ARG OTP_VERSION=27.1.2
ARG ALPINE_VERSION=3.19.4
ARG MIX_ENV=prod

ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-alpine-${ALPINE_VERSION}"
Expand Down
11 changes: 6 additions & 5 deletions assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description = "Plataforma Digital PEA Pescarte";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
flake-parts.url = "github:hercules-ci/flake-parts";
systems.url = "github:nix-systems/default";
};
Expand All @@ -19,9 +19,19 @@
system,
...
}: let
inherit (pkgs.beam.interpreters) erlangR26;
inherit (pkgs.beam.interpreters) erlang_27;
inherit (pkgs.beam) packagesWith;
beam = packagesWith erlangR26;
beam = packagesWith erlang_27;

elixir_1_18 = beam.elixir.override {
version = "1.18.0";
src = pkgs.fetchFromGitHub {
repo = "elixir";
owner = "elixir-lang";
rev = "v1.18.0";
sha256 = "fT3J8h2uuJ+dSR58kwlUkN023yFlmTwq2/O12KbjJc4=";
};
};
in {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
Expand All @@ -31,7 +41,7 @@
mkShell {
name = "peapescarte";
packages = with pkgs;
[beam.elixir_1_16 nodejs supabase-cli ghostscript zlib postgresql flyctl pass]
[elixir_1_18 nodejs supabase-cli ghostscript zlib postgresql flyctl pass]
++ lib.optional stdenv.isLinux [inotify-tools chromium]
++ lib.optional stdenv.isDarwin [
darwin.apple_sdk.frameworks.CoreServices
Expand Down
5 changes: 2 additions & 3 deletions lib/pescarte.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
defmodule Pescarte do
@environment Application.compile_env!(:pescarte, :env)

@spec env :: :dev | :test | :prod
def env do
@environment
Application.get_env(:pescarte, :env)
end

@spec get_supabase_client :: {:ok, Supabase.Client.t()} | {:error, term}
Expand Down
22 changes: 22 additions & 0 deletions lib/pescarte/identidades/adapters/usuario_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ defmodule Pescarte.Identidades.Adapters.UsuarioAdapter do

@contato_fields ~w(email_principal celular_principal emails_adicionais celulares_adicionais endereco)a

@type external :: %{
role: String.t(),
phone: String.t(),
email: String.t(),
password: String.t(),
app_metadata: %{
cpf: String.t(),
rg: String.t(),
contato: %{
email_principal: String.t(),
celular_principal: String.t(),
emails_adicionais: list(String.t()),
celulares_adicionais: list(String.t()),
endereco: String.t()
},
primeiro_nome: String.t(),
sobrenome: String.t(),
nome_completo: String.t()
}
}

@spec to_external(Usuario.t(), String.t()) :: external
def to_external(%Usuario{} = usuario, pass \\ @default_pass) do
%{
role: Atom.to_string(usuario.papel),
Expand Down
19 changes: 11 additions & 8 deletions lib/pescarte/identidades/register_usuario.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Pescarte.Identidades.RegisterUsuario do
Repo.transaction(fn ->
attrs = Map.put(attrs, :papel, tipo)
{:ok, internal} = do_create_usuario(attrs)
{:ok, external} = UsuarioAdapter.to_external(internal)
%{} = external = UsuarioAdapter.to_external(internal)
{:ok, client} = Pescarte.get_supabase_client()
{:ok, _} = GoTrue.Admin.create_user(client, external)
internal
Expand All @@ -17,19 +17,22 @@ defmodule Pescarte.Identidades.RegisterUsuario do

def run(attrs) do
Repo.transaction(fn ->
{:ok, internal} = do_create_usuario(attrs)
{:ok, external} = UsuarioAdapter.to_external(internal)
{:ok, %Usuario{} = internal} = do_create_usuario(attrs)
%{} = external = UsuarioAdapter.to_external(internal)
{:ok, client} = Pescarte.get_supabase_client()
{:ok, _} = GoTrue.Admin.create_user(client, external)
internal
end)
end

@spec do_create_usuario(map) :: {:ok, Usuario.t()} | {:error, Ecto.Changeset.t()}
defp do_create_usuario(attrs) do
%Usuario{}
|> Usuario.changeset(attrs)
|> Usuario.password_changeset(attrs)
|> Repo.insert()
|> Repo.preload([:contato])
with {:ok, usuario} <-
%Usuario{}
|> Usuario.changeset(attrs)
|> Usuario.password_changeset(attrs)
|> Repo.insert() do
Repo.preload(usuario, [:contato])
end
end
end
21 changes: 6 additions & 15 deletions lib/pescarte/modulo_pesquisa/adapters/relatorio_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,26 @@ defmodule Pescarte.ModuloPesquisa.Adapters.RelatorioAdapter do
import Timex.Format.DateTime.Formatter, only: [lformat!: 3]

alias Pescarte.Identidades.Models.Usuario
alias Pescarte.ModuloPesquisa.Models.RelatorioAnualPesquisa, as: Anual
alias Pescarte.ModuloPesquisa.Models.RelatorioMensalPesquisa, as: Mensal
alias Pescarte.ModuloPesquisa.Models.RelatorioTrimestralPesquisa, as: Trimestral
alias Pescarte.ModuloPesquisa.Schemas.RelatorioPesquisa
alias Pescarte.ModuloPesquisa.Models.RelatorioPesquisa, as: Model
alias Pescarte.ModuloPesquisa.Schemas.RelatorioPesquisa, as: Schema

@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}
def internal_to_external(%{pesquisador: pesquisador} = relatorio) do
@spec internal_to_external!(Model.t()) :: Schema.t() | no_return
def internal_to_external!(%{pesquisador: pesquisador} = relatorio) do
attrs = %{
id: relatorio.id,
status: relatorio.status,
data: relatorio.data_entrega,
link: relatorio.link,
tipo: get_relatorio_tipo(relatorio),
tipo: relatorio.tipo,
periodo: get_relatorio_periodo!(relatorio),
nome_pesquisador: Usuario.build_usuario_name(pesquisador.usuario)
}

RelatorioPesquisa.parse!(attrs)
Schema.parse!(attrs)
end

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
|> Date.new!(relatorio.mes, 1)
Expand Down
4 changes: 2 additions & 2 deletions lib/pescarte/modulo_pesquisa/handlers/relatorios_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ defmodule Pescarte.ModuloPesquisa.Handlers.RelatoriosHandler do
id
|> Repository.list_relatorios_pesquisa_from_pesquisador()
|> Enum.sort_by(sorter)
|> Enum.map(&RelatorioAdapter.internal_to_external/1)
|> Enum.map(&RelatorioAdapter.internal_to_external!/1)
end

@impl true
def list_relatorios(sorter \\ &sort_by_periodo/1) do
%{}
|> Repository.list_relatorios_pesquisa()
|> Enum.sort_by(sorter)
|> Enum.map(&RelatorioAdapter.internal_to_external/1)
|> Enum.map(&RelatorioAdapter.internal_to_external!/1)
end

defp sort_by_periodo(relatorio) do
Expand Down
18 changes: 14 additions & 4 deletions lib/pescarte_web/controllers/relatorio_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule PescarteWeb.Pesquisa.RelatorioController do
alias Pescarte.RelatorioCompiler
alias PescarteWeb.Pesquisa.RelatorioLive

require Logger

def download_pdf(conn, %{"id" => id}) do
case RelatorioCompiler.gerar_pdf(id) do
{:ok, pdf_filename, pdf_binary} ->
Expand All @@ -20,7 +22,8 @@ defmodule PescarteWeb.Pesquisa.RelatorioController do
)

{:error, reason} ->
conn |> put_status(:bad_request) |> send(inspect(reason))
Logger.error("failed with #{inspect(reason, pretty: true)}")
conn |> put_status(:internal_error) |> send_resp()
end
end

Expand All @@ -31,9 +34,16 @@ defmodule PescarteWeb.Pesquisa.RelatorioController do
relatorios_selecionados,
&send_relatorios_compiled(&1, conn)
) do
{:ok, {:error, reason}} -> conn |> put_status(:bad_request) |> send(inspect(reason))
{:ok, {:ok, conn}} -> conn
{:error, reason} -> conn |> put_status(:bad_request) |> send(inspect(reason))
{:ok, {:error, reason}} ->
Logger.error("failed with #{inspect(reason, pretty: true)}")
send_resp(conn, :internal_error, "")

{:ok, {:ok, conn}} ->
conn

{:error, reason} ->
Logger.error("failed with #{inspect(reason, pretty: true)}")
send_resp(conn, :internal_error, "")
end
end

Expand Down
Loading

0 comments on commit 55ca8d5

Please sign in to comment.