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.

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