content: {-# OPTIONS_GHC -Wno-dodgy-exports #-}
  {-# OPTIONS_GHC -Wno-orphans #-}
  {-# OPTIONS_GHC -Wno-unused-imports #-}
  
  module Storage.Queries.DriverRCAssociation (module Storage.Queries.DriverRCAssociation, module ReExport) where
  
  import qualified Domain.Types.DriverRCAssociation
  import qualified Domain.Types.Person
  import qualified Domain.Types.VehicleRegistrationCertificate
  import Kernel.Beam.Functions
  import Kernel.External.Encryption
  import Kernel.Prelude
  import qualified Kernel.Prelude
  import Kernel.Types.Error
  import qualified Kernel.Types.Id
  import Kernel.Utils.Common (CacheFlow, EsqDBFlow, MonadFlow, fromMaybeM, getCurrentTime)
  import qualified Sequelize as Se
  import qualified Storage.Beam.DriverRCAssociation as Beam
  import Storage.Queries.DriverRCAssociationExtra as ReExport
  
  create :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Domain.Types.DriverRCAssociation.DriverRCAssociation -> m ())
  create = createWithKV
  
  createMany :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => ([Domain.Types.DriverRCAssociation.DriverRCAssociation] -> m ())
  createMany = traverse_ create
  
  deactivateRCForDriver ::
    (EsqDBFlow m r, MonadFlow m, CacheFlow m r) =>
    (Kernel.Prelude.Bool -> Kernel.Types.Id.Id Domain.Types.Person.Person -> Kernel.Types.Id.Id Domain.Types.VehicleRegistrationCertificate.VehicleRegistrationCertificate -> m ())
  deactivateRCForDriver isRcActive driverId rcId = do
    _now <- getCurrentTime
    updateWithKV
      [Se.Set Beam.isRcActive isRcActive, Se.Set Beam.updatedAt _now]
      [ Se.And
          [ Se.Is Beam.driverId $ Se.Eq (Kernel.Types.Id.getId driverId),
            Se.Is Beam.rcId $ Se.Eq (Kernel.Types.Id.getId rcId)
          ]
      ]
  
  deleteByDriverId :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Kernel.Types.Id.Id Domain.Types.Person.Person -> m ())
  deleteByDriverId driverId = do deleteWithKV [Se.Is Beam.driverId $ Se.Eq (Kernel.Types.Id.getId driverId)]
  
  findActiveAssociationByDriver ::
    (EsqDBFlow m r, MonadFlow m, CacheFlow m r) =>
    (Kernel.Types.Id.Id Domain.Types.Person.Person -> Kernel.Prelude.Bool -> m (Maybe Domain.Types.DriverRCAssociation.DriverRCAssociation))
  findActiveAssociationByDriver driverId isRcActive = do findOneWithKV [Se.And [Se.Is Beam.driverId $ Se.Eq (Kernel.Types.Id.getId driverId), Se.Is Beam.isRcActive $ Se.Eq isRcActive]]
  
  findActiveAssociationByRC ::
    (EsqDBFlow m r, MonadFlow m, CacheFlow m r) =>
    (Kernel.Types.Id.Id Domain.Types.VehicleRegistrationCertificate.VehicleRegistrationCertificate -> Kernel.Prelude.Bool -> m (Maybe Domain.Types.DriverRCAssociation.DriverRCAssociation))
  findActiveAssociationByRC rcId isRcActive = do findOneWithKV [Se.And [Se.Is Beam.rcId $ Se.Eq (Kernel.Types.Id.getId rcId), Se.Is Beam.isRcActive $ Se.Eq isRcActive]]
  
  findById :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Kernel.Types.Id.Id Domain.Types.DriverRCAssociation.DriverRCAssociation -> m (Maybe Domain.Types.DriverRCAssociation.DriverRCAssociation))
  findById id = do findOneWithKV [Se.Is Beam.id $ Se.Eq (Kernel.Types.Id.getId id)]
  
  findByPrimaryKey ::
    (EsqDBFlow m r, MonadFlow m, CacheFlow m r) =>
    (Kernel.Types.Id.Id Domain.Types.DriverRCAssociation.DriverRCAssociation -> m (Maybe Domain.Types.DriverRCAssociation.DriverRCAssociation))
  findByPrimaryKey id = do findOneWithKV [Se.And [Se.Is Beam.id $ Se.Eq (Kernel.Types.Id.getId id)]]
  
  updateByPrimaryKey :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Domain.Types.DriverRCAssociation.DriverRCAssociation -> m ())
  updateByPrimaryKey (Domain.Types.DriverRCAssociation.DriverRCAssociation {..}) = do
    _now <- getCurrentTime
    updateWithKV
      [ Se.Set Beam.associatedOn associatedOn,
        Se.Set Beam.associatedTill associatedTill,
        Se.Set Beam.consent consent,
        Se.Set Beam.consentTimestamp consentTimestamp,
        Se.Set Beam.driverId (Kernel.Types.Id.getId driverId),
        Se.Set Beam.isRcActive isRcActive,
        Se.Set Beam.rcId (Kernel.Types.Id.getId rcId),
        Se.Set Beam.merchantId (Kernel.Types.Id.getId <$> merchantId),
        Se.Set Beam.merchantOperatingCityId (Kernel.Types.Id.getId <$> merchantOperatingCityId),
        Se.Set Beam.createdAt createdAt,
        Se.Set Beam.updatedAt _now
      ]
      [Se.And [Se.Is Beam.id $ Se.Eq (Kernel.Types.Id.getId id)]]
  