wallet
This commit is contained in:
@@ -10,14 +10,16 @@ import { UpdateUserAddressDto } from '../dto/update-user-address.dto';
|
||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||
import { UserRepository } from '../repositories/user.repository';
|
||||
import { normalizePhone } from '../../utils/phone.util';
|
||||
import { ConvertScoreToWalletDto } from '../dto/convert-score-to-wallet.dto';
|
||||
import { RestRepository } from 'src/modules/restaurants/repositories/rest.repository';
|
||||
import { UserWallet } from '../entities/user-wallet.entity';
|
||||
import { UserWalletRepository } from '../repositories/user-wallet.repository';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly restaurantRepository: RestRepository,
|
||||
private readonly userWalletRepository: UserWalletRepository,
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
@@ -85,7 +87,7 @@ export class UserService {
|
||||
return user;
|
||||
}
|
||||
|
||||
async updateUser(userId: string, dto: UpdateUserDto): Promise<User> {
|
||||
async updateUser(userId: string, restId: string, dto: UpdateUserDto): Promise<User> {
|
||||
const user = await this.userRepository.findOne({ id: userId });
|
||||
if (!user) {
|
||||
throw new NotFoundException(`User with ID ${userId} not found.`);
|
||||
@@ -99,7 +101,7 @@ export class UserService {
|
||||
if (dto.marriageDate) {
|
||||
assignData.marriageDate = new Date(dto.marriageDate);
|
||||
}
|
||||
|
||||
// get restuarant
|
||||
this.em.assign(user, assignData);
|
||||
await this.em.flush();
|
||||
|
||||
@@ -287,27 +289,34 @@ export class UserService {
|
||||
return address;
|
||||
}
|
||||
|
||||
async convertScoreToWallet(userId: string, restId: string, dto: ConvertScoreToWalletDto): Promise<User> {
|
||||
async convertScoreToWallet(userId: string, restId: string) {
|
||||
const user = await this.userRepository.findOne({ id: userId });
|
||||
if (!user) {
|
||||
throw new NotFoundException(`User with ID ${userId} not found.`);
|
||||
}
|
||||
|
||||
const restaurant = await this.restaurantRepository.findOne({ id: restId });
|
||||
if (!restaurant) {
|
||||
throw new NotFoundException(`Restaurant with ID ${restId} not found.`);
|
||||
}
|
||||
let userWallet = await this.userWalletRepository.findOne({ user: { id: userId }, restaurant: { id: restId } });
|
||||
if (!userWallet) {
|
||||
userWallet = this.userWalletRepository.create({ user: user, restaurant, wallet: 0, points: 0 });
|
||||
await this.em.persistAndFlush(userWallet);
|
||||
}
|
||||
// Determine how many points to convert
|
||||
const pointsToConvert = dto.points ?? user.points;
|
||||
const pointsToConvert = userWallet.points;
|
||||
|
||||
// Validate points to convert
|
||||
if (pointsToConvert <= 0) {
|
||||
throw new BadRequestException('Points to convert must be greater than 0.');
|
||||
}
|
||||
|
||||
if (user.points < pointsToConvert) {
|
||||
throw new BadRequestException(`Insufficient points. Available: ${user.points}, Requested: ${pointsToConvert}.`);
|
||||
}
|
||||
const restaurant = await this.restaurantRepository.findOne({ id: restId });
|
||||
if (!restaurant) {
|
||||
throw new NotFoundException(`Restaurant with ID ${restId} not found.`);
|
||||
if (userWallet.points < pointsToConvert) {
|
||||
throw new BadRequestException(
|
||||
`Insufficient points. Available: ${userWallet.points}, Requested: ${pointsToConvert}.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (!restaurant.score) {
|
||||
throw new BadRequestException('Restaurant score not found.');
|
||||
}
|
||||
@@ -322,11 +331,14 @@ export class UserService {
|
||||
// Convert points to wallet (1:1 ratio - can be made configurable)
|
||||
|
||||
// Update user's wallet and points
|
||||
user.wallet = (user.wallet || 0) + walletIncreaseAmount;
|
||||
user.points = user.points - pointsToConvert;
|
||||
userWallet.wallet = (userWallet.wallet || 0) + walletIncreaseAmount;
|
||||
userWallet.points = (userWallet.points || 0) - pointsToConvert;
|
||||
|
||||
await this.em.flush();
|
||||
|
||||
return user;
|
||||
}
|
||||
getUserWallet(userId: string, restId: string): Promise<UserWallet | null> {
|
||||
return this.userWalletRepository.findOne({ user: { id: userId }, restaurant: { id: restId } });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user