Skip to content

Commit

Permalink
✨ Implement bank name update and bank delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Jan 5, 2024
1 parent 2a2d2f6 commit 8238e81
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/banks/banks.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Body, Controller, Delete, Get, HttpStatus, Patch, Post, Req, UseGuards} from "@nestjs/common";
import {Body, Controller, Delete, Get, HttpStatus, Param, Patch, Post, Req, UseGuards} from "@nestjs/common";
import {MaintenanceGuard} from "../maintenance/guards/maintenance.guard";
import {ApiBearerAuth, ApiResponse} from "@nestjs/swagger";
import {AtGuard} from "../auth/guards/at.guard";
import {BanksService} from "./banks.service";
import {BankEntity} from "./models/entities/bank.entity";
import {BankNameDto} from "./models/dto/bank-name.dto";
import {IdDto} from "../models/dto/id.dto";

@Controller("banks")
@UseGuards(MaintenanceGuard)
Expand All @@ -30,27 +31,27 @@ export class BanksController{
@ApiResponse({status: HttpStatus.UNAUTHORIZED, description: "Invalid or missing access token"})
@ApiResponse({status: HttpStatus.NOT_FOUND, description: "User not found"})
@ApiResponse({status: HttpStatus.CONFLICT, description: "Bank already exists"})
async addBank(@Req() req: any, @Body() addBankDto: BankNameDto): Promise<BankEntity>{
return this.banksService.addBank(req.user.id, addBankDto.name);
async addBank(@Req() req: any, @Body() bankNameDto: BankNameDto): Promise<BankEntity>{
return this.banksService.addBank(req.user.id, bankNameDto.name);
}

@Patch(":id/name")
@UseGuards(AtGuard)
@ApiBearerAuth()
@ApiResponse({status: HttpStatus.OK, description: "Bank name updated", type: BankEntity})
@ApiResponse({status: HttpStatus.UNAUTHORIZED, description: "Invalid or missing access token"})
async updateBankName(): Promise<BankEntity>{
// TODO: Update bank name
return null;
@ApiResponse({status: HttpStatus.NOT_FOUND, description: "User or bank not found"})
async updateBankName(@Req() req: any, @Param() idDto: IdDto, @Body() bankNameDto: BankNameDto): Promise<BankEntity>{
return this.banksService.updateBankName(req.user.id, idDto.id, bankNameDto.name);
}

@Delete(":id")
@UseGuards(AtGuard)
@ApiBearerAuth()
@ApiResponse({status: HttpStatus.OK, description: "Bank deleted", type: BankEntity})
@ApiResponse({status: HttpStatus.UNAUTHORIZED, description: "Invalid or missing access token"})
async deleteBank(): Promise<BankEntity>{
// TODO: Delete bank
return null;
@ApiResponse({status: HttpStatus.NOT_FOUND, description: "User or bank not found"})
async deleteBank(@Req() req: any, @Param() idDto: IdDto): Promise<BankEntity>{
return this.banksService.deleteBank(req.user.id, idDto.id);
}
}
47 changes: 47 additions & 0 deletions src/banks/banks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,51 @@ export class BanksService{
bank.name = bankName;
return bank;
}

async updateBankName(userId: number, bankId: number, name: string): Promise<BankEntity>{
if(!await this.usersService.isUserExists(userId))
throw new NotFoundException("User not found");
const bank: BankEntity = await this.prismaService.banks.findUnique({
where: {
id: bankId,
user_id: userId,
}
});
if(!bank)
throw new NotFoundException("User bank not found");
const banks = await this.getBanks(userId);
for(const bank of banks)
if(bank.name === name)
throw new NotFoundException("Bank already exists with this name");
await this.prismaService.banks.update({
where: {
id: bankId,
},
data: {
name: this.encryptionService.encryptSymmetric(name, this.configService.get("SYMMETRIC_ENCRYPTION_KEY")),
}
});
bank.name = name;
return bank;
}

async deleteBank(userId: number, bankId: number): Promise<BankEntity>{
if(!await this.usersService.isUserExists(userId))
throw new NotFoundException("User not found");
const bank: BankEntity = await this.prismaService.banks.findUnique({
where: {
id: bankId,
user_id: userId,
}
});
if(!bank)
throw new NotFoundException("User bank not found");
await this.prismaService.banks.delete({
where: {
id: bankId,
}
});
bank.name = this.encryptionService.decryptSymmetric(bank.name, this.configService.get("SYMMETRIC_ENCRYPTION_KEY"));
return bank;
}
}

0 comments on commit 8238e81

Please sign in to comment.