content: {-
   Copyright 2022-23, Juspay India Pvt Ltd
  
   This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License
  
   as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program
  
   is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  
   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of
  
   the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
  -}
  
  module Domain.Action.UI.OnMessage
    ( FCMReq (..),
      sendMessageFCM,
    )
  where
  
  import Data.HashMap.Strict as HMS
  import qualified Data.HashMap.Strict as HM
  import qualified Data.Text as Text
  import qualified Domain.Types.Merchant as DM
  import qualified Domain.Types.MerchantOperatingCity as DMOC
  import qualified Domain.Types.Person as Person
  import qualified Domain.Types.Ride as Ride
  import Kernel.Beam.Functions
  import Kernel.External.Encryption
  import Kernel.Prelude
  import Kernel.Storage.Esqueleto.Config (EsqDBReplicaFlow)
  import Kernel.Streaming.Kafka.Producer.Types (KafkaProducerTools)
  import qualified Kernel.Types.APISuccess as APISuccess
  import Kernel.Types.Id
  import Kernel.Utils.Common
  import qualified SharedLogic.CallBAP as BP
  import qualified Storage.Queries.Booking as QBooking
  import qualified Storage.Queries.Ride as QRide
  import Tools.Error
  import TransactionLogs.Types
  
  data FCMReq = FCMReq
    { rideId :: Id Ride.Ride,
      message :: Text
    }
    deriving (Generic, ToJSON, FromJSON, ToSchema)
  
  sendMessageFCM :: (EncFlow m r, CacheFlow m r, EsqDBFlow m r, EsqDBReplicaFlow m r, HasShortDurationRetryCfg r c, HasFlowEnv m r '["nwAddress" ::: BaseUrl], HasHttpClientOptions r c, HasFlowEnv m r '["internalEndPointHashMap" ::: HM.HashMap BaseUrl BaseUrl], HasFlowEnv m r '["kafkaProducerTools" ::: KafkaProducerTools], HasFlowEnv m r '["ondcTokenHashMap" ::: HMS.HashMap KeyConfig TokenConfig]) => (Id Person.Person, Id DM.Merchant, Id DMOC.MerchantOperatingCity) -> FCMReq -> m APISuccess.APISuccess
  sendMessageFCM (_personId, _, _) FCMReq {..} = do
    ride <- runInReplica $ QRide.findById rideId >>= fromMaybeM (RideDoesNotExist rideId.getId)
    booking <- runInReplica $ QBooking.findById ride.bookingId >>= fromMaybeM (BookingNotFound ride.bookingId.getId)
    unless (isValidRideStatus ride.status) $ throwError $ RideInvalidStatus ("Cannot send message during this stage of ride" <> Text.pack (show ride.status))
    BP.sendNewMessageToBAP booking ride message
    pure APISuccess.Success
    where
      isValidRideStatus status = status `elem` [Ride.NEW, Ride.INPROGRESS]
  