-- SPDX-FileCopyrightText: 2022-2025 Steffen Jost -- -- SPDX-License-Identifier: AGPL-3.0-or-later {-# LANGUAGE BlockArguments #-} -- do starts is own block {-# OPTIONS_GHC -fno-warn-orphans #-} module Handler.Utils.Company where import Import -- import Data.CaseInsensitive (CI) -- import qualified Data.CaseInsensitive as CI -- import qualified Data.Char as Char -- import qualified Data.Text as Text import Database.Persist.Postgresql import Database.Esqueleto.Experimental ((:&)(..)) import qualified Database.Esqueleto.Experimental as E -- needs TypeApplications Lang-Pragma import qualified Database.Esqueleto.Utils as E import qualified Database.Esqueleto.PostgreSQL as E import Handler.Utils.Users import Handler.Utils.Widgets -- KeyCompany is CompanyShorthand, i.e. CI Text instance E.SqlString (Key Company) -- Snippet to restrict to primary company only -- E.&&. E.notExists (do -- othr <- E.from $ E.table @UserCompany -- E.where_ $ othr E.^. UserCompanyPriority E.>. userCompany E.^. UserCompanyPriority -- E.&&. othr E.^. UserCompanyUser E.==. userCompany E.^. UserCompanyUser -- E.&&. othr E.^. UserCompanyCompany E.!=. E.val cid -- redundant due to > above, but likely performance improving -- ) company2msg :: CompanyId -> SomeMessage UniWorX company2msg = text2message . ciOriginal . unCompanyKey -- for convenience in debugging instance ToText (Maybe CompanyId) where toText Nothing = toText ("-None-"::Text) toText (Just fsh) = toText $ unCompanyKey fsh wgtCompanies :: Bool -> UserId -> DB (Maybe Widget) wgtCompanies useShort = (wrapUL . fst <<$>>) . wgtCompanies' useShort where wrapUL wgt = [whamlet|