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
runMech FetchProjectI = do
numPledges <- count [PatronPledgeSince !=. Nothing]
-- Persistent terrible SQL :|
receivable <-
fmap
(sum . map (Model.patronDonationPayable . entityVal))
(selectList [] [])
receivable <- Skeleton.sumField PatronDonationPayable
-- This should be verified against Stripe. This calculation is nothing more
-- than a "guess".
received <-
fmap
(sum . map (Model.donationHistoryAmount . entityVal))
(selectList [] [])
received <- Skeleton.sumField DonationHistoryAmount
let pledgevalue = DonationUnits (fromIntegral numPledges)
income = unitsToCents (pledgevalue * pledgevalue)
......
{-# LANGUAGE TypeFamilies #-}
-- | This is the closet, where we keep the skeletons.
--
-- Esqueleto produces the most wicked type errors when things go awry.
......@@ -48,3 +50,19 @@ patronsReceivable minBal =
where_ (not_ (isNothing (p ^. PatronPaymentToken))
&&. (p ^. PatronDonationPayable >=. val minBal))
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