You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

282 lines
10 KiB

//
// KycInteractor.swift
// GMERemittance
//
// Created by gme_2 on 12/09/2018.
//Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
//
import Foundation
class KycInteractor {
// MARK: Properties
weak var output: KycInteractorOutput?
private let service: KycServiceType
// MARK: Initialization
init(service: KycServiceType) {
self.service = service
}
// MARK: Converting entities
// form 1
func _validate(model: KycForm1Model?) -> (isValid: Bool, errorsDick: [String: String]) {
var errorsDick: [String: String] = [:]
var sucks = true // isValid = true
let formDick =
[
KycForm1FieldKeys.firstName: model?.firstName,
KycForm1FieldKeys.gender: model?.gender,
KycForm1FieldKeys.email: model?.email,
// KycForm1FieldKeys.dob: model?.dob,
KycForm1FieldKeys.nativeCountry: model?.nativeCountry,
KycForm1FieldKeys.country: model?.country,
KycForm1FieldKeys.occupation: model?.occupation,
KycForm1FieldKeys.province: model?.province,
KycForm1FieldKeys.mobileNumber: model?.mobile
]
formDick.forEach({
if ($0.value ?? "").isEmpty {
sucks = false // isValid = false
errorsDick[$0.key] = "Please enter a valid \($0.key)"
if ($0.key == KycForm1FieldKeys.gender) || ($0.key == KycForm1FieldKeys.nativeCountry) || ($0.key == KycForm1FieldKeys.province) || ($0.key == KycForm1FieldKeys.occupation) {
errorsDick[$0.key] = "Please select \($0.key)"
}
}
})
if let email = model?.email, email != "" {
if !Utility.isValidEmail(email: email){
sucks = false
errorsDick[KycForm1FieldKeys.email] = "invalid email"
}
}
if let mobileNo = model?.mobile, mobileNo != "", mobileNo.count != AppConstants.maxKoreanMobileNoLength {
sucks = false
errorsDick[KycForm1FieldKeys.mobileNumber] = "please enter valid korean mobile number"
}
let result = (sucks, errorsDick) // (isValid, errorsDick)
return result
// self.output?.show(result1: result)
}
// form 2
func _validate(model: KycForm2Model?) -> (isValid: Bool, errorsDick: [String: String]) {
var errorsDick: [String: String] = [:]
var sucks = true // isValid = true
let shoulValidateExpiryDate = model?.checkExpiryDate ?? false
let shoulValidateIssueDate = model?.checkIssueDate ?? false
var cardType: KycVerificationIdType?
let alienAndNationalCardNumberLenght = 14
let formDick =
[
KycForm2FieldKeys.bank: model?.bank,
KycForm2FieldKeys.accountNumber: model?.accountNumber,
KycForm2FieldKeys.verificationId: model?.verificationId,
KycForm2FieldKeys.verificationIdNumber: model?.verificationIdNumber,
KycForm2FieldKeys.expiryDate: model?.expiryDate,
KycForm2FieldKeys.sourceOfFund: model?.sourceOfFund,
KycForm2FieldKeys.issueDate: model?.issueDate
]
formDick.forEach({
if ($0.value ?? "").isEmpty {
if $0.key == KycForm2FieldKeys.issueDate {
if shoulValidateIssueDate {
sucks = false // isValid = false
errorsDick[$0.key] = "Please select a \($0.key)"
}
}else if ( $0.key == KycForm2FieldKeys.expiryDate) {
if shoulValidateExpiryDate {
sucks = false // isValid = false
errorsDick[$0.key] = "Please select a \($0.key)"
}
}else if $0.key == KycForm2FieldKeys.bank || ( $0.key == KycForm2FieldKeys.verificationId) || ( $0.key == KycForm2FieldKeys.sourceOfFund) {
sucks = false // isValid = false
errorsDick[$0.key] = "Please select a \($0.key)"
}
else {
sucks = false // isValid = false
errorsDick[$0.key] = "Please enter a valid \($0.key)"
}
if $0.key == KycForm2FieldKeys.verificationId {
if let id = KycVerificationIdType.init(rawValue: $0.value ?? "") {
switch id {
case .alieanCard, .nationalIdCard:
if let count = $0.value?.count, count != 0, count != alienAndNationalCardNumberLenght {
errorsDick[$0.key] = "Please enter a \(alienAndNationalCardNumberLenght) digit \($0.key)"
}
case .passport:
break
}
}
}
}
})
let result = (sucks, errorsDick) // (isValid, errorsDick)
return result
// self.output?.show(result2: result)
}
// form 3
func _validate(model: KycForm3Model?) -> (isValid: Bool, errorsDick: [String: String]) {
var errorsDick: [String: String] = [:]
var sucks = true // isValid = true
let formDick =
[
KycForm3FieldKeys.selfieImage: model?.selfieImage,
KycForm3FieldKeys.frontImage: model?.frontImage,
KycForm3FieldKeys.backImage: model?.backImage,
KycForm3FieldKeys.passbookImage: model?.passbookImage
]
formDick.forEach({
if $0.value == nil {
sucks = false // isValid = false
errorsDick[$0.key] = "\($0.key) filed is required"
}
})
let result = (sucks, errorsDick) // (isValid, errorsDick)
return result
// self.output?.show(result3: result)
}
private func getImageParams(model: KYCRequestModel) -> [String: Data] {
var images: [String: Data] = [:]
let model = model.kycForm3
// selfie
if let image = model?.selfieImage {
if let data = getCompressedImage(image: image) {
images["selfieUrl"] = data
}
}
// front
if let image = model?.frontImage {
if let data = getCompressedImage(image: image) {
images["regIdcardFrontUrl"] = data
}
}
// back
if let image = model?.backImage {
if let data = getCompressedImage(image: image) {
images["regIdcardBackUrl"] = data
}
}
// passbookImage
if let image = model?.passbookImage {
if let data = getCompressedImage(image: image) {
images["passbookUrl"] = data
}
}
// passportImage
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.6)
}
private func getParams(model: KYCRequestModel) -> [String: String] {
let defaults = UserDefaults.standard
let userName = defaults.string(forKey: UserKeys.userId) ?? ""
let gender = model.kycForm1?.gender ?? ""
var _gender = ""
if gender.lowercased() == "Male".lowercased() {
_gender = "M"
}
if gender.lowercased() == "Female".lowercased() {
_gender = "F"
}
if gender.lowercased() == "other".lowercased() {
_gender = "O"
}
let param: [String: String] =
[
"userId": userName,
"mobileNumber": model.kycForm1?.mobile ?? "",
"email": model.kycForm1?.email ?? "",
"gender": _gender,
"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 ?? "",
"issueDate": model.kycForm2?.issueDate ?? "",
"sourceOfFund": model.kycForm2?.sourceOfFund ?? "",
"firstName": model.kycForm1?.firstName ?? "",
"middleName": model.kycForm1?.middleName ?? "",
"lastName": model.kycForm1?.lastName ?? "",
"address": model.kycForm1?.country ?? ""
]
return param
}
}
// MARK: Kyc interactor input interface
extension KycInteractor: KycInteractorInput {
func validate(model: KYCRequestModel) {
let result1 = self._validate(model: model.kycForm1)
let result2 = self._validate(model: model.kycForm2)
let result3 = self._validate(model: model.kycForm3)
let shouldSubmit = result1.isValid && result2.isValid && result3.isValid
// let shouldSubmit = result3.isValid
if shouldSubmit {
/// call api here.
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)
// Todo: After success what?
// show message from api
UserDefaults.standard.set(true, forKey: UserKeys.kyc)
self.output?.submitSuccess()
}) { (error) in
self.output?.show(error: error)
}
}else {
self.output?.show(result1: result1)
self.output?.show(result2: result2)
self.output?.show(result3: result3)
}
}
}