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.

177 lines
5.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. //
  2. // TotalManageAccountsInteractor.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon James Kim on 2019/11/15.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. class TotalManageAccountsInteractor {
  10. // MARK: Properties
  11. weak var output: TotalManageAccountsInteractorOutput?
  12. private let service: TotalManageAccountsServiceType
  13. private var accounts: [TotalAccount]?
  14. private var kftcModel: KFTCModel?
  15. // MARK: Initialization
  16. init(service: TotalManageAccountsServiceType) {
  17. self.service = service
  18. }
  19. }
  20. // MARK: TotalManageAccounts interactor input interface
  21. extension TotalManageAccountsInteractor: TotalManageAccountsInteractorInput {
  22. func fetchAccounts(_ type: AccountType) {
  23. guard let accounts = self.accounts else {
  24. callAPI()
  25. return
  26. }
  27. switch type {
  28. case .all:
  29. output?.setAccounts(accounts, kftcModel: kftcModel)
  30. case .primary:
  31. output?.setAccounts(
  32. accounts.filter {$0.accountType.filter {element in element == "primary"}.count != 0},
  33. kftcModel: kftcModel
  34. )
  35. case .autodebit:
  36. output?.setAccounts(
  37. accounts.filter {$0.accountType.filter {element in element == "autodebit"}.count != 0},
  38. kftcModel: kftcModel
  39. )
  40. case .inbound:
  41. output?.setAccounts(
  42. accounts.filter {$0.accountType.filter {element in element == "inbound"}.count != 0},
  43. kftcModel: kftcModel
  44. )
  45. case .none:
  46. output?.setAccounts(
  47. accounts.filter {$0.accountType.count == 0},
  48. kftcModel: kftcModel
  49. )
  50. }
  51. }
  52. private func callAPI() {
  53. let userId = GMEDB.shared.user.string(.userId) ?? ""
  54. service.fetchAutodebitAccounts(
  55. username: userId,
  56. success: {[weak self] kftcModel in
  57. var accounts = kftcModel?.model?.map {
  58. TotalAccount(
  59. autodebitID: $0.kftcAccountId,
  60. accountNo: $0.accountNumMasked,
  61. bankName: $0.bankName,
  62. bankCode: $0.bankCode,
  63. isPrimary: nil,
  64. isPennyTestRequested: $0.isInboundPennyTestRequested,
  65. isPennyTestSuccess: nil
  66. )
  67. }
  68. self?.service.fetchInboundAccounts(
  69. success: {[weak self] models in
  70. let inboundAccounts = models.map {
  71. TotalAccount(
  72. autodebitID: nil,
  73. accountNo: $0.accountNo,
  74. bankName: $0.bankName,
  75. bankCode: $0.bankCode,
  76. isPrimary: $0.isPrimary,
  77. isPennyTestRequested: nil,
  78. isPennyTestSuccess: $0.isPennyTestSuccess
  79. )
  80. }
  81. var dic = accounts?.toDictionary { "\($0.bankCode ?? "")/\($0.accountNo ?? "")" }
  82. inboundAccounts.forEach {
  83. if var element = dic?["\($0.bankCode ?? "")/\($0.accountNo ?? "")"] {
  84. element.isPennyTestSuccess = $0.isPennyTestSuccess
  85. element.isPrimary = $0.isPrimary
  86. dic?["\($0.bankCode ?? "")/\($0.accountNo ?? "")"] = element
  87. } else {
  88. dic?["\($0.bankCode ?? "")/\($0.accountNo ?? "")"] = $0
  89. }
  90. }
  91. accounts = dic?.map { $0.value }.sorted { $0.accountType.count > $1.accountType.count }
  92. if let primaryElement = accounts?.enumerated().first(where: { $0.element.isPrimary ?? false }) {
  93. accounts?.remove(at: primaryElement.offset)
  94. accounts?.insert(primaryElement.element, at: 0)
  95. }
  96. self?.accounts = accounts
  97. self?.kftcModel = kftcModel
  98. self?.output?.setAccounts(self?.accounts ?? [], kftcModel: kftcModel)
  99. },
  100. failure: {[weak self] in
  101. self?.output?.setError(with: $0)
  102. }
  103. )
  104. },
  105. failure: {[weak self] in
  106. self?.output?.setError(with: $0)
  107. }
  108. )
  109. }
  110. func refreshToken(language: KftcLanguage?) {
  111. guard
  112. let email = GMEDB.shared.user.string(.email),
  113. let language = language else { return }
  114. let service2 = AddAccountService()
  115. service.refreshTokenStep1(
  116. username: email,
  117. success: { response in
  118. let url = response?.url?.replacingOccurrences(
  119. of: "&lang=", with: "&lang=\(language.key ?? "eng")"
  120. ) ?? ""
  121. var header = [String: String]()
  122. response?.header?.forEach({
  123. header[$0.key ?? ""] = $0.value ?? ""
  124. })
  125. service2.fetchKftcUrlService(
  126. url: url,
  127. header: header,
  128. success: {
  129. self.output?.refreshTokenSuccess(header: response?.header, url: $0)
  130. },
  131. failure: {
  132. if $0.localizedDescription.contains("JSON") {
  133. self.output?.refreshTokenSuccess(header: response?.header, url: url)
  134. } else {
  135. self.output?.setError(with: $0)
  136. }
  137. }
  138. )
  139. },
  140. failure: {
  141. self.output?.setError(with: $0)
  142. }
  143. )
  144. }
  145. }
  146. fileprivate extension Array {
  147. func toDictionary<Key: Hashable>(with selectKey: (Element) -> Key) -> [Key:Element] {
  148. var dict = [Key: Element]()
  149. for element in self {
  150. dict[selectKey(element)] = element
  151. }
  152. return dict
  153. }
  154. }