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 } }); } }