diff --git a/lib/safira/interaction/interaction.ex b/lib/safira/interaction/interaction.ex index c0215b60..d700122b 100644 --- a/lib/safira/interaction/interaction.ex +++ b/lib/safira/interaction/interaction.ex @@ -208,6 +208,15 @@ defmodule Safira.Interaction do Repo.transaction(multi) |> case do {:ok, result} -> + Phoenix.PubSub.broadcast(Safira.PubSub, "spotlight", %{ + "spotlight" => %{ + "id" => result.update_company.badge_id, + "name" => result.update_company.name, + "badge_id" => result.update_company.badge_id, + "end" => result.upsert_spotlight.end + } + }) + result {:error, _failed_operation, changeset, _changes_so_far} -> diff --git a/lib/safira_web/channels/spotlight_channel.ex b/lib/safira_web/channels/spotlight_channel.ex new file mode 100644 index 00000000..9225a7ef --- /dev/null +++ b/lib/safira_web/channels/spotlight_channel.ex @@ -0,0 +1,35 @@ +defmodule SafiraWeb.SpotlightChannel do + @moduledoc """ + Phoenix Channel to handle spotlight creations + """ + use Phoenix.Channel + + alias Safira.Accounts + alias Safira.Interaction + + def join("spotlight", _params, socket) do + Phoenix.PubSub.subscribe(Safira.PubSub, "spotlight") + send(self(), :after_join) + {:ok, socket} + end + + def handle_info(:after_join, socket) do + spotlight = Interaction.get_spotlight() + company = Accounts.get_company_by_badge!(spotlight.badge_id) + + data = %{ + "id" => company.id, + "name" => company.name, + "badge_id" => company.badge_id, + "end" => spotlight.end + } + + push(socket, "spotlight", data) + {:noreply, socket} + end + + def handle_info(%{"spotlight" => body}, socket) do + push(socket, "spotlight", body) + {:noreply, socket} + end +end diff --git a/lib/safira_web/channels/user_socket.ex b/lib/safira_web/channels/user_socket.ex index ef100a89..f737a1c2 100644 --- a/lib/safira_web/channels/user_socket.ex +++ b/lib/safira_web/channels/user_socket.ex @@ -2,7 +2,7 @@ defmodule SafiraWeb.UserSocket do use Phoenix.Socket ## Channels - # channel "room:*", SafiraWeb.RoomChannel + channel "spotlight", SafiraWeb.SpotlightChannel # Socket params are passed from the client and can # be used to verify and authenticate a user. After