Compare commits
2
Commits
v2.4.1
...
a2b5306a4c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2b5306a4c | ||
|
|
812b989130 |
@@ -12,6 +12,7 @@ import type { TbankInstrument } from './tbank.client';
|
|||||||
import { PrismaService } from '../../prisma/prisma.service';
|
import { PrismaService } from '../../prisma/prisma.service';
|
||||||
import { isJoinableRunningAttempt, type TbankSyncAttempt } from './tbank-access.service';
|
import { isJoinableRunningAttempt, type TbankSyncAttempt } from './tbank-access.service';
|
||||||
import { TbankErrorCode } from './tbank-error-code';
|
import { TbankErrorCode } from './tbank-error-code';
|
||||||
|
import { claimSyncWork, lockConnectionGeneration } from './tbank-sync-claim';
|
||||||
|
|
||||||
const FIRST_BATCH_OFFSET = 0;
|
const FIRST_BATCH_OFFSET = 0;
|
||||||
const UNCHANGED_REVISION_INCREMENT = 0;
|
const UNCHANGED_REVISION_INCREMENT = 0;
|
||||||
@@ -119,11 +120,8 @@ export class InstrumentMetadataRepository {
|
|||||||
active: ReadonlySet<string>,
|
active: ReadonlySet<string>,
|
||||||
) {
|
) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const valid = await tx.tbankConnection.updateMany({
|
const valid = await lockConnectionGeneration(tx, attemptOwner);
|
||||||
where: { ...attemptOwner, invalidToken: false },
|
if (!valid) return null;
|
||||||
data: { membershipRevision: { increment: 0 } },
|
|
||||||
});
|
|
||||||
if (!valid.count) return null;
|
|
||||||
const scope = {
|
const scope = {
|
||||||
...attemptOwner,
|
...attemptOwner,
|
||||||
scopeKey: `metadata:${metadataKey(identity)}`,
|
scopeKey: `metadata:${metadataKey(identity)}`,
|
||||||
@@ -167,15 +165,11 @@ export class InstrumentMetadataRepository {
|
|||||||
|
|
||||||
publish(attempt: TbankSyncAttempt, results: MetadataLookup[], now: Date) {
|
publish(attempt: TbankSyncAttempt, results: MetadataLookup[], now: Date) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const claimed = await tx.tbankSyncWork.updateMany({
|
const claimed = await claimSyncWork(tx, attempt, {
|
||||||
where: {
|
state: 'complete',
|
||||||
...attempt,
|
progress: {},
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: { state: 'complete', progress: {} },
|
|
||||||
});
|
});
|
||||||
if (!claimed.count) return false;
|
if (!claimed) return false;
|
||||||
const aliases = new Map(
|
const aliases = new Map(
|
||||||
results.map((result) => [`${result.type}:${result.id}`, result]),
|
results.map((result) => [`${result.type}:${result.id}`, result]),
|
||||||
);
|
);
|
||||||
@@ -264,18 +258,17 @@ export class InstrumentMetadataRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async finishObserver(attempt: TbankSyncAttempt, success: boolean) {
|
async finishObserver(attempt: TbankSyncAttempt, success: boolean) {
|
||||||
await this.prisma.tbankSyncWork.updateMany({
|
await claimSyncWork(
|
||||||
where: {
|
this.prisma,
|
||||||
...attempt,
|
attempt,
|
||||||
state: 'running',
|
{
|
||||||
...(success ? { connection: { invalidToken: false } } : {}),
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
state: success ? 'complete' : 'failed',
|
state: success ? 'complete' : 'failed',
|
||||||
errorCode: success ? null : TbankErrorCode.Unavailable,
|
errorCode: success ? null : TbankErrorCode.Unavailable,
|
||||||
},
|
},
|
||||||
});
|
{ withoutConnectionFence: !success },
|
||||||
|
);
|
||||||
if (success) {
|
if (success) {
|
||||||
|
// Fails sibling attempts whose connection was invalidated mid-flight.
|
||||||
await this.prisma.tbankSyncWork.updateMany({
|
await this.prisma.tbankSyncWork.updateMany({
|
||||||
where: {
|
where: {
|
||||||
...attempt,
|
...attempt,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { PrismaService } from '../../prisma/prisma.service';
|
|||||||
import { supportedTbankAccounts } from './tbank-account.policy';
|
import { supportedTbankAccounts } from './tbank-account.policy';
|
||||||
import { TbankErrorCode } from './tbank-error-code';
|
import { TbankErrorCode } from './tbank-error-code';
|
||||||
import { tbankSourceNeedsRefresh } from './tbank-source.policy';
|
import { tbankSourceNeedsRefresh } from './tbank-source.policy';
|
||||||
|
import { claimSyncWork, lockConnectionGeneration } from './tbank-sync-claim';
|
||||||
import { TbankTokenService } from './tbank-token.service';
|
import { TbankTokenService } from './tbank-token.service';
|
||||||
|
|
||||||
const EXPECTED_UPDATED_CONNECTIONS = 1;
|
const EXPECTED_UPDATED_CONNECTIONS = 1;
|
||||||
@@ -56,11 +57,8 @@ export class TbankAccessService {
|
|||||||
force: boolean,
|
force: boolean,
|
||||||
) {
|
) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const current = await tx.tbankConnection.updateMany({
|
const current = await lockConnectionGeneration(tx, { userId, revision });
|
||||||
where: { userId, revision, invalidToken: false },
|
if (!current) return null;
|
||||||
data: { membershipRevision: { increment: 0 } },
|
|
||||||
});
|
|
||||||
if (!current.count) return null;
|
|
||||||
const connection = await tx.tbankConnection.findUniqueOrThrow({
|
const connection = await tx.tbankConnection.findUniqueOrThrow({
|
||||||
where: { userId },
|
where: { userId },
|
||||||
});
|
});
|
||||||
@@ -110,15 +108,11 @@ export class TbankAccessService {
|
|||||||
|
|
||||||
async failMembership(attempt: TbankSyncAttempt, invalidToken: boolean) {
|
async failMembership(attempt: TbankSyncAttempt, invalidToken: boolean) {
|
||||||
if (invalidToken) return this.rejectMembershipToken(attempt);
|
if (invalidToken) return this.rejectMembershipToken(attempt);
|
||||||
const result = await this.prisma.tbankSyncWork.updateMany({
|
const result = await claimSyncWork(this.prisma, attempt, {
|
||||||
where: {
|
state: 'failed',
|
||||||
...attempt,
|
errorCode: TbankErrorCode.Unavailable,
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: { state: 'failed', errorCode: TbankErrorCode.Unavailable },
|
|
||||||
});
|
});
|
||||||
return result.count === EXPECTED_UPDATED_CONNECTIONS;
|
return result === EXPECTED_UPDATED_CONNECTIONS;
|
||||||
}
|
}
|
||||||
|
|
||||||
async tokenForAttempt(attempt: TbankSyncAttempt): Promise<null | string> {
|
async tokenForAttempt(attempt: TbankSyncAttempt): Promise<null | string> {
|
||||||
@@ -163,15 +157,8 @@ export class TbankAccessService {
|
|||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
// Acquire the write fence before reading membership. Replacement and
|
// Acquire the write fence before reading membership. Replacement and
|
||||||
// attempt supersession cannot interleave with the publication below.
|
// attempt supersession cannot interleave with the publication below.
|
||||||
const claimed = await tx.tbankSyncWork.updateMany({
|
const claimed = await claimSyncWork(tx, attempt, { updatedAt: new Date() });
|
||||||
where: {
|
if (!claimed) return false;
|
||||||
...attempt,
|
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: { updatedAt: new Date() },
|
|
||||||
});
|
|
||||||
if (!claimed.count) return false;
|
|
||||||
const { userId, revision } = attempt;
|
const { userId, revision } = attempt;
|
||||||
const previous = await tx.tbankAccount.findMany({
|
const previous = await tx.tbankAccount.findMany({
|
||||||
where: { userId, revision },
|
where: { userId, revision },
|
||||||
@@ -244,11 +231,11 @@ export class TbankAccessService {
|
|||||||
|
|
||||||
async rejectMembershipToken(attempt: TbankSyncAttempt): Promise<boolean> {
|
async rejectMembershipToken(attempt: TbankSyncAttempt): Promise<boolean> {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const claimed = await tx.tbankSyncWork.updateMany({
|
const claimed = await claimSyncWork(tx, attempt, {
|
||||||
where: { ...attempt, state: 'running' },
|
state: 'failed',
|
||||||
data: { state: 'failed', errorCode: TbankErrorCode.InvalidToken },
|
errorCode: TbankErrorCode.InvalidToken,
|
||||||
});
|
}, { withoutConnectionFence: true });
|
||||||
if (!claimed.count) return false;
|
if (!claimed) return false;
|
||||||
await tx.tbankConnection.update({
|
await tx.tbankConnection.update({
|
||||||
where: {
|
where: {
|
||||||
userId_revision: {
|
userId_revision: {
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import type { Prisma } from '../../../generated/prisma/client';
|
||||||
|
import type { TbankSyncAttempt } from './tbank-access.service';
|
||||||
|
|
||||||
|
const NO_UPDATED_ROWS = 0;
|
||||||
|
|
||||||
|
// Shared owners for the tbank-sync DB claim/lock protocol.
|
||||||
|
// Both helpers are CAS fences: they run inside the caller's transaction and
|
||||||
|
// keep the exact where/data shapes they replaced. Callers keep deciding
|
||||||
|
// success from the claim count exactly as before.
|
||||||
|
|
||||||
|
// Claims the active TbankSyncWork row: only the still-running attempt of a
|
||||||
|
// valid connection may transition. `data` is the state transition to apply.
|
||||||
|
export type ClaimOptions = {
|
||||||
|
// Overrides the default 'running' fence (e.g. an initiator row also
|
||||||
|
// accepts 'complete').
|
||||||
|
state: Prisma.TbankSyncWorkWhereInput['state'] | undefined;
|
||||||
|
// true drops the `connection: { invalidToken: false }` guard for fences
|
||||||
|
// that must also fire on an invalidated connection.
|
||||||
|
withoutConnectionFence: boolean | undefined;
|
||||||
|
};
|
||||||
|
export async function claimSyncWork(
|
||||||
|
tx: Prisma.TransactionClient,
|
||||||
|
attempt: TbankSyncAttempt,
|
||||||
|
data: Prisma.TbankSyncWorkUpdateManyMutationInput,
|
||||||
|
options: Partial<ClaimOptions> = {},
|
||||||
|
): Promise<number> {
|
||||||
|
const result = await tx.tbankSyncWork.updateMany({
|
||||||
|
where: {
|
||||||
|
...attempt,
|
||||||
|
state: options.state ?? 'running',
|
||||||
|
...(options.withoutConnectionFence
|
||||||
|
? {}
|
||||||
|
: { connection: { invalidToken: false } }),
|
||||||
|
},
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
return result.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Locks the connection generation before reserving or publishing work: the
|
||||||
|
// row must still be the current revision with a valid token. `extra`
|
||||||
|
// (e.g. account membership) is merged into the where clause.
|
||||||
|
export async function lockConnectionGeneration(
|
||||||
|
tx: Prisma.TransactionClient,
|
||||||
|
owner: { revision: string; userId: string },
|
||||||
|
extra: Prisma.TbankConnectionWhereInput = {},
|
||||||
|
): Promise<boolean> {
|
||||||
|
const result = await tx.tbankConnection.updateMany({
|
||||||
|
where: { ...owner, ...extra, invalidToken: false },
|
||||||
|
data: { membershipRevision: { increment: 0 } },
|
||||||
|
});
|
||||||
|
return result.count > NO_UPDATED_ROWS;
|
||||||
|
}
|
||||||
@@ -2,8 +2,15 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { createHash, randomUUID } from 'node:crypto';
|
import { createHash, randomUUID } from 'node:crypto';
|
||||||
|
|
||||||
import type { Prisma } from '../../../../../generated/prisma/client';
|
import type { Prisma } from '../../../../../generated/prisma/client';
|
||||||
import type { TbankSyncAttempt } from '../../../../integrations/tbank/tbank.api';
|
|
||||||
|
|
||||||
|
import {
|
||||||
|
claimSyncWork,
|
||||||
|
lockConnectionGeneration,
|
||||||
|
} from '../../../../integrations/tbank/tbank-sync-claim';
|
||||||
|
import {
|
||||||
|
isJoinableRunningAttempt,
|
||||||
|
type TbankSyncAttempt,
|
||||||
|
} from '../../../../integrations/tbank/tbank.api';
|
||||||
import { PrismaService } from '../../../../prisma/prisma.service';
|
import { PrismaService } from '../../../../prisma/prisma.service';
|
||||||
import {
|
import {
|
||||||
type CandleRange,
|
type CandleRange,
|
||||||
@@ -62,34 +69,30 @@ export class CandleRepository {
|
|||||||
priority = 1,
|
priority = 1,
|
||||||
) {
|
) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const current = await tx.tbankConnection.updateMany({
|
const current = await lockConnectionGeneration(tx, owner);
|
||||||
where: { ...owner, invalidToken: false },
|
if (!current) return null;
|
||||||
data: { membershipRevision: { increment: 0 } },
|
|
||||||
});
|
|
||||||
if (!current.count) return null;
|
|
||||||
const scope = { ...owner, scopeKey };
|
const scope = { ...owner, scopeKey };
|
||||||
const previous = await tx.tbankSyncWork.findUnique({
|
const previous = await tx.tbankSyncWork.findUnique({
|
||||||
where: { userId_revision_scopeKey: scope },
|
where: { userId_revision_scopeKey: scope },
|
||||||
});
|
});
|
||||||
if (
|
const joinable = isJoinableRunningAttempt(previous, runId, active)
|
||||||
previous?.state === 'running' &&
|
? previous
|
||||||
previous.processRunId === runId &&
|
: null;
|
||||||
active.has(previous.attemptId)
|
if (joinable) {
|
||||||
) {
|
const previousIntent = joinable.intent as Prisma.JsonObject;
|
||||||
const previousIntent = previous.intent as Prisma.JsonObject;
|
|
||||||
const previousNow =
|
const previousNow =
|
||||||
typeof previousIntent.now === 'string' ? previousIntent.now : '';
|
typeof previousIntent.now === 'string' ? previousIntent.now : '';
|
||||||
const nextPriority = Math.min(previous.priority, priority);
|
const nextPriority = Math.min(joinable.priority, priority);
|
||||||
if (
|
if (
|
||||||
nextPriority === previous.priority &&
|
nextPriority === joinable.priority &&
|
||||||
(!intent.force ||
|
(!intent.force ||
|
||||||
(previousIntent.force === true &&
|
(previousIntent.force === true &&
|
||||||
(!intent.now || intent.now <= previousNow)))
|
(!intent.now || intent.now <= previousNow)))
|
||||||
) {
|
) {
|
||||||
return { work: previous, launch: false };
|
return { work: joinable, launch: false };
|
||||||
}
|
}
|
||||||
const work = await tx.tbankSyncWork.update({
|
const work = await tx.tbankSyncWork.update({
|
||||||
where: { id: previous.id },
|
where: { id: joinable.id },
|
||||||
data: {
|
data: {
|
||||||
priority: nextPriority,
|
priority: nextPriority,
|
||||||
intent: {
|
intent: {
|
||||||
@@ -163,15 +166,10 @@ export class CandleRepository {
|
|||||||
).values(),
|
).values(),
|
||||||
];
|
];
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const valid = await tx.tbankSyncWork.updateMany({
|
const valid = await claimSyncWork(tx, attempt, {
|
||||||
where: {
|
progress: { lastWindow: key },
|
||||||
...attempt,
|
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: { progress: { lastWindow: key } },
|
|
||||||
});
|
});
|
||||||
if (!valid.count) return false;
|
if (!valid) return false;
|
||||||
const identity = {
|
const identity = {
|
||||||
workId: attempt.id,
|
workId: attempt.id,
|
||||||
attemptId: attempt.attemptId,
|
attemptId: attempt.attemptId,
|
||||||
@@ -196,18 +194,11 @@ export class CandleRepository {
|
|||||||
completeWork = true,
|
completeWork = true,
|
||||||
) {
|
) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const claimed = await tx.tbankSyncWork.updateMany({
|
const claimed = await claimSyncWork(tx, attempt, {
|
||||||
where: {
|
...(completeWork ? { state: 'complete' } : {}),
|
||||||
...attempt,
|
errorCode: null,
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
...(completeWork ? { state: 'complete' } : {}),
|
|
||||||
errorCode: null,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
if (!claimed.count) return false;
|
if (!claimed) return false;
|
||||||
const staged = await tx.tbankSyncStage.findMany({
|
const staged = await tx.tbankSyncStage.findMany({
|
||||||
where: {
|
where: {
|
||||||
workId: attempt.id,
|
workId: attempt.id,
|
||||||
@@ -311,28 +302,17 @@ export class CandleRepository {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!origin) return false;
|
if (!origin) return false;
|
||||||
const result = await tx.tbankSyncWork.updateMany({
|
const result = await claimSyncWork(tx, attempt, {
|
||||||
where: {
|
state: 'complete',
|
||||||
...attempt,
|
errorCode: null,
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: { state: 'complete', errorCode: null },
|
|
||||||
});
|
});
|
||||||
return result.count > NO_UPDATED_ROWS;
|
return result > NO_UPDATED_ROWS;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fail(attempt: TbankSyncAttempt, errorCode: string) {
|
fail(attempt: TbankSyncAttempt, errorCode: string) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
await tx.tbankSyncWork.updateMany({
|
await claimSyncWork(tx, attempt, { state: 'failed', errorCode });
|
||||||
where: {
|
|
||||||
...attempt,
|
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: { state: 'failed', errorCode },
|
|
||||||
});
|
|
||||||
await tx.tbankSyncStage.deleteMany({
|
await tx.tbankSyncStage.deleteMany({
|
||||||
where: { workId: attempt.id, attemptId: attempt.attemptId },
|
where: { workId: attempt.id, attemptId: attempt.attemptId },
|
||||||
});
|
});
|
||||||
|
|||||||
+22
-40
@@ -5,6 +5,7 @@ import { isDeepStrictEqual } from 'node:util';
|
|||||||
import type { Prisma } from '../../../../../generated/prisma/client';
|
import type { Prisma } from '../../../../../generated/prisma/client';
|
||||||
|
|
||||||
import { TbankErrorCode } from '../../../../integrations/tbank/tbank-error-code';
|
import { TbankErrorCode } from '../../../../integrations/tbank/tbank-error-code';
|
||||||
|
import { claimSyncWork, lockConnectionGeneration } from '../../../../integrations/tbank/tbank-sync-claim';
|
||||||
import { isJoinableRunningAttempt, type TbankBond, type TbankShare, type TbankSyncAttempt } from '../../../../integrations/tbank/tbank.api';
|
import { isJoinableRunningAttempt, type TbankBond, type TbankShare, type TbankSyncAttempt } from '../../../../integrations/tbank/tbank.api';
|
||||||
import { PrismaService } from '../../../../prisma/prisma.service';
|
import { PrismaService } from '../../../../prisma/prisma.service';
|
||||||
|
|
||||||
@@ -159,24 +160,18 @@ export class CatalogDiscoveryRepository {
|
|||||||
initiator: TbankSyncAttempt = attempt,
|
initiator: TbankSyncAttempt = attempt,
|
||||||
) {
|
) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const origin = await tx.tbankSyncWork.updateMany({
|
const origin = await claimSyncWork(
|
||||||
where: {
|
tx,
|
||||||
...initiator,
|
initiator,
|
||||||
state: { in: ['running', 'complete'] },
|
{ progress: {} },
|
||||||
connection: { invalidToken: false },
|
{ state: { in: ['running', 'complete'] } },
|
||||||
},
|
);
|
||||||
data: { progress: {} },
|
if (!origin) return false;
|
||||||
|
const claimed = await claimSyncWork(tx, attempt, {
|
||||||
|
state: 'complete',
|
||||||
|
progress: {},
|
||||||
});
|
});
|
||||||
if (!origin.count) return false;
|
if (!claimed) return false;
|
||||||
const claimed = await tx.tbankSyncWork.updateMany({
|
|
||||||
where: {
|
|
||||||
...attempt,
|
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: { state: 'complete', progress: {} },
|
|
||||||
});
|
|
||||||
if (!claimed.count) return false;
|
|
||||||
const payload = JSON.parse(
|
const payload = JSON.parse(
|
||||||
JSON.stringify(value),
|
JSON.stringify(value),
|
||||||
) as Prisma.InputJsonObject;
|
) as Prisma.InputJsonObject;
|
||||||
@@ -284,11 +279,8 @@ export class CatalogDiscoveryRepository {
|
|||||||
active: ReadonlySet<string>,
|
active: ReadonlySet<string>,
|
||||||
) {
|
) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const current = await tx.tbankConnection.updateMany({
|
const current = await lockConnectionGeneration(tx, owner);
|
||||||
where: { ...owner, invalidToken: false },
|
if (!current) return null;
|
||||||
data: { membershipRevision: { increment: 0 } },
|
|
||||||
});
|
|
||||||
if (!current.count) return null;
|
|
||||||
const scope = { ...owner, scopeKey: catalogScope(input) };
|
const scope = { ...owner, scopeKey: catalogScope(input) };
|
||||||
const previous = await tx.tbankSyncWork.findUnique({
|
const previous = await tx.tbankSyncWork.findUnique({
|
||||||
where: { userId_revision_scopeKey: scope },
|
where: { userId_revision_scopeKey: scope },
|
||||||
@@ -341,15 +333,11 @@ export class CatalogDiscoveryRepository {
|
|||||||
now: Date,
|
now: Date,
|
||||||
) {
|
) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const claimed = await tx.tbankSyncWork.updateMany({
|
const claimed = await claimSyncWork(tx, attempt, {
|
||||||
where: {
|
state: 'complete',
|
||||||
...attempt,
|
progress: {},
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: { state: 'complete', progress: {} },
|
|
||||||
});
|
});
|
||||||
if (!claimed.count) return false;
|
if (!claimed) return false;
|
||||||
for (const instrument of instruments) {
|
for (const instrument of instruments) {
|
||||||
// Discovery can create a minimal identity, but cannot overwrite verified metadata.
|
// Discovery can create a minimal identity, but cannot overwrite verified metadata.
|
||||||
const identity = { provider, uid: instrument.uid };
|
const identity = { provider, uid: instrument.uid };
|
||||||
@@ -461,6 +449,7 @@ export class CatalogDiscoveryRepository {
|
|||||||
|
|
||||||
finish(attempt: TbankSyncAttempt, success: boolean) {
|
finish(attempt: TbankSyncAttempt, success: boolean) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
|
// Fails sibling attempts whose connection was invalidated mid-flight.
|
||||||
await tx.tbankSyncWork.updateMany({
|
await tx.tbankSyncWork.updateMany({
|
||||||
where: {
|
where: {
|
||||||
...attempt,
|
...attempt,
|
||||||
@@ -469,16 +458,9 @@ export class CatalogDiscoveryRepository {
|
|||||||
},
|
},
|
||||||
data: { state: 'failed', errorCode: TbankErrorCode.InvalidToken },
|
data: { state: 'failed', errorCode: TbankErrorCode.InvalidToken },
|
||||||
});
|
});
|
||||||
await tx.tbankSyncWork.updateMany({
|
await claimSyncWork(tx, attempt, {
|
||||||
where: {
|
state: success ? 'complete' : 'failed',
|
||||||
...attempt,
|
errorCode: success ? null : TbankErrorCode.Unavailable,
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
state: success ? 'complete' : 'failed',
|
|
||||||
errorCode: success ? null : TbankErrorCode.Unavailable,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-25
@@ -7,13 +7,14 @@ import { createHash, randomUUID } from 'node:crypto';
|
|||||||
import { isDeepStrictEqual } from 'node:util';
|
import { isDeepStrictEqual } from 'node:util';
|
||||||
|
|
||||||
import type { Prisma } from '../../../../../generated/prisma/client';
|
import type { Prisma } from '../../../../../generated/prisma/client';
|
||||||
import type {
|
|
||||||
TbankOperationItem,
|
|
||||||
TbankOperationsByCursorResponse,
|
|
||||||
} from '../../../../integrations/tbank/tbank.api';
|
|
||||||
|
|
||||||
import { TbankErrorCode } from '../../../../integrations/tbank/tbank-error-code';
|
import { TbankErrorCode } from '../../../../integrations/tbank/tbank-error-code';
|
||||||
import { isTbankProtocolValue, TbankOperationState } from '../../../../integrations/tbank/tbank-protocol';
|
import { isTbankProtocolValue, TbankOperationState } from '../../../../integrations/tbank/tbank-protocol';
|
||||||
|
import { claimSyncWork, lockConnectionGeneration } from '../../../../integrations/tbank/tbank-sync-claim';
|
||||||
|
import {
|
||||||
|
isJoinableRunningAttempt,
|
||||||
|
type TbankOperationItem, type TbankOperationsByCursorResponse,
|
||||||
|
} from '../../../../integrations/tbank/tbank.api';
|
||||||
import { PrismaService } from '../../../../prisma/prisma.service';
|
import { PrismaService } from '../../../../prisma/prisma.service';
|
||||||
import {
|
import {
|
||||||
dateOnlyBounds,
|
dateOnlyBounds,
|
||||||
@@ -268,16 +269,10 @@ export class BrokerageOperationsRepository {
|
|||||||
.update(JSON.stringify([...new Set(intervals.map(intervalKey))].sort()))
|
.update(JSON.stringify([...new Set(intervals.map(intervalKey))].sort()))
|
||||||
.digest('hex');
|
.digest('hex');
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const locked = await tx.tbankConnection.updateMany({
|
const locked = await lockConnectionGeneration(tx, { userId, revision }, {
|
||||||
where: {
|
membershipAt: { not: null },
|
||||||
userId,
|
|
||||||
revision,
|
|
||||||
invalidToken: false,
|
|
||||||
membershipAt: { not: null },
|
|
||||||
},
|
|
||||||
data: { membershipRevision: { increment: 0 } },
|
|
||||||
});
|
});
|
||||||
if (!locked.count) return null;
|
if (!locked) return null;
|
||||||
const accounts = await tx.tbankAccount.findMany({
|
const accounts = await tx.tbankAccount.findMany({
|
||||||
where: { userId, revision },
|
where: { userId, revision },
|
||||||
select: { accountId: true, openedOn: true },
|
select: { accountId: true, openedOn: true },
|
||||||
@@ -379,9 +374,7 @@ export class BrokerageOperationsRepository {
|
|||||||
publicationAtomic && previousIntent && !previousIntent.atomic;
|
publicationAtomic && previousIntent && !previousIntent.atomic;
|
||||||
if (
|
if (
|
||||||
touching.length <= SINGLE_WORK_GROUP &&
|
touching.length <= SINGLE_WORK_GROUP &&
|
||||||
previous?.state === 'running' &&
|
isJoinableRunningAttempt(previous, processRunId, runningAttempts) &&
|
||||||
previous.processRunId === processRunId &&
|
|
||||||
runningAttempts.has(previous.attemptId) &&
|
|
||||||
!upgradeGroup
|
!upgradeGroup
|
||||||
) {
|
) {
|
||||||
const pending = previousIntent?.intervals.map(decodeInterval) ?? [];
|
const pending = previousIntent?.intervals.map(decodeInterval) ?? [];
|
||||||
@@ -933,15 +926,8 @@ async function claim(
|
|||||||
tx: Prisma.TransactionClient,
|
tx: Prisma.TransactionClient,
|
||||||
attempt: OperationAttempt,
|
attempt: OperationAttempt,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const result = await tx.tbankSyncWork.updateMany({
|
const count = await claimSyncWork(tx, attempt, { updatedAt: new Date() });
|
||||||
where: {
|
return count === EXPECTED_UPDATED_WORK_ITEMS;
|
||||||
...attempt,
|
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: { updatedAt: new Date() },
|
|
||||||
});
|
|
||||||
return result.count === EXPECTED_UPDATED_WORK_ITEMS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function decodeInterval(key: string): OperationInterval {
|
function decodeInterval(key: string): OperationInterval {
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import type { Prisma } from '../../../../../generated/prisma/client';
|
|||||||
import type { PortfolioDto } from '../presentation/portfolio.dto';
|
import type { PortfolioDto } from '../presentation/portfolio.dto';
|
||||||
|
|
||||||
import { TbankErrorCode } from '../../../../integrations/tbank/tbank-error-code';
|
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';
|
import { PrismaService } from '../../../../prisma/prisma.service';
|
||||||
|
|
||||||
const EXPECTED_UPDATED_ACCOUNTS = 1;
|
const EXPECTED_UPDATED_ACCOUNTS = 1;
|
||||||
@@ -56,16 +57,10 @@ export class PortfolioRepository {
|
|||||||
force: boolean,
|
force: boolean,
|
||||||
) {
|
) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const current = await tx.tbankConnection.updateMany({
|
const current = await lockConnectionGeneration(tx, { userId, revision }, {
|
||||||
where: {
|
accounts: { some: { accountId } },
|
||||||
userId,
|
|
||||||
revision,
|
|
||||||
invalidToken: false,
|
|
||||||
accounts: { some: { accountId } },
|
|
||||||
},
|
|
||||||
data: { membershipRevision: { increment: 0 } },
|
|
||||||
});
|
});
|
||||||
if (!current.count) return null;
|
if (!current) return null;
|
||||||
const snapshot = await tx.tbankPortfolio.findUnique({
|
const snapshot = await tx.tbankPortfolio.findUnique({
|
||||||
where: { userId_revision_accountId: { userId, revision, accountId } },
|
where: { userId_revision_accountId: { userId, revision, accountId } },
|
||||||
});
|
});
|
||||||
@@ -86,11 +81,7 @@ export class PortfolioRepository {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const scope = portfolioWorkScope;
|
const scope = portfolioWorkScope;
|
||||||
if (
|
if (isJoinableRunningAttempt(previous, processRunId, pending)) {
|
||||||
previous?.state === 'running' &&
|
|
||||||
previous.processRunId === processRunId &&
|
|
||||||
pending.has(previous.attemptId)
|
|
||||||
) {
|
|
||||||
return { work: previous, launch: false };
|
return { work: previous, launch: false };
|
||||||
}
|
}
|
||||||
const data = {
|
const data = {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import type { TbankSyncAttempt } from '../../../integrations/tbank/tbank.api';
|
|||||||
import type { InvestmentAttemptDto } from '../investment-resource.dto';
|
import type { InvestmentAttemptDto } from '../investment-resource.dto';
|
||||||
import type { InvestmentScreenDto } from './tbank-sync.dto';
|
import type { InvestmentScreenDto } from './tbank-sync.dto';
|
||||||
|
|
||||||
|
import { claimSyncWork, lockConnectionGeneration } from '../../../integrations/tbank/tbank-sync-claim';
|
||||||
import { PrismaService } from '../../../prisma/prisma.service';
|
import { PrismaService } from '../../../prisma/prisma.service';
|
||||||
export type ScreenIntent = { now: string; scope: InvestmentScreenDto };
|
export type ScreenIntent = { now: string; scope: InvestmentScreenDto };
|
||||||
|
|
||||||
@@ -88,11 +89,8 @@ export class TbankSyncRepository {
|
|||||||
runId: string,
|
runId: string,
|
||||||
) {
|
) {
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const valid = await tx.tbankConnection.updateMany({
|
const valid = await lockConnectionGeneration(tx, { userId, revision });
|
||||||
where: { userId, revision, invalidToken: false },
|
if (!valid) return null;
|
||||||
data: { membershipRevision: { increment: 0 } },
|
|
||||||
});
|
|
||||||
if (!valid.count) return null;
|
|
||||||
const scopeKey = `screen:${JSON.stringify(scope)}`;
|
const scopeKey = `screen:${JSON.stringify(scope)}`;
|
||||||
const key = { userId, revision, scopeKey };
|
const key = { userId, revision, scopeKey };
|
||||||
const previous = await tx.tbankSyncWork.findUnique({
|
const previous = await tx.tbankSyncWork.findUnique({
|
||||||
@@ -176,17 +174,11 @@ export class TbankSyncRepository {
|
|||||||
...Object.fromEntries(rows.map((row) => [row.id, row.attemptId])),
|
...Object.fromEntries(rows.map((row) => [row.id, row.attemptId])),
|
||||||
...old.attempts,
|
...old.attempts,
|
||||||
};
|
};
|
||||||
return tx.tbankSyncWork.updateMany({
|
const claimed = await claimSyncWork(tx, attempt, {
|
||||||
where: {
|
progress: { children: [...new Set(children)], attempts, dispatched },
|
||||||
...attempt,
|
...(errorCode ? { state: 'failed', errorCode } : {}),
|
||||||
state: 'running',
|
|
||||||
connection: { invalidToken: false },
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
progress: { children: [...new Set(children)], attempts, dispatched },
|
|
||||||
...(errorCode ? { state: 'failed', errorCode } : {}),
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
return { count: claimed };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -364,20 +356,17 @@ export class TbankSyncRepository {
|
|||||||
where: { userId: attempt.userId, id: child.id },
|
where: { userId: attempt.userId, id: child.id },
|
||||||
select: { attemptId: true },
|
select: { attemptId: true },
|
||||||
});
|
});
|
||||||
const updated = await tx.tbankSyncWork.updateMany({
|
const updated = await claimSyncWork(tx, attempt, {
|
||||||
where: { ...attempt, state: 'running' },
|
progress: {
|
||||||
data: {
|
attempts: {
|
||||||
progress: {
|
...progress.attempts,
|
||||||
attempts: {
|
...(row ? { [child.id]: row.attemptId } : {}),
|
||||||
...progress.attempts,
|
|
||||||
...(row ? { [child.id]: row.attemptId } : {}),
|
|
||||||
},
|
|
||||||
children: [...(progress.children ?? []), child],
|
|
||||||
sections: progress.sections ?? [],
|
|
||||||
},
|
},
|
||||||
|
children: [...(progress.children ?? []), child],
|
||||||
|
sections: progress.sections ?? [],
|
||||||
},
|
},
|
||||||
});
|
}, { withoutConnectionFence: true });
|
||||||
return Boolean(updated.count);
|
return Boolean(updated);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user