Skip to content

Commit

Permalink
Bugfix/several bugfixes (#135)
Browse files Browse the repository at this point in the history
* fix: add link to all nav-items; fix css

* fix: create mask for cpf; create handle to parse cpf string

* fix: validate status of report

---------

Co-authored-by: Zoey de Souza Pessanha <[email protected]>
  • Loading branch information
douglastofoli and zoedsoupe authored Nov 29, 2023
1 parent d883761 commit ebdc6b3
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 43 deletions.
8 changes: 7 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ if ! test -d apps/plataforma_digital/assets/node_modules && test -f apps/platafo
cd apps/plataforma_digital/assets && npm install && cd $PWD
fi

USE_LOCAL_POSTGRES=false

# Setup postgresql
if test -d "/Applications/Postgres.app"; then
export DATABASE_USER="$(whoami)"
export DATABASE_PASSWORD=""
else
elif $USE_LOCAL_POSTGRES; then
# postges related
export DATABASE_USER="peapescarte"
export DATABASE_PASSWORD="peapescarte"
Expand All @@ -45,4 +47,8 @@ else
# creates local databse
echo "CREATE DATABASE $PG_DATABASE;" | postgres --single -E postgres
fi
else
export DATABASE_USER="postgres"
export DATABASE_PASSWORD="postgres"
export PG_DATABASE="peapescarte_dev"
fi
9 changes: 8 additions & 1 deletion apps/identidades/lib/identidades/handlers/usuario_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ defmodule Identidades.Handlers.UsuarioHandler do
"""
@impl true
def fetch_usuario_by_cpf_and_password(cpf, pass) do
with {:ok, user} <- Repository.fetch_usuario_by_cpf(cpf) do
with cpf <- handle_cpf(cpf),
{:ok, user} <- Repository.fetch_usuario_by_cpf(cpf) do
if valid_password?(user, pass) do
{:ok, user}
else
Expand Down Expand Up @@ -103,6 +104,12 @@ defmodule Identidades.Handlers.UsuarioHandler do
end
end

defp handle_cpf(cpf) do
cpf
|> String.replace(~r/[.-]/, "")
|> String.trim()
end

@impl true
defdelegate list_usuario, to: Repository
end
32 changes: 17 additions & 15 deletions apps/identidades/lib/identidades/models/usuario.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,28 @@ defmodule Identidades.Models.Usuario do

@primary_key {:id_publico, Database.Types.PublicId, autogenerate: true}
schema "usuario" do
field :cpf, :string
field :rg, :string
field :confirmado_em, :naive_datetime
field :hash_senha, :string, redact: true
field :senha, :string, virtual: true, redact: true
field :data_nascimento, :date
field :tipo, Ecto.Enum, values: @valid_roles
field :primeiro_nome, :string
field :sobrenome, :string
field :ativo?, :boolean, default: false

has_one :pesquisador, Pesquisador,
field(:cpf, :string)
field(:rg, :string)
field(:confirmado_em, :naive_datetime)
field(:hash_senha, :string, redact: true)
field(:senha, :string, virtual: true, redact: true)
field(:data_nascimento, :date)
field(:tipo, Ecto.Enum, values: @valid_roles)
field(:primeiro_nome, :string)
field(:sobrenome, :string)
field(:ativo?, :boolean, default: false)

has_one(:pesquisador, Pesquisador,
references: :id_publico,
foreign_key: :usuario_id
)

belongs_to :contato, Contato,
belongs_to(:contato, Contato,
foreign_key: :contato_email,
references: :email_principal,
on_replace: :update,
type: :string
)

timestamps()
end
Expand Down Expand Up @@ -85,6 +87,8 @@ defmodule Identidades.Models.Usuario do
|> maybe_hash_password(opts)
end

def user_roles, do: @valid_roles

defp maybe_hash_password(changeset, opts) do
hash_password? = Keyword.get(opts, :hash, true)
password = get_change(changeset, :senha)
Expand All @@ -98,6 +102,4 @@ defmodule Identidades.Models.Usuario do
changeset
end
end

def user_roles, do: @valid_roles
end
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ defmodule ModuloPesquisa.Models.RelatorioPesquisa do
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)
Expand All @@ -67,6 +60,7 @@ defmodule ModuloPesquisa.Models.RelatorioPesquisa do
|> validate_inclusion(:status, @status)
|> foreign_key_constraint(:pesquisador_id)
|> validate_period()
|> validate_status()
end

defp validate_period(changeset) do
Expand All @@ -79,10 +73,26 @@ defmodule ModuloPesquisa.Models.RelatorioPesquisa do

{_, _} ->
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")
add_error(changeset, :data_inicio, "A data de início deve ser anterior a data de fim")
else
changeset
end
end
end

defp validate_status(changeset) do
status = get_field(changeset, :status)

case status do
:entregue ->
add_error(
changeset,
:status,
"O relatório foi marcado como entregue e não é possível fazer novas alterações"
)

_ ->
changeset
end
end
end
30 changes: 21 additions & 9 deletions apps/plataforma_digital/assets/css/navbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@apply flex justify-between items-center;
padding: 1rem 1.5rem;

> svg {
>svg {
margin-left: 0.5rem;
}
}
Expand All @@ -38,7 +38,7 @@
transform: translateZ(0) scale(1, 1);
border-right-width: 1px;

> span {
>span {
@apply border-black-10 rounded-full;

border-width: 1.5px;
Expand All @@ -50,7 +50,7 @@
left: calc($nav-width + 1rem);
z-index: 1000;

> svg {
>svg {
@apply text-blue-100;

width: 1rem;
Expand Down Expand Up @@ -81,11 +81,12 @@
}

.navbar.authenticated.open {
@apply bg-white-100 items-center;
@apply relative bg-white-100 items-center;
z-index: 999;

width: 20rem;

> span {
>span {
left: 21rem;
}

Expand All @@ -103,18 +104,29 @@

.nav-menu {
@apply items-start;
width: 80%;
width: 85%;

.nav-item {
width: 100%;
padding: 1rem 1.5rem;
width: max-content;
justify-content: left;

a {
display: flex;
justify-content: center;
align-items: center;

p {
padding: 0.5rem;
}
}

.logo {
margin: 0;
}
}

.nav-item > svg {
.nav-item>svg {
margin-right: 0.5rem;
}
}
Expand All @@ -123,4 +135,4 @@
width: 12.25rem;
height: 3.8rem;
}
}
}
11 changes: 11 additions & 0 deletions apps/plataforma_digital/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ if (window.location.pathname === "/acessar") {
// Phoenix Hooks
let Hooks = {};

Hooks.CpfNumberMask = {
mounted() {
this.el.addEventListener("input", e => {
let match = this.el.value.replace(/\D/g, "").match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/)
if (match) {
this.el.value = `${match[1]}.${match[2]}.${match[3]}-${match[4]}`
}
})
}
}

Hooks.NavbarHover = {
mounted() {
const navbar = document.querySelector("#auth-navbar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defmodule PlataformaDigital.DesignSystem.AuthenticatedNavbar do
alias Identidades.Models.Usuario
alias PlataformaDigital.DesignSystem

attr :user, Usuario, required: true
attr :open, :boolean, default: nil
attr(:user, Usuario, required: true)
attr(:open, :boolean, default: nil)

@impl true
def render(assigns) do
Expand All @@ -22,8 +22,10 @@ defmodule PlataformaDigital.DesignSystem.AuthenticatedNavbar do
<img :if={@open} src={~p"/images/pescarte_logo.svg"} class="logo" />
</li>
<li class="nav-item">
<Lucideicons.home />
<.text :if={@open} size="base" color="text-black-60">Home</.text>
<DesignSystem.link navigate={~p"/"}>
<Lucideicons.home />
<.text :if={@open} size="base" color="text-black-60">Home</.text>
</DesignSystem.link>
</li>
<li class="nav-item">
<DesignSystem.link navigate={~p"/app/pesquisa/pesquisadores"}>
Expand All @@ -42,12 +44,16 @@ defmodule PlataformaDigital.DesignSystem.AuthenticatedNavbar do
</DesignSystem.link>
</li>
<li class="nav-item">
<Lucideicons.calendar_days />
<.text :if={@open} size="base" color="text-black-60">Agenda</.text>
<DesignSystem.link navigate={~p"/"}>
<Lucideicons.calendar_days />
<.text :if={@open} size="base" color="text-black-60">Agenda</.text>
</DesignSystem.link>
</li>
<li class="nav-item">
<Lucideicons.mail />
<.text :if={@open} size="base" color="text-black-60">Mensagens</.text>
<DesignSystem.link navigate={~p"/"}>
<Lucideicons.mail />
<.text :if={@open} size="base" color="text-black-60">Mensagens</.text>
</DesignSystem.link>
</li>
</ul>
<div class="user-info">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule PlataformaDigital.LoginHTML do
</.text>
<fieldset class="login-fieldset">
<.text_input field={@form[:cpf]} type="text" mask="999.999.999-99" label="CPF" required />
<.text_input field={@form[:cpf]} type="text" label="CPF" required phx-hook="CpfNumberMask" />
</fieldset>
<fieldset class="login-fieldset">
Expand Down

0 comments on commit ebdc6b3

Please sign in to comment.