From 1f26d15f0944ca5aa54b25d4a6a8e9b7da2f66f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9EBAS8243=E2=80=9C?= <gerd.embruch@uni-hamburg.de> Date: Fri, 9 Aug 2024 14:39:14 +0200 Subject: [PATCH] added default sort --- controllers/AI.js | 2 ++ models/Chat.js | 7 +++++++ models/User.js | 7 +++++++ routes/ai.js | 1 - 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/controllers/AI.js b/controllers/AI.js index 7d57c9f..8690107 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 d78331b..46ad9bb 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 b05f5b4..7ef3597 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 3792abd..bfbbecd 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 -- GitLab