fix(core): update
This commit is contained in:
@ -1,19 +1,82 @@
|
||||
import * as plugins from './bunq.plugins';
|
||||
import * as paths from './bunq.paths';
|
||||
import { MonetaryAccount } from './bunq.classes.monetaryaccount';
|
||||
|
||||
export interface IBunqConstructorOptions {
|
||||
encryptionKey: string; // 16 byte encryption key
|
||||
apiKey: "abcd-1234-abcd-1234"; // Your bunq API key
|
||||
deviceName: string;
|
||||
apiKey: string;
|
||||
environment: 'SANDBOX' | 'PRODUCTION';
|
||||
}
|
||||
|
||||
/**
|
||||
* the main bunq account
|
||||
*/
|
||||
export class BunqAccount {
|
||||
constructor(optionsArg) {
|
||||
public options: IBunqConstructorOptions;
|
||||
|
||||
public bunqJSClient: plugins.bunqCommunityClient.default;
|
||||
public encryptionKey: string;
|
||||
public permittedIps = []; // bunq will use the current ip if omitted
|
||||
|
||||
/**
|
||||
* user id is needed for doing stuff like listing accounts;
|
||||
*/
|
||||
public userId: number;
|
||||
|
||||
constructor(optionsArg: IBunqConstructorOptions) {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
init() {
|
||||
public async init() {
|
||||
this.encryptionKey = plugins.smartcrypto.nodeForge.util.bytesToHex(
|
||||
plugins.smartcrypto.nodeForge.random.getBytesSync(16)
|
||||
);
|
||||
|
||||
// lets setup bunq client
|
||||
await plugins.smartfile.fs.ensureDir(paths.nogitDir);
|
||||
await plugins.smartfile.fs.ensureFile(paths.bunqJsonFile, '{}');
|
||||
const storageInstance = plugins.JSONFileStore(paths.bunqJsonFile);
|
||||
this.bunqJSClient = new plugins.bunqCommunityClient.default(storageInstance);
|
||||
|
||||
// run the bunq application with our API key
|
||||
await this.bunqJSClient.run(
|
||||
this.options.apiKey,
|
||||
this.permittedIps,
|
||||
this.options.environment,
|
||||
this.encryptionKey
|
||||
);
|
||||
|
||||
// install a new keypair
|
||||
await this.bunqJSClient.install();
|
||||
|
||||
// register this device
|
||||
await this.bunqJSClient.registerDevice(this.options.deviceName);
|
||||
|
||||
// register a new session
|
||||
await this.bunqJSClient.registerSession();
|
||||
await this.getUserId();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* lists all users
|
||||
*/
|
||||
private async getUserId() {
|
||||
const users = await this.bunqJSClient.api.user.list();
|
||||
if (users.UserPerson) {
|
||||
this.userId = users.UserPerson.id;
|
||||
} else if (users.UserCompany) {
|
||||
this.userId = users.UserCompany.id;
|
||||
} else {
|
||||
console.log('could not determine user id');
|
||||
}
|
||||
}
|
||||
|
||||
public async getAccounts() {
|
||||
const apiMonetaryAccounts = await this.bunqJSClient.api.monetaryAccount.list(this.userId);
|
||||
const accountsArray: MonetaryAccount[] = [];
|
||||
for (const apiAccount of apiMonetaryAccounts) {
|
||||
accountsArray.push(MonetaryAccount.fromAPIObject(this, apiAccount));
|
||||
}
|
||||
return accountsArray;
|
||||
}
|
||||
}
|
||||
|
101
ts/bunq.classes.monetaryaccount.ts
Normal file
101
ts/bunq.classes.monetaryaccount.ts
Normal file
@ -0,0 +1,101 @@
|
||||
import * as plugins from './bunq.plugins';
|
||||
import { BunqAccount } from './bunq.classes.account';
|
||||
import { Transaction } from './bunq.classes.transaction';
|
||||
|
||||
export type TAccountType = 'joint' | 'savings' | 'bank';
|
||||
|
||||
/**
|
||||
* a monetary account
|
||||
*/
|
||||
export class MonetaryAccount {
|
||||
public static fromAPIObject(bunqAccountRef: BunqAccount, apiObject: any) {
|
||||
const newMonetaryAccount = new this(bunqAccountRef);
|
||||
|
||||
let type: TAccountType;
|
||||
let accessor: 'MonetaryAccountBank' | 'MonetaryAccountJoint' | 'MonetaryAccountSavings';
|
||||
|
||||
switch (true) {
|
||||
case !!apiObject.MonetaryAccountBank:
|
||||
type = 'bank';
|
||||
accessor = 'MonetaryAccountBank';
|
||||
break;
|
||||
case !!apiObject.MonetaryAccountJoint:
|
||||
type = 'joint';
|
||||
accessor = 'MonetaryAccountJoint';
|
||||
break;
|
||||
case !!apiObject.MonetaryAccountSavings:
|
||||
type = 'savings';
|
||||
accessor = 'MonetaryAccountSavings';
|
||||
break;
|
||||
case !!apiObject.default:
|
||||
console.log(apiObject);
|
||||
throw new Error('unknown accoun type');
|
||||
}
|
||||
|
||||
Object.assign(newMonetaryAccount, apiObject[accessor], {type});
|
||||
return newMonetaryAccount;
|
||||
}
|
||||
|
||||
// computed
|
||||
public type: TAccountType;
|
||||
|
||||
// from API
|
||||
public id: number;
|
||||
public created: string;
|
||||
public updated: string;
|
||||
public alias: any[];
|
||||
public avatar: {
|
||||
uuid: string;
|
||||
image: any[];
|
||||
anchor_uuid: string;
|
||||
};
|
||||
public balance: {
|
||||
currency: string;
|
||||
value: string;
|
||||
};
|
||||
public country: string;
|
||||
public currency: string;
|
||||
public daily_limit: {
|
||||
currency: string;
|
||||
value: string;
|
||||
};
|
||||
public daily_spent: {
|
||||
currency: string;
|
||||
value: string;
|
||||
};
|
||||
public description: string;
|
||||
public public_uuid: string;
|
||||
public status: string;
|
||||
public sub_status: string;
|
||||
public timezone: string;
|
||||
public user_id: number;
|
||||
public monetary_account_profile: null;
|
||||
public notification_filters: any[];
|
||||
public setting: any[];
|
||||
public connected_cards: any[];
|
||||
public overdraft_limit: {
|
||||
currency: string;
|
||||
value: string;
|
||||
};
|
||||
public reason: string;
|
||||
public reason_description: string;
|
||||
public auto_save_id: null;
|
||||
public all_auto_save_id: any[];
|
||||
|
||||
|
||||
public bunqAccountRef: BunqAccount;
|
||||
constructor(bunqAccountRefArg: BunqAccount) {
|
||||
this.bunqAccountRef = bunqAccountRefArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all transactions no this account
|
||||
*/
|
||||
public async getTransactions() {
|
||||
const apiTransactions = await this.bunqAccountRef.bunqJSClient.api.payment.list(this.bunqAccountRef.userId, this.id);
|
||||
const transactionsArray: Transaction[] = [];
|
||||
for (const apiTransaction of apiTransactions) {
|
||||
transactionsArray.push(Transaction.fromApiObject(this, apiTransaction));
|
||||
}
|
||||
}
|
||||
}
|
45
ts/bunq.classes.transaction.ts
Normal file
45
ts/bunq.classes.transaction.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import * as plugins from './bunq.plugins';
|
||||
import { MonetaryAccount } from './bunq.classes.monetaryaccount';
|
||||
|
||||
export class Transaction {
|
||||
public static fromApiObject(monetaryAccountRefArg: MonetaryAccount, apiObjectArg: any) {
|
||||
const newTransaction = new this(monetaryAccountRefArg);
|
||||
Object.assign(newTransaction, apiObjectArg);
|
||||
return newTransaction;
|
||||
}
|
||||
|
||||
|
||||
public id: number;
|
||||
public created: string;
|
||||
public updated: string;
|
||||
public monetary_account_id: number;
|
||||
public amount: {
|
||||
currency: string;
|
||||
value: string;
|
||||
};
|
||||
public description: string;
|
||||
public type: 'MASTERCARD' | 'BUNQ';
|
||||
public merchant_reference: null;
|
||||
public alias: [Object];
|
||||
public counterparty_alias: [Object];
|
||||
public attachment: [];
|
||||
public geolocation: null;
|
||||
public batch_id: null;
|
||||
public allow_chat: boolean;
|
||||
public scheduled_id: null;
|
||||
public address_billing: null;
|
||||
public address_shipping: null;
|
||||
public sub_type: 'PAYMENT';
|
||||
public request_reference_split_the_bill: [];
|
||||
public balance_after_mutation: {
|
||||
currency: string;
|
||||
value: string;
|
||||
};
|
||||
|
||||
public monetaryAccountRef: MonetaryAccount;
|
||||
|
||||
constructor(monetaryAccountRefArg: MonetaryAccount) {
|
||||
this.monetaryAccountRef = monetaryAccountRefArg;
|
||||
}
|
||||
|
||||
}
|
6
ts/bunq.paths.ts
Normal file
6
ts/bunq.paths.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import * as plugins from './bunq.plugins';
|
||||
|
||||
export const packageDir = plugins.path.join(__dirname, '../');
|
||||
export const nogitDir = plugins.path.join(packageDir, './.nogit/');
|
||||
|
||||
export const bunqJsonFile = plugins.path.join(nogitDir, 'bunq.json');
|
@ -1,3 +1,23 @@
|
||||
// node natice
|
||||
import * as path from 'path';
|
||||
|
||||
export {
|
||||
path
|
||||
};
|
||||
|
||||
// @pushrocks scope
|
||||
import * as smartcrypto from '@pushrocks/smartcrypto';
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
export {
|
||||
smartcrypto,
|
||||
smartfile,
|
||||
smartpromise,
|
||||
};
|
||||
|
||||
// third party
|
||||
import JSONFileStore from "@bunq-community/bunq-js-client/dist/Stores/JSONFileStore";
|
||||
import * as bunqCommunityClient from '@bunq-community/bunq-js-client';
|
||||
|
||||
export { bunqCommunityClient };
|
||||
export { JSONFileStore, bunqCommunityClient };
|
||||
|
@ -0,0 +1 @@
|
||||
export * from './bunq.classes.account';
|
||||
|
Reference in New Issue
Block a user