2025-05-30 22:18:13 -04:00
|
|
|
import { connectDb } from '@librechat/backend/db/connect';
|
2024-12-23 11:12:07 +01:00
|
|
|
import {
|
2025-05-30 22:18:13 -04:00
|
|
|
findUser,
|
2024-12-23 11:12:07 +01:00
|
|
|
deleteConvos,
|
2025-05-30 22:18:13 -04:00
|
|
|
deleteMessages,
|
2024-12-23 11:12:07 +01:00
|
|
|
deleteAllUserSessions,
|
|
|
|
|
} from '@librechat/backend/models';
|
2025-05-30 22:18:13 -04:00
|
|
|
|
2023-09-18 15:19:50 -04:00
|
|
|
type TUser = { email: string; password: string };
|
|
|
|
|
|
|
|
|
|
export default async function cleanupUser(user: TUser) {
|
|
|
|
|
const { email } = user;
|
|
|
|
|
try {
|
|
|
|
|
console.log('🤖: global teardown has been started');
|
|
|
|
|
const db = await connectDb();
|
|
|
|
|
console.log('🤖: ✅ Connected to Database');
|
|
|
|
|
|
2025-05-30 22:18:13 -04:00
|
|
|
const foundUser = await findUser({ email });
|
|
|
|
|
if (!foundUser) {
|
|
|
|
|
console.log('🤖: ⚠️ User not found in Database');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const userId = foundUser._id;
|
2023-09-18 15:19:50 -04:00
|
|
|
console.log('🤖: ✅ Found user in Database');
|
|
|
|
|
|
|
|
|
|
// Delete all conversations & associated messages
|
2025-05-30 22:18:13 -04:00
|
|
|
const { deletedCount, messages } = await deleteConvos(userId, {});
|
2023-09-18 15:19:50 -04:00
|
|
|
|
|
|
|
|
if (messages.deletedCount > 0 || deletedCount > 0) {
|
|
|
|
|
console.log(`🤖: ✅ Deleted ${deletedCount} convos & ${messages.deletedCount} messages`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure all user messages are deleted
|
2025-05-30 22:18:13 -04:00
|
|
|
const { deletedCount: deletedMessages } = await deleteMessages({ user: userId });
|
2023-09-18 15:19:50 -04:00
|
|
|
if (deletedMessages > 0) {
|
|
|
|
|
console.log(`🤖: ✅ Deleted ${deletedMessages} remaining message(s)`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-30 22:18:13 -04:00
|
|
|
// Delete all user sessions
|
|
|
|
|
await deleteAllUserSessions(userId.toString());
|
|
|
|
|
|
|
|
|
|
// Get models from the registered models
|
|
|
|
|
const { User, Balance, Transaction } = getModels();
|
2023-09-18 15:19:50 -04:00
|
|
|
|
2025-05-30 22:18:13 -04:00
|
|
|
// Delete user, balance, and transactions using the registered models
|
|
|
|
|
await User.deleteMany({ _id: userId });
|
|
|
|
|
await Balance.deleteMany({ user: userId });
|
|
|
|
|
await Transaction.deleteMany({ user: userId });
|
2023-09-18 15:19:50 -04:00
|
|
|
|
|
|
|
|
console.log('🤖: ✅ Deleted user from Database');
|
|
|
|
|
|
|
|
|
|
await db.connection.close();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process.on('uncaughtException', (err) => console.error('Uncaught Exception:', err));
|