diff --git a/.vscode/settings.json b/.vscode/settings.json index b336fb1..158d031 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,7 +7,7 @@ "source.fixAll.eslint": "explicit" }, "[typescript]": { - "editor.defaultFormatter": "vscode.typescript-language-features" + "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[json]": { "editor.defaultFormatter": "vscode.json-language-features" diff --git a/src/modules/users/controllers/users.controller.ts b/src/modules/users/controllers/users.controller.ts index fee04f0..a32a6de 100644 --- a/src/modules/users/controllers/users.controller.ts +++ b/src/modules/users/controllers/users.controller.ts @@ -14,11 +14,15 @@ import { API_HEADER_SLUG } from 'src/common/constants/index'; import { UserSuccessMessage } from 'src/common/enums/message.enum'; import { Permissions } from 'src/common/decorators/permissions.decorator'; import { Permission } from 'src/common/enums/permission.enum'; +import { WalletService } from '../providers/wallet.service'; @ApiTags('User') @Controller() export class UsersController { - constructor(private readonly userService: UserService) { } + constructor( + private readonly userService: UserService, + private readonly walletService: WalletService, + ) {} @UseGuards(AuthGuard) @ApiBearerAuth() @@ -55,9 +59,19 @@ export class UsersController { @ApiBearerAuth() @ApiOperation({ summary: 'Get User Wallet' }) @ApiHeader(API_HEADER_SLUG) - @Get('public/user/wallet') + @Get('public/user/wallet/balance') + async getUserWalletBalance(@UserId() userId: string, @RestId() restId: string) { + const wallet = await this.walletService.getUserCurrentWalletBalance(userId, restId); + return wallet; + } + + @UseGuards(AuthGuard) + @ApiBearerAuth() + @ApiOperation({ summary: 'Get User Wallet' }) + @ApiHeader(API_HEADER_SLUG) + @Get('public/user/points/balance') async getUserWallet(@UserId() userId: string, @RestId() restId: string) { - const wallet = await this.userService.getUserWallet(userId, restId); + const wallet = await this.walletService.getUserCurrentPoinrBalance(userId, restId); return wallet; } @@ -166,6 +180,4 @@ export class UsersController { ) { return this.userService.findAll(restId, query); } - - } diff --git a/src/modules/users/entities/point-transaction.entity.ts b/src/modules/users/entities/point-transaction.entity.ts index 8c2a186..91e97a2 100644 --- a/src/modules/users/entities/point-transaction.entity.ts +++ b/src/modules/users/entities/point-transaction.entity.ts @@ -1,11 +1,11 @@ -import { Entity, Property, ManyToOne, Index, Unique, Enum } from '@mikro-orm/core'; +import { Entity, Property, ManyToOne, Index, Enum } from '@mikro-orm/core'; import { BaseEntity } from '../../../common/entities/base.entity'; import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity'; import { User } from './user.entity'; import { PointTransactionReason } from '../interface/point'; import { PointTransactionType } from '../interface/point'; -@Entity({ tableName: 'user_wallets' }) +@Entity({ tableName: 'point_transactions' }) @Index({ properties: ['user', 'restaurant'] }) // Composite index for most common query: find wallet by user and restaurant @Index({ properties: ['user'] }) // Index for queries finding all wallets for a user @Index({ properties: ['restaurant'] }) // Index for queries finding all wallets for a restaurant diff --git a/src/modules/users/providers/wallet.service.ts b/src/modules/users/providers/wallet.service.ts index 9c67e99..457baee 100644 --- a/src/modules/users/providers/wallet.service.ts +++ b/src/modules/users/providers/wallet.service.ts @@ -7,12 +7,13 @@ import { FindWalletTransactionsDto } from '../dto/find-wallet-transactions.dto'; import { CreateWalletTransactionDto } from '../dto/create-wallet-transaction.dto'; import { PaginatedResult } from 'src/common/interfaces/pagination.interface'; import { WalletTransactionType } from '../interface/wallet'; +import { PointTransactionRepository } from '../repositories/point-transaction.repository'; @Injectable() export class WalletService { constructor( private readonly walletTransactionRepository: WalletTransactionRepository, - private readonly em: EntityManager, + private readonly pointTransactionRepository: PointTransactionRepository, ) { } async getUserWalletTransactions( @@ -107,9 +108,13 @@ export class WalletService { } getUserWallet(userId: string, restId: string): Promise { - return this.walletTransactionRepository.findOne({ user: { id: userId }, - restaurant: { id: restId } }, - { orderBy: { createdAt: 'DESC' } }); + return this.walletTransactionRepository.findOne( + { + user: { id: userId }, + restaurant: { id: restId }, + }, + { orderBy: { createdAt: 'DESC' } }, + ); } async createTransaction( @@ -143,9 +148,7 @@ export class WalletService { newBalance = currentBalance + amount; } else if (type === WalletTransactionType.DEBIT) { if (currentBalance < amount) { - throw new BadRequestException( - `Insufficient wallet balance. Current: ${currentBalance}, Required: ${amount}.`, - ); + throw new BadRequestException(`Insufficient wallet balance. Current: ${currentBalance}, Required: ${amount}.`); } newBalance = currentBalance - amount; } else { @@ -168,4 +171,11 @@ export class WalletService { await em.persistAndFlush([walletTransaction, transaction]); return transaction; } + + getUserCurrentWalletBalance(userId: string, restuarantId: string) { + return this.walletTransactionRepository.getCurrentWalletBalance(userId, restuarantId) + } + getUserCurrentPoinrBalance(userId: string, restuarantId: string) { + return this.pointTransactionRepository.getcurrentPointBalance(userId, restuarantId) + } } diff --git a/src/modules/users/repositories/wallet-transaction.repository.ts b/src/modules/users/repositories/wallet-transaction.repository.ts index f58650f..a127509 100644 --- a/src/modules/users/repositories/wallet-transaction.repository.ts +++ b/src/modules/users/repositories/wallet-transaction.repository.ts @@ -8,10 +8,14 @@ export class WalletTransactionRepository extends EntityRepository { - const walletTransaction = await this.em.findOne(WalletTransaction, { - user: { id: userId }, - restaurant: { id: restaurantId }, - }, { orderBy: { createdAt: 'desc' } }); + const walletTransaction = await this.em.findOne( + WalletTransaction, + { + user: { id: userId }, + restaurant: { id: restaurantId }, + }, + { orderBy: { createdAt: 'desc' } }, + ); return walletTransaction?.balance || 0; } } diff --git a/src/modules/users/user.module.ts b/src/modules/users/user.module.ts index 750622f..61a74d1 100644 --- a/src/modules/users/user.module.ts +++ b/src/modules/users/user.module.ts @@ -11,17 +11,16 @@ import { WalletTransactionRepository } from './repositories/wallet-transaction.r import { WalletService } from './providers/wallet.service'; import { WalletTransaction } from './entities/wallet-transaction.entity'; import { PointTransaction } from './entities/point-transaction.entity'; +import { PointTransactionRepository } from './repositories/point-transaction.repository'; @Module({ - providers: [UserService, WalletService, UserRepository, WalletTransactionRepository], + providers: [UserService, WalletService, UserRepository, WalletTransactionRepository, PointTransactionRepository], controllers: [UsersController], - imports: [MikroOrmModule.forFeature([ - User, - UserAddress, - WalletTransaction, - PointTransaction - ]), - JwtModule, RestaurantsModule], - exports: [UserService, WalletService, UserRepository, WalletTransactionRepository], + imports: [ + MikroOrmModule.forFeature([User, UserAddress, WalletTransaction, PointTransaction]), + JwtModule, + RestaurantsModule, + ], + exports: [UserService, WalletService, UserRepository, WalletTransactionRepository, PointTransactionRepository], }) -export class UserModule { } +export class UserModule {}