cannery/lib/cannery_web/controllers/export_controller.ex

80 lines
2.6 KiB
Elixir
Raw Normal View History

2022-11-09 23:33:50 -05:00
defmodule CanneryWeb.ExportController do
use CanneryWeb, :controller
alias Cannery.{ActivityLog, Ammo, Containers}
def export(%{assigns: %{current_user: current_user}} = conn, %{"mode" => "json"}) do
2023-06-03 20:14:20 -04:00
types = Ammo.list_types(current_user)
used_counts =
ActivityLog.get_grouped_used_counts(current_user, types: types, group_by: :type_id)
2023-06-05 22:08:42 -04:00
round_counts = Ammo.get_grouped_round_count(current_user, types: types, group_by: :type_id)
2023-06-05 18:29:36 -04:00
pack_counts = Ammo.get_grouped_packs_count(current_user, types: types, group_by: :type_id)
2023-06-05 18:29:36 -04:00
total_pack_counts =
Ammo.get_grouped_packs_count(current_user, types: types, group_by: :type_id, show_used: true)
2023-03-30 21:53:52 -04:00
average_costs = types |> Ammo.get_average_cost_for_types(current_user)
2022-11-09 23:33:50 -05:00
2023-03-30 21:53:52 -04:00
types =
types
|> Enum.map(fn %{id: type_id} = type ->
type
2022-11-09 23:33:50 -05:00
|> Jason.encode!()
|> Jason.decode!()
|> Map.merge(%{
2023-03-30 21:53:52 -04:00
"average_cost" => Map.get(average_costs, type_id),
"round_count" => Map.get(round_counts, type_id, 0),
"used_count" => Map.get(used_counts, type_id, 0),
"pack_count" => Map.get(pack_counts, type_id, 0),
"total_pack_count" => Map.get(total_pack_counts, type_id, 0)
2022-11-09 23:33:50 -05:00
})
end)
2023-06-04 00:00:51 -04:00
packs = Ammo.list_packs(current_user, show_used: true)
used_counts =
ActivityLog.get_grouped_used_counts(current_user, packs: packs, group_by: :pack_id)
2023-03-29 22:54:55 -04:00
original_counts = packs |> Ammo.get_original_counts(current_user)
cprs = packs |> Ammo.get_cprs(current_user)
percentages_remaining = packs |> Ammo.get_percentages_remaining(current_user)
2023-03-29 22:54:55 -04:00
packs =
packs
|> Enum.map(fn %{id: pack_id} = pack ->
pack
2022-11-09 23:33:50 -05:00
|> Jason.encode!()
|> Jason.decode!()
|> Map.merge(%{
2023-03-29 22:54:55 -04:00
"used_count" => Map.get(used_counts, pack_id),
"percentage_remaining" => Map.fetch!(percentages_remaining, pack_id),
"original_count" => Map.get(original_counts, pack_id),
"cpr" => Map.get(cprs, pack_id)
2022-11-09 23:33:50 -05:00
})
end)
2023-06-05 21:47:03 -04:00
shot_records = ActivityLog.list_shot_records(current_user)
2022-11-09 23:33:50 -05:00
containers =
Containers.list_containers(current_user)
|> Enum.map(fn container ->
container
|> Jason.encode!()
|> Jason.decode!()
|> Map.merge(%{
2023-06-05 20:47:12 -04:00
"pack_count" => Ammo.get_packs_count(current_user, container_id: container.id),
"round_count" => Ammo.get_round_count(current_user, container_id: container.id)
2022-11-09 23:33:50 -05:00
})
end)
json(conn, %{
user: current_user,
2023-03-30 21:53:52 -04:00
types: types,
2023-03-29 22:54:55 -04:00
packs: packs,
2023-03-30 20:43:30 -04:00
shot_records: shot_records,
2022-11-09 23:33:50 -05:00
containers: containers
})
end
end