You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.5 KiB
98 lines
3.5 KiB
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"; |
|
import { Transaction } from "./entities/transaction.entity"; |
|
|
|
@Injectable() |
|
export class WalletService { |
|
constructor( |
|
@InjectModel(Wallet) private walletModel: typeof Wallet, |
|
@InjectModel(Transaction) private transactionModel: typeof Transaction, |
|
) {} |
|
//get wallet info |
|
async getWalletInfo(userId: number) { |
|
const wallet = await this.walletModel.findOne({ where: { userId } }); |
|
|
|
if (!wallet) { |
|
const newWallet = await this.walletModel.create({ userId, balance: 0 }); |
|
return { walletId: newWallet.id, userId: newWallet.userId, balance: newWallet.balance }; |
|
} |
|
|
|
return { walletId: wallet.id, userId: wallet.userId, balance: wallet.balance }; |
|
} |
|
//get wallet balance |
|
async getBalance(userId: number) { |
|
const wallet = await this.walletModel.findOne({ where: { userId } }); |
|
|
|
if (!wallet) { |
|
throw new HttpException("Wallet not found!", HttpStatus.NOT_FOUND); |
|
} |
|
|
|
return { balance: wallet.balance }; |
|
} |
|
//charge balance of wallet by user |
|
async addBalance(userId: number, amount: number): Promise<AddBalanceResponse> { |
|
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); |
|
} |
|
} |
|
//process of payment |
|
async processPayment(userId: number, amount: number): Promise<string> { |
|
const wallet = await this.walletModel.findOne({ where: { userId } }); |
|
|
|
if (!wallet) { |
|
throw new HttpException("Please Charge your wallet", HttpStatus.NOT_FOUND); |
|
} |
|
|
|
if (wallet.balance < amount) { |
|
throw new HttpException("Insufficient funds", HttpStatus.BAD_REQUEST); |
|
} |
|
try { |
|
wallet.balance -= amount; |
|
|
|
await this.transactionModel.create({ |
|
walletId: wallet.id, |
|
amount: `-${amount}`, |
|
}); |
|
|
|
await wallet.save(); |
|
|
|
return "Payment processed successfully"; |
|
} catch (error) { |
|
console.error("Error processing payment:", error.message); |
|
throw new HttpException("An error occurred while processing the payment.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
} |
|
} |
|
//getting transaction |
|
async getTransactionById(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 }, |
|
}); |
|
} |
|
//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 }, |
|
}); |
|
} |
|
}
|
|
|