1
+ import { PaymentHistory } from "app/common" ;
2
+ import { CommonResponse } from "app/common/types/response" ;
1
3
import {
2
4
concat ,
3
5
defer ,
@@ -10,7 +12,7 @@ import {
10
12
import { Module } from "." ;
11
13
12
14
export default class Service {
13
- cookies : string ;
15
+ cookies ? : string ;
14
16
constructor ( private readonly module : Module ) {
15
17
this . module = module ;
16
18
}
@@ -40,17 +42,55 @@ export default class Service {
40
42
return result$ ;
41
43
}
42
44
43
- async getHistory ( ) {
44
- await this . module . urlChanger . moveToPaymentHistoryURL ( ) ;
45
- await this . module . pageInteractor . loadPaymentHistoryUntilPageEnds ( ) ;
46
-
47
- const paymentElements =
48
- await this . module . elementParser . parsePaymentElements ( ) ;
45
+ private async isResponseValid ( response : CommonResponse ) {
46
+ return response . status === 200 ;
47
+ }
48
+ private async getHistoryResult ( response : CommonResponse ) {
49
+ if ( ! this . isResponseValid ( response ) ) {
50
+ throw new Error ( `Invalid response: ${ response . status } ${ response . data } ` ) ;
51
+ }
52
+ const historyItems = this . module . parser . parsePaymentHistory ( response . data ) ;
53
+ const infoForNextPaymentHistory =
54
+ this . module . parser . parseInformationForNextPaymentHistory ( response . data ) ;
55
+ return { historyItems, infoForNextPaymentHistory } ;
56
+ }
57
+ private async getAllPaymentHistories ( cookies : string ) {
58
+ let historyItems : PaymentHistory [ ] ;
59
+ let infoForNextPaymentHistory : {
60
+ hasNext : boolean ;
61
+ lastHistoryId : string ;
62
+ lastHistoryDateTimestamp : number ;
63
+ } ;
49
64
50
- return await Promise . all (
51
- paymentElements . map ( ( element ) =>
52
- this . module . elementParser . parseElement ( element )
53
- )
65
+ const firstResponse = await this . module . scraper . searchPaymentHistory (
66
+ cookies
54
67
) ;
68
+ let firstResult = await this . getHistoryResult ( firstResponse ) ;
69
+ historyItems = firstResult . historyItems ;
70
+ infoForNextPaymentHistory = firstResult . infoForNextPaymentHistory ;
71
+ // get the very first payment history items
72
+
73
+ while ( infoForNextPaymentHistory . hasNext ) {
74
+ const response = await this . module . scraper . nextPaymentHistory (
75
+ cookies ,
76
+ infoForNextPaymentHistory . lastHistoryId ,
77
+ infoForNextPaymentHistory . lastHistoryDateTimestamp
78
+ ) ;
79
+ const result = await this . getHistoryResult ( response ) ;
80
+ historyItems = historyItems . concat ( result . historyItems ) ;
81
+ infoForNextPaymentHistory = result . infoForNextPaymentHistory ;
82
+ }
83
+ // get payment history continuously until hasNext is false, append it to historyItems
84
+
85
+ return historyItems ;
86
+ }
87
+
88
+ async getHistory ( ) {
89
+ if ( ! this . cookies ) {
90
+ throw new Error ( "Not logged in" ) ;
91
+ }
92
+
93
+ const paymentHistories = await this . getAllPaymentHistories ( this . cookies ) ;
94
+ return paymentHistories ;
55
95
}
56
96
}
0 commit comments