Skip to content

Commit ca96a80

Browse files
committed
getHistory 가 구매기록 조회시에 puppeteer 를 사용하지 않도록 변경
1 parent 7c7fef1 commit ca96a80

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

src/app/naver/service.ts

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PaymentHistory } from "app/common";
2+
import { CommonResponse } from "app/common/types/response";
13
import {
24
concat,
35
defer,
@@ -10,7 +12,7 @@ import {
1012
import { Module } from ".";
1113

1214
export default class Service {
13-
cookies: string;
15+
cookies?: string;
1416
constructor(private readonly module: Module) {
1517
this.module = module;
1618
}
@@ -40,17 +42,55 @@ export default class Service {
4042
return result$;
4143
}
4244

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+
};
4964

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
5467
);
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;
5595
}
5696
}

0 commit comments

Comments
 (0)