Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions app/mock-registry/src/Flow/UpdateCities.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
module Flow.UpdateCities where

import App.Types (FlowHandler)
import qualified Data.Aeson as Aeson
import qualified Data.HashSet as Set
import Data.List ((\\))
import qualified Data.Text as T
import Domain.Types.UpdateCities
import EulerHS.Prelude
import Kernel.Storage.Esqueleto (runTransaction)
import Kernel.Types.Beckn.Country
import Kernel.Types.Common
import Kernel.Types.Error (GenericError (InvalidRequest))
import Kernel.Utils.Common
Expand Down Expand Up @@ -52,34 +54,50 @@ updateCities _apiKey req = withFlowHandlerAPI' . withLogTag updateCitiesLogTag $
case subscribers of
[sub] -> do
(citiesAdded, citiesRemoved, newCities) <- getNewCities sub & fromEitherM InvalidRequest
_ <- validateCities newCities & fromEitherM (InvalidRequest . (<>) "newCities: ")
_ <- validateCities newCities sub.country & fromEitherM (InvalidRequest . (<>) "newCities: ")
void . runTransaction $ QSub.updateCities req.uniqueKeyId req.subscriberId req.domain req.subscriberType newCities
return $ UpdateCitiesRes {added = citiesAdded, removed = citiesRemoved}
[] -> throwError $ InvalidRequest "No subscribers found for unique-key-id , might be a new Merchant"
_ -> throwError $ InvalidRequest "Multiple subscribers returned for a unique key"
where
updateCitiesLogTag = "ukId:-" <> req.uniqueKeyId <> ",subId:-" <> req.subscriberId

getNewCities sub = case (req.appendCities, req.replaceCities) of
(Just appendCities, Nothing) -> do
_ <- validateCities appendCities
_ <- validateCities appendCities sub.country
let newCities = sub.city <> appendCities
Right (Just appendCities, Nothing, newCities)
(Nothing, Just replaceCities) -> do
_ <- validateCities replaceCities
_ <- validateCities replaceCities sub.country
let citiesAdded = guarded notNull (replaceCities \\ sub.city)
citiesRemoved = guarded notNull (sub.city \\ replaceCities)
Right (citiesAdded, citiesRemoved, replaceCities)
(Just _, Just _) -> Left "Both \"appendCities\" and \"replaceCities\" cannot be present in the request"
(Nothing, Nothing) -> Left "Either \"appendCities\" or \"replaceCities\" should be present in the request"

validateCities :: [Text] -> Either Text ()
validateCities = \case
[] -> Left "Cities cannot be empty"
["*"] -> Right ()
cities -> do
let patternStr = "^std:[0-9]{2,8}$"
invalidCities = filter (isNothing . matchRegex (mkRegex patternStr) . T.unpack) cities
_ <- case invalidCities of
[] -> Right ()
xs -> Left $ "cities " <> T.pack (show xs) <> " doesn't match the regex pattern:" <> T.pack patternStr
bool (Right ()) (Left "Cities cannot have duplicates") $ hasDuplicates cities
validateCities :: [Text] -> Maybe Country -> Either Text ()
validateCities mbCities mbCountry =
case mbCities of
[] -> Left "Cities cannot be empty"
["*"] -> Right ()
cities -> do
let countryCode = maybe "std" countryCodePrefix mbCountry
let patternStr = "^(std|" <> T.unpack countryCode <> "):[0-9]{2,8}$"
invalidCities = filter (isNothing . matchRegex (mkRegex patternStr) . T.unpack) cities
_ <- case invalidCities of
[] -> Right ()
xs -> Left $ "cities " <> T.pack (show xs) <> " doesn't match the regex pattern:" <> T.pack patternStr
bool (Right ()) (Left "Cities cannot have duplicates") $ hasDuplicates cities

countryCodePrefix :: Country -> Text
countryCodePrefix India = "std"
countryCodePrefix country =
case Aeson.toJSON country of
Aeson.String code -> T.toLower code
_ -> "std"

{-
This check only allow same std / country codes cities in a merchant ,
for example for India it will allow "std:***" but will not allow other country cities
same for netherlands it will check with "nld:***"
-}
13 changes: 6 additions & 7 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.