fix: support soft-delete user uniqueness in tools

This commit is contained in:
hectorzhao
2026-07-26 13:14:46 +08:00
parent 2ce682c3fc
commit 059b38e8fe
3 changed files with 56 additions and 43 deletions
+27 -21
View File
@@ -19,7 +19,9 @@ function hashPassword(value) {
}
async function main() {
const existingUser = await prisma.user.findUnique({ where: { username } });
const existingUser = await prisma.user.findFirst({
where: { username, deletedAt: null },
});
const createPassword = configuredPassword || randomBytes(18).toString('base64url');
const updatePassword = configuredPassword;
const role = await prisma.role.upsert({
@@ -27,26 +29,30 @@ async function main() {
update: { name: '平台管理员', scope: 'platform' },
create: { code: 'platform_admin', name: '平台管理员', scope: 'platform' },
});
const user = await prisma.user.upsert({
where: { username },
update: {
email,
displayName: '生产平台管理员',
...(updatePassword ? { passwordHash: hashPassword(updatePassword) } : {}),
status: 'active',
failedLoginCount: 0,
lockedUntil: null,
deletedAt: null,
tenantId: null,
},
create: {
username,
email,
displayName: '生产平台管理员',
passwordHash: hashPassword(createPassword),
status: 'active',
},
});
const userData = {
email,
displayName: '生产平台管理员',
...(updatePassword ? { passwordHash: hashPassword(updatePassword) } : {}),
status: 'active',
failedLoginCount: 0,
lockedUntil: null,
deletedAt: null,
tenantId: null,
};
const user = existingUser
? await prisma.user.update({
where: { id: existingUser.id },
data: userData,
})
: await prisma.user.create({
data: {
username,
email,
displayName: '生产平台管理员',
passwordHash: hashPassword(createPassword),
status: 'active',
},
});
await prisma.userRole.upsert({
where: { userId_roleId: { userId: user.id, roleId: role.id } },
update: {},