From e18858fd60b6005a2325d09f4f28a4971f9d4467 Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Tue, 31 Dec 2024 16:07:34 +0330 Subject: [PATCH] Add functionality to add balance to user's wallet in wallet module --- src/wallet/add-balance-response.interface.ts | 4 +++ src/wallet/wallet.controller.ts | 8 ++++++ src/wallet/wallet.service.ts | 28 +++++++++++++++----- 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 src/wallet/add-balance-response.interface.ts diff --git a/src/wallet/add-balance-response.interface.ts b/src/wallet/add-balance-response.interface.ts new file mode 100644 index 0000000..3cd0b52 --- /dev/null +++ b/src/wallet/add-balance-response.interface.ts @@ -0,0 +1,4 @@ +export interface AddBalanceResponse { + message: string; + balance: number; +} diff --git a/src/wallet/wallet.controller.ts b/src/wallet/wallet.controller.ts index e2bff43..18dc9b8 100644 --- a/src/wallet/wallet.controller.ts +++ b/src/wallet/wallet.controller.ts @@ -1,5 +1,6 @@ import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; import { WalletService } from './wallet.service'; +import { AddBalanceResponse } from './add-balance-response.interface'; @Controller('wallet') export class WalletController { @@ -8,4 +9,11 @@ export class WalletController { async getBalance(@Param('userId') userId: number): Promise { return this.walletService.getBalance(userId); } + @Post(':userId/add') + async addBalance( + @Param('userId') userId: number, + @Body('amount') amount: number + ): Promise { + return this.walletService.addBalance(userId, amount); + } } diff --git a/src/wallet/wallet.service.ts b/src/wallet/wallet.service.ts index 86751b1..2656832 100644 --- a/src/wallet/wallet.service.ts +++ b/src/wallet/wallet.service.ts @@ -1,15 +1,31 @@ -import { Injectable } from '@nestjs/common'; -import { InjectModel } from '@nestjs/sequelize'; -import { Wallet } from './entities/wallet.entity'; +import { Injectable } from "@nestjs/common"; +import { InjectModel } from "@nestjs/sequelize"; +import { Wallet } from "./entities/wallet.entity"; +import { HttpException, HttpStatus } from "@nestjs/common"; +import { AddBalanceResponse } from "./add-balance-response.interface"; @Injectable() export class WalletService { - constructor( - @InjectModel(Wallet) private walletModel: typeof Wallet, - ) {} + constructor(@InjectModel(Wallet) private walletModel: typeof Wallet) {} async getBalance(userId: number): Promise { const wallet = await this.walletModel.findOne({ where: { userId } }); return wallet ? wallet.balance : 0; } + async addBalance(userId: number, amount: number): Promise { + try { + const wallet = await this.walletModel.findOne({ where: { userId } }); + + if (wallet) { + wallet.balance += Number(amount); + await wallet.save(); + return { message: "Balance updated successfully.", balance: wallet.balance }; + } else { + const newWallet = await this.walletModel.create({ userId, balance: amount }); + return { message: "Wallet created and balance added successfully.", balance: newWallet.balance }; + } + } catch (error) { + throw new HttpException("An error occurred while adding balance to the wallet.", HttpStatus.INTERNAL_SERVER_ERROR); + } + } }