fix(tbank): isolate caches by connection generation

This commit is contained in:
2026-09-01 05:55:01 +03:00
parent da17893350
commit 19fe7e75d5
10 changed files with 317 additions and 59 deletions
+10 -5
View File
@@ -40,8 +40,8 @@ export class BrokerageOperationsService {
this.requestContext.set({ userId, brokerageAccountId: accountId });
const range = this.range(query);
return this.access.withAccount(userId, accountId, ({ token }) =>
this.page(token, userId, accountId, query, range),
return this.access.withAccount(userId, accountId, ({ token, generation }) =>
this.page(token, generation, userId, accountId, query, range),
);
}
@@ -59,7 +59,7 @@ export class BrokerageOperationsService {
return this.access.withAccount(
userId,
accountId,
async ({ token, account }) => {
async ({ token, generation, account }) => {
const accountOpenedAt = account.openedDate?.seconds
? new Date(Number(account.openedDate.seconds) * 1000)
: undefined;
@@ -69,7 +69,7 @@ export class BrokerageOperationsService {
accountOpenedAt,
pages: await Promise.all(
selected.map((range) =>
this.collectRange(token, userId, accountId, range),
this.collectRange(token, generation, userId, accountId, range),
),
),
};
@@ -79,6 +79,7 @@ export class BrokerageOperationsService {
private async collectRange(
token: string,
generation: string,
userId: string,
accountId: string,
range: DateOnlyRange,
@@ -89,6 +90,7 @@ export class BrokerageOperationsService {
do {
const page = await this.page(
token,
generation,
userId,
accountId,
{
@@ -111,12 +113,13 @@ export class BrokerageOperationsService {
private async page(
token: string,
generation: string,
userId: string,
accountId: string,
query: BrokerageOperationsQueryDto,
range: { from: Date; to: Date },
): Promise<BrokerageOperationsPageDto> {
const key = cacheKey(userId, accountId, query, range);
const key = cacheKey(generation, userId, accountId, query, range);
const cached = this.pages.get(key);
if (cached) return cached;
if (range.from > range.to) {
@@ -193,6 +196,7 @@ export class BrokerageOperationsService {
}
function cacheKey(
generation: string,
userId: string,
accountId: string,
query: BrokerageOperationsQueryDto,
@@ -203,6 +207,7 @@ function cacheKey(
? range.to.toISOString()
: Math.floor(range.to.valueOf() / freshForMs);
return JSON.stringify({
generation,
userId,
accountId,
from: range.from.toISOString(),