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.

384 lines
15 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // KycInteractor.swift
  3. // GMERemittance
  4. //
  5. // Created by gme_2 on 12/09/2018.
  6. //Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import FirebaseInstanceID
  10. class KycInteractor {
  11. // MARK: Properties
  12. weak var output: KycInteractorOutput?
  13. private let service: KycServiceType
  14. struct StringConstants {
  15. // personal
  16. let firstNameError = "kyc_first_name_error".localized()
  17. let genderError = "kyc_gender_error".localized()
  18. let emailError = "invalid email".localized()
  19. let nativeCountryError = "kyc_native_country_error".localized()
  20. let addressError = "kyc_address_error".localized()
  21. let occupationError = "kyc_occupation_error".localized()
  22. let provinceError = "kyc_province_error".localized()
  23. let mobileNumberError = "kyc_mobile_invalid_number_error".localized()
  24. // security
  25. let bankError = "kyc_bank_name_error".localized()
  26. let accountNumberError = "kyc_account_number_error".localized()
  27. let verificationIdError = "kyc_verification_id_error".localized()
  28. let verificationIdNumberError = "kyc_verification_no_error".localized()
  29. let sourceofFundError = "kyc_source_of_fund_error".localized()
  30. let issueDateError = "document_issue_date_error_text".localized()
  31. let expiryDateError = "document_expiry_date_error_text".localized()
  32. }
  33. // MARK: Initialization
  34. init(service: KycServiceType) {
  35. self.service = service
  36. }
  37. // MARK: Converting entities
  38. // form 1
  39. func _validate(model: KycForm1Model?) -> (isValid: Bool, errorsDick: [String: String]) {
  40. var errorsDick: [String: String] = [:]
  41. var sucks = true // isValid = true
  42. let formDick =
  43. [
  44. KycForm1FieldKeys.firstName: model?.firstName,
  45. KycForm1FieldKeys.gender: model?.gender,
  46. KycForm1FieldKeys.email: model?.email,
  47. KycForm1FieldKeys.nativeCountry: model?.nativeCountry,
  48. KycForm1FieldKeys.country: model?.country,
  49. KycForm1FieldKeys.occupation: model?.occupation,
  50. KycForm1FieldKeys.province: model?.province,
  51. KycForm1FieldKeys.mobileNumber: model?.mobile
  52. ]
  53. formDick.forEach({
  54. if ($0.value ?? "").isEmpty {
  55. sucks = false // isValid = false
  56. errorsDick[$0.key] = "Please enter a valid \($0.key)"
  57. if ($0.key == KycForm1FieldKeys.gender) || ($0.key == KycForm1FieldKeys.nativeCountry) || ($0.key == KycForm1FieldKeys.province) || ($0.key == KycForm1FieldKeys.occupation) {
  58. errorsDick[$0.key] = "Please select \($0.key)"
  59. }
  60. }
  61. })
  62. if let value = model?.firstName, value.isEmpty {
  63. sucks = false
  64. errorsDick[KycForm1FieldKeys.firstName] = StringConstants().firstNameError
  65. }
  66. if let value = model?.gender, value.isEmpty {
  67. sucks = false
  68. errorsDick[KycForm1FieldKeys.gender] = StringConstants().genderError
  69. }
  70. if let value = model?.nativeCountry, value.isEmpty {
  71. sucks = false
  72. errorsDick[KycForm1FieldKeys.nativeCountry] = StringConstants().nativeCountryError
  73. }
  74. if let value = model?.country, value.isEmpty {
  75. sucks = false
  76. errorsDick[KycForm1FieldKeys.country] = StringConstants().addressError
  77. }
  78. if let value = model?.occupation, value.isEmpty {
  79. sucks = false
  80. errorsDick[KycForm1FieldKeys.occupation] = StringConstants().occupationError
  81. }
  82. if let value = model?.province, value.isEmpty {
  83. sucks = false
  84. errorsDick[KycForm1FieldKeys.province] = StringConstants().provinceError
  85. }
  86. if let email = model?.email, email.isEmpty {
  87. if !Utility.isValidEmail(email: email){
  88. sucks = false
  89. errorsDick[KycForm1FieldKeys.email] = StringConstants().emailError
  90. }
  91. }
  92. if let mobileNo = model?.mobile, mobileNo.isEmpty , mobileNo.count != AppConstants.maxKoreanMobileNoLength {
  93. sucks = false
  94. errorsDick[KycForm1FieldKeys.mobileNumber] = StringConstants().mobileNumberError
  95. }
  96. let result = (sucks, errorsDick) // (isValid, errorsDick)
  97. return result
  98. // self.output?.show(result1: result)
  99. }
  100. // form 2
  101. func _validate(model: KycForm2Model?) -> (isValid: Bool, errorsDick: [String: String]) {
  102. var errorsDick: [String: String] = [:]
  103. var sucks = true // isValid = true
  104. let shoulValidateExpiryDate = model?.checkExpiryDate ?? false
  105. let shoulValidateIssueDate = model?.checkIssueDate ?? false
  106. var cardType: KycVerificationIdType?
  107. let alienAndNationalCardNumberLenght = 14
  108. let formDick =
  109. [
  110. KycForm2FieldKeys.bank: model?.bank,
  111. KycForm2FieldKeys.accountNumber: model?.accountNumber,
  112. KycForm2FieldKeys.verificationId: model?.verificationId,
  113. KycForm2FieldKeys.verificationIdNumber: model?.verificationIdNumber,
  114. KycForm2FieldKeys.expiryDate: model?.expiryDate,
  115. KycForm2FieldKeys.sourceOfFund: model?.sourceOfFund,
  116. KycForm2FieldKeys.issueDate: model?.issueDate
  117. ]
  118. formDick.forEach({
  119. if ($0.value ?? "").isEmpty {
  120. if $0.key == KycForm2FieldKeys.issueDate {
  121. if shoulValidateIssueDate {
  122. sucks = false // isValid = false
  123. errorsDick[$0.key] = StringConstants().issueDateError
  124. }
  125. }else if ( $0.key == KycForm2FieldKeys.expiryDate) {
  126. if shoulValidateExpiryDate {
  127. sucks = false // isValid = false
  128. errorsDick[$0.key] = StringConstants().expiryDateError
  129. }
  130. }else
  131. if $0.key == KycForm2FieldKeys.bank {
  132. sucks = false // isValid = false
  133. errorsDick[$0.key] = StringConstants().bankError
  134. }else
  135. if $0.key == KycForm2FieldKeys.verificationId {
  136. sucks = false // isValid = false
  137. errorsDick[$0.key] = StringConstants().verificationIdError
  138. }else
  139. if $0.key == KycForm2FieldKeys.sourceOfFund {
  140. sucks = false // isValid = false
  141. errorsDick[$0.key] = StringConstants().sourceofFundError
  142. }else
  143. if $0.key == KycForm2FieldKeys.accountNumber {
  144. sucks = false // isValid = false
  145. errorsDick[$0.key] = StringConstants().accountNumberError
  146. }else
  147. if $0.key == KycForm2FieldKeys.verificationIdNumber {
  148. sucks = false // isValid = false
  149. errorsDick[$0.key] = StringConstants().verificationIdNumberError
  150. }
  151. if $0.key == KycForm2FieldKeys.verificationId {
  152. sucks = false
  153. if let id = KycVerificationIdType.init(rawValue: $0.value ?? "") {
  154. switch id {
  155. case .alieanCard:
  156. if let count = $0.value?.count, count != 0, count != alienAndNationalCardNumberLenght {
  157. errorsDick[$0.key] = "Please enter a 14 digit Alien Registration Card"
  158. }else {
  159. errorsDick[$0.key] = "Please enter a 14 digit Alien Registration Card"
  160. }
  161. case .nationalIdCard:
  162. if let count = $0.value?.count, count != 0, count != alienAndNationalCardNumberLenght {
  163. errorsDick[$0.key] = "Please enter a 14 digit National ID"
  164. }
  165. case .passport:
  166. break
  167. }
  168. }
  169. }
  170. }
  171. })
  172. let result = (sucks, errorsDick) // (isValid, errorsDick)
  173. return result
  174. // self.output?.show(result2: result)
  175. }
  176. // form 3
  177. func _validate(model: KycForm3Model?) -> (isValid: Bool, errorsDick: [String: String]) {
  178. var errorsDick: [String: String] = [:]
  179. var sucks = true // isValid = true
  180. let formDick =
  181. [
  182. KycForm3FieldKeys.selfieImage: model?.selfieImage,
  183. KycForm3FieldKeys.frontImage: model?.frontImage,
  184. KycForm3FieldKeys.backImage: model?.backImage,
  185. KycForm3FieldKeys.passbookImage: model?.passbookImage
  186. ]
  187. formDick.forEach({
  188. if $0.value == nil {
  189. sucks = false // isValid = false
  190. errorsDick[$0.key] = "\($0.key) filed is required"
  191. }
  192. })
  193. let result = (sucks, errorsDick) // (isValid, errorsDick)
  194. return result
  195. // self.output?.show(result3: result)
  196. }
  197. private func getImageParams(model: KYCRequestModel) -> [String: Data] {
  198. var images: [String: Data] = [:]
  199. let model = model.kycForm3
  200. // selfie
  201. if let image = model?.selfieImage {
  202. if let data = getCompressedImage(image: image) {
  203. images["selfieUrl"] = data
  204. }
  205. }
  206. // front
  207. if let image = model?.frontImage {
  208. if let data = getCompressedImage(image: image) {
  209. images["regIdcardFrontUrl"] = data
  210. }
  211. }
  212. // back
  213. if let image = model?.backImage {
  214. if let data = getCompressedImage(image: image) {
  215. images["regIdcardBackUrl"] = data
  216. }
  217. }
  218. // passbookImage
  219. if let image = model?.passbookImage {
  220. if let data = getCompressedImage(image: image) {
  221. images["passbookUrl"] = data
  222. }
  223. }
  224. // passportImage
  225. if let image = model?.passportImage {
  226. if let data = getCompressedImage(image: image) {
  227. images["passportUrl"] = data
  228. }
  229. }
  230. return images
  231. }
  232. private func getCompressedImage(image: UIImage) -> Data? {
  233. return UIImageJPEGRepresentation(image, 0.6)
  234. }
  235. private func getParams(model: KYCRequestModel) -> [String: String] {
  236. let defaults = UserDefaults.standard
  237. let userName = defaults.string(forKey: UserKeys.userId) ?? ""
  238. let gender = model.kycForm1?.gender ?? ""
  239. var _gender = ""
  240. if gender.lowercased() == "Male".lowercased() {
  241. _gender = "M"
  242. }
  243. if gender.lowercased() == "Female".lowercased() {
  244. _gender = "F"
  245. }
  246. if gender.lowercased() == "other".lowercased() {
  247. _gender = "O"
  248. }
  249. let param: [String: String] =
  250. [
  251. "userId": userName,
  252. "mobileNumber": model.kycForm1?.mobile ?? "",
  253. "email": model.kycForm1?.email ?? "",
  254. "gender": _gender,
  255. "nativeCountry": model.kycForm1?.nativeCountry ?? "",
  256. "ProvinceId": model.kycForm1?.province ?? "",
  257. "occupation": model.kycForm1?.occupation ?? "",
  258. "referralCode": model.kycForm1?.referralCode ?? "",
  259. "primaryBankName": model.kycForm2?.bank ?? "",
  260. "primaryAccountNumber": model.kycForm2?.accountNumber ?? "",
  261. "verificationIdType": model.kycForm2?.verificationId ?? "",
  262. "verificationIdNumber": model.kycForm2?.verificationIdNumber ?? "",
  263. "expiryDate": model.kycForm2?.expiryDate ?? "",
  264. "issueDate": model.kycForm2?.issueDate ?? "",
  265. "sourceOfFund": model.kycForm2?.sourceOfFund ?? "",
  266. "firstName": model.kycForm1?.firstName ?? "",
  267. "middleName": model.kycForm1?.middleName ?? "",
  268. "lastName": model.kycForm1?.lastName ?? "",
  269. "address": model.kycForm1?.country ?? ""
  270. ]
  271. return param
  272. }
  273. }
  274. // MARK: Kyc interactor input interface
  275. extension KycInteractor: KycInteractorInput {
  276. private func getFcmToken() -> String? {
  277. let token = InstanceID.instanceID().token()
  278. return token
  279. }
  280. func validate(model: KYCRequestModel) {
  281. let result1 = self._validate(model: model.kycForm1)
  282. let result2 = self._validate(model: model.kycForm2)
  283. let result3 = self._validate(model: model.kycForm3)
  284. let shouldSubmit = result1.isValid && result2.isValid && result3.isValid
  285. // let shouldSubmit = result3.isValid
  286. if shouldSubmit {
  287. /// call api here.
  288. let params = self.getParams(model: model)
  289. let images = self.getImageParams(model: model)
  290. self.service.submit(param: params, images: images, success: { (response) in
  291. // print(response?.firstName)
  292. // Todo: After success what?
  293. // show message from api
  294. let userId = Utility.getMyUserName()
  295. var param = ["userId" : userId]
  296. param["uuid"] = Utility.getUUid() ?? ""
  297. param["appVersion"] = Utility.getAppVersion()
  298. param["phoneBrand"] = Utility.getPhoneBrand()
  299. param["phoneOs"] = Utility.getPhoneOs()
  300. param["fcmId"] = self.getFcmToken()
  301. param["osVersion"] = Utility.getOsVersion()
  302. self.service.fetchUserInfo(param: param, success: { (user) in
  303. Utility.save(user: user)
  304. UserDefaults.standard.set(true, forKey: UserKeys.kyc)
  305. self.output?.submitSuccess()
  306. }, failure: { (error) in
  307. })
  308. }) { (error) in
  309. self.output?.show(error: error)
  310. }
  311. }else {
  312. self.output?.show(result1: result1)
  313. self.output?.show(result2: result2)
  314. self.output?.show(result3: result3)
  315. }
  316. }
  317. }