| 1 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 2 |
|
| 3 |
{-# LANGUAGE FlexibleInstances #-} |
| 4 |
{-# LANGUAGE NamedFieldPuns #-} |
| 5 |
{-# LANGUAGE OverloadedStrings #-} |
| 6 |
{-# LANGUAGE RecordWildCards #-} |
| 7 |
{-# LANGUAGE TemplateHaskell #-} |
| 8 |
|
| 9 |
module Hledger.Web.Handler.MiscR |
| 10 |
( getVersionR |
| 11 |
, getAccountnamesR |
| 12 |
, getTransactionsR |
| 13 |
, getPricesR |
| 14 |
, getCommoditiesR |
| 15 |
, getAccountsR |
| 16 |
, getAccounttransactionsR |
| 17 |
, getDownloadR |
| 18 |
, getFaviconR |
| 19 |
, getManageR |
| 20 |
, getRobotsR |
| 21 |
, getRootR |
| 22 |
, getOpenApiR |
| 23 |
) where |
| 24 |
|
| 25 |
import Data.Map qualified as M |
| 26 |
import Data.Text qualified as T |
| 27 |
import Data.Yaml qualified as Yaml |
| 28 |
import Data.ByteString qualified as BS |
| 29 |
import Yesod.Default.Handlers (getFaviconR, getRobotsR) |
| 30 |
|
| 31 |
import Hledger |
| 32 |
import Hledger.Web.Import |
| 33 |
import Hledger.Web.WebOptions (packageversion) |
| 34 |
import Hledger.Web.Widget.Common (journalFile404) |
| 35 |
|
| 36 |
getRootR :: Handler Html |
| 37 |
getRootR = do |
| 38 |
checkServerSideUiEnabled |
| 39 |
redirect JournalR |
| 40 |
|
| 41 |
getManageR :: Handler Html |
| 42 |
getManageR = do |
| 43 |
checkServerSideUiEnabled |
| 44 |
VD{j} <- getViewData |
| 45 |
require EditPermission |
| 46 |
defaultLayout $ do |
| 47 |
setTitle "Edit journal" |
| 48 |
$(widgetFile "manage") |
| 49 |
|
| 50 |
getDownloadR :: FilePath -> Handler TypedContent |
| 51 |
getDownloadR f = do |
| 52 |
checkServerSideUiEnabled |
| 53 |
VD{j} <- getViewData |
| 54 |
require EditPermission |
| 55 |
(f', txt) <- journalFile404 f j |
| 56 |
addHeader "Content-Disposition" ("attachment; filename=\"" <> T.pack f' <> "\"") |
| 57 |
sendResponse ("text/plain" :: ByteString, toContent txt) |
| 58 |
|
| 59 |
-- hledger-web equivalents of the old hledger-api's handlers |
| 60 |
|
| 61 |
getVersionR :: Handler TypedContent |
| 62 |
getVersionR = do |
| 63 |
require ViewPermission |
| 64 |
selectRep $ provideJson $ packageversion |
| 65 |
|
| 66 |
getAccountnamesR :: Handler TypedContent |
| 67 |
getAccountnamesR = do |
| 68 |
VD{j} <- getViewData |
| 69 |
require ViewPermission |
| 70 |
selectRep $ provideJson $ journalAccountNames j |
| 71 |
|
| 72 |
getTransactionsR :: Handler TypedContent |
| 73 |
getTransactionsR = do |
| 74 |
VD{j} <- getViewData |
| 75 |
require ViewPermission |
| 76 |
selectRep $ provideJson $ jtxns j |
| 77 |
|
| 78 |
getPricesR :: Handler TypedContent |
| 79 |
getPricesR = do |
| 80 |
VD{j} <- getViewData |
| 81 |
require ViewPermission |
| 82 |
selectRep $ |
| 83 |
provideJson $ map priceDirectiveToMarketPrice $ jpricedirectives j |
| 84 |
|
| 85 |
getCommoditiesR :: Handler TypedContent |
| 86 |
getCommoditiesR = do |
| 87 |
VD{j} <- getViewData |
| 88 |
require ViewPermission |
| 89 |
selectRep $ do |
| 90 |
provideJson $ (M.keys . jinferredcommoditystyles) j |
| 91 |
|
| 92 |
getAccountsR :: Handler TypedContent |
| 93 |
getAccountsR = do |
| 94 |
VD{j} <- getViewData |
| 95 |
require ViewPermission |
| 96 |
selectRep $ do |
| 97 |
provideJson $ |
| 98 |
styleAmounts (journalCommodityStylesWith HardRounding j) $ |
| 99 |
flattenAccounts $ mapAccounts (accountSetDeclarationInfo j) $ ledgerRootAccount $ ledgerFromJournal Any j |
| 100 |
|
| 101 |
getAccounttransactionsR :: Text -> Handler TypedContent |
| 102 |
getAccounttransactionsR a = do |
| 103 |
VD{j} <- getViewData |
| 104 |
require ViewPermission |
| 105 |
let |
| 106 |
rspec = defreportspec |
| 107 |
thisacctq = Acct $ accountNameToAccountRegex a -- includes subs |
| 108 |
selectRep $ do |
| 109 |
provideJson $ |
| 110 |
styleAmounts (journalCommodityStylesWith HardRounding j) $ |
| 111 |
accountTransactionsReport rspec{_rsQuery=Any} j thisacctq |
| 112 |
|
| 113 |
openApiYaml :: BS.ByteString |
| 114 |
openApiYaml = $(embedFileRelative "config/openapi.yaml") |
| 115 |
|
| 116 |
getOpenApiR :: Handler Value |
| 117 |
getOpenApiR = |
| 118 |
case Yaml.decodeEither' openApiYaml of |
| 119 |
Left _ -> notFound |
| 120 |
Right openapi -> do |
| 121 |
addHeader "Content-Type" "application/json" |
| 122 |
return openapi |