refactor: move bank card queries into repository (WealthLedger/backend#171)
This commit is contained in:
@@ -13,6 +13,7 @@ import { BankOperationKind } from '../domain/bank-operation-kind';
|
||||
|
||||
const INITIAL_RETRY_COUNT = 0;
|
||||
const LAST_SEED_OFFSET = 1;
|
||||
const EXPECTED_UPDATED_CARDS = 1;
|
||||
const EXPECTED_UPDATED_CATEGORIES = 1;
|
||||
const VERSION_INCREMENT = 1;
|
||||
|
||||
@@ -56,6 +57,40 @@ export class BankCardsRepository {
|
||||
});
|
||||
}
|
||||
|
||||
cardById(tx: Tx, id: string) {
|
||||
return tx.bankCard.findUnique({ where: { id } });
|
||||
}
|
||||
|
||||
createCard(tx: Tx, data: Prisma.BankCardUncheckedCreateInput) {
|
||||
return tx.bankCard.create({ data });
|
||||
}
|
||||
|
||||
async updateCard(
|
||||
tx: Tx,
|
||||
userId: string,
|
||||
id: string,
|
||||
version: number,
|
||||
data: Prisma.BankCardUpdateManyMutationInput,
|
||||
) {
|
||||
const changed = await tx.bankCard.updateMany({
|
||||
where: { id, userId, version },
|
||||
data,
|
||||
});
|
||||
if (changed.count !== EXPECTED_UPDATED_CARDS) conflict();
|
||||
return this.card(tx, userId, id);
|
||||
}
|
||||
|
||||
hasOperationsBefore(tx: Tx, userId: string, id: string, openingDate: string) {
|
||||
return tx.bankCardOperation.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
deletedAt: null,
|
||||
date: { lt: openingDate },
|
||||
OR: [{ cardId: id }, { targetCardId: id }],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async categories(userId: string) {
|
||||
return (
|
||||
await this.prisma.expenseCategory.findMany({
|
||||
|
||||
Reference in New Issue
Block a user