fix: 清理重复合同数据 + batchRenew截断旧合同endDate + addContract防重复检查
This commit is contained in:
@@ -653,6 +653,15 @@ export async function batchRenew(orgId: string, userId: string, contractIds: str
|
||||
const newEndDate = new Date(newStartDate)
|
||||
newEndDate.setFullYear(newEndDate.getFullYear() + years)
|
||||
|
||||
// 将旧合同 endDate 截止到续签开始日前一天,避免重叠
|
||||
const oldEndDate = new Date(newStartDate)
|
||||
oldEndDate.setDate(oldEndDate.getDate() - 1)
|
||||
|
||||
await prisma.laborContract.update({
|
||||
where: { id: contract.id },
|
||||
data: { endDate: oldEndDate },
|
||||
})
|
||||
|
||||
await prisma.laborContract.create({
|
||||
data: {
|
||||
orgId,
|
||||
@@ -682,6 +691,19 @@ export async function addContract(orgId: string, userId: string, data: any) {
|
||||
throw { code: 'NOT_FOUND', message: '员工不存在' }
|
||||
}
|
||||
|
||||
// 检查是否存在日期完全相同的合同,防止重复提交
|
||||
const duplicate = await prisma.laborContract.findFirst({
|
||||
where: {
|
||||
employeeId: data.employeeId,
|
||||
orgId,
|
||||
startDate: new Date(data.startDate),
|
||||
...(data.endDate ? { endDate: new Date(data.endDate) } : { endDate: null }),
|
||||
},
|
||||
})
|
||||
if (duplicate) {
|
||||
throw { code: 'DUPLICATE', message: '该员工已存在相同日期的合同,请勿重复添加' }
|
||||
}
|
||||
|
||||
const contractMonths = data.endDate
|
||||
? Math.ceil(daysBetween(new Date(data.endDate), new Date(data.startDate)) / 30.44)
|
||||
: data.contractYears * 12
|
||||
|
||||
Reference in New Issue
Block a user