Skip to content

Commit

Permalink
feat: csv generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ruioliveira02 committed Jan 2, 2025
1 parent 77e4d4e commit 8d7efd7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/safira/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Safira.Event do
@moduledoc """
The event context.
"""
alias Safira.Accounts
alias Safira.Constants

@doc """
Expand Down
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\"")
|> 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"/dashboard")
|> 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
end
7 changes: 5 additions & 2 deletions lib/safira_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ defmodule SafiraWeb.Router do
live "/vault", VaultLive.Index, :index
end

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

scope "/dashboard", Backoffice do
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 @@ -83,6 +83,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 @@ -36,6 +36,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

0 comments on commit 8d7efd7

Please sign in to comment.