fix: 创建年度标准时同effectiveFrom已存在则更新而非报错

问题:创建年度标准时如果该账户已有同 effectiveFrom 的标准,
会触发 P2002 唯一约束冲突,返回400"数据已存在"。

修复:先查是否已存在同 effectiveFrom 的标准,存在则更新,
不存在才创建新标准。

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
selfrelease
2026-08-16 15:48:21 +08:00
parent b5aa5552ff
commit 1d9890e607
3 changed files with 51 additions and 0 deletions
+16
View File
@@ -263,6 +263,22 @@ router.post('/accounts/:accountId/standards', async (req: AuthRequest, res: Resp
const account = await prisma.socialAccount.findFirst({ where: { id: accountId, orgId } })
if (!account) return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '账户不存在' } })
// 检查是否已存在同 effectiveFrom 的标准
const existing = await prisma.socialYearStandard.findFirst({ where: { accountId, effectiveFrom: data.effectiveFrom } })
if (existing) {
// 已存在:更新而非报错
const standard = await prisma.socialYearStandard.update({
where: { id: existing.id },
data: { ...data, isCurrent: true, effectiveTo: null },
})
// 将其他当前版本标记为失效
await prisma.socialYearStandard.updateMany({
where: { accountId, isCurrent: true, id: { not: existing.id } },
data: { isCurrent: false, effectiveTo: data.effectiveFrom },
})
return res.json({ success: true, data: standard })
}
// 将旧当前版本标记为失效
const current = await prisma.socialYearStandard.findFirst({ where: { accountId, isCurrent: true } })
if (current) {