fix bug in wallet transaction
This commit is contained in:
@@ -18,6 +18,7 @@ import { UserWallet } from '../entities/user-wallet.entity';
|
||||
import { UserWalletRepository } from '../repositories/user-wallet.repository';
|
||||
import { WalletService } from './wallet.service';
|
||||
import { WalletTransactionReason, WalletTransactionType } from '../interface/wallet';
|
||||
import { WalletTransaction } from '../entities/wallet-transaction.entity';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
@@ -322,12 +323,6 @@ export class UserService {
|
||||
throw new BadRequestException('Points to convert must be greater than 0.');
|
||||
}
|
||||
|
||||
if (userWallet.points < pointsToConvert) {
|
||||
throw new BadRequestException(
|
||||
`Insufficient points. Available: ${userWallet.points}, Requested: ${pointsToConvert}.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (!restaurant.score) {
|
||||
throw new BadRequestException('Restaurant score not found.');
|
||||
}
|
||||
@@ -341,15 +336,19 @@ export class UserService {
|
||||
(Number(pointsToConvert) * Number(restaurant.score.purchaseAmount)) / Number(restaurant.score.purchaseScore);
|
||||
// Convert points to wallet (1:1 ratio - can be made configurable)
|
||||
|
||||
this.walletService.createTransaction(em, userId, restId, {
|
||||
const walletTransaction = em.create(WalletTransaction, {
|
||||
userWallet: userWallet,
|
||||
amount: walletIncreaseAmount,
|
||||
type: WalletTransactionType.CREDIT,
|
||||
reason: WalletTransactionReason.CONVERT_SCORE_TO_WALLET,
|
||||
balance: userWallet.wallet + walletIncreaseAmount,
|
||||
});
|
||||
// Update user's wallet and points
|
||||
userWallet.wallet = (userWallet.wallet || 0) + walletIncreaseAmount;
|
||||
userWallet.points = (userWallet.points || 0) - pointsToConvert;
|
||||
|
||||
em.persist([userWallet, walletTransaction]);
|
||||
|
||||
await em.flush();
|
||||
|
||||
return user;
|
||||
|
||||
Reference in New Issue
Block a user