diff --git a/controllers/AI.js b/controllers/AI.js index 7d57c9f21369dbdb5ec2920683936558969dcd92..8690107dd5a154a5a5ed3cb1f900c1ee0c030aff 100644 --- a/controllers/AI.js +++ b/controllers/AI.js @@ -121,7 +121,9 @@ export const getChat = async (req, res, next) => { */ export const getChats = async (req, res, next) => { try { + // TODO sort chats by createdAt const chats = await findRecords(Chat, { createdBy: global.currentUserId }); + // const chats = await Chat.find({ createdBy: global.currentUserId }).sort({ 'createdAt': 1 }); return res.json({ chats }); } catch (error) { next(error); diff --git a/models/Chat.js b/models/Chat.js index d78331b7e66f4a91371a4ff6fa43fddfe3accd59..46ad9bb5a6ceac9fb4485ec80afc36b1fa116a10 100644 --- a/models/Chat.js +++ b/models/Chat.js @@ -57,6 +57,13 @@ ChatSchema.pre('save', async function (next) { // ################################# STATICS // ################################# QUERY HELPERS +ChatSchema.pre('find', function (next) { + // set default sort + if (typeof this.options.sort === 'undefined') { + this.options.sort = { createdAt: 1 }; + } + next(); +}); // ################################# INSTANCE METHODS /** diff --git a/models/User.js b/models/User.js index b05f5b4582ce1ad11a123fcc4246b43e0353e2b7..7ef3597ab30686d80162318656e16a4c5119dacd 100644 --- a/models/User.js +++ b/models/User.js @@ -107,6 +107,13 @@ UserSchema.pre('save', async function (next) { // UserSchema.query.byMail = function(email){ // return this.where({email: new RegExp(email, "i")}) // } +UserSchema.pre('find', function (next) { + // set default sort + if (typeof this.options.sort === 'undefined') { + this.options.sort = { username: 1 }; + } + next(); +}); // ################################# INSTANCE METHODS // example method diff --git a/routes/ai.js b/routes/ai.js index 3792abd0e57d75b628d71dd4bcae86b2e16ae442..bfbbecdd3f08044977c66acb16a97cdf66387e68 100644 --- a/routes/ai.js +++ b/routes/ai.js @@ -91,7 +91,6 @@ router.post('/chat', verifyAccessToken, validate(chatSchema), checkRequestedMode * @header {authorization} Bearer [required] access token * @return {object} list of found conversations, ordered by updated */ -// TODO sort chats by createdAt router.get('/chats', verifyAccessToken, getChats); export default router; \ No newline at end of file