|
|
|
@ -1,5 +1,5 @@ |
|
|
|
|
import { Controller, Get, Post, Body, Param, Delete, Query, Put, UseGuards, Request, Patch, HttpException, HttpStatus } from "@nestjs/common"; |
|
|
|
|
import { ProductsService } from "./shop.service"; |
|
|
|
|
import { ShopService } from "./shop.service"; |
|
|
|
|
import { Product } from "./entities/product.entity"; |
|
|
|
|
import { CreateProductDto } from "./dto/products/create-product.dto"; |
|
|
|
|
import { UpdateProductDto } from "./dto/products/update-product.dto"; |
|
|
|
@ -12,9 +12,9 @@ import { Transaction } from "./entities/transaction.entity"; |
|
|
|
|
import { Payment } from "./entities/payment.entity"; |
|
|
|
|
|
|
|
|
|
@Controller("shop") |
|
|
|
|
export class ProductsController { |
|
|
|
|
export class ShopController { |
|
|
|
|
constructor( |
|
|
|
|
private readonly productsService: ProductsService, |
|
|
|
|
private readonly shopService: ShopService, |
|
|
|
|
@InjectModel(Transaction) private readonly transactionModel: typeof Transaction, |
|
|
|
|
@InjectModel(Payment) private readonly paymentModel: typeof Payment, |
|
|
|
|
) {} |
|
|
|
@ -24,7 +24,7 @@ export class ProductsController { |
|
|
|
|
@UseGuards(RoleGuard) |
|
|
|
|
@Post("product") |
|
|
|
|
async create(@Body() createProductDto: CreateProductDto) { |
|
|
|
|
const product = await this.productsService.create(createProductDto); |
|
|
|
|
const product = await this.shopService.create(createProductDto); |
|
|
|
|
return { |
|
|
|
|
message: "Product created successfully!", |
|
|
|
|
product, |
|
|
|
@ -34,18 +34,18 @@ export class ProductsController { |
|
|
|
|
@Get("product") |
|
|
|
|
async findAll(@Query() query: { search?: string; priceMin?: number; priceMax?: number }) { |
|
|
|
|
const { search, priceMin, priceMax } = query; |
|
|
|
|
return this.productsService.findAll(search, priceMin, priceMax); |
|
|
|
|
return this.shopService.findAll(search, priceMin, priceMax); |
|
|
|
|
} |
|
|
|
|
//get a product detail
|
|
|
|
|
@Get("product/:id") |
|
|
|
|
async findOne(@Param("id") id: string) { |
|
|
|
|
return this.productsService.findOne(id); |
|
|
|
|
return this.shopService.findOne(id); |
|
|
|
|
} |
|
|
|
|
//edit a product info (admin)
|
|
|
|
|
@UseGuards(RoleGuard) |
|
|
|
|
@Put("product/:id") |
|
|
|
|
async update(@Param("id") id: string, @Body() updateProductDto: UpdateProductDto) { |
|
|
|
|
const product = await this.productsService.update(id, updateProductDto); |
|
|
|
|
const product = await this.shopService.update(id, updateProductDto); |
|
|
|
|
return { |
|
|
|
|
message: "product updated successful", |
|
|
|
|
product, |
|
|
|
@ -55,7 +55,7 @@ export class ProductsController { |
|
|
|
|
@UseGuards(RoleGuard) |
|
|
|
|
@Delete("product/:id") |
|
|
|
|
async remove(@Param("id") id: string): Promise<{ message: string }> { |
|
|
|
|
return this.productsService.remove(id); |
|
|
|
|
return this.shopService.remove(id); |
|
|
|
|
} |
|
|
|
|
////////////////////////////////////////cart////////////////////////////////////////
|
|
|
|
|
//create and a item to cart (user)
|
|
|
|
@ -63,21 +63,21 @@ export class ProductsController { |
|
|
|
|
@Post("cart") |
|
|
|
|
async createAndAddItemToCart(@Body() addToCartDto: AddToCartDto, @Request() req: any) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return this.productsService.createAndAddItemToCart({ ...addToCartDto, userId }); |
|
|
|
|
return this.shopService.createAndAddItemToCart({ ...addToCartDto, userId }); |
|
|
|
|
} |
|
|
|
|
//get user cart items(user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Get("cart") |
|
|
|
|
async getUserOpenCart(@Request() req: any) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return this.productsService.getUserOpenCart(userId); |
|
|
|
|
return this.shopService.getUserOpenCart(userId); |
|
|
|
|
} |
|
|
|
|
//edit quantity an item in cart (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Patch("cart/:productId") |
|
|
|
|
async updateCart(@Param("productId") productId: number, @Body() updateCartDto: UpdateCartDto, @Request() req: any) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
const updatedCart = await this.productsService.updateCart(userId, productId, updateCartDto.quantity); |
|
|
|
|
const updatedCart = await this.shopService.updateCart(userId, productId, updateCartDto.quantity); |
|
|
|
|
return { |
|
|
|
|
message: "Cart updated successfully", |
|
|
|
|
updatedCart, |
|
|
|
@ -88,14 +88,14 @@ export class ProductsController { |
|
|
|
|
@Delete("cart/:productId") |
|
|
|
|
async removeFromCart(@Param("productId") productId: number, @Request() req: any) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return await this.productsService.removeFromCart(userId, productId); |
|
|
|
|
return await this.shopService.removeFromCart(userId, productId); |
|
|
|
|
} |
|
|
|
|
//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); |
|
|
|
|
return await this.shopService.clearCart(userId); |
|
|
|
|
} |
|
|
|
|
//get checkout process (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
@ -103,8 +103,8 @@ export class ProductsController { |
|
|
|
|
async processOrder(@Request() req: any) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
try { |
|
|
|
|
const totalAmount = (await this.productsService.getUserOpenCart(userId)).totalPrice; |
|
|
|
|
const result = await this.productsService.processOrder(userId, totalAmount); |
|
|
|
|
const totalAmount = (await this.shopService.getUserOpenCart(userId)).totalPrice; |
|
|
|
|
const result = await this.shopService.processOrder(userId, totalAmount); |
|
|
|
|
return result; |
|
|
|
|
} catch (error) { |
|
|
|
|
throw new HttpException(error.message || "Order processing failed.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
@ -116,7 +116,7 @@ export class ProductsController { |
|
|
|
|
@Get("wallet") |
|
|
|
|
async getBalance(@Request() req) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return this.productsService.getBalance(userId); |
|
|
|
|
return this.shopService.getBalance(userId); |
|
|
|
|
} |
|
|
|
|
//charging wallet (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
@ -124,7 +124,7 @@ export class ProductsController { |
|
|
|
|
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.productsService.requestPayment(amount, "Wallet Charge", callbackUrl); |
|
|
|
|
const paymentUrl = this.shopService.requestPayment(amount, "Wallet Charge", callbackUrl); |
|
|
|
|
return paymentUrl; |
|
|
|
|
} |
|
|
|
|
//get transaction (user)
|
|
|
|
@ -132,13 +132,13 @@ export class ProductsController { |
|
|
|
|
@Get("wallet/transaction") |
|
|
|
|
async getTransactionById(@Request() req) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return this.productsService.getTransactionById(userId); |
|
|
|
|
return this.shopService.getTransactionById(userId); |
|
|
|
|
} |
|
|
|
|
//get specific user transaction (admin)
|
|
|
|
|
@UseGuards(RoleGuard) |
|
|
|
|
@Get("wallet/transaction/:id") |
|
|
|
|
async getTransactionByIdForAdmin(@Param("id") id: number) { |
|
|
|
|
return this.productsService.getTransactionByIdForAdmin(id); |
|
|
|
|
return this.shopService.getTransactionByIdForAdmin(id); |
|
|
|
|
} |
|
|
|
|
////////////////////////////////////////payment////////////////////////////////////////
|
|
|
|
|
//payment request
|
|
|
|
@ -146,18 +146,18 @@ export class ProductsController { |
|
|
|
|
@Get("payment/request") |
|
|
|
|
async requestPayment(@Request() req) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
const invoice = await this.productsService.getInvoicePendingByUser(userId); |
|
|
|
|
const invoice = await this.shopService.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.productsService.requestPayment(totalAmount, "Purchase products", callbackUrl); |
|
|
|
|
const callbackUrl = `http://localhost:3000/shop/payment/verify?userId=${userId}&amount=${totalAmount}`; |
|
|
|
|
const paymentUrl = await this.shopService.requestPayment(totalAmount, "Purchase products", callbackUrl); |
|
|
|
|
|
|
|
|
|
return { url: paymentUrl }; |
|
|
|
|
} |
|
|
|
|
//payment verify
|
|
|
|
|
@Get("verify") |
|
|
|
|
@Get("payment/verify") |
|
|
|
|
async verifyPayment(@Query() query: { Authority: string; Status: string; userId: number; amount: number }): Promise<any> { |
|
|
|
|
const { Authority, Status, userId, amount } = query; |
|
|
|
|
|
|
|
|
@ -168,11 +168,11 @@ export class ProductsController { |
|
|
|
|
if (!userId) { |
|
|
|
|
throw new Error("User ID is required."); |
|
|
|
|
} |
|
|
|
|
const wallet = this.productsService.getWalletInfo(userId); |
|
|
|
|
const wallet = this.shopService.getWalletInfo(userId); |
|
|
|
|
try { |
|
|
|
|
const refId = await this.productsService.verifyPayment(Authority, amount); |
|
|
|
|
await this.productsService.addBalance(userId, amount); |
|
|
|
|
const wallet = this.productsService.getWalletInfo(userId); |
|
|
|
|
const refId = await this.shopService.verifyPayment(Authority, amount); |
|
|
|
|
await this.shopService.addBalance(userId, amount); |
|
|
|
|
const wallet = this.shopService.getWalletInfo(userId); |
|
|
|
|
await this.paymentModel.create({ |
|
|
|
|
userId, |
|
|
|
|
walletId: (await wallet).walletId, |
|
|
|
@ -185,7 +185,9 @@ export class ProductsController { |
|
|
|
|
}); |
|
|
|
|
return { message: "Payment successful", refId }; |
|
|
|
|
} catch (error) { |
|
|
|
|
console.log(error); |
|
|
|
|
if(error instanceof HttpException){ |
|
|
|
|
throw error |
|
|
|
|
} |
|
|
|
|
await this.paymentModel.create({ |
|
|
|
|
userId, |
|
|
|
|
walletId: (await wallet).walletId, |
|
|
|
@ -201,19 +203,19 @@ export class ProductsController { |
|
|
|
|
@Get('invoice') |
|
|
|
|
async getInvoiceByUser(@Request() req) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return this.productsService.getInvoiceByUser(userId); |
|
|
|
|
return this.shopService.getInvoiceByUser(userId); |
|
|
|
|
} |
|
|
|
|
//get invoices list (admin)
|
|
|
|
|
@UseGuards(RoleGuard) |
|
|
|
|
@Get('invoice/list') |
|
|
|
|
async getInvoices() { |
|
|
|
|
return this.productsService.getInvoices(); |
|
|
|
|
return this.shopService.getInvoices(); |
|
|
|
|
} |
|
|
|
|
//get specific user invoices
|
|
|
|
|
@UseGuards(RoleGuard) |
|
|
|
|
@Get('invoice/:id') |
|
|
|
|
async getUserInvoice(@Param('id') id:number) { |
|
|
|
|
return this.productsService.getUserInvoices(id); |
|
|
|
|
return this.shopService.getUserInvoices(id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|