refactor: route bank card reads through repository (WealthLedger/backend#171)
Backend verify / commits (pull_request) Successful in 1m30s
Backend verify / security (pull_request) Successful in 1m8s
Backend verify / verify (pull_request) Failing after 5m52s

This commit is contained in:
2026-11-04 16:30:45 +03:00
parent 0f65ba7f13
commit 5c7ff312ee
2 changed files with 29 additions and 18 deletions
@@ -31,13 +31,7 @@ export class BankCardsService {
) {}
private async cardView(tx: Tx, card: BankCard): Promise<BankCardDto> {
const rows = await tx.bankCardOperation.findMany({
where: {
userId: card.userId,
deletedAt: null,
OR: [{ cardId: card.id }, { targetCardId: card.id }],
},
});
const rows = await this.repository.cardOperations(tx, card);
return {
id: card.id,
name: card.name,
@@ -53,20 +47,15 @@ export class BankCardsService {
}
list(userId: string, includeArchived = false) {
return this.prisma.$transaction(async (tx) =>
Promise.all(
(
await tx.bankCard.findMany({
where: { userId, ...(includeArchived ? {} : { archivedAt: null }) },
orderBy: [{ createdAt: 'asc' }, { id: 'asc' }],
})
).map((card) => this.cardView(tx, card)),
),
return this.repository.read(async (tx) =>
Promise.all((await this.repository.cards(tx, userId, includeArchived)).map(
(card) => this.cardView(tx, card),
)),
);
}
get(userId: string, id: string) {
return this.prisma.$transaction(async (tx) =>
return this.repository.read(async (tx) =>
this.cardView(tx, await this.repository.card(tx, userId, id)),
);
}
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { randomUUID } from 'node:crypto';
import { Prisma } from '../../../../../generated/prisma/client';
import { BankCard, Prisma } from '../../../../../generated/prisma/client';
import { isPrismaErrorCode, PrismaErrorCode } from '../../../../prisma/prisma-error-code';
import { PrismaService } from '../../../../prisma/prisma.service';
import {
@@ -34,6 +34,28 @@ const seeds = [
@Injectable()
export class BankCardsRepository {
constructor(private readonly prisma: PrismaService) {}
read<T>(run: (tx: Tx) => Promise<T>): Promise<T> {
return this.prisma.$transaction(run);
}
cards(tx: Tx, userId: string, includeArchived = false) {
return tx.bankCard.findMany({
where: { userId, ...(includeArchived ? {} : { archivedAt: null }) },
orderBy: [{ createdAt: 'asc' }, { id: 'asc' }],
});
}
cardOperations(tx: Tx, card: BankCard) {
return tx.bankCardOperation.findMany({
where: {
userId: card.userId,
deletedAt: null,
OR: [{ cardId: card.id }, { targetCardId: card.id }],
},
});
}
async categories(userId: string) {
return (
await this.prisma.expenseCategory.findMany({