fix(contribution): 修复系统账户查询中 nullable regionCode 的 TypeScript 类型错误

## 问题
- Prisma 生成的类型不允许在 unique where 条件中传递 null
- addContribution 方法被传入多余参数
- findByType 返回数组被当作单个对象使用

## 修复
- findByTypeAndRegion: 使用 findFirst 替代 findUnique
- ensureSystemAccountsExist: 使用 findFirst + create 替代 upsert
- addContribution: 使用 findFirst + create/update 替代 upsert
- 修正 HEADQUARTERS 账户同步事件调用参数

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-20 21:37:44 -08:00
parent 9062346650
commit 40ac037c03
2 changed files with 43 additions and 26 deletions

View File

@ -286,19 +286,17 @@ export class ContributionCalculationService {
new ContributionAmount(0),
);
await this.systemAccountRepository.addContribution(
'HEADQUARTERS',
'HEADQUARTERS',
null,
totalUnallocatedAmount,
);
// 发布 HEADQUARTERS 账户同步事件
const headquartersAccount = await this.systemAccountRepository.findByType('HEADQUARTERS');
const headquartersAccount = await this.systemAccountRepository.findByTypeAndRegion('HEADQUARTERS', null);
if (headquartersAccount) {
const hqEvent = new SystemAccountSyncedEvent(
'HEADQUARTERS',
'HEADQUARTERS', // 新增:基础类型
null, // 新增:区域代码(总部没有区域)
null, // 区域代码(总部没有区域)
headquartersAccount.name,
headquartersAccount.contributionBalance.value.toString(),
headquartersAccount.createdAt,

View File

@ -39,14 +39,16 @@ export class SystemAccountRepository {
/**
* accountType + regionCode
* regionCode 使 findFirst findUnique
*/
async findByTypeAndRegion(
accountType: SystemAccountType,
regionCode: string | null,
): Promise<SystemAccount | null> {
const record = await this.client.systemAccount.findUnique({
const record = await this.client.systemAccount.findFirst({
where: {
accountType_regionCode: { accountType, regionCode },
accountType,
regionCode: regionCode === null ? { equals: null } : regionCode,
},
});
@ -87,19 +89,25 @@ export class SystemAccountRepository {
];
for (const account of accounts) {
await this.client.systemAccount.upsert({
// 由于 regionCode 是 nullable使用 findFirst + create 替代 upsert
const existing = await this.client.systemAccount.findFirst({
where: {
accountType_regionCode: { accountType: account.accountType, regionCode: null },
},
create: {
accountType: account.accountType,
regionCode: null,
name: account.name,
contributionBalance: 0,
contributionNeverExpires: true,
regionCode: { equals: null },
},
update: {},
});
if (!existing) {
await this.client.systemAccount.create({
data: {
accountType: account.accountType,
regionCode: null,
name: account.name,
contributionBalance: 0,
contributionNeverExpires: true,
},
});
}
}
}
@ -113,21 +121,32 @@ export class SystemAccountRepository {
): Promise<void> {
const name = this.getAccountName(accountType, regionCode);
await this.client.systemAccount.upsert({
// 由于 regionCode 是 nullable使用 findFirst + create/update 替代 upsert
const existing = await this.client.systemAccount.findFirst({
where: {
accountType_regionCode: { accountType, regionCode },
},
create: {
accountType,
regionCode,
name,
contributionBalance: amount.value,
contributionNeverExpires: true,
},
update: {
contributionBalance: { increment: amount.value },
regionCode: regionCode === null ? { equals: null } : regionCode,
},
});
if (existing) {
await this.client.systemAccount.update({
where: { id: existing.id },
data: {
contributionBalance: { increment: amount.value },
},
});
} else {
await this.client.systemAccount.create({
data: {
accountType,
regionCode,
name,
contributionBalance: amount.value,
contributionNeverExpires: true,
},
});
}
}
/**