2023-03-22 01:31:01 -04:00
|
|
|
const Message = require('./schema/messageSchema');
|
2023-05-09 23:51:39 +02:00
|
|
|
|
2023-02-05 23:05:07 -05:00
|
|
|
module.exports = {
|
2023-03-16 16:22:08 -04:00
|
|
|
Message,
|
2023-05-18 11:09:31 -07:00
|
|
|
|
2023-05-09 23:51:39 +02:00
|
|
|
async saveMessage({
|
2023-04-04 01:10:50 +08:00
|
|
|
messageId,
|
|
|
|
|
newMessageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
sender,
|
|
|
|
|
text,
|
|
|
|
|
isCreatedByUser = false,
|
2023-04-11 03:26:38 +08:00
|
|
|
error,
|
|
|
|
|
unfinished,
|
|
|
|
|
cancelled
|
2023-05-09 23:51:39 +02:00
|
|
|
}) {
|
2023-02-08 00:02:29 -05:00
|
|
|
try {
|
2023-04-09 11:17:08 -04:00
|
|
|
// may also need to update the conversation here
|
2023-04-04 01:10:50 +08:00
|
|
|
await Message.findOneAndUpdate(
|
|
|
|
|
{ messageId },
|
|
|
|
|
{
|
|
|
|
|
messageId: newMessageId || messageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
sender,
|
|
|
|
|
text,
|
|
|
|
|
isCreatedByUser,
|
2023-04-11 03:26:38 +08:00
|
|
|
error,
|
|
|
|
|
unfinished,
|
|
|
|
|
cancelled
|
2023-04-04 01:10:50 +08:00
|
|
|
},
|
|
|
|
|
{ upsert: true, new: true }
|
|
|
|
|
);
|
2023-05-18 11:09:31 -07:00
|
|
|
|
2023-05-09 23:51:39 +02:00
|
|
|
return {
|
|
|
|
|
messageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
sender,
|
|
|
|
|
text,
|
|
|
|
|
isCreatedByUser
|
|
|
|
|
};
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`Error saving message: ${err}`);
|
|
|
|
|
throw new Error('Failed to save message.');
|
2023-02-08 00:02:29 -05:00
|
|
|
}
|
2023-02-06 14:05:02 -05:00
|
|
|
},
|
2023-05-18 11:09:31 -07:00
|
|
|
|
2023-05-09 23:51:39 +02:00
|
|
|
async deleteMessagesSince({ messageId, conversationId }) {
|
2023-03-13 05:26:17 +08:00
|
|
|
try {
|
2023-04-04 01:10:50 +08:00
|
|
|
const message = await Message.findOne({ messageId }).exec();
|
2023-03-13 05:26:17 +08:00
|
|
|
|
2023-05-09 23:51:39 +02:00
|
|
|
if (message) {
|
2023-04-04 01:10:50 +08:00
|
|
|
return await Message.find({ conversationId })
|
|
|
|
|
.deleteMany({ createdAt: { $gt: message.createdAt } })
|
|
|
|
|
.exec();
|
2023-05-09 23:51:39 +02:00
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`Error deleting messages: ${err}`);
|
|
|
|
|
throw new Error('Failed to delete messages.');
|
2023-03-13 05:26:17 +08:00
|
|
|
}
|
|
|
|
|
},
|
2023-05-18 11:09:31 -07:00
|
|
|
|
2023-05-09 23:51:39 +02:00
|
|
|
async getMessages(filter) {
|
2023-02-08 15:26:42 -05:00
|
|
|
try {
|
2023-04-04 01:10:50 +08:00
|
|
|
return await Message.find(filter).sort({ createdAt: 1 }).exec();
|
2023-05-09 23:51:39 +02:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`Error getting messages: ${err}`);
|
|
|
|
|
throw new Error('Failed to get messages.');
|
2023-02-08 15:26:42 -05:00
|
|
|
}
|
|
|
|
|
},
|
2023-05-18 11:09:31 -07:00
|
|
|
|
2023-05-09 23:51:39 +02:00
|
|
|
async deleteMessages(filter) {
|
2023-02-08 15:26:42 -05:00
|
|
|
try {
|
2023-04-04 01:10:50 +08:00
|
|
|
return await Message.deleteMany(filter).exec();
|
2023-05-09 23:51:39 +02:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`Error deleting messages: ${err}`);
|
|
|
|
|
throw new Error('Failed to delete messages.');
|
2023-02-08 15:26:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-04 01:10:50 +08:00
|
|
|
};
|