From 851067ecd11898b0ad34097e526fd44c4170053c Mon Sep 17 00:00:00 2001 From: dhruv-1010 Date: Mon, 25 Aug 2025 19:50:22 +0530 Subject: [PATCH] registry : added multiCountry decode in city Code validation --- app/mock-registry/src/Flow/UpdateCities.hs | 46 +++++++++++++++------- flake.lock | 13 +++--- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/app/mock-registry/src/Flow/UpdateCities.hs b/app/mock-registry/src/Flow/UpdateCities.hs index a52b69c..2bc37ca 100644 --- a/app/mock-registry/src/Flow/UpdateCities.hs +++ b/app/mock-registry/src/Flow/UpdateCities.hs @@ -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 @@ -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:***" +-} diff --git a/flake.lock b/flake.lock index 0264666..66d8835 100644 --- a/flake.lock +++ b/flake.lock @@ -494,16 +494,15 @@ "tinylog": "tinylog" }, "locked": { - "lastModified": 1740461572, - "narHash": "sha256-kTcrUDW99AoGSPB8OxP4VKK/CJCWJRdT/4hncjnAS0s=", + "lastModified": 1741173147, + "narHash": "sha256-ksarQpBsANKu35mwK5iSvEZK9L7INyZlBelU39HkMZA=", "owner": "nammayatri", "repo": "euler-hs", - "rev": "9aabd552e31847cede4aea1a1b20cf26ec6f4f1b", + "rev": "1e5873dfe47eae540df4ffe975c8a1e17c27c423", "type": "github" }, "original": { "owner": "nammayatri", - "ref": "backend/hedis-upgrade-for-zone-aware-replica-usage", "repo": "euler-hs", "type": "github" } @@ -2569,11 +2568,11 @@ "prometheus-haskell": "prometheus-haskell_2" }, "locked": { - "lastModified": 1740745195, - "narHash": "sha256-Y6dtumQyeZtAODn+GtNV2OxRE0M0U0uGPzFwjf9hkB0=", + "lastModified": 1756129195, + "narHash": "sha256-uYytF3oCqoU1CrjdOMcyTozhI7G7DtdXkU9ydwrhpAw=", "owner": "nammayatri", "repo": "shared-kernel", - "rev": "27ce3da1f9b9426c3b5a1ad45a5f280a10229466", + "rev": "171bec2c2330f115abf3d493aeedf97864df83fe", "type": "github" }, "original": {