|
| 1 | +defmodule SafiraWeb.Backoffice.ScannerLive.EnrolmentLive.Index do |
| 2 | + use SafiraWeb, :backoffice_view |
| 3 | + |
| 4 | + alias Safira.{Accounts, Activities} |
| 5 | + |
| 6 | + @impl true |
| 7 | + def render(assigns) do |
| 8 | + ~H""" |
| 9 | + <div> |
| 10 | + <div class="-translate-y-4 sm:translate-y-0"> |
| 11 | + <.page> |
| 12 | + <div class="absolute flex justify-center inset-0 z-10 top-20 select-none"> |
| 13 | + <span class="bg-dark text-light dark:bg-light dark:text-dark py-4 px-6 rounded-full font-semibold text-xl h-min"> |
| 14 | + <%= gettext("Checking enrolments for %{activity_name}", activity_name: @activity.title) %> |
| 15 | + </span> |
| 16 | + </div> |
| 17 | + <div |
| 18 | + id="qr-scanner" |
| 19 | + phx-hook="QrScanner" |
| 20 | + data-ask_perm="permission-button" |
| 21 | + data-open_on_mount |
| 22 | + data-on_start="document.getElementById('scan-info').style.display = 'none'" |
| 23 | + data-on_success="scan" |
| 24 | + class="relative" |
| 25 | + > |
| 26 | + </div> |
| 27 | + <div id="scan-info" class="flex flex-col items-center gap-8 text-center py-40"> |
| 28 | + <p id="loadingMessage"> |
| 29 | + <%= gettext("Unable to access camera.") %> |
| 30 | + <%= gettext( |
| 31 | + "Make sure you allow the use of your camera on this browser and that it isn't being used elsewhere." |
| 32 | + ) %> |
| 33 | + </p> |
| 34 | + <.button id="permission-button" type="button"> |
| 35 | + <%= gettext("Request Permission") %> |
| 36 | + </.button> |
| 37 | + </div> |
| 38 | + </.page> |
| 39 | + </div> |
| 40 | + <.modal |
| 41 | + :if={@modal_data != nil} |
| 42 | + id="modal-scan-error" |
| 43 | + show |
| 44 | + on_cancel={JS.push("close-modal")} |
| 45 | + wrapper_class="px-4" |
| 46 | + > |
| 47 | + <div class="flex flex-row gap-4 items-center"> |
| 48 | + <.icon name="hero-x-circle" class="text-red-500 w-8" /> |
| 49 | + <p> |
| 50 | + <%= if @modal_data do %> |
| 51 | + <%= error_message(@modal_data) %> |
| 52 | + <% end %> |
| 53 | + </p> |
| 54 | + </div> |
| 55 | + </.modal> |
| 56 | + </div> |
| 57 | + """ |
| 58 | + end |
| 59 | + |
| 60 | + @impl true |
| 61 | + def mount(_params, _session, socket) do |
| 62 | + {:ok, |
| 63 | + socket |
| 64 | + |> assign(:current_page, :scanner) |
| 65 | + |> assign(:modal_data, nil) |
| 66 | + |> assign(:given_list, [])} |
| 67 | + end |
| 68 | + |
| 69 | + @impl true |
| 70 | + def handle_params(%{"id" => id}, _url, socket) do |
| 71 | + activity = Activities.get_activity!(id) |
| 72 | + {:noreply, socket |> assign(:activity, activity)} |
| 73 | + end |
| 74 | + |
| 75 | + @impl true |
| 76 | + def handle_event("scan", data, socket) do |
| 77 | + case safely_extract_id_from_url(data) do |
| 78 | + {:ok, id} -> process_scan(id, socket) |
| 79 | + {:error, _} -> {:noreply, assign(socket, :modal_data, :invalid)} |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + @impl true |
| 84 | + def handle_event("close-modal", _, socket) do |
| 85 | + {:noreply, socket |> assign(:modal_data, nil)} |
| 86 | + end |
| 87 | + |
| 88 | + defp process_scan(id, socket) do |
| 89 | + if id in socket.assigns.given_list do |
| 90 | + {:noreply, socket} |
| 91 | + else |
| 92 | + check_credential(id, socket) |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + defp check_credential(id, socket) do |
| 97 | + if Accounts.credential_exists?(id) do |
| 98 | + handle_attendee_lookup(id, socket) |
| 99 | + else |
| 100 | + {:noreply, assign(socket, :modal_data, :not_found)} |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + defp handle_attendee_lookup(id, socket) do |
| 105 | + case Accounts.get_attendee_from_credential(id) do |
| 106 | + nil -> |
| 107 | + {:noreply, assign(socket, :modal_data, :not_linked)} |
| 108 | + |
| 109 | + attendee -> |
| 110 | + handle_enrol_check( |
| 111 | + %{ |
| 112 | + activity_id: socket.assigns.activity.id, |
| 113 | + attendee_id: attendee.id, |
| 114 | + credential_id: id |
| 115 | + }, |
| 116 | + socket |
| 117 | + ) |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + defp handle_enrol_check( |
| 122 | + %{activity_id: activity_id, attendee_id: attendee_id, credential_id: credential_id}, |
| 123 | + socket |
| 124 | + ) do |
| 125 | + if Activities.attendee_enrolled?(activity_id, attendee_id) do |
| 126 | + {:noreply, |
| 127 | + socket |
| 128 | + |> assign(:modal_data, nil) |
| 129 | + |> assign(:given_list, [credential_id | socket.assigns.given_list])} |
| 130 | + else |
| 131 | + {:noreply, |
| 132 | + socket |
| 133 | + |> assign(:modal_data, :not_enrolled)} |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + defp error_message(:not_enrolled), |
| 138 | + do: gettext("Attendee not enrolled! (404)") |
| 139 | + |
| 140 | + defp error_message(:not_found), |
| 141 | + do: gettext("This credential is not registered in the event's system! (404)") |
| 142 | + |
| 143 | + defp error_message(:not_linked), |
| 144 | + do: gettext("This credential is not linked to any attendee! (400)") |
| 145 | + |
| 146 | + defp error_message(:invalid), do: gettext("Not a valid credential! (400)") |
| 147 | +end |
0 commit comments