From 40ac037c034dd1fb063075028dd433393a7342e6 Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 20 Jan 2026 21:37:44 -0800 Subject: [PATCH] =?UTF-8?q?fix(contribution):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E8=B4=A6=E6=88=B7=E6=9F=A5=E8=AF=A2=E4=B8=AD?= =?UTF-8?q?=20nullable=20regionCode=20=E7=9A=84=20TypeScript=20=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 问题 - 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 --- .../contribution-calculation.service.ts | 6 +- .../repositories/system-account.repository.ts | 63 ++++++++++++------- 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts b/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts index 91a33bda..665a9582 100644 --- a/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts +++ b/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts @@ -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, diff --git a/backend/services/contribution-service/src/infrastructure/persistence/repositories/system-account.repository.ts b/backend/services/contribution-service/src/infrastructure/persistence/repositories/system-account.repository.ts index b9f47bb9..23a2eee1 100644 --- a/backend/services/contribution-service/src/infrastructure/persistence/repositories/system-account.repository.ts +++ b/backend/services/contribution-service/src/infrastructure/persistence/repositories/system-account.repository.ts @@ -39,14 +39,16 @@ export class SystemAccountRepository { /** * 根据 accountType + regionCode 查找系统账户 + * 注意:由于 regionCode 是可选字段,使用 findFirst 替代 findUnique */ async findByTypeAndRegion( accountType: SystemAccountType, regionCode: string | null, ): Promise { - 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 { 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, + }, + }); + } } /**