refactor: share sync reservation join rule (WealthLedger/backend#119)
Backend verify / commits (pull_request) Successful in 1m32s
Backend verify / security (pull_request) Successful in 1m25s
Backend verify / verify (pull_request) Successful in 6m30s

This commit is contained in:
2026-11-03 22:32:35 +03:00
parent bade5fae1c
commit 1bc254ae80
5 changed files with 129 additions and 17 deletions
+17 -5
View File
@@ -25,6 +25,22 @@ export type TbankSyncAttempt = {
userId: string;
};
// Join rule shared by sync reservations: the same process joins its own running attempt
// only while that attempt is still active in memory. Set and Map both satisfy `has`.
export function isJoinableRunningAttempt<
T extends { attemptId: string; processRunId: string; state: string },
>(
previous: null | T,
processRunId: string,
active: { has(attemptId: string): boolean },
): previous is T {
return (
previous?.state === 'running' &&
previous.processRunId === processRunId &&
active.has(previous.attemptId)
);
}
@Injectable()
export class TbankAccessService {
constructor(
@@ -62,11 +78,7 @@ export class TbankAccessService {
const previous = await tx.tbankSyncWork.findUnique({
where: { userId_revision_scopeKey: scope },
});
if (
previous?.state === 'running' &&
previous.processRunId === processRunId &&
active.has(previous.attemptId)
) {
if (isJoinableRunningAttempt(previous, processRunId, active)) {
return { work: previous, launch: false };
}
const data = {