diff --git a/package.json b/package.json index 9eec921..a812603 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fugle/trade", - "version": "2.0.1", + "version": "2.1.0", "description": "FugleTrade API SDK for Node.js", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -34,7 +34,8 @@ }, "homepage": "https://github.com/fugle-dev/fugle-trade-node#readme", "dependencies": { - "@fugle/trade-core": "^2.0.0", + "@fugle/trade-core": "^2.1.0", + "@fugle/trade-core-darwin-arm64": "^2.1.0", "ini": "^2.0.0", "inquirer": "^8.2.1", "keytar": "^7.9.0", diff --git a/src/client.ts b/src/client.ts index 2b90ce5..e7956cc 100644 --- a/src/client.ts +++ b/src/client.ts @@ -6,7 +6,7 @@ import { PriceFlag, Market } from './enums'; import { loadCredentials, removeCredentials } from './utils'; import { ClientConfig } from './interfaces/client-config.interface'; import { ParsedCertInfo, CertInfo } from './interfaces/parsed-cert-info-interface'; -import { ParsedInventories, Stock } from './interfaces/parsed-inventories.interface'; +import { ParsedInventories, renameInventoriesMemoToUserDef, Stock } from './interfaces/parsed-inventories.interface'; import { ParsedKeyInfo, KeyInfo } from './interfaces/parsed-key-info-interface'; import { ParsedMachineTime } from './interfaces/parsed-machine-time.interface'; import { ParsedOrderResult } from './interfaces/parsed-order-result.interface'; @@ -14,7 +14,7 @@ import { ParsedSettlements, Settlement } from './interfaces/parsed-settlements.i import { ParsedBalanceStatus, BalanceStatus } from './interfaces/parsed-balance.interface'; import { ParsedTradeStatus, TradeStatus } from './interfaces/parsed-trade-status.interface'; import { ParsedMarketStatus, MarketStatus } from './interfaces/parsed-market-status.interface'; -import { ParsedTransactions, Trade } from './interfaces/parsed-transactions.interface'; +import { ParsedTransactions, renameTransactionsMemoToUserDef, Trade } from './interfaces/parsed-transactions.interface'; import { ParsedPlaceOrderResponse, PlaceOrderResponse } from './interfaces/parsed-place-order-response.interface'; import { ParsedReplaceOrderResponse, ReplaceOrderResponse } from './interfaces/parsed-replace-order-response.interface'; import { ParsedOrderResultHistory } from './interfaces/parsed-order-result-history.interface'; @@ -68,7 +68,7 @@ export class Client { // Must login first async placeOrder(order: Order): Promise { - const response = this.sdk.order(order.toObject()); + const response = this.sdk.order(order.toObject(), order.payload.userDef || ''); const parsed = JSON.parse(response) as ParsedPlaceOrderResponse; return parsed.data; } @@ -119,7 +119,7 @@ export class Client { async getHistoricalOrders(options: { startDate: string, endDate: string }): Promise { const market: Market = Market.All; const { startDate, endDate } = options; - const response = this.sdk.getOrderResultHistory(market, startDate, endDate); + const response = this.sdk.getOrderResultHistory(startDate, endDate, market); const parsed = JSON.parse(response) as ParsedOrderResultHistory; return parsed.data.orderResultHistory.map(order => new PlacedOrder({ ...order })); } @@ -136,14 +136,15 @@ export class Client { const response = duration ? this.sdk.getTransactions(duration) : this.sdk.getTransactionsByDate(startDate as string, endDate as string); - const parsed = JSON.parse(response) as ParsedTransactions; + // const parsed = JSON.parse(response) as ParsedTransactions; + const parsed = renameTransactionsMemoToUserDef(JSON.parse(response) as ParsedTransactions); return parsed.data.matSums; } // Must login first async getInventories(): Promise { const response = this.sdk.getInventories(); - const parsed = JSON.parse(response) as ParsedInventories; + const parsed = renameInventoriesMemoToUserDef(JSON.parse(response) as ParsedInventories); return parsed.data.stkSums; } diff --git a/src/interfaces/order-payload.interface.ts b/src/interfaces/order-payload.interface.ts index 6d7ec82..fadfe8a 100644 --- a/src/interfaces/order-payload.interface.ts +++ b/src/interfaces/order-payload.interface.ts @@ -9,4 +9,5 @@ export interface OrderPayload { priceFlag?: PriceFlag; bsFlag?: BsFlag; trade?: TradeType; + userDef?: string; } diff --git a/src/interfaces/parsed-inventories.interface.ts b/src/interfaces/parsed-inventories.interface.ts index ae67b1d..d510fb9 100644 --- a/src/interfaces/parsed-inventories.interface.ts +++ b/src/interfaces/parsed-inventories.interface.ts @@ -20,6 +20,7 @@ export interface StockDat { tTime: string; valueMkt: string; valueNow: string; + userDef: string; } export interface Stock { @@ -54,3 +55,24 @@ export interface ParsedInventories { stkSums: Stock[]; } } + +//helper function to map memo prop to userDef, can be remove after sdk update +export function renameInventoriesMemoToUserDef(parsedTransactions: ParsedInventories): ParsedInventories { + // Helper function to rename memo to userDef in StockDat objects + const renameInStockDat = (stockDat: StockDat & { memo?: string }): StockDat => { + const { memo, ...rest } = stockDat; + return { ...rest, userDef: memo || "" }; + }; + + // Helper function to rename memo to userDef in Stock objects + const renameInStkDat = (trade: Stock): Stock => { + const renamedStkDats = trade.stkDats.map(renameInStockDat); + return { ...trade, stkDats: renamedStkDats }; + }; + + // Map through the stkSums array and rename memo to userDef + const renamedStkSums = parsedTransactions.data.stkSums.map(renameInStkDat); + + // Return the updated ParsedInventories object + return { data: { stkSums: renamedStkSums } }; +} diff --git a/src/interfaces/parsed-order-result-history.interface.ts b/src/interfaces/parsed-order-result-history.interface.ts index b4b0bdf..62f63de 100644 --- a/src/interfaces/parsed-order-result-history.interface.ts +++ b/src/interfaces/parsed-order-result-history.interface.ts @@ -21,6 +21,7 @@ export interface OrderResultHistory { bsFlag: string; source?: string; market: string; + userDef: string; } export interface ParsedOrderResultHistory { @@ -28,3 +29,18 @@ export interface ParsedOrderResultHistory { orderResultHistory: OrderResultHistory[]; }; } + +//helper function to map memo prop to userDef, can be remove after sdk update +export function renameMemoToUserDef(parsedTransactions: ParsedOrderResultHistory): ParsedOrderResultHistory { + // Helper function to rename memo to userDef in OrderResultHistory objects + const renameInOrderResultHistory = (tradeDate: OrderResultHistory & { memo?: string }): OrderResultHistory => { + const { memo, ...rest } = tradeDate; + return { ...rest, userDef: memo || "" }; + }; + + // Map through the orderResultHistory array and rename memo to userDef + const renamedOrderResultHistory = parsedTransactions.data.orderResultHistory.map(renameInOrderResultHistory); + + // Return the updated ParsedOrderResultHistory object + return { data: { orderResultHistory: renamedOrderResultHistory } }; +} diff --git a/src/interfaces/parsed-order-result.interface.ts b/src/interfaces/parsed-order-result.interface.ts index 227ade7..3009aa3 100644 --- a/src/interfaces/parsed-order-result.interface.ts +++ b/src/interfaces/parsed-order-result.interface.ts @@ -22,6 +22,7 @@ export interface OrderResult { errMsg: string; avgPrice: string; bsFlag: string; + memo: string; } export interface ParsedOrderResult { diff --git a/src/interfaces/parsed-transactions.interface.ts b/src/interfaces/parsed-transactions.interface.ts index b4e6233..4fd98cb 100644 --- a/src/interfaces/parsed-transactions.interface.ts +++ b/src/interfaces/parsed-transactions.interface.ts @@ -18,6 +18,7 @@ export interface TradeDate { tax: string; taxG: string; trade: string; + userDef: string; } export interface Trade { @@ -43,3 +44,24 @@ export interface ParsedTransactions { matSums: Trade[]; }; } + +//helper function to map memo prop to userDef, can be remove after sdk update +export function renameTransactionsMemoToUserDef(parsedTransactions: ParsedTransactions): ParsedTransactions { + // Helper function to rename memo to userDef in TradeDate objects + const renameInTradeDate = (tradeDate: TradeDate & { memo?: string }): TradeDate => { + const { memo, ...rest } = tradeDate; + return { ...rest, userDef: memo || "" }; + }; + + // Helper function to rename memo to userDef in Trade objects + const renameInTrade = (trade: Trade): Trade => { + const renamedMatDats = trade.matDats.map(renameInTradeDate); + return { ...trade, matDats: renamedMatDats }; + }; + + // Map through the matSums array and rename memo to userDef + const renamedMatSums = parsedTransactions.data.matSums.map(renameInTrade); + + // Return the updated ParsedTransactions object + return { data: { matSums: renamedMatSums } }; +} diff --git a/src/placed-order.ts b/src/placed-order.ts index 1cb0331..14b9014 100644 --- a/src/placed-order.ts +++ b/src/placed-order.ts @@ -32,7 +32,9 @@ export class PlacedOrder { ...(payload.stockNo && { stockNo: String(payload.stockNo) }), ...(payload.trade && { trade: String(payload.trade) as TradeType }), ...(payload.workDate && { workDate: String(payload.workDate) }), + ...(payload.memo && { userDef: String(payload.memo) }) }; + delete payload.memo; } get payload(): PlacedOrderPayload { diff --git a/test/client.spec.ts b/test/client.spec.ts index d97a241..31854e7 100644 --- a/test/client.spec.ts +++ b/test/client.spec.ts @@ -262,16 +262,28 @@ describe('Client', () => { const response = await client.getTransactions({ duration: '3d' }); const data = readFileSync('./test/fixtures/response-transactions.txt').toString(); const parsed = JSON.parse(data); - expect(response).toEqual(parsed.data.matSums); + const actual = renameMemoToUserDef(parsed); + expect(response).toEqual(actual.data.matSums); }); + function renameMemoToUserDef(data: { data: { matSums: any[]; }; }) { + data.data.matSums.forEach(trade => { + trade.matDats.forEach((tradeDate: { userDef: any; memo: any; }) => { + tradeDate.userDef = tradeDate.memo; + delete tradeDate.memo; + }); + }); + return data; + } + it('should get parsed transactions by startDate and endDate options', async () => { const client = new Client(config); await client.login(); const response = await client.getTransactions({ startDate: '2023-01-01', endDate: '2023-02-28' }); const data = readFileSync('./test/fixtures/response-transactions.txt').toString(); const parsed = JSON.parse(data); - expect(response).toEqual(parsed.data.matSums); + const actual = renameMemoToUserDef(parsed); + expect(response).toEqual(actual.data.matSums); }); }); @@ -282,6 +294,12 @@ describe('Client', () => { const response = await client.getInventories(); const data = readFileSync('./test/fixtures/response-inventories.txt').toString(); const parsed = JSON.parse(data); + parsed.data.stkSums.forEach((stk: { stkDats: any[]; }) => { + stk.stkDats.forEach((dat: { userDef: any; memo: any; }) => { + dat.userDef = dat.memo; + delete dat.memo; + }); + }); expect(response).toEqual(parsed.data.stkSums); }); }); diff --git a/test/fixtures/response-inventories.txt b/test/fixtures/response-inventories.txt index 688592e..268467c 100644 --- a/test/fixtures/response-inventories.txt +++ b/test/fixtures/response-inventories.txt @@ -24,7 +24,8 @@ "valueNow": "3148", "makeA": "725", "makeAPer": "30.21", - "priceEvn": "25.78" + "priceEvn": "25.78", + "memo": "" }, { "tDate": "20200807", @@ -47,7 +48,8 @@ "valueNow": "4387", "makeA": "762", "makeAPer": "21.16", - "priceEvn": "27.67" + "priceEvn": "27.67", + "memo": "" }, { "tDate": "20210113", @@ -70,7 +72,8 @@ "valueNow": "8405", "makeA": "1176", "makeAPer": "16.33", - "priceEvn": "28.80" + "priceEvn": "28.80", + "memo": "" }, { "tDate": "20211022", @@ -93,7 +96,8 @@ "valueNow": "10884", "makeA": "55", "makeAPer": "0.51", - "priceEvn": "33.33" + "priceEvn": "33.33", + "memo": "" }, { "tDate": "20220207", @@ -116,7 +120,8 @@ "valueNow": "3784", "makeA": "161", "makeAPer": "4.47", - "priceEvn": "32.07" + "priceEvn": "32.07", + "memo": "" } ], "stkNo": "0056", diff --git a/test/fixtures/response-order-history-from-sdk.txt b/test/fixtures/response-order-history-from-sdk.txt index 1a8ca83..d875cfc 100644 --- a/test/fixtures/response-order-history-from-sdk.txt +++ b/test/fixtures/response-order-history-from-sdk.txt @@ -20,7 +20,8 @@ "celQtyShare": "0", "celable": "1", "avgPrice": "0", - "bsFlag": "R" + "bsFlag": "R", + "memo": "" } ] } diff --git a/test/fixtures/response-orders-from-sdk.txt b/test/fixtures/response-orders-from-sdk.txt index 928def4..4998075 100644 --- a/test/fixtures/response-orders-from-sdk.txt +++ b/test/fixtures/response-orders-from-sdk.txt @@ -24,7 +24,8 @@ "errCode": "00000000", "errMsg": "", "avgPrice": "0", - "bsFlag": "R" + "bsFlag": "R", + "memo": "" }, { "workDate": "20220222", @@ -50,7 +51,8 @@ "errCode": "00000000", "errMsg": "", "avgPrice": "0.0", - "bsFlag": "R" + "bsFlag": "R", + "memo": "" } ] } diff --git a/test/fixtures/response-transactions.txt b/test/fixtures/response-transactions.txt index 80e647a..106c38a 100644 --- a/test/fixtures/response-transactions.txt +++ b/test/fixtures/response-transactions.txt @@ -22,7 +22,8 @@ "payN": "-30042", "make": "0", "makePer": "0.00", - "cDate": "20220222" + "cDate": "20220222", + "memo": "" } ], "tDate": "20220222", diff --git a/yarn.lock b/yarn.lock index fe78a93..5d7e706 100644 --- a/yarn.lock +++ b/yarn.lock @@ -516,47 +516,47 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" -"@fugle/trade-core-darwin-arm64@2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@fugle/trade-core-darwin-arm64/-/trade-core-darwin-arm64-2.0.0.tgz#05ad2014bd69927a08b7d77e0f99a112ef41ccfb" - integrity sha512-vIt8+ZBtliRPcwen0ntDdFvzmrncLM3l3rivO5Oxvh2NoT+8jd+Rwg+13b/NkvU5vWgthc2OsmVBk1tNuEBFTA== +"@fugle/trade-core-darwin-arm64@2.1.0", "@fugle/trade-core-darwin-arm64@^2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@fugle/trade-core-darwin-arm64/-/trade-core-darwin-arm64-2.1.0.tgz#f36479b5d82de7682b9498f7d1dd10beee299cf6" + integrity sha512-/9NLSmXCKy/tYt5jpPwLK5Gb+Tnqm2hX6GBOxKR0kQcrozpE1L0AAUk7gRvDNdZlbgPshcKEdxCtWrkSzTHRvA== -"@fugle/trade-core-darwin-x64@2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@fugle/trade-core-darwin-x64/-/trade-core-darwin-x64-2.0.0.tgz#f198e1f8e17120686342d2019a87c9814256a878" - integrity sha512-v2eSnLfWXsR/P92wkpneDT/syGM1g3w1faGwZVnQDCgVQwHtMnt+94Dv0Nh64l0+Zv8YBgdxs6cR38vI2d+ivQ== +"@fugle/trade-core-darwin-x64@2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@fugle/trade-core-darwin-x64/-/trade-core-darwin-x64-2.1.0.tgz#bb63bc22db2bfe67e6dd80b3a8bf0dbb9d18986f" + integrity sha512-tGXfo1BZ3sVQKmYBNbDn3JyYiq4VPUqVvxlwLfY7BaDccNVTOS46tThod6umf7S0xBZcn0s40024UiqVMLp7mw== -"@fugle/trade-core-linux-arm64-gnu@2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@fugle/trade-core-linux-arm64-gnu/-/trade-core-linux-arm64-gnu-2.0.0.tgz#7dd0110599b94e2d53a0002c557298850cc63233" - integrity sha512-VLfn+2S+0Wd4yB4CIREkdVf/mQvMhS2RJDm1q1GzV3B3X/QmSEHsgKbWvqgp8OhqxSn6g0AfB2QYwrzu7FVjCQ== +"@fugle/trade-core-linux-arm64-gnu@2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@fugle/trade-core-linux-arm64-gnu/-/trade-core-linux-arm64-gnu-2.1.0.tgz#bb63b01bee485b83e00b6ca848a6953be798f3b7" + integrity sha512-V9UbsMjMYPhLWYJPljv3gcwPXPiEzeIzn0XQHl5tnyDK4In2v3kXLZDCpIDd0afUZalg2aF00A/g56FmVXvmGg== -"@fugle/trade-core-linux-x64-gnu@2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@fugle/trade-core-linux-x64-gnu/-/trade-core-linux-x64-gnu-2.0.0.tgz#ab3df665c09ff3211be6b32e59e9f48cacc86f2c" - integrity sha512-DN6JPxJwaiNSUO4hQZ03lkPWUWsFispzprleYX1OPSKUSXXQUszG96itP6C5VVGl67fRw7H4tNloxGtwEoMxBg== +"@fugle/trade-core-linux-x64-gnu@2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@fugle/trade-core-linux-x64-gnu/-/trade-core-linux-x64-gnu-2.1.0.tgz#a5adc39dc7fd5295f65f4bcda84cfde6b3918324" + integrity sha512-4L5t0TIGc4aZYOWvn/PZmUfjKH6CUftsPtW8EKgR0glLxbXfKCWp5QT2kUBknsE1Lo+r37YEeS3ou6uMmKijCA== -"@fugle/trade-core-linux-x64-musl@2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@fugle/trade-core-linux-x64-musl/-/trade-core-linux-x64-musl-2.0.0.tgz#c3d58d8ad8afcc558059f1f77b288639c8e4f243" - integrity sha512-JUAZFGCfkn0mAfbgEuLyqJYBrMTcDVSpBMRGmQ7c82vuBI2dxfXEtm7mptSHXz0L7QL7RPi5QoQUsYaCfi9gjA== +"@fugle/trade-core-linux-x64-musl@2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@fugle/trade-core-linux-x64-musl/-/trade-core-linux-x64-musl-2.1.0.tgz#4a3e3e85960fc2807b4be7db297c6febcd1212f9" + integrity sha512-o9ILGuP4UOnUq4yOCMeA/s3QspOpNI7Qh6PKcafFuDKg6rSTUWaJnyt716Foe1DCIjp2dB1RrSzG6zZsHare9Q== -"@fugle/trade-core-win32-x64-msvc@2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@fugle/trade-core-win32-x64-msvc/-/trade-core-win32-x64-msvc-2.0.0.tgz#68aa024d54c51fcd955cc922ad324aafd94dc3c5" - integrity sha512-l+pPvksB5tKG1ca68W1GEZHrAM8mU8oJne1p76O72xo6VBpWpKO5CvRC58k8QNTorwUR/etpUzOveGjml2PAFg== +"@fugle/trade-core-win32-x64-msvc@2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@fugle/trade-core-win32-x64-msvc/-/trade-core-win32-x64-msvc-2.1.0.tgz#2d50a97558861094a926c0c639b17ae0d6e0a7bc" + integrity sha512-yGVIGOJ/TsOW3KOKhhqtNZurDhyKiViiQZ2uVTG24oXERIIFd+6X/yT2YI4elinphum1HY0wybzGz7phSO5cQQ== -"@fugle/trade-core@^2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@fugle/trade-core/-/trade-core-2.0.0.tgz#8cde7e593365d1877710b4e383c8140a449a81a0" - integrity sha512-AzpYdPREOXKAo3mWdH3wfh9xLiC4EJY8AfilckDAfR0I1nK1e2efQazAJnRdBq5/yop1HuvGhZAmScZ+lwbJ+w== +"@fugle/trade-core@^2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@fugle/trade-core/-/trade-core-2.1.0.tgz#b22ee535ef0a204ffb71835ed2716316153ac193" + integrity sha512-DnhwfQ+OxHlF3KsrcvlYz/LYH6sIA6qV73Fo0gZ6mPUoXaMxCGfy7XZnI3fMLeT1/OZMNXgsKdDjcpc2n4nGSg== optionalDependencies: - "@fugle/trade-core-darwin-arm64" "2.0.0" - "@fugle/trade-core-darwin-x64" "2.0.0" - "@fugle/trade-core-linux-arm64-gnu" "2.0.0" - "@fugle/trade-core-linux-x64-gnu" "2.0.0" - "@fugle/trade-core-linux-x64-musl" "2.0.0" - "@fugle/trade-core-win32-x64-msvc" "2.0.0" + "@fugle/trade-core-darwin-arm64" "2.1.0" + "@fugle/trade-core-darwin-x64" "2.1.0" + "@fugle/trade-core-linux-arm64-gnu" "2.1.0" + "@fugle/trade-core-linux-x64-gnu" "2.1.0" + "@fugle/trade-core-linux-x64-musl" "2.1.0" + "@fugle/trade-core-win32-x64-msvc" "2.1.0" "@humanwhocodes/config-array@^0.5.0": version "0.5.0"