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

fix: attendee credential association #434

Merged
merged 16 commits into from
Nov 6, 2024
57 changes: 57 additions & 0 deletions lib/safira/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,27 @@ defmodule Safira.Accounts do
|> Repo.update()
end

@doc """
Links a credential to an attendee.

## Examples

iex> link_credential(credential_id, attendee_id)
{:ok, %Credential{}}

iex> link_credential(credential_id, attendee_id)
{:error, %Ecto.Changeset{}}

"""
def link_credential(credential_id, attendee_id) do
credential = get_credential!(credential_id)
attendee = get_attendee!(attendee_id)

credential
|> Credential.changeset(%{attendee_id: attendee.id})
|> Repo.update()
end

@doc """
Deletes a credential.

Expand Down Expand Up @@ -614,6 +635,42 @@ defmodule Safira.Accounts do
Credential.changeset(credential, attrs)
end

@doc """
Checks if a credential exists.

## Examples

iex> credential_exists?(123)
true

iex> credential_exists?(456)
false

"""
def credential_exists?(id) do
Credential
|> where([c], c.id == ^id)
|> Repo.exists?()
end

@doc """
Checks if a credential is linked to an attendee.

## Examples

iex> credential_linked?(credential_id)
true

iex> credential_linked?(credential_id)
false

"""
def credential_linked?(credential_id) do
credential = get_credential!(credential_id)

credential.attendee_id != nil
end

@doc """
Gets a single credential associated to the given attendee.

Expand Down
2 changes: 1 addition & 1 deletion lib/safira_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</button>
</div>
<main class="text-light bg-primaryDark flex-1 relative z-0 overflow-y-auto focus:outline-none">
<div class="px-4 sm:px-6 lg:px-8 py-8">
<div class="px-6 sm:px-6 lg:px-8 py-8">
<.flash_group flash={@flash} />
<%= @inner_content %>
</div>
Expand Down
4 changes: 2 additions & 2 deletions lib/safira_web/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule SafiraWeb.Helpers do

case URI.parse(url) do
%URI{host: host, path: path} ->
if host == app_host or Mix.env() == :dev do
if (host == app_host or Mix.env() == :dev) and not is_nil(path) do
case extract_id_from_url_path(path) do
:error -> {:error, "not a valid id"}
result -> result
Expand Down Expand Up @@ -154,7 +154,7 @@ defmodule SafiraWeb.Helpers do
end

def draw_qr_code(qr_code) do
internal_route = "/qr_codes/#{qr_code.id}"
internal_route = "/app/attendees/#{qr_code.id}"
url = build_url() <> internal_route

url
Expand Down
48 changes: 48 additions & 0 deletions lib/safira_web/live/app/credential_live/edit.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule SafiraWeb.App.CredentialLive.Edit do
use SafiraWeb, :app_view

alias Safira.Accounts

@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end

@impl true
def handle_params(_params, _url, socket) do
{:noreply, socket |> assign(:modal_data, nil)}
end

@impl true
def handle_event("scan", data, socket) do
case safely_extract_id_from_url(data) do
{:ok, id} ->
if Accounts.credential_exists?(id) do
if Accounts.credential_linked?(id) do
{:noreply, socket |> assign(:modal_data, :already_linked)}
else
Accounts.link_credential(id, socket.assigns.current_user.attendee.id)
{:noreply, socket |> push_navigate(to: ~p"/app")}
end
else
{:noreply, socket |> assign(:modal_data, :not_found)}
end

{:error, _} ->
{:noreply, socket |> assign(:modal_data, :invalid)}
end
end

@impl true
def handle_event("close-modal", _, socket) do
{:noreply, socket |> assign(:modal_data, nil)}
end

def error_message(:not_found),
do: gettext("This credential is not registered in the event's system! (404)")

def error_message(:already_linked),
do: gettext("This credential is already linked to another attendee! (400)")

def error_message(:invalid), do: gettext("Not a valid credential! (400)")
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<.page title="Identify an attendee">
<.page title="Link Credential" size={:xl} title_class="font-terminal uppercase">
<div
id="qr-scanner"
phx-hook="QrScanner"
Expand Down Expand Up @@ -33,10 +33,8 @@
<div class="flex flex-row gap-4 items-center">
<.icon name="hero-x-circle" class="text-red-500 w-8" />
<p>
<%= if @modal_data == :not_found do %>
<%= gettext("This credential is not associated with an attendee!") %>
<% else %>
<%= gettext("Not a valid credential!") %>
<%= if @modal_data do %>
<%= error_message(@modal_data) %>
<% end %>
</p>
</div>
Expand Down
16 changes: 16 additions & 0 deletions lib/safira_web/live/app/credential_live/index.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule SafiraWeb.App.CredentialLive.Index do
use SafiraWeb, :app_view

alias Safira.Accounts

@impl true
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:current_page, :credential)
|> assign(
:credential,
Accounts.get_credential_of_attendee!(socket.assigns.current_user.attendee)
)}
end
end
13 changes: 13 additions & 0 deletions lib/safira_web/live/app/credential_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<.page title="Credential" size={:xl} title_class="font-terminal uppercase">
<p>
<%= gettext(
"The code below is used to identify you in case you are missing your physical credential."
) %>
</p>
<div class="mt-24 mx-auto w-fit scale-125 sm:scale-150 p-1 bg-white rounded-xl select-none">
<%= draw_qr_code(@credential) |> raw %>
<p class="text-center text-xs text-primaryDark font-semibold pb-1">
<%= @current_user.name %>
</p>
</div>
</.page>
33 changes: 0 additions & 33 deletions lib/safira_web/live/backoffice/scanner_live/index.ex

This file was deleted.

37 changes: 0 additions & 37 deletions lib/safira_web/live/credential_live/edit.ex

This file was deleted.

17 changes: 0 additions & 17 deletions lib/safira_web/live/credential_live/edit.html.heex

This file was deleted.

16 changes: 0 additions & 16 deletions lib/safira_web/live/credential_live/index.ex

This file was deleted.

44 changes: 0 additions & 44 deletions lib/safira_web/live/credential_live/show.ex

This file was deleted.

5 changes: 0 additions & 5 deletions lib/safira_web/live/credential_live/show.html.heex

This file was deleted.

Loading
Loading