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:
@@ -0,0 +1,10 @@
|
|||||||
|
import { PrismaClient } from '@prisma/client'
|
||||||
|
const prisma = new PrismaClient()
|
||||||
|
async function main() {
|
||||||
|
const accounts = await prisma.socialAccount.findMany({ where: { type: 'SOCIAL', city: '北京' }, select: { id: true, name: true } })
|
||||||
|
for (const a of accounts) {
|
||||||
|
const std = await prisma.socialYearStandard.findFirst({ where: { accountId: a.id, isCurrent: true }, select: { medicalOrgExtra: true, medicalEmpExtra: true, extraInsurances: true, effectiveFrom: true } })
|
||||||
|
console.log(a.name + ' | effective=' + std?.effectiveFrom + ' | medicalOrgExtra=' + std?.medicalOrgExtra + ' | medicalEmpExtra=' + std?.medicalEmpExtra + ' | extraInsurances=' + JSON.stringify(std?.extraInsurances))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
main().then(() => process.exit(0))
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* 更新北京社保账户的大病医疗个人附加(3元/月)
|
||||||
|
*/
|
||||||
|
import { PrismaClient } from '@prisma/client'
|
||||||
|
const prisma = new PrismaClient()
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
// 北京社保账户:医疗个人附加3元(大病医疗)
|
||||||
|
const beijingSocial = await prisma.socialAccount.findMany({ where: { type: 'SOCIAL', city: '北京' }, select: { id: true, name: true } })
|
||||||
|
for (const a of beijingSocial) {
|
||||||
|
const result = await prisma.socialYearStandard.updateMany({
|
||||||
|
where: { accountId: a.id, isCurrent: true },
|
||||||
|
data: { medicalEmpExtra: 3 },
|
||||||
|
})
|
||||||
|
console.log(`[更新] ${a.name} → medicalEmpExtra=3,影响 ${result.count} 条`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证
|
||||||
|
for (const a of beijingSocial) {
|
||||||
|
const std = await prisma.socialYearStandard.findFirst({ where: { accountId: a.id, isCurrent: true }, select: { medicalEmpExtra: true, effectiveFrom: true } })
|
||||||
|
console.log(`[验证] ${a.name} | effective=${std?.effectiveFrom} | medicalEmpExtra=${std?.medicalEmpExtra}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main().then(() => process.exit(0)).catch(e => { console.error(e); process.exit(1) })
|
||||||
@@ -263,6 +263,22 @@ router.post('/accounts/:accountId/standards', async (req: AuthRequest, res: Resp
|
|||||||
const account = await prisma.socialAccount.findFirst({ where: { id: accountId, orgId } })
|
const account = await prisma.socialAccount.findFirst({ where: { id: accountId, orgId } })
|
||||||
if (!account) return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '账户不存在' } })
|
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 } })
|
const current = await prisma.socialYearStandard.findFirst({ where: { accountId, isCurrent: true } })
|
||||||
if (current) {
|
if (current) {
|
||||||
|
|||||||
Reference in New Issue
Block a user