J6S23MDGHVSCVVIRB6XRNSY3EGTDNWFJHV7RYLIEHBUK5KU63CFQC
ZIG57EE6RB22JGB3CT33EN2HVYCHCXBT5GROBTBMBLEMDTGQOOBQC
WRPIYG3EUHZR6N6T74ZXZDXATRMIRLXAQ24UNUNSVTVYGMT2VDSQC
QU5FW67RGCWOWT2YFM4NYMJFFHWIRPQANQBBAHBKZUY7UYMCSIMQC
JXG3FCXYBDKMUD77DOM7RCIJYKB7BILC43OHHDZBE7YQRGAMUCCAC
NJNMO72S7VIUV22JXB4IFPZMHWTJAOTP6EC6Z4QSKYIROSXT52MQC
BFZN4SUAGYNFFYVAP36BAX32DMO622PK4EPEVQQEAGC2IHTEAAPQC
EFSXYZPOGA5M4DN65IEIDO7JK6U34DMQELAPHOIL2UAT6HRC66BAC
B6HWAPDPXIWH7CHK5VLMWLL6EQN6NOFZEFYO47BPUY2ZO4SL7VDAC
IZEVQF627FA7VV25KJAWYWGGC35LZUUBBQRPN5ZAINTQLPEDRTEAC
RN7EI6INGUUHGMNY5RU3NH56WPLRZY5ZMYDNFMNE3TGV4ESFLQIAC
data Event i
= StartEvent i
| StopEvent i
derive instance eventFunctor :: Functor Event
instance eventFoldable :: Foldable Event where
foldr f b = case _ of
StartEvent a -> f a b
StopEvent a -> f a b
foldl f b = case _ of
StartEvent a -> f b a
StopEvent a -> f b a
foldMap = foldMapDefaultR
instance eventTraversable :: Traversable Event where
traverse f = case _ of
StartEvent a -> StartEvent <$> f a
StopEvent a -> StopEvent <$> f a
sequence = traverse identity
instance decodeJsonEvent :: DecodeJson (Event String) where
decodeJson json = do
obj <- decodeJson json
event <- obj .: "event"
start' <- traverse (_ .: "eventTime") =<< event .:? "start"
stop' <- traverse (_ .: "eventTime") =<< event .:? "stop"
note "Only 'stop' and 'start' events are supported." $ (StartEvent <$> start') <|> (StopEvent <$> stop')
StatusCode 403 -> pure <<< Left <<< LogFailure $ Forbidden
StatusCode 200 -> Right <$> liftEffect now
other -> pure <<< Left <<< LogFailure $ Error { status: Just other, message: r.statusText }
StatusCode 403 ->
throwError $ LogFailure Forbidden
StatusCode 200 ->
withExceptT (LogFailure <<< ParseFailure r.body) $ do
event <- except $ decodeJson r.body
timeEvent <- traverse parseDate event
case timeEvent of
StartEvent t -> pure $ fromDateTime t
StopEvent _ -> throwError $ "Expected start event, got stop."
other ->
throwError <<< LogFailure $ Error { status: Just other, message: r.statusText }
StatusCode 403 -> pure <<< Left <<< LogFailure $ Forbidden
StatusCode 200 -> Right <$> liftEffect now
other -> pure <<< Left <<< LogFailure $ Error { status: Just other, message: r.statusText }
StatusCode 403 ->
throwError $ LogFailure Forbidden
StatusCode 200 ->
withExceptT (LogFailure <<< ParseFailure r.body) $ do
event <- except $ decodeJson r.body
timeEvent <- traverse parseDate event
case timeEvent of
StartEvent _ -> throwError $ "Expected stop event, got start."
StopEvent t -> pure $ fromDateTime t
other ->
throwError <<< LogFailure $ Error { status: Just other, message: r.statusText }
instance showAPIError :: Show APIError where
show = case _ of
Forbidden -> "Forbidden"
ParseFailure js e -> "ParseFailure (" <> show (stringify js) <> ") " <> show e
Error r -> "Error { status: " <> show r.status <> ", message: " <> r.message <> "}"
parseJsonDate :: Json -> ExceptT String Effect DateTime
parseJsonDate json = do
str <- except $ decodeJson json
parseDate str
parseDate :: String -> ExceptT String Effect DateTime
parseDate str = do
jsDate <- lift $ JSDate.parse str
except $ note ("Unable to convert date " <> show jsDate <> " to a valid DateTime value.")
(JSDate.toDateTime jsDate)
creditToParser' mode f v =
let
creditToParser' mode f v = do
tn <- typename f
if tn /= "credit_to_t"
then returnError Incompatible f "column was not of type credit_to_t"
else maybe empty (pure . parser . decodeUtf8) v
where
parser "credit_to_address" =
CreditToCurrency <$> (addressParser mode <* nullField <* nullField)
parser "credit_to_user" =
CreditToUser <$> (nullField *> nullField *> idParser UserId <* nullField)
parser "credit_to_project" =
CreditToProject
<$> (nullField *> nullField *> nullField *> idParser ProjectId)
parser _ = empty
in
do
tn <- typename f
if tn /= "credit_to_t"
then returnError Incompatible f "column was not of type credit_to_t"
else maybe empty (pure . parser . decodeUtf8) v
parser = \case
"credit_to_address" -> CreditToCurrency <$> (addressParser mode <* nullField <* nullField)
"credit_to_user" -> CreditToUser <$> (nullField *> nullField *> idParser UserId <* nullField)
"credit_to_project" -> CreditToProject <$> (nullField *> nullField *> nullField *> idParser ProjectId)
_ -> empty