Add check of existing order
This commit is contained in:
parent
4bd49c76d4
commit
6e0cb54032
1 changed files with 150 additions and 127 deletions
|
@ -668,146 +668,169 @@ routes pipe config = do
|
||||||
, "shop" .= (Nothing :: Maybe String)
|
, "shop" .= (Nothing :: Maybe String)
|
||||||
])
|
])
|
||||||
Just o' -> do
|
Just o' -> do
|
||||||
res <-
|
existingOrder <-
|
||||||
liftAndCatchIO $
|
liftAndCatchIO $
|
||||||
requestXeroToken pipe (c_dbName config) c "none" $ oaddress o'
|
run $
|
||||||
if res
|
findXeroOrder
|
||||||
then do
|
(oaddress o')
|
||||||
resInv <-
|
(xr_invNo invReq)
|
||||||
|
(xr_shortCode invReq)
|
||||||
|
case cast' . Doc =<< existingOrder of
|
||||||
|
Nothing -> do
|
||||||
|
res <-
|
||||||
liftAndCatchIO $
|
liftAndCatchIO $
|
||||||
getXeroInvoice pipe (c_dbName config) (xr_invNo invReq) $
|
requestXeroToken pipe (c_dbName config) c "none" $ oaddress o'
|
||||||
oaddress o'
|
if res
|
||||||
case resInv of
|
then do
|
||||||
Nothing -> do
|
resInv <-
|
||||||
status ok200
|
liftAndCatchIO $
|
||||||
Web.Scotty.json
|
getXeroInvoice pipe (c_dbName config) (xr_invNo invReq) $
|
||||||
(object
|
oaddress o'
|
||||||
[ "reportType" .= (2 :: Integer)
|
case resInv of
|
||||||
, "order" .= (Nothing :: Maybe ZGoOrder)
|
Nothing -> do
|
||||||
, "shop" .= (Nothing :: Maybe String)
|
status ok200
|
||||||
])
|
Web.Scotty.json
|
||||||
Just xI -> do
|
(object
|
||||||
if xi_type xI == "ACCREC"
|
[ "reportType" .= (2 :: Integer)
|
||||||
then if xi_status xI == "AUTHORISED"
|
, "order" .= (Nothing :: Maybe ZGoOrder)
|
||||||
then if xi_currency xI == T.toUpper (ocurrency o')
|
, "shop" .= (Nothing :: Maybe String)
|
||||||
then if xi_total xI == xr_amount invReq
|
])
|
||||||
then do
|
Just xI -> do
|
||||||
now <- liftIO getCurrentTime
|
if xi_type xI == "ACCREC"
|
||||||
tk <- liftIO generateToken
|
then if xi_status xI == "AUTHORISED"
|
||||||
pr <-
|
then if xi_currency xI ==
|
||||||
liftAndCatchIO $
|
T.toUpper (ocurrency o')
|
||||||
run
|
then if xi_total xI == xr_amount invReq
|
||||||
(findPrice $
|
then do
|
||||||
T.unpack . ocurrency $ o')
|
now <- liftIO getCurrentTime
|
||||||
case parseZGoPrice =<< pr of
|
tk <- liftIO generateToken
|
||||||
Nothing -> do
|
pr <-
|
||||||
|
liftAndCatchIO $
|
||||||
|
run
|
||||||
|
(findPrice $
|
||||||
|
T.unpack . ocurrency $ o')
|
||||||
|
case parseZGoPrice =<< pr of
|
||||||
|
Nothing -> do
|
||||||
|
status ok200
|
||||||
|
Web.Scotty.json
|
||||||
|
(object
|
||||||
|
[ "reportType" .=
|
||||||
|
(7 :: Integer)
|
||||||
|
, "order" .=
|
||||||
|
(Nothing :: Maybe ZGoOrder)
|
||||||
|
, "shop" .=
|
||||||
|
(Nothing :: Maybe String)
|
||||||
|
])
|
||||||
|
Just cp -> do
|
||||||
|
let newOrder =
|
||||||
|
ZGoOrder
|
||||||
|
Nothing
|
||||||
|
(oaddress o')
|
||||||
|
("Xero-" <>
|
||||||
|
T.pack
|
||||||
|
(show $ o_id o'))
|
||||||
|
now
|
||||||
|
True
|
||||||
|
(ocurrency o')
|
||||||
|
(price cp)
|
||||||
|
(xi_total xI)
|
||||||
|
(xi_total xI /
|
||||||
|
price cp)
|
||||||
|
[ LineItem
|
||||||
|
1
|
||||||
|
("Invoice from " <>
|
||||||
|
oname o' <>
|
||||||
|
" [" <>
|
||||||
|
xi_number xI <>
|
||||||
|
"]")
|
||||||
|
(xi_total xI)
|
||||||
|
]
|
||||||
|
False
|
||||||
|
(xi_number xI)
|
||||||
|
(xi_shortcode xI)
|
||||||
|
(T.pack tk)
|
||||||
|
_ <-
|
||||||
|
liftAndCatchIO $
|
||||||
|
run $
|
||||||
|
upsertOrder newOrder
|
||||||
|
finalOrder <-
|
||||||
|
liftAndCatchIO $
|
||||||
|
run $
|
||||||
|
findXeroOrder
|
||||||
|
(oaddress o')
|
||||||
|
(xi_number xI)
|
||||||
|
(xi_shortcode xI)
|
||||||
|
case cast' . Doc =<<
|
||||||
|
finalOrder of
|
||||||
|
Nothing -> do
|
||||||
|
status
|
||||||
|
internalServerError500
|
||||||
|
text
|
||||||
|
"Unable to save order to DB"
|
||||||
|
Just fO -> do
|
||||||
|
status created201
|
||||||
|
Web.Scotty.json
|
||||||
|
(object
|
||||||
|
[ "reportType" .=
|
||||||
|
(0 :: Integer)
|
||||||
|
, "order" .=
|
||||||
|
toJSON
|
||||||
|
(fO :: ZGoOrder)
|
||||||
|
, "shop" .=
|
||||||
|
oname o'
|
||||||
|
])
|
||||||
|
else do
|
||||||
status ok200
|
status ok200
|
||||||
Web.Scotty.json
|
Web.Scotty.json
|
||||||
(object
|
(object
|
||||||
[ "reportType" .=
|
[ "reportType" .=
|
||||||
(7 :: Integer)
|
(8 :: Integer)
|
||||||
, "order" .=
|
, "order" .=
|
||||||
(Nothing :: Maybe ZGoOrder)
|
(Nothing :: Maybe ZGoOrder)
|
||||||
, "shop" .=
|
, "shop" .=
|
||||||
(Nothing :: Maybe String)
|
(Nothing :: Maybe String)
|
||||||
])
|
])
|
||||||
Just cp -> do
|
else do
|
||||||
let newOrder =
|
status ok200
|
||||||
ZGoOrder
|
Web.Scotty.json
|
||||||
Nothing
|
(object
|
||||||
(oaddress o')
|
[ "reportType" .= (7 :: Integer)
|
||||||
("Xero-" <>
|
, "order" .=
|
||||||
T.pack
|
(Nothing :: Maybe ZGoOrder)
|
||||||
(show $ o_id o'))
|
, "shop" .=
|
||||||
now
|
(Nothing :: Maybe String)
|
||||||
True
|
])
|
||||||
(ocurrency o')
|
else do
|
||||||
(price cp)
|
status ok200
|
||||||
(xi_total xI)
|
Web.Scotty.json
|
||||||
(xi_total xI / price cp)
|
(object
|
||||||
[ LineItem
|
[ "reportType" .= (6 :: Integer)
|
||||||
1
|
, "order" .= (Nothing :: Maybe ZGoOrder)
|
||||||
("Invoice from " <>
|
, "shop" .= (Nothing :: Maybe String)
|
||||||
oname o' <>
|
])
|
||||||
" [" <>
|
else do
|
||||||
xi_number xI <>
|
status ok200
|
||||||
"]")
|
Web.Scotty.json
|
||||||
(xi_total xI)
|
(object
|
||||||
]
|
[ "reportType" .= (5 :: Integer)
|
||||||
False
|
, "order" .= (Nothing :: Maybe ZGoOrder)
|
||||||
(xi_number xI)
|
, "shop" .= (Nothing :: Maybe String)
|
||||||
(xi_shortcode xI)
|
])
|
||||||
(T.pack tk)
|
else do
|
||||||
_ <-
|
status ok200
|
||||||
liftAndCatchIO $
|
Web.Scotty.json
|
||||||
run $ upsertOrder newOrder
|
(object
|
||||||
finalOrder <-
|
[ "reportType" .= (1 :: Integer)
|
||||||
liftAndCatchIO $
|
, "order" .= (Nothing :: Maybe ZGoOrder)
|
||||||
run $
|
, "shop" .= (Nothing :: Maybe String)
|
||||||
findXeroOrder
|
])
|
||||||
(oaddress o')
|
Just eO -> do
|
||||||
(xi_number xI)
|
status created201
|
||||||
(xi_shortcode xI)
|
|
||||||
case cast' . Doc =<< finalOrder of
|
|
||||||
Nothing -> do
|
|
||||||
status
|
|
||||||
internalServerError500
|
|
||||||
text
|
|
||||||
"Unable to save order to DB"
|
|
||||||
Just fO -> do
|
|
||||||
status created201
|
|
||||||
Web.Scotty.json
|
|
||||||
(object
|
|
||||||
[ "reportType" .=
|
|
||||||
(0 :: Integer)
|
|
||||||
, "order" .=
|
|
||||||
toJSON
|
|
||||||
(fO :: ZGoOrder)
|
|
||||||
, "shop" .= oname o'
|
|
||||||
])
|
|
||||||
else do
|
|
||||||
status ok200
|
|
||||||
Web.Scotty.json
|
|
||||||
(object
|
|
||||||
[ "reportType" .=
|
|
||||||
(8 :: Integer)
|
|
||||||
, "order" .=
|
|
||||||
(Nothing :: Maybe ZGoOrder)
|
|
||||||
, "shop" .=
|
|
||||||
(Nothing :: Maybe String)
|
|
||||||
])
|
|
||||||
else do
|
|
||||||
status ok200
|
|
||||||
Web.Scotty.json
|
|
||||||
(object
|
|
||||||
[ "reportType" .= (7 :: Integer)
|
|
||||||
, "order" .=
|
|
||||||
(Nothing :: Maybe ZGoOrder)
|
|
||||||
, "shop" .= (Nothing :: Maybe String)
|
|
||||||
])
|
|
||||||
else do
|
|
||||||
status ok200
|
|
||||||
Web.Scotty.json
|
|
||||||
(object
|
|
||||||
[ "reportType" .= (6 :: Integer)
|
|
||||||
, "order" .= (Nothing :: Maybe ZGoOrder)
|
|
||||||
, "shop" .= (Nothing :: Maybe String)
|
|
||||||
])
|
|
||||||
else do
|
|
||||||
status ok200
|
|
||||||
Web.Scotty.json
|
|
||||||
(object
|
|
||||||
[ "reportType" .= (5 :: Integer)
|
|
||||||
, "order" .= (Nothing :: Maybe ZGoOrder)
|
|
||||||
, "shop" .= (Nothing :: Maybe String)
|
|
||||||
])
|
|
||||||
else do
|
|
||||||
status ok200
|
|
||||||
Web.Scotty.json
|
Web.Scotty.json
|
||||||
(object
|
(object
|
||||||
[ "reportType" .= (1 :: Integer)
|
[ "reportType" .= (0 :: Integer)
|
||||||
, "order" .= (Nothing :: Maybe ZGoOrder)
|
, "order" .= toJSON (eO :: ZGoOrder)
|
||||||
, "shop" .= (Nothing :: Maybe String)
|
, "shop" .= oname o'
|
||||||
])
|
])
|
||||||
-- Get the xeroaccount code
|
-- Get the xeroaccount code
|
||||||
get "/api/xeroaccount" $ do
|
get "/api/xeroaccount" $ do
|
||||||
|
|
Loading…
Reference in a new issue