LibreChat/e2e/setup/cleanupUser.ts
Marco Beretta bdb222d5f4
🔒 fix: resolve session persistence post password reset (#5077)
*  feat: Implement session management with CRUD operations and integrate into user workflows

*  refactor: Update session model import paths and enhance session creation logic in AuthService

*  refactor: Validate session and user ID formats in session management functions

*  style: Enhance UI components with improved styling and accessibility features

* chore: Update login form tests to use getByTestId instead of getByRole, remove console.log()

* chore: Update login form tests to use getByTestId instead of getByRole

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
2024-12-23 05:12:07 -05:00

50 lines
1.6 KiB
TypeScript

import connectDb from '@librechat/backend/lib/db/connectDb';
import {
deleteMessages,
deleteConvos,
User,
deleteAllUserSessions,
Balance,
} from '@librechat/backend/models';
import { Transaction } from '@librechat/backend/models/Transaction';
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');
const { _id: user } = await User.findOne({ email }).lean();
console.log('🤖: ✅ Found user in Database');
// Delete all conversations & associated messages
const { deletedCount, messages } = await deleteConvos(user, {});
if (messages.deletedCount > 0 || deletedCount > 0) {
console.log(`🤖: ✅ Deleted ${deletedCount} convos & ${messages.deletedCount} messages`);
}
// Ensure all user messages are deleted
const { deletedCount: deletedMessages } = await deleteMessages({ user });
if (deletedMessages > 0) {
console.log(`🤖: ✅ Deleted ${deletedMessages} remaining message(s)`);
}
// TODO: fix this to delete all user sessions with the user's email
await deleteAllUserSessions(user);
await User.deleteMany({ _id: user });
await Balance.deleteMany({ user });
await Transaction.deleteMany({ user });
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));