Commit badb2d89 authored by Henri Jones's avatar Henri Jones

Move summation into the database

parent 4696f452
Pipeline #62871263 passed with stages
in 8 minutes and 42 seconds
...@@ -275,17 +275,10 @@ runMech (DeletePledgeI pptr) = do ...@@ -275,17 +275,10 @@ runMech (DeletePledgeI pptr) = do
runMech FetchProjectI = do runMech FetchProjectI = do
numPledges <- count [PatronPledgeSince !=. Nothing] numPledges <- count [PatronPledgeSince !=. Nothing]
-- Persistent terrible SQL :| receivable <- Skeleton.sumField PatronDonationPayable
receivable <-
fmap
(sum . map (Model.patronDonationPayable . entityVal))
(selectList [] [])
-- This should be verified against Stripe. This calculation is nothing more -- This should be verified against Stripe. This calculation is nothing more
-- than a "guess". -- than a "guess".
received <- received <- Skeleton.sumField DonationHistoryAmount
fmap
(sum . map (Model.donationHistoryAmount . entityVal))
(selectList [] [])
let pledgevalue = DonationUnits (fromIntegral numPledges) let pledgevalue = DonationUnits (fromIntegral numPledges)
income = unitsToCents (pledgevalue * pledgevalue) income = unitsToCents (pledgevalue * pledgevalue)
......
{-# LANGUAGE TypeFamilies #-}
-- | This is the closet, where we keep the skeletons. -- | This is the closet, where we keep the skeletons.
-- --
-- Esqueleto produces the most wicked type errors when things go awry. -- Esqueleto produces the most wicked type errors when things go awry.
...@@ -48,3 +50,19 @@ patronsReceivable minBal = ...@@ -48,3 +50,19 @@ patronsReceivable minBal =
where_ (not_ (isNothing (p ^. PatronPaymentToken)) where_ (not_ (isNothing (p ^. PatronPaymentToken))
&&. (p ^. PatronDonationPayable >=. val minBal)) &&. (p ^. PatronDonationPayable >=. val minBal))
return p return p
sumField
:: ( PersistEntityBackend val ~ SqlBackend
, PersistEntity val
, PersistField a
, Num a
, MonadIO m
)
=> EntityField val a
-> SqlPersistT m a
sumField f = do
[row] <-
select $
from $ \entity ->
return $ coalesceDefault [sum_ (entity ^. f)] $ val 0
return $ unValue row
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment