refactor: original changes

This commit is contained in:
Danny Avila 2025-05-30 04:28:22 -04:00
parent fa9177180f
commit f9c0e9853f
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
83 changed files with 413 additions and 505 deletions

View file

@ -1,6 +1,5 @@
const { Conversation, logger } = require('@librechat/data-schemas');
const { getMessages, deleteMessages } = require('./Message');
const logger = require('~/config/winston');
const db = require('~/lib/db/connectDb');
/**
* Searches for a conversation by conversationId and returns a lean document with only conversationId and user.
* @param {string} conversationId - The conversation's ID.
@ -8,7 +7,7 @@ const db = require('~/lib/db/connectDb');
*/
const searchConversation = async (conversationId) => {
try {
return await db.models.Conversation.findOne({ conversationId }, 'conversationId user').lean();
return await Conversation.findOne({ conversationId }, 'conversationId user').lean();
} catch (error) {
logger.error('[searchConversation] Error searching conversation', error);
throw new Error('Error searching conversation');
@ -23,7 +22,7 @@ const searchConversation = async (conversationId) => {
*/
const getConvo = async (user, conversationId) => {
try {
return await db.models.Conversation.findOne({ user, conversationId }).lean();
return await Conversation.findOne({ user, conversationId }).lean();
} catch (error) {
logger.error('[getConvo] Error getting single conversation', error);
return { message: 'Error getting single conversation' };
@ -40,7 +39,7 @@ const deleteNullOrEmptyConversations = async () => {
],
};
const result = await db.models.Conversation.deleteMany(filter);
const result = await Conversation.deleteMany(filter);
// Delete associated messages
const messageDeleteResult = await deleteMessages(filter);
@ -66,7 +65,7 @@ const deleteNullOrEmptyConversations = async () => {
*/
const getConvoFiles = async (conversationId) => {
try {
return (await db.models.Conversation.findOne({ conversationId }, 'files').lean())?.files ?? [];
return (await Conversation.findOne({ conversationId }, 'files').lean())?.files ?? [];
} catch (error) {
logger.error('[getConvoFiles] Error getting conversation files', error);
throw new Error('Error getting conversation files');
@ -112,7 +111,7 @@ module.exports = {
}
/** Note: the resulting Model object is necessary for Meilisearch operations */
const conversation = await db.models.Conversation.findOneAndUpdate(
const conversation = await Conversation.findOneAndUpdate(
{ conversationId, user: req.user.id },
updateOperation,
{
@ -141,7 +140,7 @@ module.exports = {
},
}));
const result = await db.models.Conversation.bulkWrite(bulkOps);
const result = await Conversation.bulkWrite(bulkOps);
return result;
} catch (error) {
logger.error('[saveBulkConversations] Error saving conversations in bulk', error);
@ -153,7 +152,6 @@ module.exports = {
{ cursor, limit = 25, isArchived = false, tags, search, order = 'desc' } = {},
) => {
const filters = [{ user }];
const { Conversation } = db.models;
if (isArchived) {
filters.push({ isArchived: true });
} else {
@ -217,7 +215,7 @@ module.exports = {
const conversationIds = convoIds.map((convo) => convo.conversationId);
const results = await db.models.Conversation.find({
const results = await Conversation.find({
user,
conversationId: { $in: conversationIds },
$or: [{ expiredAt: { $exists: false } }, { expiredAt: null }],
@ -286,7 +284,6 @@ module.exports = {
deleteConvos: async (user, filter) => {
try {
const userFilter = { ...filter, user };
const { Conversation } = db.models;
const conversations = await Conversation.find(userFilter).select('conversationId');
const conversationIds = conversations.map((c) => c.conversationId);