From 45d905b3841a9164aa71c5f299e87aaabe22ca41 Mon Sep 17 00:00:00 2001 From: shibao Date: Tue, 19 Apr 2022 19:38:55 -0400 Subject: [PATCH] Fix bug with average price per round calculation --- CHANGELOG.md | 1 + lib/cannery/ammo.ex | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bab8246..269fe3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # v0.5.0 - Add German translation: Thank you [Kaia](https://shitposter.club/users/kaia)! - Fix not being able to edit ammo group when fully used up +- Fix bug with average price per round calculation # v0.4.1 - Fix button and tag text wrapping diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index 122adb4..00988b6 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -6,6 +6,7 @@ defmodule Cannery.Ammo do import Ecto.Query, warn: false alias Cannery.{Accounts.User, Containers, Repo} alias Cannery.Ammo.{AmmoGroup, AmmoType} + alias Cannery.ActivityLog.ShotGroup alias Ecto.Changeset @ammo_group_create_limit 10_000 @@ -48,24 +49,29 @@ defmodule Cannery.Ammo do ## Examples - iex> get_ammo_type!(123, %User{id: 123}) - %AmmoType{} - - iex> get_ammo_type!(456, %User{id: 123}) - ** (Ecto.NoResultsError) + iex> get_average_cost_for_ammo_type!(%AmmoType{id: 123}, %User{id: 123}) + 1.50 """ - @spec get_average_cost_for_ammo_type!(AmmoType.t(), User.t()) :: float() + @spec get_average_cost_for_ammo_type!(AmmoType.t(), User.t()) :: float() | nil def get_average_cost_for_ammo_type!( %AmmoType{id: ammo_type_id, user_id: user_id}, %User{id: user_id} ) do + sg_total_query = + from sg in ShotGroup, + where: not (sg.count |> is_nil()), + group_by: sg.ammo_group_id, + select: %{ammo_group_id: sg.ammo_group_id, total: sum(sg.count)} + Repo.one!( from ag in AmmoGroup, - left_join: sg in assoc(ag, :shot_groups), + as: :ammo_group, + left_join: sg_query in subquery(sg_total_query), + on: ag.id == sg_query.ammo_group_id, where: ag.ammo_type_id == ^ammo_type_id, where: not (ag.price_paid |> is_nil()), - select: sum(ag.price_paid) / (sum(ag.count) + sum(sg.count)) + select: sum(ag.price_paid) / sum(ag.count + coalesce(sg_query.total, 0)) ) end