diff --git a/src/transactions/transactions.controller.ts b/src/transactions/transactions.controller.ts new file mode 100644 index 0000000..3392d56 --- /dev/null +++ b/src/transactions/transactions.controller.ts @@ -0,0 +1,48 @@ +import { BadRequestException, Body, Controller, Delete, Get, Param, ParseUUIDPipe, Patch, Post, Query } from '@nestjs/common'; +import { CurrentSession } from '../auth/session.decorator'; +import type { AuthenticatedSession } from '../auth/tenant.guard'; +import { CreateTransactionDto, UpdateTransactionDto } from './dto'; +import { TransactionsService } from './transactions.service'; + +@Controller('transactions') +export class TransactionsController { + constructor(private readonly transactionsService: TransactionsService) {} + + @Get() + list( + @CurrentSession() session: AuthenticatedSession, + @Query('organizationId') organizationId?: string, + ) { + if (organizationId) { + try { new (require('crypto').randomUUID)(); } catch { /* ok */ } + if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(organizationId)) { + throw new BadRequestException('organizationId must be a valid UUID'); + } + } + return this.transactionsService.list(session, organizationId); + } + + @Get(':id') + getById(@CurrentSession() session: AuthenticatedSession, @Param('id', ParseUUIDPipe) id: string) { + return this.transactionsService.getById(session, id); + } + + @Post() + create(@CurrentSession() session: AuthenticatedSession, @Body() dto: CreateTransactionDto) { + return this.transactionsService.create(session, dto); + } + + @Patch(':id') + update( + @CurrentSession() session: AuthenticatedSession, + @Param('id', ParseUUIDPipe) id: string, + @Body() dto: UpdateTransactionDto, + ) { + return this.transactionsService.update(session, id, dto); + } + + @Delete(':id') + softDelete(@CurrentSession() session: AuthenticatedSession, @Param('id', ParseUUIDPipe) id: string) { + return this.transactionsService.softDelete(session, id); + } +}