Files
edr-platform/apps/edr-passenger-api/src/modules/wallet/wallet.service.ts
2026-05-13 16:58:49 +03:00

22 lines
1.0 KiB
TypeScript

import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../common/prisma.service';
@Injectable()
export class WalletService {
constructor(private prisma: PrismaService) {}
async getWallet(passengerId: string) {
const wallet = await this.prisma.walletAccount.findUnique({ where: { passengerId }, include: { ledger: { orderBy: { createdAt: 'desc' }, take: 20 } } });
if (!wallet) throw new NotFoundException('Wallet not found');
return wallet;
}
async topUp(passengerId: string, amountMinor: number, description = 'Top-up') {
const wallet = await this.prisma.walletAccount.findUnique({ where: { passengerId } });
if (!wallet) throw new NotFoundException('Wallet not found');
const newBalance = wallet.balanceMinor + amountMinor;
await this.prisma.walletAccount.update({ where: { passengerId }, data: { balanceMinor: newBalance } });
return this.prisma.walletLedgerEntry.create({ data: { walletId: wallet.id, type: 'CREDIT', amountMinor, balanceAfterMinor: newBalance, description } });
}
}