0.0.5.1
This commit is contained in:
@@ -12,12 +12,32 @@ const createMemberSchema = z.object({
|
||||
fullName: z.string().min(1),
|
||||
gender: z.nativeEnum(Gender),
|
||||
generation: z.number().int().positive(),
|
||||
generationName: z.string().optional(),
|
||||
courtesyName: z.string().optional(),
|
||||
artName: z.string().optional(),
|
||||
posthumousName: z.string().optional(),
|
||||
rank: z.number().optional(),
|
||||
birthDate: z.string().optional(),
|
||||
deathDate: z.string().optional(),
|
||||
isLunarDate: z.boolean().optional(),
|
||||
ancestralHome: z.string().optional(),
|
||||
birthPlace: z.string().optional(),
|
||||
burialPlace: z.string().optional(),
|
||||
phone: z.string().optional(),
|
||||
telephone: z.string().optional(),
|
||||
email: z.string().optional(),
|
||||
address: z.string().optional(),
|
||||
fatherId: z.string().optional(),
|
||||
motherId: z.string().optional(),
|
||||
spouseIds: z.array(z.string()).optional(),
|
||||
// ... 其他字段
|
||||
childrenIds: z.array(z.string()).optional(),
|
||||
spouseFatherName: z.string().optional(),
|
||||
spouseMotherName: z.string().optional(),
|
||||
bio: z.string().optional(),
|
||||
tags: z.array(z.string()).optional(),
|
||||
avatarUrl: z.string().optional(),
|
||||
avatarImageId: z.string().optional(),
|
||||
photoIds: z.array(z.string()).optional(),
|
||||
})
|
||||
|
||||
// 获取家族树的所有成员
|
||||
@@ -116,6 +136,81 @@ export async function POST(
|
||||
}
|
||||
})
|
||||
|
||||
// 维护配偶双向关系
|
||||
if (data.spouseIds && data.spouseIds.length > 0) {
|
||||
for (const spouseId of data.spouseIds) {
|
||||
const spouse = await prisma.familyMember.findUnique({
|
||||
where: { id: spouseId }
|
||||
})
|
||||
|
||||
if (spouse && !spouse.spouseIds.includes(member.id)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: spouseId },
|
||||
data: {
|
||||
spouseIds: [...spouse.spouseIds, member.id]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 维护父母-子女双向关系
|
||||
// 1. 如果设置了父亲,将当前成员添加到父亲的子女列表
|
||||
if (data.fatherId) {
|
||||
const father = await prisma.familyMember.findUnique({
|
||||
where: { id: data.fatherId }
|
||||
})
|
||||
|
||||
if (father && !father.childrenIds.includes(member.id)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: data.fatherId },
|
||||
data: {
|
||||
childrenIds: [...father.childrenIds, member.id]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 如果设置了母亲,将当前成员添加到母亲的子女列表
|
||||
if (data.motherId) {
|
||||
const mother = await prisma.familyMember.findUnique({
|
||||
where: { id: data.motherId }
|
||||
})
|
||||
|
||||
if (mother && !mother.childrenIds.includes(member.id)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: data.motherId },
|
||||
data: {
|
||||
childrenIds: [...mother.childrenIds, member.id]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 如果设置了子女,将当前成员设置为子女的父亲或母亲
|
||||
if (data.childrenIds && data.childrenIds.length > 0) {
|
||||
for (const childId of data.childrenIds) {
|
||||
const child = await prisma.familyMember.findUnique({
|
||||
where: { id: childId }
|
||||
})
|
||||
|
||||
if (child) {
|
||||
// 根据当前成员的性别设置为父亲或母亲
|
||||
if (data.gender === 'MALE' && !child.fatherId) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: childId },
|
||||
data: { fatherId: member.id }
|
||||
})
|
||||
} else if (data.gender === 'FEMALE' && !child.motherId) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: childId },
|
||||
data: { motherId: member.id }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 记录操作日志
|
||||
await prisma.activityLog.create({
|
||||
data: {
|
||||
|
||||
Reference in New Issue
Block a user