refactor: share tbank-sync claim and generation lock (WealthLedger/backend#166) (#188)
This commit was merged in pull request #188.
This commit is contained in:
@@ -5,7 +5,8 @@ import type { Prisma } from '../../../../../generated/prisma/client';
|
||||
import type { PortfolioDto } from '../presentation/portfolio.dto';
|
||||
|
||||
import { TbankErrorCode } from '../../../../integrations/tbank/tbank-error-code';
|
||||
import { tbankSourceNeedsRefresh, tbankSourceTtlMs } from '../../../../integrations/tbank/tbank.api';
|
||||
import { lockConnectionGeneration } from '../../../../integrations/tbank/tbank-sync-claim';
|
||||
import { isJoinableRunningAttempt, tbankSourceNeedsRefresh, tbankSourceTtlMs } from '../../../../integrations/tbank/tbank.api';
|
||||
import { PrismaService } from '../../../../prisma/prisma.service';
|
||||
|
||||
const EXPECTED_UPDATED_ACCOUNTS = 1;
|
||||
@@ -56,16 +57,10 @@ export class PortfolioRepository {
|
||||
force: boolean,
|
||||
) {
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const current = await tx.tbankConnection.updateMany({
|
||||
where: {
|
||||
userId,
|
||||
revision,
|
||||
invalidToken: false,
|
||||
accounts: { some: { accountId } },
|
||||
},
|
||||
data: { membershipRevision: { increment: 0 } },
|
||||
const current = await lockConnectionGeneration(tx, { userId, revision }, {
|
||||
accounts: { some: { accountId } },
|
||||
});
|
||||
if (!current.count) return null;
|
||||
if (!current) return null;
|
||||
const snapshot = await tx.tbankPortfolio.findUnique({
|
||||
where: { userId_revision_accountId: { userId, revision, accountId } },
|
||||
});
|
||||
@@ -86,11 +81,7 @@ export class PortfolioRepository {
|
||||
return null;
|
||||
}
|
||||
const scope = portfolioWorkScope;
|
||||
if (
|
||||
previous?.state === 'running' &&
|
||||
previous.processRunId === processRunId &&
|
||||
pending.has(previous.attemptId)
|
||||
) {
|
||||
if (isJoinableRunningAttempt(previous, processRunId, pending)) {
|
||||
return { work: previous, launch: false };
|
||||
}
|
||||
const data = {
|
||||
|
||||
Reference in New Issue
Block a user