import * as plugins from './bunq.plugins.js'; import { BunqAccount } from './bunq.classes.account.js'; import { BunqTransaction } from './bunq.classes.transaction.js'; import { BunqPayment } from './bunq.classes.payment.js'; import { IBunqPaginationOptions, IBunqMonetaryAccountBank } from './bunq.interfaces.js'; export type TAccountType = 'joint' | 'savings' | 'bank'; /** * a monetary account */ export class BunqMonetaryAccount { 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; default: console.log(apiObject); throw new Error('unknown account 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 on this account */ public async getTransactions(startingIdArg: number | false = false): Promise { const paginationOptions: IBunqPaginationOptions = { count: 200, newer_id: startingIdArg, }; 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[] = []; 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 { return payment.create(); } /** * Update account settings */ public async update(updates: any): Promise { 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 { 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 { await this.update({ status: 'CANCELLED', sub_status: 'REDEMPTION_VOLUNTARY', reason: 'OTHER', reason_description: reason }); } }