content: {-# OPTIONS_GHC -Wno-dodgy-exports #-}
  {-# OPTIONS_GHC -Wno-orphans #-}
  {-# OPTIONS_GHC -Wno-unused-imports #-}
  
  module Storage.Queries.DriverLicense (module Storage.Queries.DriverLicense, module ReExport) where
  
  import qualified Domain.Types.DriverLicense
  import qualified Domain.Types.Image
  import qualified Domain.Types.Person
  import Kernel.Beam.Functions
  import Kernel.External.Encryption
  import Kernel.Prelude
  import qualified Kernel.Types.Documents
  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.DriverLicense as Beam
  import Storage.Queries.DriverLicenseExtra as ReExport
  
  create :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Domain.Types.DriverLicense.DriverLicense -> m ())
  create = createWithKV
  
  createMany :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => ([Domain.Types.DriverLicense.DriverLicense] -> m ())
  createMany = traverse_ create
  
  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)]
  
  findByDriverId :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Kernel.Types.Id.Id Domain.Types.Person.Person -> m (Maybe Domain.Types.DriverLicense.DriverLicense))
  findByDriverId driverId = do findOneWithKV [Se.Is Beam.driverId $ Se.Eq (Kernel.Types.Id.getId driverId)]
  
  findById :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Kernel.Types.Id.Id Domain.Types.DriverLicense.DriverLicense -> m (Maybe Domain.Types.DriverLicense.DriverLicense))
  findById id = do findOneWithKV [Se.Is Beam.id $ Se.Eq (Kernel.Types.Id.getId id)]
  
  updateVerificationStatus :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Kernel.Types.Documents.VerificationStatus -> Kernel.Types.Id.Id Domain.Types.Image.Image -> m ())
  updateVerificationStatus verificationStatus documentImageId1 = do
    _now <- getCurrentTime
    updateWithKV [Se.Set Beam.verificationStatus verificationStatus, Se.Set Beam.updatedAt _now] [Se.Is Beam.documentImageId1 $ Se.Eq (Kernel.Types.Id.getId documentImageId1)]
  
  findByPrimaryKey :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Kernel.Types.Id.Id Domain.Types.DriverLicense.DriverLicense -> m (Maybe Domain.Types.DriverLicense.DriverLicense))
  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.DriverLicense.DriverLicense -> m ())
  updateByPrimaryKey (Domain.Types.DriverLicense.DriverLicense {..}) = do
    _now <- getCurrentTime
    updateWithKV
      [ Se.Set Beam.classOfVehicles classOfVehicles,
        Se.Set Beam.consent consent,
        Se.Set Beam.consentTimestamp consentTimestamp,
        Se.Set Beam.dateOfIssue dateOfIssue,
        Se.Set Beam.documentImageId1 (Kernel.Types.Id.getId documentImageId1),
        Se.Set Beam.documentImageId2 (Kernel.Types.Id.getId <$> documentImageId2),
        Se.Set Beam.driverDob driverDob,
        Se.Set Beam.driverId (Kernel.Types.Id.getId driverId),
        Se.Set Beam.driverName driverName,
        Se.Set Beam.failedRules failedRules,
        Se.Set Beam.licenseExpiry licenseExpiry,
        Se.Set Beam.licenseNumberEncrypted (((licenseNumber & unEncrypted . encrypted))),
        Se.Set Beam.licenseNumberHash ((licenseNumber & hash)),
        Se.Set Beam.rejectReason rejectReason,
        Se.Set Beam.vehicleCategory vehicleCategory,
        Se.Set Beam.verificationStatus verificationStatus,
        Se.Set Beam.merchantId (Kernel.Types.Id.getId <$> merchantId),
        Se.Set Beam.createdAt createdAt,
        Se.Set Beam.updatedAt _now
      ]
      [Se.And [Se.Is Beam.id $ Se.Eq (Kernel.Types.Id.getId id)]]
  