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()
export class AppService {
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 { JwtAuthGuard } from "src/guard/auth.guard";
import { Transaction } from "src/wallet/entities/transaction.entity";
import { RoleGuard } from "src/guard/role.guard";
@Controller("payment")
export class PaymentController {
@ -20,11 +21,14 @@ export class PaymentController {
) {}
@UseGuards(JwtAuthGuard)
@Post("request/:userId")
async requestPayment(@Request() req): Promise<{ url: string }> {
@Post("request")
async requestPayment(@Request() req) {
const userId = req.user.id;
const invoice = await this.invoiceService.getInvoicePendingByUser(userId);
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 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}`);
}
}
}

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

@ -46,20 +46,20 @@ export class UsersController {
const userId = req.user.id;
return this.usersService.editProfile(userId, updateUserDto);
}
/////////////////////////////////////admin endpoints/////////////////////////////////////////////////////////
/////////////////////////////////////admin access endpoints/////////////////////////////////////////////////////////
//get users list (admin)
@UseGuards(RoleGuard)
@Get("users")
async findAll(): Promise<User[]> {
return this.usersService.findAll();
}
// get a specific user info by admin
// get a specific user info (admin)
@UseGuards(RoleGuard)
@Get("users/:id")
async findSpecificUserInfoByUser(@Param("id") id): Promise<User> {
return this.usersService.findSpecificUserInfoByUser(id);
}
//delete a specific user by admin
// delete a specific user (admin)
@UseGuards(RoleGuard)
@Delete(":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 { JwtAuthGuard } from "src/guard/auth.guard";
import { PaymentService } from "src/payment/payment.service";
import { RoleGuard } from "src/guard/role.guard";
@Controller("wallet")
export class WalletController {
@ -30,8 +31,14 @@ export class WalletController {
@UseGuards(JwtAuthGuard)
@Get("transaction")
async getTransactionById(@Request() req){
const userId = req.user.id
return this.walletService.getTransactionById(userId)
async getTransactionById(@Request() req) {
const userId = req.user.id;
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 } });
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) {
@ -86,4 +86,14 @@ export class WalletService {
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