fix(c2c): 用户无交易账户时自动创建零余额账户
createOrder 和 takeOrder 中,如果用户没有交易账户, 自动创建零余额账户而非报错"交易账户不存在"。 买单不需要余额,不应因缺少账户而无法下单。 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
a1c3657390
commit
6ccc192bc6
|
|
@ -7,6 +7,7 @@ import { RedisService } from '../../infrastructure/redis/redis.service';
|
||||||
import { PrismaService } from '../../infrastructure/persistence/prisma/prisma.service';
|
import { PrismaService } from '../../infrastructure/persistence/prisma/prisma.service';
|
||||||
import { IdentityClient } from '../../infrastructure/identity/identity.client';
|
import { IdentityClient } from '../../infrastructure/identity/identity.client';
|
||||||
import { Money } from '../../domain/value-objects/money.vo';
|
import { Money } from '../../domain/value-objects/money.vo';
|
||||||
|
import { TradingAccountAggregate } from '../../domain/aggregates/trading-account.aggregate';
|
||||||
import Decimal from 'decimal.js';
|
import Decimal from 'decimal.js';
|
||||||
|
|
||||||
// C2C 订单类型常量
|
// C2C 订单类型常量
|
||||||
|
|
@ -132,10 +133,16 @@ export class C2cService {
|
||||||
// 计算总金额
|
// 计算总金额
|
||||||
const totalAmount = priceDecimal.mul(quantityDecimal);
|
const totalAmount = priceDecimal.mul(quantityDecimal);
|
||||||
|
|
||||||
// 获取用户交易账户
|
// 获取用户交易账户,不存在则自动创建零余额账户
|
||||||
const account = await this.tradingAccountRepository.findByAccountSequence(accountSequence);
|
let account = await this.tradingAccountRepository.findByAccountSequence(accountSequence);
|
||||||
if (!account) {
|
if (!account) {
|
||||||
throw new NotFoundException('交易账户不存在');
|
this.logger.log(`C2C: 用户 ${accountSequence} 无交易账户,自动创建`);
|
||||||
|
const newAccount = TradingAccountAggregate.create(accountSequence);
|
||||||
|
await this.tradingAccountRepository.save(newAccount);
|
||||||
|
account = await this.tradingAccountRepository.findByAccountSequence(accountSequence);
|
||||||
|
if (!account) {
|
||||||
|
throw new NotFoundException('交易账户创建失败');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查余额并冻结资产
|
// 检查余额并冻结资产
|
||||||
|
|
@ -232,10 +239,16 @@ export class C2cService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取接单方账户
|
// 获取接单方账户,不存在则自动创建零余额账户
|
||||||
const takerAccount = await this.tradingAccountRepository.findByAccountSequence(takerAccountSequence);
|
let takerAccount = await this.tradingAccountRepository.findByAccountSequence(takerAccountSequence);
|
||||||
if (!takerAccount) {
|
if (!takerAccount) {
|
||||||
throw new NotFoundException('交易账户不存在');
|
this.logger.log(`C2C: 接单方 ${takerAccountSequence} 无交易账户,自动创建`);
|
||||||
|
const newAccount = TradingAccountAggregate.create(takerAccountSequence);
|
||||||
|
await this.tradingAccountRepository.save(newAccount);
|
||||||
|
takerAccount = await this.tradingAccountRepository.findByAccountSequence(takerAccountSequence);
|
||||||
|
if (!takerAccount) {
|
||||||
|
throw new NotFoundException('交易账户创建失败');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// #13: 支持部分成交,taker可指定数量
|
// #13: 支持部分成交,taker可指定数量
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue