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

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. class KycInteractor {
  10. // MARK: Properties
  11. weak var output: KycInteractorOutput?
  12. private let service: KycServiceType
  13. // MARK: Initialization
  14. init(service: KycServiceType) {
  15. self.service = service
  16. }
  17. // MARK: Converting entities
  18. // form 1
  19. func _validate(model: KycForm1Model?) -> (isValid: Bool, errorsDick: [String: String]) {
  20. var errorsDick: [String: String] = [:]
  21. var sucks = true // isValid = true
  22. let formDick =
  23. [
  24. KycForm1FieldKeys.firstName: model?.firstName,
  25. KycForm1FieldKeys.gender: model?.gender,
  26. KycForm1FieldKeys.email: model?.email,
  27. // KycForm1FieldKeys.dob: model?.dob,
  28. KycForm1FieldKeys.nativeCountry: model?.nativeCountry,
  29. KycForm1FieldKeys.country: model?.country,
  30. KycForm1FieldKeys.occupation: model?.occupation,
  31. KycForm1FieldKeys.province: model?.province,
  32. KycForm1FieldKeys.mobileNumber: model?.mobile
  33. ]
  34. formDick.forEach({
  35. if ($0.value ?? "").isEmpty {
  36. sucks = false // isValid = false
  37. errorsDick[$0.key] = "Please enter a valid \($0.key)"
  38. if ($0.key == KycForm1FieldKeys.gender) || ($0.key == KycForm1FieldKeys.nativeCountry) || ($0.key == KycForm1FieldKeys.province) || ($0.key == KycForm1FieldKeys.occupation) {
  39. errorsDick[$0.key] = "Please select \($0.key)"
  40. }
  41. }
  42. })
  43. if let email = model?.email, email != "" {
  44. if !Utility.isValidEmail(email: email){
  45. sucks = false
  46. errorsDick[KycForm1FieldKeys.email] = "invalid email"
  47. }
  48. }
  49. if let mobileNo = model?.mobile, mobileNo != "", mobileNo.count != AppConstants.maxKoreanMobileNoLength {
  50. sucks = false
  51. errorsDick[KycForm1FieldKeys.mobileNumber] = "please enter valid korean mobile number"
  52. }
  53. let result = (sucks, errorsDick) // (isValid, errorsDick)
  54. return result
  55. // self.output?.show(result1: result)
  56. }
  57. // form 2
  58. func _validate(model: KycForm2Model?) -> (isValid: Bool, errorsDick: [String: String]) {
  59. var errorsDick: [String: String] = [:]
  60. var sucks = true // isValid = true
  61. let shoulValidateExpiryDate = model?.checkExpiryDate ?? false
  62. let shoulValidateIssueDate = model?.checkIssueDate ?? false
  63. var cardType: KycVerificationIdType?
  64. let alienAndNationalCardNumberLenght = 14
  65. let formDick =
  66. [
  67. KycForm2FieldKeys.bank: model?.bank,
  68. KycForm2FieldKeys.accountNumber: model?.accountNumber,
  69. KycForm2FieldKeys.verificationId: model?.verificationId,
  70. KycForm2FieldKeys.verificationIdNumber: model?.verificationIdNumber,
  71. KycForm2FieldKeys.expiryDate: model?.expiryDate,
  72. KycForm2FieldKeys.sourceOfFund: model?.sourceOfFund,
  73. KycForm2FieldKeys.issueDate: model?.issueDate
  74. ]
  75. formDick.forEach({
  76. if ($0.value ?? "").isEmpty {
  77. if $0.key == KycForm2FieldKeys.issueDate {
  78. if shoulValidateIssueDate {
  79. sucks = false // isValid = false
  80. errorsDick[$0.key] = "Please select a \($0.key)"
  81. }
  82. }else if ( $0.key == KycForm2FieldKeys.expiryDate) {
  83. if shoulValidateExpiryDate {
  84. sucks = false // isValid = false
  85. errorsDick[$0.key] = "Please select a \($0.key)"
  86. }
  87. }else if $0.key == KycForm2FieldKeys.bank || ( $0.key == KycForm2FieldKeys.verificationId) || ( $0.key == KycForm2FieldKeys.sourceOfFund) {
  88. sucks = false // isValid = false
  89. errorsDick[$0.key] = "Please select a \($0.key)"
  90. }
  91. else {
  92. sucks = false // isValid = false
  93. errorsDick[$0.key] = "Please enter a valid \($0.key)"
  94. }
  95. if $0.key == KycForm2FieldKeys.verificationId {
  96. if let id = KycVerificationIdType.init(rawValue: $0.value ?? "") {
  97. switch id {
  98. case .alieanCard, .nationalIdCard:
  99. if let count = $0.value?.count, count != 0, count != alienAndNationalCardNumberLenght {
  100. errorsDick[$0.key] = "Please enter a \(alienAndNationalCardNumberLenght) digit \($0.key)"
  101. }
  102. case .passport:
  103. break
  104. }
  105. }
  106. }
  107. }
  108. })
  109. let result = (sucks, errorsDick) // (isValid, errorsDick)
  110. return result
  111. // self.output?.show(result2: result)
  112. }
  113. // form 3
  114. func _validate(model: KycForm3Model?) -> (isValid: Bool, errorsDick: [String: String]) {
  115. var errorsDick: [String: String] = [:]
  116. var sucks = true // isValid = true
  117. let formDick =
  118. [
  119. KycForm3FieldKeys.selfieImage: model?.selfieImage,
  120. KycForm3FieldKeys.frontImage: model?.frontImage,
  121. KycForm3FieldKeys.backImage: model?.backImage,
  122. KycForm3FieldKeys.passbookImage: model?.passbookImage
  123. ]
  124. formDick.forEach({
  125. if $0.value == nil {
  126. sucks = false // isValid = false
  127. errorsDick[$0.key] = "\($0.key) filed is required"
  128. }
  129. })
  130. let result = (sucks, errorsDick) // (isValid, errorsDick)
  131. return result
  132. // self.output?.show(result3: result)
  133. }
  134. private func getImageParams(model: KYCRequestModel) -> [String: Data] {
  135. var images: [String: Data] = [:]
  136. let model = model.kycForm3
  137. // selfie
  138. if let image = model?.selfieImage {
  139. if let data = getCompressedImage(image: image) {
  140. images["selfieUrl"] = data
  141. }
  142. }
  143. // front
  144. if let image = model?.frontImage {
  145. if let data = getCompressedImage(image: image) {
  146. images["regIdcardFrontUrl"] = data
  147. }
  148. }
  149. // back
  150. if let image = model?.backImage {
  151. if let data = getCompressedImage(image: image) {
  152. images["regIdcardBackUrl"] = data
  153. }
  154. }
  155. // passbookImage
  156. if let image = model?.passbookImage {
  157. if let data = getCompressedImage(image: image) {
  158. images["passbookUrl"] = data
  159. }
  160. }
  161. // passportImage
  162. if let image = model?.passportImage {
  163. if let data = getCompressedImage(image: image) {
  164. images["passportUrl"] = data
  165. }
  166. }
  167. return images
  168. }
  169. private func getCompressedImage(image: UIImage) -> Data? {
  170. return UIImageJPEGRepresentation(image, 0.6)
  171. }
  172. private func getParams(model: KYCRequestModel) -> [String: String] {
  173. let defaults = UserDefaults.standard
  174. let userName = defaults.string(forKey: UserKeys.userId) ?? ""
  175. let gender = model.kycForm1?.gender ?? ""
  176. var _gender = ""
  177. if gender.lowercased() == "Male".lowercased() {
  178. _gender = "M"
  179. }
  180. if gender.lowercased() == "Female".lowercased() {
  181. _gender = "F"
  182. }
  183. if gender.lowercased() == "other".lowercased() {
  184. _gender = "O"
  185. }
  186. let param: [String: String] =
  187. [
  188. "userId": userName,
  189. "mobileNumber": model.kycForm1?.mobile ?? "",
  190. "email": model.kycForm1?.email ?? "",
  191. "gender": _gender,
  192. "nativeCountry": model.kycForm1?.nativeCountry ?? "",
  193. "ProvinceId": model.kycForm1?.province ?? "",
  194. "occupation": model.kycForm1?.occupation ?? "",
  195. "primaryBankName": model.kycForm2?.bank ?? "",
  196. "primaryAccountNumber": model.kycForm2?.accountNumber ?? "",
  197. "verificationIdType": model.kycForm2?.verificationId ?? "",
  198. "verificationIdNumber": model.kycForm2?.verificationIdNumber ?? "",
  199. "expiryDate": model.kycForm2?.expiryDate ?? "",
  200. "issueDate": model.kycForm2?.issueDate ?? "",
  201. "sourceOfFund": model.kycForm2?.sourceOfFund ?? "",
  202. "firstName": model.kycForm1?.firstName ?? "",
  203. "middleName": model.kycForm1?.middleName ?? "",
  204. "lastName": model.kycForm1?.lastName ?? "",
  205. "address": model.kycForm1?.country ?? ""
  206. ]
  207. return param
  208. }
  209. }
  210. // MARK: Kyc interactor input interface
  211. extension KycInteractor: KycInteractorInput {
  212. func validate(model: KYCRequestModel) {
  213. let result1 = self._validate(model: model.kycForm1)
  214. let result2 = self._validate(model: model.kycForm2)
  215. let result3 = self._validate(model: model.kycForm3)
  216. let shouldSubmit = result1.isValid && result2.isValid && result3.isValid
  217. // let shouldSubmit = result3.isValid
  218. if shouldSubmit {
  219. /// call api here.
  220. let params = self.getParams(model: model)
  221. let images = self.getImageParams(model: model)
  222. self.service.submit(param: params, images: images, success: { (response) in
  223. // print(response?.firstName)
  224. // Todo: After success what?
  225. // show message from api
  226. UserDefaults.standard.set(true, forKey: UserKeys.kyc)
  227. self.output?.submitSuccess()
  228. }) { (error) in
  229. self.output?.show(error: error)
  230. }
  231. }else {
  232. self.output?.show(result1: result1)
  233. self.output?.show(result2: result2)
  234. self.output?.show(result3: result3)
  235. }
  236. }
  237. }