chore: strengthen backend eslint profile
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { tbankSourceNeedsRefresh } from './tbank-source.policy';
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
@@ -6,16 +5,19 @@ import {
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { supportedTbankAccounts } from './tbank-account.policy';
|
||||
import { TbankTokenService } from './tbank-token.service';
|
||||
|
||||
import type { TbankAccount } from './tbank.client';
|
||||
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { supportedTbankAccounts } from './tbank-account.policy';
|
||||
import { tbankSourceNeedsRefresh } from './tbank-source.policy';
|
||||
import { TbankTokenService } from './tbank-token.service';
|
||||
|
||||
export type TbankSyncAttempt = {
|
||||
userId: string;
|
||||
revision: string;
|
||||
id: string;
|
||||
attemptId: string;
|
||||
id: string;
|
||||
revision: string;
|
||||
userId: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
@@ -48,8 +50,9 @@ export class TbankAccessService {
|
||||
connection.membershipAt,
|
||||
new Date(),
|
||||
)
|
||||
)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const scope = { userId, revision, scopeKey: 'membership' };
|
||||
const previous = await tx.tbankSyncWork.findUnique({
|
||||
where: { userId_revision_scopeKey: scope },
|
||||
@@ -58,8 +61,9 @@ export class TbankAccessService {
|
||||
previous?.state === 'running' &&
|
||||
previous.processRunId === processRunId &&
|
||||
active.has(previous.attemptId)
|
||||
)
|
||||
) {
|
||||
return { work: previous, launch: false };
|
||||
}
|
||||
const data = {
|
||||
attemptId: randomUUID(),
|
||||
processRunId,
|
||||
@@ -100,7 +104,7 @@ export class TbankAccessService {
|
||||
return result.count === 1;
|
||||
}
|
||||
|
||||
async tokenForAttempt(attempt: TbankSyncAttempt): Promise<string | null> {
|
||||
async tokenForAttempt(attempt: TbankSyncAttempt): Promise<null | string> {
|
||||
const work = await this.prisma.tbankSyncWork.findFirst({
|
||||
where: {
|
||||
...attempt,
|
||||
@@ -119,8 +123,9 @@ export class TbankAccessService {
|
||||
completeWork = false,
|
||||
): Promise<boolean> {
|
||||
const supported = supportedTbankAccounts(accounts);
|
||||
if (new Set(supported.map(({ id }) => id)).size !== supported.length)
|
||||
if (new Set(supported.map(({ id }) => id)).size !== supported.length) {
|
||||
throw new Error('Duplicate T-Bank account identity');
|
||||
}
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
// Acquire the write fence before reading membership. Replacement and
|
||||
// attempt supersession cannot interleave with the publication below.
|
||||
@@ -146,6 +151,7 @@ export class TbankAccessService {
|
||||
const changed =
|
||||
previous.length !== next.length ||
|
||||
previous.some((account, index) => {
|
||||
// eslint-disable-next-line security/detect-object-injection -- The index comes from the paired account array iteration.
|
||||
const incoming = next[index];
|
||||
return (
|
||||
account.accountId !== incoming.accountId ||
|
||||
@@ -181,11 +187,12 @@ export class TbankAccessService {
|
||||
membershipRevision: { increment: changed ? 1 : 0 },
|
||||
},
|
||||
});
|
||||
if (completeWork)
|
||||
if (completeWork) {
|
||||
await tx.tbankSyncWork.update({
|
||||
where: { id: attempt.id },
|
||||
data: { state: 'complete', progress: { accounts: supported.length } },
|
||||
});
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
@@ -224,17 +231,20 @@ export class TbankAccessService {
|
||||
accounts: { orderBy: { upstreamOrder: 'asc' } },
|
||||
},
|
||||
});
|
||||
if (!connection)
|
||||
if (!connection) {
|
||||
throw new ConflictException({ code: 'TBANK_NOT_CONNECTED' });
|
||||
if (connection.invalidToken)
|
||||
}
|
||||
if (connection.invalidToken) {
|
||||
throw new BadRequestException({ code: 'INVALID_TBANK_TOKEN' });
|
||||
}
|
||||
if (
|
||||
connection.membershipAt &&
|
||||
accountId &&
|
||||
accountId !== 'all' &&
|
||||
!connection.accounts.some((account) => account.accountId === accountId)
|
||||
)
|
||||
) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user