@ -10,7 +10,7 @@ import Foundation
class KycInteractor {
// MARK: P r o p e r t i e s
// MARK: P r o p e r t i e s
weak var output : KycInteractorOutput ?
private let service : KycServiceType
@ -20,12 +20,18 @@ class KycInteractor {
init ( service : KycServiceType ) {
self . service = service
}
// MARK: C o n v e r t i n g e n t i t i e s
// f o r m 1
func _validate ( model : KycForm1Model ? ) -> ( isValid : Bool , errorsDick : [ String : String ] ) {
func isValidEmail ( email : String ) -> Bool {
let emailRegex = " [A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+ \\ .[A-Za-z]{2,} "
let emailTest = NSPredicate ( format : " SELF MATCHES %@ " , emailRegex )
return emailTest . evaluate ( with : email )
}
var errorsDick : [ String : String ] = [ : ]
var sucks = true // i s V a l i d = t r u e
@ -50,9 +56,18 @@ class KycInteractor {
}
} )
if let email = model ? . email {
if ! isValidEmail ( email : email ) {
sucks = false
errorsDick [ KycForm1FieldKeys . email ] = " invalid email "
}
}
let result = ( sucks , errorsDick ) // ( i s V a l i d , e r r o r s D i c k )
return result
// s e l f . o u t p u t ? . s h o w ( r e s u l t 1 : r e s u l t )
// s e l f . o u t p u t ? . s h o w ( r e s u l t 1 : r e s u l t )
}
// f o r m 2
@ -79,7 +94,7 @@ class KycInteractor {
} )
let result = ( sucks , errorsDick ) // ( i s V a l i d , e r r o r s D i c k )
return result
// s e l f . o u t p u t ? . s h o w ( r e s u l t 2 : r e s u l t )
// s e l f . o u t p u t ? . s h o w ( r e s u l t 2 : r e s u l t )
}
@ -105,7 +120,81 @@ class KycInteractor {
} )
let result = ( sucks , errorsDick ) // ( i s V a l i d , e r r o r s D i c k )
return result
// s e l f . o u t p u t ? . s h o w ( r e s u l t 3 : r e s u l t )
// s e l f . o u t p u t ? . s h o w ( r e s u l t 3 : r e s u l t )
}
private func getImageParams ( model : KYCRequestModel ) -> [ String : Data ] {
var images : [ String : Data ] = [ : ]
let model = model . kycForm3
// s e l f i e
if let image = model ? . selfieImage {
if let data = getCompressedImage ( image : image ) {
images [ " selfieUrl " ] = data
}
}
// f r o n t
if let image = model ? . frontImage {
if let data = getCompressedImage ( image : image ) {
images [ " regIdcardFrontUrl " ] = data
}
}
// b a c k
if let image = model ? . backImage {
if let data = getCompressedImage ( image : image ) {
images [ " regIdcardBackUrl " ] = data
}
}
// p a s s b o o k I m a g e
if let image = model ? . passbookImage {
if let data = getCompressedImage ( image : image ) {
images [ " passbookUrl " ] = data
}
}
// p a s s p o r t I m a g e
if let image = model ? . passportImage {
if let data = getCompressedImage ( image : image ) {
images [ " passportUrl " ] = data
}
}
return images
}
private func getCompressedImage ( image : UIImage ) -> Data ? {
return UIImageJPEGRepresentation ( image , 0.5 )
}
private func getParams ( model : KYCRequestModel ) -> [ String : String ] {
let defaults = UserDefaults . standard
let userName = defaults . string ( forKey : UserKeys . userId ) ? ? " "
let param : [ String : String ] =
[
" userId " : userName ,
" mobileNumber " : model . kycForm1 ? . mobile ? ? " " ,
" email " : model . kycForm1 ? . email ? ? " " ,
" gender " : model . kycForm1 ? . gender ? ? " " ,
" dateOfBirth " : model . kycForm1 ? . dob ? ? " " ,
" nativeCountry " : model . kycForm1 ? . nativeCountry ? ? " " ,
" ProvinceId " : model . kycForm1 ? . province ? ? " " ,
" occupation " : model . kycForm1 ? . occupation ? ? " " ,
" primaryBankName " : model . kycForm2 ? . bank ? ? " " ,
" primaryAccountNumber " : model . kycForm2 ? . accountNumber ? ? " " ,
" verificationIdType " : model . kycForm2 ? . verificationId ? ? " " ,
" verificationIdNumber " : model . kycForm2 ? . verificationIdNumber ? ? " " ,
" expiryDate " : model . kycForm2 ? . expiryDate ? ? " " ,
" sourceOfFund " : model . kycForm2 ? . sourceOfFund ? ? " " ,
" firstName " : model . kycForm1 ? . firstName ? ? " " ,
" middleName " : model . kycForm1 ? . middleName ? ? " " ,
" lastName " : model . kycForm1 ? . lastName ? ? " " ,
" address " : model . kycForm1 ? . country ? ? " "
]
return param
}
}
@ -121,7 +210,13 @@ extension KycInteractor: KycInteractorInput {
let shouldSubmit = result1 . isValid && result2 . isValid && result3 . isValid
if shouldSubmit {
// / c a l l a p i h e r e .
let params = self . getParams ( model : model )
let images = self . getImageParams ( model : model )
self . service . submit ( param : params , images : images , success : { ( response ) in
print ( response ? . firstName )
} ) { ( error ) in
self . output ? . show ( error : error )
}
} else {
self . output ? . show ( result1 : result1 )
self . output ? . show ( result2 : result2 )