update
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import * as plugins from './bunq.plugins';
|
||||
import { BunqAccount } from './bunq.classes.account';
|
||||
import { BunqTransaction } from './bunq.classes.transaction';
|
||||
import { BunqPayment } from './bunq.classes.payment';
|
||||
import { IBunqPaginationOptions, IBunqMonetaryAccountBank } from './bunq.interfaces';
|
||||
|
||||
export type TAccountType = 'joint' | 'savings' | 'bank';
|
||||
|
||||
@@ -27,9 +29,9 @@ export class BunqMonetaryAccount {
|
||||
type = 'savings';
|
||||
accessor = 'MonetaryAccountSavings';
|
||||
break;
|
||||
case !!apiObject.default:
|
||||
default:
|
||||
console.log(apiObject);
|
||||
throw new Error('unknown accoun type');
|
||||
throw new Error('unknown account type');
|
||||
}
|
||||
|
||||
Object.assign(newMonetaryAccount, apiObject[accessor], { type });
|
||||
@@ -88,27 +90,98 @@ export class BunqMonetaryAccount {
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all transactions no this account
|
||||
* gets all transactions on this account
|
||||
*/
|
||||
public async getTransactions(startingIdArg: number | false = false) {
|
||||
const paginationOptions: {
|
||||
count?: number;
|
||||
newer_id?: number | false;
|
||||
older_id?: number | false;
|
||||
} = {
|
||||
public async getTransactions(startingIdArg: number | false = false): Promise<BunqTransaction[]> {
|
||||
const paginationOptions: IBunqPaginationOptions = {
|
||||
count: 200,
|
||||
newer_id: startingIdArg,
|
||||
};
|
||||
|
||||
const apiTransactions = await this.bunqAccountRef.bunqJSClient.api.payment.list(
|
||||
this.bunqAccountRef.userId,
|
||||
this.id,
|
||||
await this.bunqAccountRef.apiContext.ensureValidSession();
|
||||
|
||||
const response = await this.bunqAccountRef.getHttpClient().list(
|
||||
`/v1/user/${this.bunqAccountRef.userId}/monetary-account/${this.id}/payment`,
|
||||
paginationOptions
|
||||
);
|
||||
|
||||
const transactionsArray: BunqTransaction[] = [];
|
||||
for (const apiTransaction of apiTransactions) {
|
||||
transactionsArray.push(BunqTransaction.fromApiObject(this, apiTransaction));
|
||||
|
||||
if (response.Response) {
|
||||
for (const apiTransaction of response.Response) {
|
||||
transactionsArray.push(BunqTransaction.fromApiObject(this, apiTransaction));
|
||||
}
|
||||
}
|
||||
|
||||
return transactionsArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a payment from this account
|
||||
*/
|
||||
public async createPayment(payment: BunqPayment): Promise<number> {
|
||||
return payment.create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update account settings
|
||||
*/
|
||||
public async update(updates: any): Promise<void> {
|
||||
await this.bunqAccountRef.apiContext.ensureValidSession();
|
||||
|
||||
const endpoint = `/v1/user/${this.bunqAccountRef.userId}/monetary-account/${this.id}`;
|
||||
|
||||
// Determine the correct update key based on account type
|
||||
let updateKey: string;
|
||||
switch (this.type) {
|
||||
case 'bank':
|
||||
updateKey = 'MonetaryAccountBank';
|
||||
break;
|
||||
case 'joint':
|
||||
updateKey = 'MonetaryAccountJoint';
|
||||
break;
|
||||
case 'savings':
|
||||
updateKey = 'MonetaryAccountSavings';
|
||||
break;
|
||||
default:
|
||||
throw new Error('Unknown account type');
|
||||
}
|
||||
|
||||
await this.bunqAccountRef.getHttpClient().put(endpoint, {
|
||||
[updateKey]: updates
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get account details
|
||||
*/
|
||||
public async refresh(): Promise<void> {
|
||||
await this.bunqAccountRef.apiContext.ensureValidSession();
|
||||
|
||||
const response = await this.bunqAccountRef.getHttpClient().get(
|
||||
`/v1/user/${this.bunqAccountRef.userId}/monetary-account/${this.id}`
|
||||
);
|
||||
|
||||
if (response.Response && response.Response[0]) {
|
||||
const refreshedAccount = BunqMonetaryAccount.fromAPIObject(
|
||||
this.bunqAccountRef,
|
||||
response.Response[0]
|
||||
);
|
||||
|
||||
// Update this instance with refreshed data
|
||||
Object.assign(this, refreshedAccount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close this monetary account
|
||||
*/
|
||||
public async close(reason: string): Promise<void> {
|
||||
await this.update({
|
||||
status: 'CANCELLED',
|
||||
sub_status: 'REDEMPTION_VOLUNTARY',
|
||||
reason: 'OTHER',
|
||||
reason_description: reason
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user