فهرست منبع

disbale to merge twice / create sort function and sorting contributors

ryohi15 5 سال پیش
والد
کامیت
e215e309e3
1فایلهای تغییر یافته به همراه22 افزوده شده و 5 حذف شده
  1. 22 5
      src/server/routes/apiv3/staffs.js

+ 22 - 5
src/server/routes/apiv3/staffs.js

@@ -13,6 +13,7 @@ const contributors = require('../../../../resource/Contributor');
 
 let expiredAt;
 let contributorsCache = contributors;
+let gcContributors;
 
 
 module.exports = (crowi) => {
@@ -23,15 +24,31 @@ module.exports = (crowi) => {
 
     if (expiredAt == null || isAfter(now, expiredAt) || growiCloudUri != null) {
       const url = new URL('_api/staffCredit', growiCloudUri);
-      const growiContributors = contributors.slice(0, 1);
-      const otherContributors = contributors.slice(1);
+
+      const compareFunction = function (a, b) {
+        const aOrder = a["order"];
+        const bOrder = b["order"];
+        if (aOrder < bOrder) {
+          return -1;
+        }
+        if(aOrder > bOrder) {
+          return 1
+        }
+        return 0;
+      }
       try {
         const gcContributorsRes = await axios.get(url.toString());
-        const gcContributors = gcContributorsRes.data;
+        if (gcContributors == null) {
+          gcContributors = gcContributorsRes.data;
+          // Give order to gcContributors
+          gcContributors.order = 2;
+          // merging contributors
+          contributorsCache.push(gcContributors);
+        }
+        // Change the order of section
+        contributorsCache.sort(compareFunction);
         // caching 'expiredAt' for 1 hour
         expiredAt = addHours(now, 1);
-        // caching merged contributors
-        contributorsCache = growiContributors.concat(gcContributors, otherContributors);
       }
       catch (err) {
         logger.warn('Getting GROWI.cloud staffcredit is failed');