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.

634 lines
21 KiB

6 years ago
  1. //
  2. // TrackTransactionViewModel.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 2/6/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. class TrackTransactionViewModel: ModelExtension {
  10. var transactionListObtained: Box<Bool?> = Box(nil)
  11. var transactionListConnectionTimeOut: Box<Bool?> = Box(nil)
  12. private var transactionArray: [Transaction] = [Transaction] ()
  13. private var walletArray: [Transaction] = [Transaction] ()
  14. //Search Transaction
  15. /**
  16. APi request for specific recipient
  17. - parameter recipientId: recipient Id
  18. - parameter recipientName: recipient name
  19. - parameter transactionType: type of transaction
  20. - parameter startDate: Start date for search
  21. - parameter endDate: end date for search
  22. - parameter txPage: page number
  23. - parameter txSize: number of transactions
  24. */
  25. func fetchTransactionList(recipientId: String, recipientName: String, transactionType: String, startDate: String?, endDate: String?, txPage: String?, txSize: String?) {
  26. if !Reachability.isConnectedToNetwork() {
  27. self.internetConnection.value = false
  28. } else {
  29. let recipient_id = recipientId
  30. let recipient_name = recipientName
  31. let transaction_type = transactionType
  32. var start_date = startDate
  33. var end_date = endDate
  34. let date = Date()
  35. let formatter = DateFormatter()
  36. formatter.dateFormat = "yyyy/MM/dd"
  37. if end_date == nil {
  38. end_date = formatter.string(from: date)
  39. }
  40. let yearsToAdd = -5
  41. if start_date == nil {
  42. start_date = formatter.string(from: Calendar.current.date(byAdding: .year, value: yearsToAdd, to: date)!)
  43. }
  44. var page = txPage
  45. var size = txSize
  46. if page == nil {
  47. page = ""
  48. }
  49. if size == nil {
  50. size = ""
  51. }
  52. RestApiMananger.sharedInstance.getTransactionList(userId: self.getUserId(), recipientId: recipientId, recipientName: recipientName, transactionType: transactionType, startDate: start_date!, endDate: end_date!, txPage: page!, txSize: size!) { result in
  53. switch result {
  54. case let .success(fetchedJSON):
  55. self.transactionArray.removeAll()
  56. guard fetchedJSON.count > 0 else {
  57. self.transactionListObtained.value = true
  58. return
  59. }
  60. for i in 0 ... (fetchedJSON.count-1) {
  61. do {
  62. let transaction = try JSONDecoder().decode(Transaction.self, from: fetchedJSON[i].rawData())
  63. self.transactionArray.append(transaction)
  64. self.walletArray.append(transaction)
  65. } catch {
  66. self.transactionListObtained.value = false
  67. }
  68. }
  69. self.transactionListObtained.value = true
  70. case let .failure(errorJSON):
  71. self.setErrorMessage(message: errorJSON["message"].stringValue)
  72. self.transactionListObtained.value = false
  73. case .updateAccessCode:
  74. RestApiMananger.sharedInstance.updateAccessCode(userId: self.getUserId(), password: self.getLoginPassword()) {
  75. result in
  76. if result != "Error"{
  77. UserDefaults.standard.set((result + ":" + RestApiMananger.sharedInstance.getUUID()).toBase64(), forKey: "com.gmeremit.accessCode")
  78. self.fetchTransactionList(recipientId: recipient_id, recipientName: recipient_name, transactionType: transaction_type, startDate: nil, endDate: nil, txPage: txPage, txSize: txSize)
  79. }
  80. }
  81. case .logOutUser():
  82. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  83. self.anotherLogin.value = true
  84. case .timeOut:
  85. self.transactionListConnectionTimeOut.value = false
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. Transaction Pagination
  92. APi request for specific recipient
  93. - parameter recipientId: recipient Id
  94. - parameter recipientName: recipient name
  95. - parameter transactionType: type of transaction
  96. - parameter startDate: Start date for search
  97. - parameter endDate: end date for search
  98. - parameter page: page number
  99. - parameter size: number of transactions
  100. */
  101. func fetchTransactionList(recipientId: String, recipientName: String, transactionType: String, startDate: String?, endDate: String?, page: Int?, size: Int?) {
  102. if !Reachability.isConnectedToNetwork() {
  103. self.internetConnection.value = false
  104. } else {
  105. self.transactionArray.removeAll()
  106. let recipient_id = recipientId
  107. let recipient_name = recipientName
  108. let transaction_type = transactionType
  109. var start_date = startDate
  110. var end_date = endDate
  111. let date = Date()
  112. let formatter = DateFormatter()
  113. formatter.dateFormat = "yyyy/MM/dd"
  114. if start_date == nil {
  115. start_date = formatter.string(from: date)
  116. }
  117. let yearsToAdd = -5
  118. if end_date == nil {
  119. end_date = formatter.string(from: Calendar.current.date(byAdding: .year, value: yearsToAdd, to: date)!)
  120. }
  121. RestApiMananger.sharedInstance.getTransactionList(userId: getUserId(), recipientId: recipientId, recipientName: "", transactionType: transactionType, startDate: end_date!, endDate: start_date!, page: page!, size: size!) { result in
  122. switch result {
  123. case let .success(fetchedJSON):
  124. self.transactionArray.removeAll()
  125. guard fetchedJSON.count > 0 else {
  126. self.transactionListObtained.value = true
  127. return
  128. }
  129. for i in 0 ... (fetchedJSON.count-1) {
  130. do {
  131. let transaction = try JSONDecoder().decode(Transaction.self, from: fetchedJSON[i].rawData())
  132. self.transactionArray.append(transaction)
  133. } catch {
  134. self.transactionListObtained.value = false
  135. }
  136. }
  137. self.transactionListObtained.value = true
  138. case let .failure(errorJSON):
  139. self.setErrorMessage(message: errorJSON["message"].stringValue)
  140. self.transactionListObtained.value = false
  141. case .updateAccessCode:
  142. RestApiMananger.sharedInstance.updateAccessCode(userId: self.getUserId(), password: self.getLoginPassword()) {
  143. result in
  144. if result != "Error"{
  145. UserDefaults.standard.set((result + ":" + RestApiMananger.sharedInstance.getUUID()).toBase64(), forKey: "com.gmeremit.accessCode")
  146. self.fetchTransactionList(recipientId: recipient_id, recipientName: recipient_name, transactionType: transaction_type, startDate: nil, endDate: nil, page: page!, size: size!)
  147. }
  148. }
  149. case .logOutUser():
  150. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  151. self.anotherLogin.value = true
  152. case .timeOut:
  153. self.transactionListConnectionTimeOut.value = false
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. - returns: the count of array
  160. */
  161. func getCount() -> Int {
  162. return transactionArray.count
  163. }
  164. /**
  165. - returns: the count of array
  166. */
  167. func getTotalCount() -> Int {
  168. return walletArray.count
  169. }
  170. /**
  171. - parameter index: position of transaction in an array
  172. - returns: transaction
  173. */
  174. func getTransaction(index: Int) -> Transaction {
  175. return transactionArray[index]
  176. }
  177. /**
  178. get transaction in an array
  179. - returns: transaction array
  180. */
  181. func getTransactions() -> [Transaction]{
  182. return transactionArray
  183. }
  184. /**
  185. - parameter index: position of reciver name in an array
  186. - returns: reciver name
  187. */
  188. func getRecipient(index: Int) -> String {
  189. if let name = transactionArray[index].receiverName{
  190. return name
  191. }
  192. else {
  193. return "N/A"
  194. }
  195. }
  196. /**
  197. - parameter index: position of recivername in wallet array
  198. - returns: reciver name
  199. */
  200. func getRecipientForWallet(index: Int) -> String {
  201. if let name = walletArray[index].receiverName{
  202. return name
  203. }
  204. else {
  205. return "N/A"
  206. }
  207. }
  208. /**
  209. - parameter index: position of sender name in wallet array
  210. - returns: sendar name
  211. */
  212. func getSenderForWallet(index: Int) -> String {
  213. if let name = walletArray[index].senderName{
  214. return name
  215. }
  216. else {
  217. return "N/A"
  218. }
  219. }
  220. /**
  221. - parameter index: position of transaction date in an array
  222. - returns: date
  223. */
  224. func getDate(index: Int) -> String {
  225. if let date = transactionArray[index].date {
  226. return date
  227. }
  228. else {
  229. return "N/A"
  230. }
  231. }
  232. /**
  233. - parameter index: position of transaction date in wallet array
  234. - returns: date
  235. */
  236. func getDateForWallet(index: Int) -> String {
  237. if let date = walletArray[index].date {
  238. return date
  239. }
  240. else {
  241. return "N/A"
  242. }
  243. }
  244. /**
  245. - parameter index: position of transaction payoutMethod in an array
  246. - returns: paymentMode
  247. */
  248. func getPaymentMethod(index: Int) -> String {
  249. if let paymentMode = transactionArray[index].payoutMethod {
  250. return paymentMode
  251. }
  252. else {
  253. return "N/A"
  254. }
  255. }
  256. /**
  257. - parameter index: position of transaction type in an array
  258. - returns: transactionType
  259. */
  260. func getTransactionType(index: Int) -> String {
  261. if let transactionType = transactionArray[index].transactionType {
  262. return transactionType
  263. }
  264. else {
  265. return "N/A"
  266. }
  267. }
  268. /**
  269. - parameter index: position of transaction payout method in an array
  270. - returns: payout method
  271. */
  272. func getPaymentMethodUpdated(index: Int) -> String {
  273. if let paymentMethod = transactionArray[index].payoutMethod {
  274. return paymentMethod
  275. }
  276. else {
  277. return "N/A"
  278. }
  279. }
  280. /**
  281. - parameter index: position of payment method in wallet array
  282. - returns: paymentMethod
  283. */
  284. func getPaymentMethodUpdatedForWallet(index: Int) -> String {
  285. if let paymentMethod = walletArray[index].payoutMethod {
  286. return paymentMethod
  287. }
  288. else {
  289. return "N/A"
  290. }
  291. }
  292. /**
  293. - parameter index: position of transaction amount in an array
  294. - returns: amount
  295. */
  296. func getAmount(index: Int) -> String {
  297. if let amount = transactionArray[index].payoutAmountOriginal {
  298. return amount
  299. }
  300. else {
  301. return "N/A"
  302. }
  303. }
  304. /**
  305. - parameter index: position of amount for wallet in an array
  306. - returns: amount
  307. */
  308. func getAmountForWallet(index: Int) -> String {
  309. if let amount = walletArray[index].payoutAmountOriginal {
  310. return amount
  311. }
  312. else {
  313. return "N/A"
  314. }
  315. }
  316. /**
  317. - parameter index: position of transferType in an array
  318. - returns: transferType
  319. */
  320. func getTransferType(index: Int) -> String {
  321. if let transferType = transactionArray[index].transferType {
  322. return transferType
  323. } else {
  324. return "N/A"
  325. }
  326. }
  327. /**
  328. - parameter index: position of transactionId in an array
  329. - returns: transactionId
  330. */
  331. func getTransactionId(index: Int) -> String {
  332. if let getTransactionId = transactionArray[index].transactionId {
  333. return getTransactionId
  334. } else {
  335. return "N/A"
  336. }
  337. }
  338. /**
  339. - parameter index: position of walletId in an array
  340. - returns: wallet id
  341. */
  342. func getTransactionIdForWallet(index: Int) -> String {
  343. if let getTransactionId = walletArray[index].transactionId {
  344. return getTransactionId
  345. } else {
  346. return "N/A"
  347. }
  348. }
  349. /**
  350. - parameter index: position of countrol number in an array
  351. - returns: gme countrol number
  352. */
  353. func getGMEControlNo(index: Int) -> String {
  354. if let getGMEControlNo = transactionArray[index].gmeControlNumber {
  355. return getGMEControlNo
  356. } else {
  357. return "N/A"
  358. }
  359. }
  360. /**
  361. - parameter index: position of relation in an array
  362. - returns: relation
  363. */
  364. func getRelation(index: Int) -> String {
  365. if let getRelation = transactionArray[index].relation{
  366. return getRelation
  367. } else {
  368. return "N/A"
  369. }
  370. }
  371. /**
  372. - parameter index: position of mobile nos. in an array
  373. - returns: mobile nos.
  374. */
  375. func getMobileNo(index: Int) -> String {
  376. if let getMobileNo = transactionArray[index].receiverPhone {
  377. return getMobileNo
  378. } else {
  379. return "N/A"
  380. }
  381. }
  382. /**
  383. - parameter index: position of email in an array
  384. - returns: email
  385. */
  386. func getEmail(index: Int) -> String {
  387. if let getEmail = transactionArray[index].receiverEmail {
  388. return getEmail
  389. } else {
  390. return "-"
  391. }
  392. }
  393. func getPayoutAgent(index: Int) -> String {
  394. if let getBankName = transactionArray[index].bankName {
  395. return getBankName
  396. } else {
  397. return "[Any Where]"
  398. }
  399. }
  400. /**
  401. - parameter index: position of address in an array
  402. - returns: address
  403. */
  404. func getAddess(index: Int) -> String {
  405. if let getAddess = transactionArray[index].receiverAddress {
  406. return getAddess
  407. } else {
  408. return "N/A"
  409. }
  410. }
  411. /**
  412. - parameter index: position of bank branch in an array
  413. - returns: bank branch
  414. */
  415. func getBankBranch(index: Int) -> String {
  416. if let getBankBranch = transactionArray[index].bankBranch{
  417. return getBankBranch
  418. } else {
  419. return "-"
  420. }
  421. }
  422. /**
  423. - parameter index: position of account in an array
  424. - returns: account number
  425. */
  426. func getAccountNo(index: Int) -> String {
  427. if let getAccountNo = transactionArray[index].accountNumber{
  428. return getAccountNo
  429. } else {
  430. return "-"
  431. }
  432. }
  433. /**
  434. - parameter index: position of payoutmode in an array
  435. - returns: payoutMethod
  436. */
  437. func getPayoutMode(index: Int) -> String {
  438. if let getPayoutMethod = transactionArray[index].payoutMethod{
  439. return getPayoutMethod
  440. } else {
  441. return "-"
  442. }
  443. }
  444. /**
  445. - parameter index: position of borrow status in an array
  446. - returns: borrow status
  447. */
  448. func getBorrowStatus(index: Int) -> String {
  449. if let status = transactionArray[index].txnStatus {
  450. return status
  451. }
  452. return "N/A"
  453. }
  454. /**
  455. - parameter index: position of borrow status in an array
  456. - returns: borrow status
  457. */
  458. func getBorrowStatusForWallet(index: Int) -> String {
  459. if let status = walletArray[index].txnStatus {
  460. return status
  461. }
  462. return "N/A"
  463. }
  464. /* USE THIS ONLY FOR TRACK YOUR TRANSFER AND WALLET BECAUSE START AND END DATES ARE OPPOSITE FROM TRANSACTION STATEMENT, HERE END DATE NEEDS TO BE SET TO THE PRESENT DATE AND START DATE TO ~5 YEARS PAST FROM NOW
  465. - parameter recipientId: recipient Id
  466. - parameter recipientName: recipient name
  467. - parameter transactionType: type of transaction
  468. - parameter startDate: Start date for search
  469. - parameter endDate: end date for search
  470. - parameter txPage: page number
  471. - parameter txSize: number of transactions
  472. */
  473. func fetchTransactionListForTrackAndWallet(recipientId: String, recipientName: String, transactionType: String, startDate: String?, endDate: String?, txPage: String?, txSize: String?) {
  474. if !Reachability.isConnectedToNetwork() {
  475. self.internetConnection.value = false
  476. } else {
  477. let recipient_id = recipientId
  478. let recipient_name = recipientName
  479. let transaction_type = transactionType
  480. var start_date = startDate
  481. var end_date = endDate
  482. let date = Date()
  483. let formatter = DateFormatter()
  484. formatter.dateFormat = "yyyy/MM/dd"
  485. if end_date == nil {
  486. end_date = formatter.string(from: date)
  487. }
  488. let yearsToAdd = -5
  489. if start_date == nil {
  490. start_date = formatter.string(from: Calendar.current.date(byAdding: .year, value: yearsToAdd, to: date)!)
  491. }
  492. var page = txPage
  493. var size = txSize
  494. if page == nil {
  495. page = ""
  496. }
  497. if size == nil {
  498. size = ""
  499. }
  500. RestApiMananger.sharedInstance.getTransactionList(userId: getUserId(), recipientId: recipientId, recipientName: "", transactionType: transactionType, startDate: start_date!, endDate: end_date!, txPage: page!, txSize: size!) { result in
  501. switch result {
  502. case let .success(fetchedJSON):
  503. self.transactionArray.removeAll()
  504. guard fetchedJSON.count > 0 else {
  505. self.transactionListObtained.value = true
  506. return
  507. }
  508. for i in 0 ... (fetchedJSON.count-1) {
  509. do {
  510. let transaction = try JSONDecoder().decode(Transaction.self, from: fetchedJSON[i].rawData())
  511. self.transactionArray.append(transaction)
  512. self.walletArray.append(transaction)
  513. } catch {
  514. self.transactionListObtained.value = false
  515. }
  516. }
  517. self.transactionListObtained.value = true
  518. case let .failure(errorJSON):
  519. self.setErrorMessage(message: errorJSON["message"].stringValue)
  520. self.transactionListObtained.value = false
  521. case .updateAccessCode:
  522. RestApiMananger.sharedInstance.updateAccessCode(userId: self.getUserId(), password: self.getLoginPassword()) {
  523. result in
  524. if result != "Error"{
  525. UserDefaults.standard.set((result + ":" + RestApiMananger.sharedInstance.getUUID()).toBase64(), forKey: "com.gmeremit.accessCode")
  526. self.fetchTransactionListForTrackAndWallet(recipientId: recipient_id, recipientName: recipient_name, transactionType: transaction_type, startDate: start_date, endDate: end_date, txPage: txPage, txSize: txSize)
  527. }
  528. }
  529. case .logOutUser():
  530. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  531. self.anotherLogin.value = true
  532. case .timeOut:
  533. self.transactionListConnectionTimeOut.value = false
  534. }
  535. }
  536. }
  537. }
  538. }