Skip to content

Commit

Permalink
refact: adapt to changes in bkper-js
Browse files Browse the repository at this point in the history
  • Loading branch information
bbcoelho committed Nov 1, 2024
1 parent 92a399b commit f2fe716
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 33 deletions.
6 changes: 3 additions & 3 deletions gcf/src/EventHandlerAccountCreatedOrUpdated.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Account, AccountType, Book } from "bkper-js";
import { Account, AccountType, Book, Group } from "bkper-js";
import { STOCK_EXC_CODE_PROP } from "./constants.js";
import { EventHandlerAccount } from "./EventHandlerAccount.js";

export class EventHandlerAccountCreatedOrUpdated extends EventHandlerAccount {

public async connectedAccountNotFound(baseBook: Book, connectedBook: Book, baseAccount: bkper.Account): Promise<string> {
let connectedAccount = connectedBook.newAccount();
let connectedAccount = new Account(connectedBook);
await this.syncAccounts(baseBook, connectedBook, baseAccount, connectedAccount);
await connectedAccount.create();
let bookAnchor = super.buildBookAnchor(connectedBook);
Expand All @@ -31,7 +31,7 @@ export class EventHandlerAccountCreatedOrUpdated extends EventHandlerAccount {
let connectedGroup = await stockBook.getGroup(baseGroup.getName());
let stockExcCode = baseGroup.getProperty(STOCK_EXC_CODE_PROP);
if (connectedGroup == null && stockExcCode != null && stockExcCode.trim() != '') {
connectedGroup = await stockBook.newGroup()
connectedGroup = await new Group(stockBook)
.setHidden(baseGroup.isHidden())
.setName(baseGroup.getName())
.setProperties(baseGroup.getProperties())
Expand Down
10 changes: 5 additions & 5 deletions gcf/src/EventHandlerGroupCreatedOrUpdated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { EventHandlerGroup } from "./EventHandlerGroup.js";

export class EventHandlerGroupCreatedOrUpdated extends EventHandlerGroup {
protected async connectedGroupNotFound(financialBook: Book, stockBook: Book, financialGroup: bkper.Group): Promise<string> {
let connectedGroup = await stockBook.newGroup()
.setName(financialGroup.name)
.setHidden(financialGroup.hidden)
.setProperties(financialGroup.properties)
.create();
let connectedGroup = await new Group(stockBook)
.setName(financialGroup.name)
.setHidden(financialGroup.hidden)
.setProperties(financialGroup.properties)
.create();
let bookAnchor = super.buildBookAnchor(stockBook);
return `${bookAnchor}: GROUP ${connectedGroup.getName()} CREATED`;
}
Expand Down
5 changes: 2 additions & 3 deletions gcf/src/EventHandlerTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ export abstract class EventHandlerTransaction extends EventHandler {
return null;
}

let iterator = stockBook.getTransactions(this.getTransactionQuery(financialTransaction));
let stockTransaction = (await stockBook.listTransactions(this.getTransactionQuery(financialTransaction))).getFirst();

let stockExcCode = this.getStockExcCodeFromTransaction(financialBook, financialTransaction);

if (!this.matchStockExchange(stockExcCode, excCode)) {
return null;
}

if (await iterator.hasNext()) {
let stockTransaction = await iterator.next();
if (stockTransaction) {
return await this.connectedTransactionFound(financialBook, stockBook, financialTransaction, stockTransaction, stockExcCode);
} else {
return await this.connectedTransactionNotFound(financialBook, stockBook, financialTransaction, stockExcCode)
Expand Down
14 changes: 7 additions & 7 deletions gcf/src/EventHandlerTransactionChecked.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Account, AccountType, Amount, Book, Transaction } from "bkper-js";
import { Account, AccountType, Amount, Book, Group, Transaction } from "bkper-js";
import { Result } from "./index.js";
import { getRealizedDateValue, getStockExchangeCode } from "./BotService.js";
import * as constants from "./constants.js";
Expand Down Expand Up @@ -49,10 +49,10 @@ export class EventHandlerTransactionChecked extends EventHandlerTransaction {
// Selling
let stockSellAccount = await stockBook.getAccount(constants.STOCK_SELL_ACCOUNT_NAME);
if (stockSellAccount == null) {
stockSellAccount = await stockBook.newAccount().setName(constants.STOCK_SELL_ACCOUNT_NAME).setType(AccountType.OUTGOING).create();
stockSellAccount = await new Account(stockBook).setName(constants.STOCK_SELL_ACCOUNT_NAME).setType(AccountType.OUTGOING).create();
}

let newTransaction = await stockBook.newTransaction()
let newTransaction = await new Transaction(stockBook)
.setDate(financialTransaction.date)
.setAmount(quantity)
.setCreditAccount(stockAccount)
Expand Down Expand Up @@ -80,10 +80,10 @@ export class EventHandlerTransactionChecked extends EventHandlerTransaction {
// Buying
let stockBuyAccount = await stockBook.getAccount(constants.STOCK_BUY_ACCOUNT_NAME);
if (stockBuyAccount == null) {
stockBuyAccount = await stockBook.newAccount().setName(constants.STOCK_BUY_ACCOUNT_NAME).setType(AccountType.INCOMING).create();
stockBuyAccount = await new Account(stockBook).setName(constants.STOCK_BUY_ACCOUNT_NAME).setType(AccountType.INCOMING).create();
}

let newTransaction = await stockBook.newTransaction()
let newTransaction = await new Transaction(stockBook)
.setDate(financialTransaction.date)
.setAmount(quantity)
.setCreditAccount(stockBuyAccount)
Expand Down Expand Up @@ -121,7 +121,7 @@ export class EventHandlerTransactionChecked extends EventHandlerTransaction {
if (stockExchangeCode != null) {
let stockAccount = await stockBook.getAccount(financialAccount.name);
if (stockAccount == null) {
stockAccount = stockBook.newAccount()
stockAccount = new Account(stockBook)
.setName(financialAccount.name)
.setType(financialAccount.type as AccountType)
.setProperties(financialAccount.properties)
Expand All @@ -132,7 +132,7 @@ export class EventHandlerTransactionChecked extends EventHandlerTransaction {
let stockGroup = await stockBook.getGroup(financialGroup.name);
let stockExcCode = financialGroup.properties[constants.STOCK_EXC_CODE_PROP];
if (stockGroup == null && stockExcCode != null && stockExcCode.trim() != '') {
stockGroup = await stockBook.newGroup()
stockGroup = await new Group(stockBook)
.setHidden(financialGroup.hidden)
.setName(financialGroup.name)
.setProperties(financialGroup.properties)
Expand Down
18 changes: 9 additions & 9 deletions gcf/src/InterceptorOrderProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Account, AccountType, Amount, Book } from "bkper-js";
import { Account, AccountType, Amount, Book, Transaction } from "bkper-js";
import { Result } from "./index.js";
import { isStockBook, getStockBook, getCalculationModel } from "./BotService.js";
import { CalculationModel } from './CalculationModel.js';
Expand Down Expand Up @@ -122,7 +122,7 @@ export class InterceptorOrderProcessor {
let instrument = this.getInstrument(transactionPayload);
let instrumentAccount = await baseBook.getAccount(instrument);
if (instrumentAccount == null) {
instrumentAccount = await baseBook.newAccount().setName(instrument).setType(AccountType.ASSET).create();
instrumentAccount = await new Account(baseBook).setName(instrument).setType(AccountType.ASSET).create();
}
return instrumentAccount;
}
Expand Down Expand Up @@ -178,7 +178,7 @@ export class InterceptorOrderProcessor {
protected async getFeesAccount(baseBook: Book, feesAccountName: string): Promise<Account> {
let feesAccount = await baseBook.getAccount(feesAccountName);
if (feesAccount == null) {
feesAccount = await baseBook.newAccount().setName(feesAccountName).setType(AccountType.OUTGOING).create();
feesAccount = await new Account(baseBook).setName(feesAccountName).setType(AccountType.OUTGOING).create();
}
return feesAccount;
}
Expand All @@ -187,7 +187,7 @@ export class InterceptorOrderProcessor {
let interestAccountName = `${instrument} Interest`;
let interestAccount = await baseBook.getAccount(interestAccountName);
if (interestAccount == null) {
interestAccount = await baseBook.newAccount().setName(interestAccountName).setType(AccountType.ASSET).create();
interestAccount = await new Account(baseBook).setName(interestAccountName).setType(AccountType.ASSET).create();
}
return interestAccount;
}
Expand All @@ -199,7 +199,7 @@ export class InterceptorOrderProcessor {
let tradeDate = this.getTradeDate(transactionPayload);
let feesAccountName = this.getFeesAccountName(exchangeAccount);
let feesAccount = await this.getFeesAccount(baseBook, feesAccountName);
let tx = await baseBook.newTransaction()
let tx = await new Transaction(baseBook)
.setAmount(fees)
.from(exchangeAccount)
.to(feesAccount)
Expand All @@ -220,7 +220,7 @@ export class InterceptorOrderProcessor {
if (!interest.eq(0)) {
let tradeDate = this.getTradeDate(transactionPayload);
let interestAccount = await this.getInterestAccount(instrument, baseBook);
let tx = await baseBook.newTransaction()
let tx = await new Transaction(baseBook)
.setAmount(interest)
.from(exchangeAccount)
.to(interestAccount)
Expand All @@ -239,7 +239,7 @@ export class InterceptorOrderProcessor {
if (!interest.eq(0)) {
let interestAccount = await this.getInterestAccount(instrument, baseBook);
let tradeDate = this.getTradeDate(transactionPayload);
let tx = await baseBook.newTransaction()
let tx = await new Transaction(baseBook)
.setAmount(interest)
.from(interestAccount)
.to(exchangeAccount)
Expand All @@ -261,7 +261,7 @@ export class InterceptorOrderProcessor {
let tradeDate = this.getTradeDate(transactionPayload);
const amount = new Amount(transactionPayload.amount).minus(interest).minus(fees);
const price = amount.div(quantity);
let tx = baseBook.newTransaction()
let tx = new Transaction(baseBook)
.setAmount(amount)
.from(exchangeAccount)
.to(instrumentAccount)
Expand Down Expand Up @@ -303,7 +303,7 @@ export class InterceptorOrderProcessor {
let tradeDate = this.getTradeDate(transactionPayload);
const amount = new Amount(transactionPayload.amount).minus(interest).plus(fees);
const price = amount.div(quantity);
let tx = baseBook.newTransaction()
let tx = new Transaction(baseBook)
.setAmount(amount)
.from(instrumentAccount)
.to(exchangeAccount)
Expand Down
10 changes: 4 additions & 6 deletions gcf/src/InterceptorOrderProcessorDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ export abstract class InterceptorOrderProcessorDelete {
}

protected async cascadeDeleteTransactions(book: Book, remoteTx: bkper.Transaction, prefix: string) {
let iterator = book.getTransactions(`remoteId:${prefix}${remoteTx.id}`);
if (await iterator.hasNext()) {
let tx = await iterator.next();
let tx = (await book.listTransactions(`remoteId:${prefix}${remoteTx.id}`)).getFirst();
if (tx) {
if (tx.isChecked()) {
tx = await tx.uncheck();
}
Expand All @@ -37,9 +36,8 @@ export abstract class InterceptorOrderProcessorDelete {
}

protected async deleteTransaction(book: Book, remoteId: string): Promise<Transaction> {
let iterator = book.getTransactions(`remoteId:${remoteId}`);
if (await iterator.hasNext()) {
let tx = await iterator.next();
let tx = (await book.listTransactions(`remoteId:${remoteId}`)).getFirst();
if (tx) {
if (tx.isChecked()) {
tx = await tx.uncheck();
}
Expand Down

0 comments on commit f2fe716

Please sign in to comment.