import mongoose from 'mongoose' import paginate from 'mongoose-paginate-v2' const Schema = mongoose.Schema const ChatHistory = new Schema({ botId: { type: String }, buttons: { type: Array }, content: { type: String }, conversionId: { type: String }, fileUrl: { type: String }, goods: { type: Array }, like: { type: Boolean }, mediaType: { type: String }, msgId: { type: String }, position: { type: String }, quoteId: { type: String }, text: { type: String }, timestamp: { type: Number }, title: { type: String }, type: { type: String }, unlike: { type: Boolean }, unlikeReason: { type: String }, }) ChatHistory.index({ botId: 1, conversionId: 1 }) ChatHistory.plugin(paginate) ChatHistory.statics['getConversationList'] = async function ({ botId, conversionId, page, limit }) { const query = { botId, conversionId } const options = { limit, page, select: '-botId -conversionId', sort: { timestamp: -1 }, // Sorting by timestamp in descending order } // @ts-ignore const res = await this.paginate(query, options) return { list: res.docs, page: res.page, total: res.totalDocs, totalPage: res.totalPages, } } ChatHistory.statics['getAnswerByMsgId'] = async function ({ botId, conversionId, msgId }) { const query = { botId, conversionId, quoteId: msgId } const options = { select: '-botId -conversionId', sort: { timestamp: -1 }, // Sorting by timestamp in descending order } // @ts-ignore const res = await this.paginate(query, options) return { list: res.docs, total: res.totalDocs, } } const model = mongoose.model('ChatHistory', ChatHistory) export default model