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

feat: final draw csv generation #445

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/safira_web/controllers/csv_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule SafiraWeb.CSVController do
use SafiraWeb, :controller
alias NimbleCSV.RFC4180, as: CSV

alias Safira.Accounts

def final_draw(conn, _params) do
if user_authorized?(conn.assigns.current_user) do
conn =
conn
|> put_resp_content_type("text/csv")
|> put_resp_header("content-disposition", "attachment; filename=\"large_data.csv\"")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
|> put_resp_header("content-disposition", "attachment; filename=\"large_data.csv\"")
|> put_resp_header("content-disposition", "attachment; filename=\"final_draw.csv\"")

|> send_chunked(200)

# Stream the data
Accounts.list_attendees()
|> Stream.flat_map(&final_draw_lines/1)
|> Stream.map(&CSV.dump_to_iodata(&1))
|> Enum.reduce(conn, fn chunk, conn ->
case chunk(conn, chunk) do
{:ok, conn} -> conn
{:error, _reason} -> conn
end
end)
else
conn
|> put_flash(:error, "You do not have permission to view this resource")
|> put_status(403)
|> redirect(to: ~p"/")
|> halt()
end
end

defp user_authorized?(user) do
not is_nil(user.staff) and
Enum.member?(Map.get(user.staff.role.permissions, "statistics"), "show")
end

defp final_draw_lines(attendee) do
for _ <- 1..attendee.attendee.entries do
[[attendee.id, attendee.name, attendee.handle]]
end
end
Comment on lines +39 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defp final_draw_lines(attendee) do
for _ <- 1..attendee.attendee.entries do
[[attendee.id, attendee.name, attendee.handle]]
end
end
defp final_draw_lines(user) do
for _ <- 1..user.attendee.entries do
[[user.id, user.name, user.handle]]
end
end

end
3 changes: 3 additions & 0 deletions lib/safira_web/live/backoffice/event_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<.link patch={~p"/dashboard/event/edit"}>
<.button><%= gettext("Edit Settings") %></.button>
</.link>
<.link patch={~p"/downloads/final_draw"} target="_blank">
<.button><%= gettext("Export Final Draw CSV") %></.button>
</.link>
</:actions>

<div class="py-4">
Expand Down
5 changes: 4 additions & 1 deletion lib/safira_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ defmodule SafiraWeb.Router do
live "/vault", VaultLive.Index, :index
end

scope "/dashboard", Backoffice do
scope "/downloads" do
pipe_through [:require_staff_user]
get "/final_draw", CSVController, :final_draw
end

scope "/dashboard", Backoffice do
Comment on lines +114 to +119
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't this remove the :require_staff_user pipeline from the dashboard?

scope "/attendees", AttendeeLive do
live "/", Index, :index
live "/:id", Show, :show
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ defmodule Safira.MixProject do
{:jason, "~> 1.2"},
{:lua, "~> 0.0.14"},
{:timex, "~> 3.7.11"},
{:nimble_csv, "~>1.1"},

# server
{:bandit, "~> 1.2"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"},
"mimerl": {:hex, :mimerl, "1.3.0", "d0cd9fc04b9061f82490f6581e0128379830e78535e017f7780f37fea7545726", [:rebar3], [], "hexpm", "a1e15a50d1887217de95f0b9b0793e32853f7c258a5cd227650889b38839fe9d"},
"mint": {:hex, :mint, "1.6.2", "af6d97a4051eee4f05b5500671d47c3a67dac7386045d87a904126fd4bbcea2e", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "5ee441dffc1892f1ae59127f74afe8fd82fda6587794278d924e4d90ea3d63f9"},
"nimble_csv": {:hex, :nimble_csv, "1.2.0", "4e26385d260c61eba9d4412c71cea34421f296d5353f914afe3f2e71cce97722", [:mix], [], "hexpm", "d0628117fcc2148178b034044c55359b26966c6eaa8e2ce15777be3bbc91b12a"},
"nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"},
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
"parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"},
Expand Down
Loading