2023-02-06 14:05:02 -05:00
|
|
|
const mongoose = require('mongoose');
|
2023-02-06 18:25:11 -05:00
|
|
|
const { getMessages } = require('./Message');
|
2023-02-06 14:05:02 -05:00
|
|
|
|
|
|
|
const convoSchema = mongoose.Schema({
|
|
|
|
conversationId: {
|
|
|
|
type: String,
|
|
|
|
unique: true,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
parentMessageId: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
},
|
2023-02-06 14:57:47 -05:00
|
|
|
title: {
|
|
|
|
type: String,
|
|
|
|
default: 'New conversation',
|
|
|
|
},
|
2023-02-06 14:05:02 -05:00
|
|
|
messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }],
|
|
|
|
created: {
|
|
|
|
type: Date,
|
|
|
|
default: Date.now
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const Conversation =
|
|
|
|
mongoose.models.Conversation || mongoose.model('Conversation', convoSchema);
|
|
|
|
|
|
|
|
module.exports = {
|
2023-02-06 14:57:47 -05:00
|
|
|
saveConversation: async ({ conversationId, parentMessageId, title }) => {
|
2023-02-06 18:25:11 -05:00
|
|
|
const messages = await getMessages({ conversationId });
|
2023-02-06 14:57:47 -05:00
|
|
|
const update = { parentMessageId, messages };
|
|
|
|
if (title) {
|
|
|
|
update.title = title;
|
|
|
|
}
|
2023-02-06 14:05:02 -05:00
|
|
|
|
|
|
|
await Conversation.findOneAndUpdate(
|
|
|
|
{ conversationId },
|
2023-02-06 14:57:47 -05:00
|
|
|
{ $set: update },
|
2023-02-06 14:05:02 -05:00
|
|
|
{ new: true, upsert: true }
|
|
|
|
).exec();
|
2023-02-06 15:17:54 -05:00
|
|
|
},
|
2023-02-06 16:00:59 -05:00
|
|
|
getConversations: async () => await Conversation.find({}).exec(),
|
2023-02-06 14:05:02 -05:00
|
|
|
};
|