Enhance error handling wallets

master
nicekid1 1 month ago
parent 49507ada65
commit 61ac8246ab
  1. 2
      src/app.service.ts
  2. 9
      src/payment/payment.controller.ts
  3. 3
      src/payment/payment.service.ts
  4. 6
      src/users/users.controller.ts
  5. 13
      src/wallet/wallet.controller.ts
  6. 12
      src/wallet/wallet.service.ts

@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable() @Injectable()
export class AppService { export class AppService {
getHello(): string { getHello(): string {
return 'Hello World!'; return 'E-commerce API';
} }
} }

@ -7,6 +7,7 @@ import { InjectModel } from "@nestjs/sequelize";
import { Payment } from "./entities/payment.entity"; import { Payment } from "./entities/payment.entity";
import { JwtAuthGuard } from "src/guard/auth.guard"; import { JwtAuthGuard } from "src/guard/auth.guard";
import { Transaction } from "src/wallet/entities/transaction.entity"; import { Transaction } from "src/wallet/entities/transaction.entity";
import { RoleGuard } from "src/guard/role.guard";
@Controller("payment") @Controller("payment")
export class PaymentController { export class PaymentController {
@ -20,11 +21,14 @@ export class PaymentController {
) {} ) {}
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post("request/:userId") @Post("request")
async requestPayment(@Request() req): Promise<{ url: string }> { async requestPayment(@Request() req) {
const userId = req.user.id; const userId = req.user.id;
const invoice = await this.invoiceService.getInvoicePendingByUser(userId); const invoice = await this.invoiceService.getInvoicePendingByUser(userId);
const totalAmount = invoice.totalPaymentAmount; const totalAmount = invoice.totalPaymentAmount;
if(totalAmount<1000){
return{message:'please enter amount above 1000'}
}
const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}&amount=${totalAmount}`; const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}&amount=${totalAmount}`;
const paymentUrl = await this.paymentService.requestPayment(totalAmount, "Purchase products", callbackUrl); const paymentUrl = await this.paymentService.requestPayment(totalAmount, "Purchase products", callbackUrl);
@ -69,4 +73,5 @@ export class PaymentController {
throw new Error(`Error during payment verification: ${error.message}`); throw new Error(`Error during payment verification: ${error.message}`);
} }
} }
} }

@ -9,7 +9,6 @@ export class PaymentService {
private zarinpal; private zarinpal;
constructor( constructor(
) { ) {
this.zarinpal = this.initializeZarinpal(); this.zarinpal = this.initializeZarinpal();
} }
@ -54,4 +53,6 @@ export class PaymentService {
throw new InternalServerErrorException(`Error in payment verification: ${error.message}`); throw new InternalServerErrorException(`Error in payment verification: ${error.message}`);
} }
} }
} }

@ -46,20 +46,20 @@ export class UsersController {
const userId = req.user.id; const userId = req.user.id;
return this.usersService.editProfile(userId, updateUserDto); return this.usersService.editProfile(userId, updateUserDto);
} }
/////////////////////////////////////admin endpoints///////////////////////////////////////////////////////// /////////////////////////////////////admin access endpoints/////////////////////////////////////////////////////////
//get users list (admin) //get users list (admin)
@UseGuards(RoleGuard) @UseGuards(RoleGuard)
@Get("users") @Get("users")
async findAll(): Promise<User[]> { async findAll(): Promise<User[]> {
return this.usersService.findAll(); return this.usersService.findAll();
} }
// get a specific user info by admin // get a specific user info (admin)
@UseGuards(RoleGuard) @UseGuards(RoleGuard)
@Get("users/:id") @Get("users/:id")
async findSpecificUserInfoByUser(@Param("id") id): Promise<User> { async findSpecificUserInfoByUser(@Param("id") id): Promise<User> {
return this.usersService.findSpecificUserInfoByUser(id); return this.usersService.findSpecificUserInfoByUser(id);
} }
//delete a specific user by admin // delete a specific user (admin)
@UseGuards(RoleGuard) @UseGuards(RoleGuard)
@Delete(":id?") @Delete(":id?")
async deleteUser(@Param("id") id) { async deleteUser(@Param("id") id) {

@ -2,6 +2,7 @@ import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Request,
import { WalletService } from "./wallet.service"; import { WalletService } from "./wallet.service";
import { JwtAuthGuard } from "src/guard/auth.guard"; import { JwtAuthGuard } from "src/guard/auth.guard";
import { PaymentService } from "src/payment/payment.service"; import { PaymentService } from "src/payment/payment.service";
import { RoleGuard } from "src/guard/role.guard";
@Controller("wallet") @Controller("wallet")
export class WalletController { export class WalletController {
@ -30,8 +31,14 @@ export class WalletController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get("transaction") @Get("transaction")
async getTransactionById(@Request() req){ async getTransactionById(@Request() req) {
const userId = req.user.id const userId = req.user.id;
return this.walletService.getTransactionById(userId) return this.walletService.getTransactionById(userId);
}
@UseGuards(RoleGuard)
@Get("transaction/:id")
async getTransactionByIdForAdmin(@Param("id") id: number) {
return this.walletService.getTransactionByIdForAdmin(id);
} }
} }

@ -53,7 +53,7 @@ export class WalletService {
const wallet = await this.walletModel.findOne({ where: { userId } }); const wallet = await this.walletModel.findOne({ where: { userId } });
if (!wallet) { if (!wallet) {
throw new HttpException("Wallet not found", HttpStatus.NOT_FOUND); throw new HttpException("Please Charge your wallet", HttpStatus.NOT_FOUND);
} }
if (wallet.balance < amount) { if (wallet.balance < amount) {
@ -86,4 +86,14 @@ export class WalletService {
where: { walletId: wallet.walletId }, where: { walletId: wallet.walletId },
}); });
} }
//getting transaction a user (admin)
async getTransactionByIdForAdmin(userId: number) {
const wallet = await this.getWalletInfo(userId);
if (!wallet) {
throw new HttpException("Wallet not found for the user.", HttpStatus.NOT_FOUND);
}
return await this.transactionModel.findAll({
where: { walletId: wallet.walletId },
});
}
} }

Loading…
Cancel
Save