1 {-# LANGUAGE TemplateHaskell #-}
    2 module Model.Notification.SQL
    3   ( selectNotifyDelivery
    4   , selectTargetNotification
    5   , selectNotification
    6   , selectPartyAuthorizationNotify
    7   ) where
    8 
    9 import qualified Language.Haskell.TH as TH
   10 
   11 import Has
   12 import Model.SQL.Select
   13 import Model.Time
   14 import Model.Id.Types
   15 import Model.Permission.Types
   16 import Model.Release.Types
   17 import Model.Party.Types
   18 import Model.Party.SQL
   19 import Model.Volume.Types
   20 import Model.Volume.SQL
   21 import Model.Container.Types
   22 import Model.Segment
   23 import Model.Asset.Types
   24 import Model.Tag.Types
   25 import Model.Tag.SQL
   26 import Model.Comment.Types
   27 import Model.Notification.Types
   28 
   29 selectNotifyDelivery :: Selector
   30 selectNotifyDelivery = selectMap (TH.VarE 'fromMaybeDelivery `TH.AppE`) $ selectColumn "notify_view" "delivery"
   31 
   32 makePartyAuthorizationNotice :: (Party, Maybe Permission) -> Delivery -> (Party, Maybe Permission, Delivery)
   33 makePartyAuthorizationNotice (p, a) d = (p, a, d)
   34 
   35 selectPartyAuthorizationNotify :: TH.Name -- ^ 'Identity'
   36   -> Selector -- ^ @('Party', Maybe 'Permission', 'Delivery')@
   37 selectPartyAuthorizationNotify ident = selectJoin 'makePartyAuthorizationNotice
   38   [ selectPartyAuthorization ident
   39   , joinOn "id = target"
   40     selectNotifyDelivery
   41   ]
   42 
   43 makeNotification :: Id Notification -> Notice -> Timestamp -> Delivery -> Maybe Permission -> Maybe (Id Container) -> Maybe Segment -> Maybe (Id Asset) -> Maybe Release -> Maybe (Id Comment) -> PartyRow -> Maybe PartyRow -> Maybe VolumeRow -> Maybe Tag -> Account -> Notification
   44 makeNotification i n t d e c s a r m w p v g u = Notification i (view u) n t d w p v e c s a r m g
   45 
   46 notificationRow :: Selector -- ^ @'PartyRow' -> Maybe 'PartyRow' -> Maybe 'VolumeRow' -> Maybe 'Tag' -> 'Account' -> 'Notification'@
   47 notificationRow = selectColumns 'makeNotification "notification" ["id", "notice", "time", "delivered", "permission", "container", "segment", "asset", "release", "comment"]
   48 
   49 selectTargetNotification :: Selector -- ^ @'Account' -> 'Notification'@
   50 selectTargetNotification = selectJoin '($)
   51   [ notificationRow
   52   , joinOn "notification.agent = agent.id"
   53     $ selectPartyRow `fromAlias` "agent"
   54   , maybeJoinOn "notification.party = nparty.id"
   55     $ selectPartyRow `fromAlias` "nparty"
   56   , maybeJoinOn "notification.volume = volume.id" selectVolumeRow
   57   , maybeJoinOn "notification.tag = tag.id" selectTag
   58   ]
   59 
   60 selectNotification :: Selector -- ^ @'Notification'@
   61 selectNotification = selectJoin '($)
   62   [ selectTargetNotification
   63   , joinOn "notification.target = account.id"
   64     selectUserAccount
   65   ]