119 lines
3.1 KiB
TypeScript
119 lines
3.1 KiB
TypeScript
/**
|
|
* 在虞氏家族中创建一个真实的更新操作
|
|
* 用于在前端查看详细的变更信息
|
|
*/
|
|
|
|
import { prisma } from '../lib/prisma'
|
|
import { recordMemberHistory } from '../lib/member-history'
|
|
|
|
async function createRealUpdate() {
|
|
console.log('🧪 在虞氏家族创建更新操作...\n')
|
|
|
|
try {
|
|
// 1. 查找虞氏家族
|
|
const tree = await prisma.familyTree.findFirst({
|
|
where: {
|
|
name: {
|
|
contains: '虞'
|
|
}
|
|
}
|
|
})
|
|
|
|
if (!tree) {
|
|
console.error('❌ 未找到虞氏家族')
|
|
return
|
|
}
|
|
|
|
console.log('✅ 找到家族树:', tree.name)
|
|
|
|
// 2. 查找虞国华
|
|
const member = await prisma.familyMember.findFirst({
|
|
where: {
|
|
treeId: tree.id,
|
|
fullName: {
|
|
contains: '虞国华'
|
|
}
|
|
}
|
|
})
|
|
|
|
if (!member) {
|
|
console.error('❌ 未找到虞国华')
|
|
return
|
|
}
|
|
|
|
console.log('✅ 找到成员:', member.fullName)
|
|
console.log()
|
|
|
|
// 3. 保存旧数据
|
|
const oldData = { ...member }
|
|
|
|
// 4. 更新成员(做一些明显的修改)
|
|
console.log('📝 更新成员信息...')
|
|
const updatedMember = await prisma.familyMember.update({
|
|
where: { id: member.id },
|
|
data: {
|
|
phone: '13912345678',
|
|
email: 'yuguohua@example.com',
|
|
address: '浙江省杭州市西湖区',
|
|
bio: (member.bio || '虞国华先生') + '\n\n【更新】添加了联系方式和地址信息。',
|
|
}
|
|
})
|
|
console.log('✅ 成员已更新')
|
|
|
|
// 5. 记录历史
|
|
const historyRecord = await recordMemberHistory({
|
|
memberId: member.id,
|
|
treeId: tree.id,
|
|
changedBy: tree.ownerId,
|
|
changeType: 'UPDATE',
|
|
oldData: oldData,
|
|
newData: updatedMember
|
|
})
|
|
console.log('✅ 历史记录已创建')
|
|
|
|
// 6. 创建操作日志
|
|
await prisma.activityLog.create({
|
|
data: {
|
|
treeId: tree.id,
|
|
userId: tree.ownerId,
|
|
action: 'UPDATE',
|
|
entityType: 'MEMBER',
|
|
entityId: member.id,
|
|
entityName: updatedMember.fullName,
|
|
changes: historyRecord?.changes || {},
|
|
}
|
|
})
|
|
console.log('✅ 操作日志已创建')
|
|
console.log()
|
|
|
|
console.log('✅ 更新操作创建成功!')
|
|
console.log()
|
|
console.log('📋 变更内容:')
|
|
if (historyRecord?.changes) {
|
|
const changes = historyRecord.changes as Record<string, { old: any, new: any }>
|
|
for (const [field, change] of Object.entries(changes)) {
|
|
console.log(` • ${field}: "${change.old}" → "${change.new}"`)
|
|
}
|
|
}
|
|
console.log()
|
|
console.log('💡 现在可以在前端查看:')
|
|
console.log(' 1. 访问 /settings 页面')
|
|
console.log(' 2. 选择"虞氏家族"')
|
|
console.log(' 3. 查看最新的"更新成员虞国华"记录')
|
|
console.log(' 4. 应该能看到详细的变更对比信息')
|
|
|
|
} catch (error) {
|
|
console.error('❌ 创建更新失败:', error)
|
|
throw error
|
|
} finally {
|
|
await prisma.$disconnect()
|
|
}
|
|
}
|
|
|
|
// 运行
|
|
createRealUpdate()
|
|
.catch((error) => {
|
|
console.error('执行失败:', error)
|
|
process.exit(1)
|
|
})
|