import { Controller, ForbiddenException, Get, UseGuards } from '@nestjs/common'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; import { DriverCreditService, CREDIT_FLOOR } from './driver-credit.service'; import { DriversService } from '../drivers/drivers.service'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { CurrentUser, AuthUser } from '../auth/decorators/current-user.decorator'; @ApiTags('credit') @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Controller('credit') export class CreditController { constructor( private readonly credit: DriverCreditService, private readonly drivers: DriversService, ) {} /** رصيدي التشغيلي — يعرضه تطبيق السائق (docs/18 — J8). */ @Get() async mine(@CurrentUser() user: AuthUser) { const driver = await this.drivers.findByUser(user.tenantId, user.userId); if (!driver) throw new ForbiddenException('Not a driver'); const c = await this.credit.get(user.tenantId, driver.id); const floor = CREDIT_FLOOR[c.currency] ?? 0; const balance = Number(c.balance); return { balance, currency: c.currency, floor, inDebt: balance < 0, blocked: balance < floor, }; } @Get('transactions') async history(@CurrentUser() user: AuthUser) { const driver = await this.drivers.findByUser(user.tenantId, user.userId); if (!driver) throw new ForbiddenException('Not a driver'); return this.credit.history(user.tenantId, driver.id); } }