feat: add brocker account list
This commit is contained in:
@@ -22,9 +22,18 @@ type GetAccounts = (
|
||||
request: { status: string },
|
||||
metadata: Metadata,
|
||||
options: { deadline: Date },
|
||||
callback: (error: ServiceError | null) => void,
|
||||
callback: (
|
||||
error: ServiceError | null,
|
||||
response: { accounts?: TbankAccount[] },
|
||||
) => void,
|
||||
) => ClientUnaryCall;
|
||||
type UsersClient = Client & { getAccounts: GetAccounts };
|
||||
export type TbankAccount = {
|
||||
id: string;
|
||||
type: string;
|
||||
name: string;
|
||||
openedDate?: { seconds?: string | number };
|
||||
};
|
||||
type TbankNamespace = {
|
||||
tinkoff: {
|
||||
public: {
|
||||
@@ -129,18 +138,23 @@ export class TbankClient {
|
||||
}
|
||||
|
||||
async validateToken(token: string): Promise<void> {
|
||||
await this.call(token, true);
|
||||
await this.getOpenAccounts(token);
|
||||
}
|
||||
|
||||
private async call(token: string, retry: boolean): Promise<void> {
|
||||
async getOpenAccounts(token: string): Promise<TbankAccount[]> {
|
||||
return this.call(token, true);
|
||||
}
|
||||
|
||||
private async call(token: string, retry: boolean): Promise<TbankAccount[]> {
|
||||
if (this.queue.size >= 50) {
|
||||
this.logger.warn({ event: 'queue_full', waiting: this.queue.size });
|
||||
throw new ServiceUnavailableException({ code: 'TBANK_UNAVAILABLE' });
|
||||
}
|
||||
try {
|
||||
this.logger.debug({ event: 'get_accounts_started', retry: !retry });
|
||||
await this.queue.run(() => this.request(token));
|
||||
const accounts = await this.queue.run(() => this.request(token));
|
||||
this.logger.debug({ event: 'get_accounts_succeeded', retry: !retry });
|
||||
return accounts;
|
||||
} catch (error) {
|
||||
const code = (error as ServiceError).code;
|
||||
this.logger.warn({
|
||||
@@ -175,7 +189,7 @@ export class TbankClient {
|
||||
}
|
||||
}
|
||||
|
||||
private request(token: string): Promise<void> {
|
||||
private request(token: string): Promise<TbankAccount[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const metadata = new Metadata();
|
||||
metadata.set('Authorization', `Bearer ${token}`);
|
||||
@@ -183,7 +197,8 @@ export class TbankClient {
|
||||
{ status: 'ACCOUNT_STATUS_OPEN' },
|
||||
metadata,
|
||||
{ deadline: new Date(Date.now() + 10_000) },
|
||||
(error) => (error ? reject(error) : resolve()),
|
||||
(error, response) =>
|
||||
error ? reject(error) : resolve(response.accounts ?? []),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user