From b9493a99245d63101be1fb7f5dd1dd55d97c4720 Mon Sep 17 00:00:00 2001 From: p29hieu <55887941+p29hieu@users.noreply.github.com> Date: Sun, 10 Dec 2023 11:00:41 +0000 Subject: [PATCH] Update API get account sales 2 --- example/console/getSalesList.ts | 55 ++++++++++++++++++++------------- package.json | 2 +- src/server.ts | 3 +- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/example/console/getSalesList.ts b/example/console/getSalesList.ts index 9585076..b90772a 100644 --- a/example/console/getSalesList.ts +++ b/example/console/getSalesList.ts @@ -1,25 +1,38 @@ import { FincodeService } from "../../src/server"; +import { FincodeNs } from "../../src/types"; -export const getSaleList = async () => { - const service = FincodeService.createInstance(); - console.log(service.configData); - await service.getSaleList( - {}, - { - onSuccess: console.log, - onError: console.error, - }, - ); - await service.getSaleItem("sales_s_22082100904_231130_00001", { - onSuccess: console.log, - onError: console.error, - }); - await service.getSaleDetail( - "sales_s_22082100904_231130_00001", - { trade_type: [1, 2] }, - { - onSuccess: console.log, - onError: console.error, - }, +const getFincodeSalesList = async (nextPage = 1): Promise => { + const fincode = FincodeService.createInstance(); + const { current_page, last_page, list } = await fincode.getSaleList({ limit: 100, page: nextPage }, { onError: console.error }); + let result: FincodeNs.Sale.SaleItem[] = list; + for (let page = current_page; page < last_page; page++) { + const res = await fincode.getSaleList({ limit: 100, page }, { onError: console.error }); + result = [...res.list, ...result]; + } + return result; +}; + +const getSaleDetailItems = async (saleId: string, nextPage = 1): Promise => { + const fincode = FincodeService.createInstance(); + const { current_page, last_page, list } = await fincode.getSaleDetail( + saleId, + { limit: 100, page: nextPage, trade_type: [1, 2, 3, 4, 5] }, + { onError: console.error }, ); + let result: FincodeNs.Sale.SaleDetailItem[] = list; + for (let page = current_page; page < last_page; page++) { + const res = await fincode.getSaleDetail(saleId, { limit: 100, page, trade_type: [1, 2, 3, 4, 5] }, { onError: console.error }); + result = [...res.list, ...result]; + } + return result; +}; + +export const getSaleList = async () => { + const saleList = await getFincodeSalesList(); + let saleDetailList: FincodeNs.Sale.SaleDetailItem[] = []; + for (const saleItem of saleList) { + const saleDetail = await getSaleDetailItems(saleItem.id); + saleDetailList = [...saleDetailList, ...saleDetail]; + } + console.log(saleDetailList); }; diff --git a/package.json b/package.json index 5c838ac..c36e01e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fincode", - "version": "1.0.1", + "version": "1.0.2", "description": "Type scripts for Fincode", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/server.ts b/src/server.ts index c669dee..64963e5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -544,9 +544,8 @@ export class FincodeService { } return data; } catch (error) { - console.error(error.response); if (options?.onError) { - await options.onError(error.response.data); + await options.onError(error.response?.data); } } }