From ea2f2960a8e33b332875eb4ad20c9d8b65cdfaad Mon Sep 17 00:00:00 2001
From: Andrey Azov <andrey@ebi.ac.uk>
Date: Thu, 12 Aug 2021 00:01:48 +0100
Subject: [PATCH] Make sure to preserve original order of related articles

---
 src/controllers/articlesController.ts | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/controllers/articlesController.ts b/src/controllers/articlesController.ts
index 82849a5..edfcaff 100644
--- a/src/controllers/articlesController.ts
+++ b/src/controllers/articlesController.ts
@@ -69,5 +69,6 @@ const getTypeSpecificArticleFields = async (article: TextArticle | VideoArticle
 const populateRelatedArticles = async (article: TextArticle | VideoArticle) => {
   const relatedArticleIds = article.data?.relatedArticles || [];
   const relatedArticles = await Article.find({ id: In(relatedArticleIds) });
-  return relatedArticles.map(article => pick(article, ['title', 'type', 'url', 'slug']));
+  const sortedRelatedArticles = relatedArticleIds.map(id => relatedArticles.find(article => article.id === id));
+  return sortedRelatedArticles.map(article => pick(article, ['title', 'type', 'url', 'slug']));
 };
-- 
GitLab