|
|
@@ -1,8 +1,8 @@
|
|
|
-import { Injectable } from '@nestjs/common'
|
|
|
+import { Injectable, InternalServerErrorException } from '@nestjs/common'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { UserBalance } from './entities/user-balance.entity'
|
|
|
import { Repository } from 'typeorm'
|
|
|
-import { BalanceRecord, Type } from './entities/balance-record.entity'
|
|
|
+import { BalanceRecord, BalanceType } from './entities/balance-record.entity'
|
|
|
|
|
|
@Injectable()
|
|
|
export class UserBalanceService {
|
|
|
@@ -26,8 +26,11 @@ export class UserBalanceService {
|
|
|
return userBalance
|
|
|
}
|
|
|
|
|
|
- async modifyBalance(userId: number, amount: number, type: Type, remark?: string, extra?: string) {
|
|
|
+ async modifyBalance(userId: number, amount: number, type: BalanceType, remark?: string, extra?: string) {
|
|
|
const userBalance = await this.getBalance(userId)
|
|
|
+ if (userBalance.balance + amount < 0) {
|
|
|
+ throw new Error('余额不足')
|
|
|
+ }
|
|
|
await this.balanceRecordRepository.save(
|
|
|
new BalanceRecord({
|
|
|
userId,
|
|
|
@@ -44,7 +47,7 @@ export class UserBalanceService {
|
|
|
await this.userBalanceRepository.save(userBalance)
|
|
|
}
|
|
|
|
|
|
- async getRecords(userId: number, ) {
|
|
|
+ async getRecords(userId: number) {
|
|
|
return await this.balanceRecordRepository.find({
|
|
|
where: {
|
|
|
userId
|
|
|
@@ -54,4 +57,8 @@ export class UserBalanceService {
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+ async applyWithdraw(userId: number, amount: number, remark?: string, extra?: string) {
|
|
|
+ return await this.modifyBalance(userId, -amount, BalanceType.WITHDRAW, remark, extra)
|
|
|
+ }
|
|
|
}
|