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

//
// TotalManageAccountsInteractor.swift
// GME Remit
//
// Created by InKwon James Kim on 2019/11/15.
//Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
//
import Foundation
class TotalManageAccountsInteractor {
// MARK: Properties
weak var output: TotalManageAccountsInteractorOutput?
private let service: TotalManageAccountsServiceType
private var accounts: [TotalAccount]?
private var kftcModel: KFTCModel?
// MARK: Initialization
init(service: TotalManageAccountsServiceType) {
self.service = service
}
}
// MARK: TotalManageAccounts interactor input interface
extension TotalManageAccountsInteractor: TotalManageAccountsInteractorInput {
func fetchAccounts(_ type: AccountType) {
guard let accounts = self.accounts else {
callAPI()
return
}
switch type {
case .all:
output?.setAccounts(accounts, kftcModel: kftcModel)
case .primary:
output?.setAccounts(
accounts.filter {$0.accountType.filter {element in element == "primary"}.count != 0},
kftcModel: kftcModel
)
case .autodebit:
output?.setAccounts(
accounts.filter {$0.accountType.filter {element in element == "autodebit"}.count != 0},
kftcModel: kftcModel
)
case .inbound:
output?.setAccounts(
accounts.filter {$0.accountType.filter {element in element == "inbound"}.count != 0},
kftcModel: kftcModel
)
case .none:
output?.setAccounts(
accounts.filter {$0.accountType.count == 0},
kftcModel: kftcModel
)
}
}
private func callAPI() {
let userId = GMEDB.shared.user.string(.userId) ?? ""
service.fetchAutodebitAccounts(
username: userId,
success: {[weak self] kftcModel in
var accounts = kftcModel?.model?.map {
TotalAccount(
autodebitID: $0.kftcAccountId,
accountNo: $0.accountNumMasked,
bankName: $0.bankName,
bankCode: $0.bankCode,
isPrimary: nil,
isPennyTestRequested: $0.isInboundPennyTestRequested,
isPennyTestSuccess: nil
)
}
self?.service.fetchInboundAccounts(
success: {[weak self] models in
let inboundAccounts = models.map {
TotalAccount(
autodebitID: nil,
accountNo: $0.accountNo,
bankName: $0.bankName,
bankCode: $0.bankCode,
isPrimary: $0.isPrimary,
isPennyTestRequested: nil,
isPennyTestSuccess: $0.isPennyTestSuccess
)
}
var dic = accounts?.toDictionary { "\($0.bankCode ?? "")/\($0.accountNo ?? "")" }
inboundAccounts.forEach {
if var element = dic?["\($0.bankCode ?? "")/\($0.accountNo ?? "")"] {
element.isPennyTestSuccess = $0.isPennyTestSuccess
element.isPrimary = $0.isPrimary
dic?["\($0.bankCode ?? "")/\($0.accountNo ?? "")"] = element
} else {
dic?["\($0.bankCode ?? "")/\($0.accountNo ?? "")"] = $0
}
}
accounts = dic?.map { $0.value }.sorted { $0.accountType.count > $1.accountType.count }
if let primaryElement = accounts?.enumerated().first(where: { $0.element.isPrimary ?? false }) {
accounts?.remove(at: primaryElement.offset)
accounts?.insert(primaryElement.element, at: 0)
}
self?.accounts = accounts
self?.kftcModel = kftcModel
self?.output?.setAccounts(self?.accounts ?? [], kftcModel: kftcModel)
},
failure: {[weak self] in
self?.output?.setError(with: $0)
}
)
},
failure: {[weak self] in
self?.output?.setError(with: $0)
}
)
}
func refreshToken(language: KftcLanguage?) {
guard
let email = GMEDB.shared.user.string(.email),
let language = language else { return }
let service2 = AddAccountService()
service.refreshTokenStep1(
username: email,
success: { response in
let url = response?.url?.replacingOccurrences(
of: "&lang=", with: "&lang=\(language.key ?? "eng")"
) ?? ""
var header = [String: String]()
response?.header?.forEach({
header[$0.key ?? ""] = $0.value ?? ""
})
service2.fetchKftcUrlService(
url: url,
header: header,
success: {
self.output?.refreshTokenSuccess(header: response?.header, url: $0)
},
failure: {
if $0.localizedDescription.contains("JSON") {
self.output?.refreshTokenSuccess(header: response?.header, url: url)
} else {
self.output?.setError(with: $0)
}
}
)
},
failure: {
self.output?.setError(with: $0)
}
)
}
}
fileprivate extension Array {
func toDictionary<Key: Hashable>(with selectKey: (Element) -> Key) -> [Key:Element] {
var dict = [Key: Element]()
for element in self {
dict[selectKey(element)] = element
}
return dict
}
}