mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-28 05:30:55 +00:00
22 lines
1.0 KiB
TypeScript
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 } });
|
|
}
|
|
}
|