content: {-# OPTIONS_GHC -Wno-dodgy-exports #-}
  {-# OPTIONS_GHC -Wno-orphans #-}
  {-# OPTIONS_GHC -Wno-unused-imports #-}
  
  module Storage.Queries.BusinessLicense where
  
  import qualified Domain.Types.BusinessLicense
  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.BusinessLicense as Beam
  
  create :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Domain.Types.BusinessLicense.BusinessLicense -> m ())
  create = createWithKV
  
  createMany :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => ([Domain.Types.BusinessLicense.BusinessLicense] -> m ())
  createMany = traverse_ create
  
  findByImageId :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Kernel.Types.Id.Id Domain.Types.Image.Image -> m (Maybe Domain.Types.BusinessLicense.BusinessLicense))
  findByImageId documentImageId = do findOneWithKV [Se.Is Beam.documentImageId $ Se.Eq (Kernel.Types.Id.getId documentImageId)]
  
  findByPersonId :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Kernel.Types.Id.Id Domain.Types.Person.Person -> m ([Domain.Types.BusinessLicense.BusinessLicense]))
  findByPersonId driverId = do findAllWithKV [Se.Is Beam.driverId $ Se.Eq (Kernel.Types.Id.getId driverId)]
  
  updateVerificationStatusByImageId :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Kernel.Types.Documents.VerificationStatus -> Kernel.Types.Id.Id Domain.Types.Image.Image -> m ())
  updateVerificationStatusByImageId verificationStatus documentImageId = do
    _now <- getCurrentTime
    updateOneWithKV [Se.Set Beam.verificationStatus verificationStatus, Se.Set Beam.updatedAt _now] [Se.Is Beam.documentImageId $ Se.Eq (Kernel.Types.Id.getId documentImageId)]
  
  findByPrimaryKey :: (EsqDBFlow m r, MonadFlow m, CacheFlow m r) => (Kernel.Types.Id.Id Domain.Types.BusinessLicense.BusinessLicense -> m (Maybe Domain.Types.BusinessLicense.BusinessLicense))
  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.BusinessLicense.BusinessLicense -> m ())
  updateByPrimaryKey (Domain.Types.BusinessLicense.BusinessLicense {..}) = do
    _now <- getCurrentTime
    updateWithKV
      [ Se.Set Beam.documentImageId (Kernel.Types.Id.getId documentImageId),
        Se.Set Beam.driverId (Kernel.Types.Id.getId driverId),
        Se.Set Beam.licenseExpiry licenseExpiry,
        Se.Set Beam.licenseNumberEncrypted (((licenseNumber & unEncrypted . encrypted))),
        Se.Set Beam.licenseNumberHash ((licenseNumber & hash)),
        Se.Set Beam.verificationStatus verificationStatus,
        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)]]
  
  instance FromTType' Beam.BusinessLicense Domain.Types.BusinessLicense.BusinessLicense where
    fromTType' (Beam.BusinessLicenseT {..}) = do
      pure $
        Just
          Domain.Types.BusinessLicense.BusinessLicense
            { documentImageId = Kernel.Types.Id.Id documentImageId,
              driverId = Kernel.Types.Id.Id driverId,
              id = Kernel.Types.Id.Id id,
              licenseExpiry = licenseExpiry,
              licenseNumber = EncryptedHashed (Encrypted licenseNumberEncrypted) licenseNumberHash,
              verificationStatus = verificationStatus,
              merchantId = Kernel.Types.Id.Id <$> merchantId,
              merchantOperatingCityId = Kernel.Types.Id.Id <$> merchantOperatingCityId,
              createdAt = createdAt,
              updatedAt = updatedAt
            }
  
  instance ToTType' Beam.BusinessLicense Domain.Types.BusinessLicense.BusinessLicense where
    toTType' (Domain.Types.BusinessLicense.BusinessLicense {..}) = do
      Beam.BusinessLicenseT
        { Beam.documentImageId = Kernel.Types.Id.getId documentImageId,
          Beam.driverId = Kernel.Types.Id.getId driverId,
          Beam.id = Kernel.Types.Id.getId id,
          Beam.licenseExpiry = licenseExpiry,
          Beam.licenseNumberEncrypted = ((licenseNumber & unEncrypted . encrypted)),
          Beam.licenseNumberHash = (licenseNumber & hash),
          Beam.verificationStatus = verificationStatus,
          Beam.merchantId = Kernel.Types.Id.getId <$> merchantId,
          Beam.merchantOperatingCityId = Kernel.Types.Id.getId <$> merchantOperatingCityId,
          Beam.createdAt = createdAt,
          Beam.updatedAt = updatedAt
        }
  