|
|
|
@ -7,10 +7,15 @@ import { RoleGuard } from "src/guard/role.guard"; |
|
|
|
|
import { AddToCartDto } from "./dto/cart/add-to-cart.dto"; |
|
|
|
|
import { JwtAuthGuard } from "src/guard/auth.guard"; |
|
|
|
|
import { UpdateCartDto } from "./dto/cart/update-cart.dto"; |
|
|
|
|
import { PaymentService } from "src/payment/payment.service"; |
|
|
|
|
|
|
|
|
|
@Controller("shop") |
|
|
|
|
export class ProductsController { |
|
|
|
|
constructor(private readonly productsService: ProductsService) {} |
|
|
|
|
constructor( |
|
|
|
|
private readonly productsService: ProductsService, |
|
|
|
|
private paymentService: PaymentService, |
|
|
|
|
|
|
|
|
|
) {} |
|
|
|
|
|
|
|
|
|
////////////////////////////////////////products////////////////////////////////////////
|
|
|
|
|
//create a new product (admin)
|
|
|
|
@ -50,22 +55,22 @@ export class ProductsController { |
|
|
|
|
async remove(@Param("id") id: string): Promise<{ message: string }> { |
|
|
|
|
return this.productsService.remove(id); |
|
|
|
|
} |
|
|
|
|
////////////////////////////////////////////cart///////////////////////////////////////////////////
|
|
|
|
|
//create and a item to cart by user
|
|
|
|
|
////////////////////////////////////////cart////////////////////////////////////////
|
|
|
|
|
//create and a item to cart (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Post("cart") |
|
|
|
|
async createAndAddItemToCart(@Body() addToCartDto: AddToCartDto, @Request() req: any) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return this.productsService.createAndAddItemToCart({ ...addToCartDto, userId }); |
|
|
|
|
} |
|
|
|
|
//get user cart items
|
|
|
|
|
//get user cart items(user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Get("cart") |
|
|
|
|
async getUserOpenCart(@Request() req: any) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return this.productsService.getUserOpenCart(userId); |
|
|
|
|
} |
|
|
|
|
//edit quantity an item in cart by user
|
|
|
|
|
//edit quantity an item in cart (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Patch("cart/:productId") |
|
|
|
|
async updateCart(@Param("productId") productId: number, @Body() updateCartDto: UpdateCartDto, @Request() req: any) { |
|
|
|
@ -76,31 +81,63 @@ export class ProductsController { |
|
|
|
|
updatedCart, |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
//delete an item from cart by user
|
|
|
|
|
//delete an item from cart (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Delete("cart/:productId") |
|
|
|
|
async removeFromCart(@Param("productId") productId: number, @Request() req: any) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return await this.productsService.removeFromCart(userId, productId); |
|
|
|
|
} |
|
|
|
|
//clear whole cart by user
|
|
|
|
|
//clear whole cart (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Get("cart/clear-cart") |
|
|
|
|
async clearCart(@Request() req: any) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return await this.productsService.clearCart(userId); |
|
|
|
|
} |
|
|
|
|
//get checkout process
|
|
|
|
|
//get checkout process (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Get("cart/checkout") |
|
|
|
|
async processOrder(@Request() req: any){ |
|
|
|
|
async processOrder(@Request() req: any) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
try { |
|
|
|
|
const totalAmount = (await this.productsService.getUserOpenCart(userId)).totalPrice |
|
|
|
|
const totalAmount = (await this.productsService.getUserOpenCart(userId)).totalPrice; |
|
|
|
|
const result = await this.productsService.processOrder(userId, totalAmount); |
|
|
|
|
return result; |
|
|
|
|
} catch (error) { |
|
|
|
|
throw new HttpException(error.message || "Order processing failed.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
////////////////////////////////////////wallet////////////////////////////////////////
|
|
|
|
|
//getting wallet balance (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Get('wallet') |
|
|
|
|
async getBalance(@Request() req) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return this.productsService.getBalance(userId); |
|
|
|
|
} |
|
|
|
|
//charging wallet (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Post("wallet/charge") |
|
|
|
|
async addBalance(@Body("amount") amount: number, @Request() req) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}&amount=${amount}`; |
|
|
|
|
const paymentUrl = this.paymentService.requestPayment(amount, "Wallet Charge", callbackUrl); |
|
|
|
|
return paymentUrl; |
|
|
|
|
} |
|
|
|
|
//get transaction (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Get("wallet/transaction") |
|
|
|
|
async getTransactionById(@Request() req) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return this.productsService.getTransactionById(userId); |
|
|
|
|
} |
|
|
|
|
//get specific user transaction (admin)
|
|
|
|
|
@UseGuards(RoleGuard) |
|
|
|
|
@Get("wallet/transaction/:id") |
|
|
|
|
async getTransactionByIdForAdmin(@Param("id") id: number) { |
|
|
|
|
return this.productsService.getTransactionByIdForAdmin(id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|