-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_export_service.hs
60 lines (55 loc) · 2.73 KB
/
import_export_service.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
--This file contains pieces of code written by me, but it is part of a project with a larger group of programmers.
--------------------
-- Export Handler --
--------------------
-- | The String parameter is a very important piece to this Handler
-- When this route gets called, the string becomes the name of the returned file
-- This is how you specify the return file as *.xlsx
-- | Exports all existing import sources to an xlsx file
getExportFullimportSourceR :: String -> Handler TypedContent
getExportFullimportSourceR _ = do
userIdentVal <- getUserIdent
aid <- requireAuthId
-- Get the user's 'owned' groups
sul <- getSuperUserList
let ownedGroupIds = entityKey <$> getOwnedGroups sul
memberGroupIds <- runHaxlInHandler userIdentVal $ getGroupIdListHaxl aid
let permissionGroupIds = nub $ ownedGroupIds ++ memberGroupIds
-- Make a request to the import-source-server through Haxl
eResp <- tryRunHaxlInHandler userIdentVal $ importSource.listimportSourcesByGroupIdsHaxl permissionGroupIds
-- Handle result
case eResp of
Left importSourceError -> sendResponseStatus status500 (toJSON @importSourceError importSourceError)
Right sources -> do
eitherBytes <- liftIO $ exportFullimportSource sources
case eitherBytes of
Left err -> sendResponseStatus status500 $ Text.pack err
Right bytes -> return $ TypedContent "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" $ toContent (LByteS.toStrict bytes)
--------------------
-- Import Handler --
--------------------
-- | Imports import sources from an xlsx file
postImportFullimportSourceR :: Handler ()
postImportFullimportSourceR = do
_ <- requireAuthId
-- import Source Group Id
gidRequired <- Foundation.importSourceGroupId . appConfig <$> getYesod
-- Expected a form with (FileInfo)
((mresult, _), _) <- runFormPostNoToken form
case mresult of
FormMissing -> sendResponseStatus status400 $ toJSON ("Failed to get file from form submission, FormMissing" :: Text.Text)
FormFailure _ -> sendResponseStatus status400 $ toJSON ("Failed to get file from form submission, FormFailure" :: Text.Text)
FormSuccess fileInfo -> do
-- Get the bytes from the form submission
fileBytes <- runResourceT $ fileSource fileInfo `connect` DCB.sinkLbs
-- Parse the bytes into a list of 'FullimportSource's
case importFullimportSource gidRequired fileBytes of
Left err -> sendResponseStatus status400 $ toJSON $ Text.pack err
Right importedSources -> do
liftIO $ print importedSources
-- Handle the creation and updating of the imported sources
updateMultipleimportSources importedSources
where
form :: Foundation.Form (FileInfo)
form =
renderDivs $ fileAFormReq "File"