import { Body, Controller, Get, HttpCode, HttpStatus, Param, Post, UseGuards } from '@nestjs/common'; import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'; import { Roles } from '~/auth/enums'; import { IJwtPayload } from '~/auth/interfaces'; import { AllowedRoles, AuthenticatedUser } from '~/common/decorators'; import { AccessTokenGuard, RolesGuard } from '~/common/guards'; import { CardEmbossingDetailsResponseDto } from '~/common/modules/neoleap/dtos/response'; import { ApiDataResponse } from '~/core/decorators'; import { ResponseFactory } from '~/core/utils'; import { FundIbanRequestDto } from '../dtos/requests'; import { AccountIbanResponseDto, CardResponseDto, ChildCardResponseDto } from '../dtos/responses'; import { CardService } from '../services'; @Controller('cards') @ApiBearerAuth() @ApiTags('Cards') @UseGuards(AccessTokenGuard) export class CardsController { constructor(private readonly cardService: CardService) {} @Post() @ApiDataResponse(CardResponseDto) async createCard(@AuthenticatedUser() { sub }: IJwtPayload) { const card = await this.cardService.createCard(sub); return ResponseFactory.data(new CardResponseDto(card)); } @Get('child-cards') @UseGuards(RolesGuard) @AllowedRoles(Roles.GUARDIAN) @ApiDataResponse(ChildCardResponseDto) async getChildCards(@AuthenticatedUser() { sub }: IJwtPayload) { const cards = await this.cardService.getChildCards(sub); return ResponseFactory.data(cards.map((card) => new ChildCardResponseDto(card))); } @Get('child-cards/:childid') @UseGuards(RolesGuard) @AllowedRoles(Roles.GUARDIAN) @ApiDataResponse(ChildCardResponseDto) async getChildCardById(@Param('childid') childId: string, @AuthenticatedUser() { sub }: IJwtPayload) { const card = await this.cardService.getCardByChildId(sub, childId); return ResponseFactory.data(new ChildCardResponseDto(card)); } @Get('child-cards/:cardid/embossing-details') @UseGuards(RolesGuard) @AllowedRoles(Roles.GUARDIAN) @ApiDataResponse(CardEmbossingDetailsResponseDto) async getChildCardEmbossingDetails(@Param('cardid') cardId: string, @AuthenticatedUser() { sub }: IJwtPayload) { const res = await this.cardService.getChildCardEmbossingInformation(cardId, sub); return ResponseFactory.data(res); } @Get('current') @ApiDataResponse(CardResponseDto) async getCurrentCard(@AuthenticatedUser() { sub }: IJwtPayload) { const card = await this.cardService.getCardByCustomerId(sub); return ResponseFactory.data(new CardResponseDto(card)); } @Get('embossing-details') @ApiDataResponse(CardEmbossingDetailsResponseDto) async getCardById(@AuthenticatedUser() { sub }: IJwtPayload) { const res = await this.cardService.getEmbossingInformation(sub); return ResponseFactory.data(res); } @Get('iban') @UseGuards(RolesGuard) @AllowedRoles(Roles.GUARDIAN) @ApiDataResponse(AccountIbanResponseDto) async getCardIban(@AuthenticatedUser() { sub }: IJwtPayload) { const iban = await this.cardService.getIbanInformation(sub); return ResponseFactory.data(new AccountIbanResponseDto(iban)); } @Post('mock/fund-iban') @ApiOperation({ summary: 'Mock endpoint to fund the IBAN - For testing purposes only' }) @UseGuards(RolesGuard) @AllowedRoles(Roles.GUARDIAN) @HttpCode(HttpStatus.NO_CONTENT) fundIban(@Body() { amount, iban }: FundIbanRequestDto) { return this.cardService.fundIban(iban, amount); } }