![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.quebec/private_html/scripts/ |
import { PrismaClient } from '@prisma/client';
import readline from 'readline';
const prisma = new PrismaClient();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function main() {
console.log('Finding users with duplicate names...');
const usersWithSameName = await prisma.user.groupBy({
by: ['name'],
_count: { name: true },
having: { name: { _count: { gt: 1 } } },
});
if (usersWithSameName.length === 0) {
console.log('No users with duplicate names found. Your user data is clean!');
return;
}
console.log(`Found ${usersWithSameName.length} name(s) that are duplicated.`);
for (const group of usersWithSameName) {
if (!group.name) continue;
console.log(`\n---------------------------------\n`);
console.log(`Users with the name "${group.name}":`);
const duplicateUsers = await prisma.user.findMany({
where: { name: group.name },
select: { id: true, email: true, role: true, createdAt: true },
});
duplicateUsers.forEach((user, index) => {
console.log(
`${index + 1}. ID: ${user.id}, Email: ${
user.email
}, Role: ${user.role}, Created: ${user.createdAt.toDateString()}`
);
});
const action = await askQuestion(
'\nChoose an action: (1) Rename a user, (2) Delete a user, (3) Skip, [Enter] to continue: '
);
switch (action) {
case '1':
await handleRename(duplicateUsers);
break;
case '2':
await handleDelete(duplicateUsers);
break;
default:
console.log('Skipping...');
break;
}
}
}
async function handleRename(users: { id: string; email: string | null }[]) {
const userIndexStr = await askQuestion('Enter the number of the user to rename: ');
const userIndex = parseInt(userIndexStr) - 1;
if (userIndex >= 0 && userIndex < users.length) {
const userToRename = users[userIndex];
const newName = await askQuestion(`Enter the new name for ${userToRename.email}: `);
await prisma.user.update({
where: { id: userToRename.id },
data: { name: newName },
});
console.log(`User ${userToRename.email} renamed to ${newName}.`);
} else {
console.log('Invalid selection.');
}
}
async function handleDelete(users: { id: string; email: string | null }[]) {
const userIndexStr = await askQuestion('Enter the number of the user to DELETE (this is permanent!): ');
const userIndex = parseInt(userIndexStr) - 1;
if (userIndex >= 0 && userIndex < users.length) {
const userToDelete = users[userIndex];
const confirmation = await askQuestion(`Type "${userToDelete.email}" to confirm deletion: `);
if (confirmation === userToDelete.email) {
try {
await prisma.user.delete({ where: { id: userToDelete.id } });
console.log(`User ${userToDelete.email} has been deleted.`);
} catch (e: any) {
console.error(`Could not delete user. They may have related records (applications, etc.). Error: ${e.message}`);
}
} else {
console.log('Confirmation did not match. Deletion cancelled.');
}
} else {
console.log('Invalid selection.');
}
}
function askQuestion(query: string): Promise<string> {
return new Promise((resolve) => {
rl.question(query, resolve);
});
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
rl.close();
});