diff --git a/src/card/controllers/cards.controller.ts b/src/card/controllers/cards.controller.ts index 385958a..b81fb0e 100644 --- a/src/card/controllers/cards.controller.ts +++ b/src/card/controllers/cards.controller.ts @@ -1,12 +1,13 @@ import { Controller, Get, Post, UseGuards } from '@nestjs/common'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; +import { Roles } from '~/auth/enums'; import { IJwtPayload } from '~/auth/interfaces'; -import { AuthenticatedUser } from '~/common/decorators'; -import { AccessTokenGuard } from '~/common/guards'; +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 { CardResponseDto } from '../dtos/responses'; +import { AccountIbanResponseDto, CardResponseDto } from '../dtos/responses'; import { CardService } from '../services'; @Controller('cards') @@ -36,4 +37,13 @@ export class CardsController { 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)); + } } diff --git a/src/card/dtos/responses/account-iban.response.dto.ts b/src/card/dtos/responses/account-iban.response.dto.ts new file mode 100644 index 0000000..a4e1537 --- /dev/null +++ b/src/card/dtos/responses/account-iban.response.dto.ts @@ -0,0 +1,10 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class AccountIbanResponseDto { + @ApiProperty({ example: 'DE89370400440532013000' }) + iban!: string; + + constructor(iban: string) { + this.iban = iban; + } +} diff --git a/src/card/dtos/responses/index.ts b/src/card/dtos/responses/index.ts index 5f2d4bb..e32607e 100644 --- a/src/card/dtos/responses/index.ts +++ b/src/card/dtos/responses/index.ts @@ -1 +1,2 @@ +export * from './account-iban.response.dto'; export * from './card.response.dto'; diff --git a/src/card/repositories/account.repository.ts b/src/card/repositories/account.repository.ts index 428b109..1e4af12 100644 --- a/src/card/repositories/account.repository.ts +++ b/src/card/repositories/account.repository.ts @@ -34,6 +34,13 @@ export class AccountRepository { }); } + getAccountByCustomerId(customerId: string): Promise { + return this.accountRepository.findOne({ + where: { cards: { customerId } }, + relations: ['cards'], + }); + } + topUpAccountBalance(accountReference: string, amount: number) { return this.accountRepository.increment({ accountReference }, 'balance', amount); } diff --git a/src/card/services/account.service.ts b/src/card/services/account.service.ts index ac9e0dd..fb6e2f3 100644 --- a/src/card/services/account.service.ts +++ b/src/card/services/account.service.ts @@ -27,10 +27,18 @@ export class AccountService { return account; } - async creditAccountBalance(accountReference: string, amount: number) { + creditAccountBalance(accountReference: string, amount: number) { return this.accountRepository.topUpAccountBalance(accountReference, amount); } + async getAccountByCustomerId(customerId: string): Promise { + const account = await this.accountRepository.getAccountByCustomerId(customerId); + if (!account) { + throw new UnprocessableEntityException('ACCOUNT.NOT_FOUND'); + } + return account; + } + async decreaseAccountBalance(accountReference: string, amount: number) { const account = await this.getAccountByReferenceNumber(accountReference); /** diff --git a/src/card/services/card.service.ts b/src/card/services/card.service.ts index c251a5a..854160e 100644 --- a/src/card/services/card.service.ts +++ b/src/card/services/card.service.ts @@ -111,6 +111,11 @@ export class CardService { } } + async getIbanInformation(customerId: string) { + const account = await this.accountService.getAccountByCustomerId(customerId); + return account.iban; + } + @Transactional() async transferToChild(juniorId: string, amount: number) { const card = await this.getCardByCustomerId(juniorId);