online payment
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Controller, Get, UseGuards, Patch, Body} from '@nestjs/common';
|
||||
import { Controller, Get, UseGuards, Patch, Body } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation, ApiBody, ApiOkResponse } from '@nestjs/swagger';
|
||||
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
|
||||
import { UserService } from '../providers/user.service';
|
||||
@@ -14,14 +14,14 @@ import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||
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';
|
||||
import { CreditService } from '../providers/credit.service';
|
||||
|
||||
@ApiTags('User')
|
||||
@Controller()
|
||||
export class UsersController {
|
||||
constructor(
|
||||
private readonly userService: UserService,
|
||||
private readonly walletService: WalletService,
|
||||
private readonly walletService: CreditService,
|
||||
) { }
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
|
||||
+25
-4
@@ -1,19 +1,40 @@
|
||||
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
|
||||
import { FilterQuery } from '@mikro-orm/core';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { CreditTransactionRepository } from '../repositories/credit-transaction.repository';
|
||||
import { CreditRepository } from '../repositories/credit.repository';
|
||||
import { FindWalletTransactionsDto } from '../dto/find-wallet-transactions.dto';
|
||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||
import { CreditTransaction } from '../entities/credit-transaction.entity';
|
||||
import { UserRepository } from '../repositories/user.repository';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class WalletService {
|
||||
export class CreditService {
|
||||
constructor(
|
||||
// private readonly walletTransactionRepository: CreditTransactionRepository,
|
||||
|
||||
private readonly creditRepository: CreditRepository,
|
||||
private readonly userRepository: UserRepository,
|
||||
|
||||
) { }
|
||||
|
||||
async getCurrentCredit(userId: string,): Promise<number> {
|
||||
const user = await this.userRepository.findOne(userId)
|
||||
if (!user) {
|
||||
throw new BadRequestException("User not found")
|
||||
}
|
||||
const maxCredit = user.maxCredit
|
||||
|
||||
const latestTransaction = await this.creditRepository.findOne(
|
||||
{
|
||||
user: { id: userId },
|
||||
},
|
||||
{ orderBy: { createdAt: 'desc' } },
|
||||
);
|
||||
const balance = latestTransaction?.balance || 0
|
||||
|
||||
const remained = maxCredit - balance
|
||||
|
||||
return remained
|
||||
}
|
||||
// async getUserWalletTransactions(
|
||||
// userId: string,
|
||||
// dto: FindWalletTransactionsDto,
|
||||
@@ -1,20 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
||||
import { CreditTransaction } from '../entities/credit-transaction.entity';
|
||||
|
||||
@Injectable()
|
||||
export class CreditTransactionRepository extends EntityRepository<CreditTransaction> {
|
||||
constructor(readonly em: EntityManager) {
|
||||
super(em, CreditTransaction);
|
||||
}
|
||||
async getCurrentCredit(userId: string,): Promise<number> {
|
||||
const lastRow = await this.em.findOne(
|
||||
CreditTransaction,
|
||||
{
|
||||
user: { id: userId },
|
||||
},
|
||||
{ orderBy: { createdAt: 'desc' } },
|
||||
);
|
||||
return lastRow?.balance || 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
||||
import { CreditTransaction } from '../entities/credit-transaction.entity';
|
||||
|
||||
@Injectable()
|
||||
export class CreditRepository extends EntityRepository<CreditTransaction> {
|
||||
constructor(readonly em: EntityManager) {
|
||||
super(em, CreditTransaction);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,17 +6,28 @@ import { User } from './entities/user.entity';
|
||||
import { UserAddress } from './entities/user-address.entity';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { UserRepository } from './repositories/user.repository';
|
||||
import { CreditTransactionRepository } from './repositories/credit-transaction.repository';
|
||||
import { WalletService } from './providers/wallet.service';
|
||||
import { CreditRepository } from './repositories/credit.repository';
|
||||
import { CreditService } from './providers/credit.service';
|
||||
import { CreditTransaction } from './entities/credit-transaction.entity';
|
||||
|
||||
@Module({
|
||||
providers: [UserService, WalletService, UserRepository, CreditTransactionRepository, CreditTransactionRepository],
|
||||
providers: [
|
||||
UserService,
|
||||
CreditService,
|
||||
UserRepository,
|
||||
CreditRepository,
|
||||
],
|
||||
controllers: [UsersController],
|
||||
imports: [
|
||||
MikroOrmModule.forFeature([User, UserAddress, CreditTransaction]),
|
||||
JwtModule,
|
||||
],
|
||||
exports: [UserService, WalletService, UserRepository, CreditTransactionRepository, CreditTransactionRepository],
|
||||
exports: [
|
||||
UserService,
|
||||
CreditService,
|
||||
UserRepository,
|
||||
CreditRepository,
|
||||
CreditRepository
|
||||
],
|
||||
})
|
||||
export class UserModule { }
|
||||
|
||||
Reference in New Issue
Block a user