From 5c7ff312ee4704747a58deacb0f17a284bf1b429 Mon Sep 17 00:00:00 2001 From: Sergey Krylov Date: Wed, 4 Nov 2026 16:30:45 +0300 Subject: [PATCH] refactor: route bank card reads through repository (WealthLedger/backend#171) --- .../application/bank-cards.service.ts | 23 +++++------------- .../infrastructure/bank-cards.repository.ts | 24 ++++++++++++++++++- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/modules/banking/bank-cards/application/bank-cards.service.ts b/src/modules/banking/bank-cards/application/bank-cards.service.ts index e1e6d74..a8c4bd6 100644 --- a/src/modules/banking/bank-cards/application/bank-cards.service.ts +++ b/src/modules/banking/bank-cards/application/bank-cards.service.ts @@ -31,13 +31,7 @@ export class BankCardsService { ) {} private async cardView(tx: Tx, card: BankCard): Promise { - 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)), ); } diff --git a/src/modules/banking/bank-cards/infrastructure/bank-cards.repository.ts b/src/modules/banking/bank-cards/infrastructure/bank-cards.repository.ts index 488dd68..8856aaf 100644 --- a/src/modules/banking/bank-cards/infrastructure/bank-cards.repository.ts +++ b/src/modules/banking/bank-cards/infrastructure/bank-cards.repository.ts @@ -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(run: (tx: Tx) => Promise): Promise { + 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({ -- 2.54.0