Futa Arai 3 лет назад
Родитель
Сommit
03a6b6b581

+ 7 - 0
packages/app/src/server/crowi/index.js

@@ -31,6 +31,7 @@ import PageOperationService from '../service/page-operation';
 // eslint-disable-next-line import/no-cycle
 import { PluginService } from '../service/plugin';
 import QuestionnaireCronService from '../service/questionnaire-cron';
+import QuestionnaireInfoService from '../service/questionnaire-info';
 import SearchService from '../service/search';
 import { SlackIntegrationService } from '../service/slack-integration';
 import { UserNotificationService } from '../service/user-notification';
@@ -84,6 +85,7 @@ function Crowi() {
   this.activityService = null;
   this.commentService = null;
   this.xss = new Xss();
+  this.questionnaireInfoService = null;
   this.questionnaireCronService = null;
 
   this.tokens = null;
@@ -148,6 +150,7 @@ Crowi.prototype.init = async function() {
     this.setupActivityService(),
     this.setupCommentService(),
     this.setupSyncPageStatusService(),
+    this.setupQuestionnaireInfoService(),
     this.setUpCustomize(), // depends on pluginService
   ]);
 
@@ -318,6 +321,10 @@ Crowi.prototype.setupCron = function() {
   this.questionnaireCronService.startCron();
 };
 
+Crowi.prototype.setupQuestionnaireInfoService = function() {
+  this.questionnaireInfoService = new QuestionnaireInfoService(this);
+};
+
 Crowi.prototype.scanRuntimeVersions = async function() {
   const self = this;
 

+ 2 - 53
packages/app/src/server/routes/apiv3/questionnaire.js

@@ -1,10 +1,6 @@
-import crypto from 'crypto';
-import * as os from 'node:os';
-
 import { Router } from 'express';
 
 import { StatusType } from '~/interfaces/questionnaire/questionnaire-answer-status';
-import { UserType } from '~/interfaces/questionnaire/user-info';
 import QuestionnaireAnswerStatus from '~/server/models/questionnaire/questionnaire-answer-status';
 import QuestionnaireOrder from '~/server/models/questionnaire/questionnaire-order';
 import axios from '~/utils/axios';
@@ -55,57 +51,10 @@ module.exports = (crowi) => {
   });
 
   router.put('/answer', loginRequired, async(req, res) => {
-    const getUserInfo = (user, appSiteUrlHashed) => {
-      if (user) {
-        const hasher = crypto.createHmac('sha256', appSiteUrlHashed);
-        hasher.update(user._id.toString());
-
-        return {
-          userIdHash: hasher.digest('hex'),
-          type: user.admin ? UserType.admin : UserType.general,
-          userCreatedAt: user.createdAt,
-        };
-      }
-
-      return { type: UserType.guest };
-    };
-
-    const getGrowiInfo = async() => {
-      const appSiteUrl = crowi.appService.getSiteUrl();
-      const hasher = crypto.createHash('sha256');
-      hasher.update(appSiteUrl);
-      const appSiteUrlHashed = hasher.digest('hex');
-
-      const currentUsersCount = await User.countDocuments();
-      const currentActiveUsersCount = await User.countActiveUsers();
-      const attachmentType = crowi.configManager.getConfig('crowi', 'app:fileUploadType');
-
-      return {
-        version: crowi.version,
-        osInfo: {
-          type: os.type(),
-          platform: os.platform(),
-          arch: os.arch(),
-          totalmem: os.totalmem(),
-        },
-        appSiteUrl,
-        appSiteUrlHashed,
-        type: 'cloud', // TODO: set actual value
-        currentUsersCount,
-        currentActiveUsersCount,
-        wikiType: 'open', // TODO: set actual value
-        attachmentType,
-        activeExternalAccountTypes: undefined, // TODO: set actual value
-        deploymentType: undefined, // TODO: set actual value
-      };
-    };
-
     const sendQuestionnaireAnswer = async(user, answers) => {
       const growiQuestionnaireServerOrigin = crowi.configManager?.getConfig('crowi', 'app:growiQuestionnaireServerOrigin');
-
-      const growiInfo = await getGrowiInfo();
-
-      const userInfo = getUserInfo(user, growiInfo.appSiteUrlHashed);
+      const growiInfo = await crowi.questionnaireInfoService.getGrowiInfo();
+      const userInfo = crowi.questionnaireInfoService.getUserInfo(user, growiInfo.appSiteUrlHashed);
 
       const questionnaireAnswer = {
         growiInfo,

+ 66 - 0
packages/app/src/server/service/questionnaire-info.ts

@@ -0,0 +1,66 @@
+import crypto from 'crypto';
+import * as os from 'node:os';
+
+import { IGrowiInfo } from '~/interfaces/questionnaire/growi-info';
+import { IUserInfo, UserType } from '~/interfaces/questionnaire/user-info';
+import { IUserHasId } from '~/interfaces/user';
+
+class QuestionnaireInfoService {
+
+  crowi: any;
+
+  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
+  constructor(crowi) {
+    this.crowi = crowi;
+  }
+
+  async getGrowiInfo(): Promise<IGrowiInfo> {
+    const User = this.crowi.model('User');
+
+    const appSiteUrl = this.crowi.appService.getSiteUrl();
+    const hasher = crypto.createHash('sha256');
+    hasher.update(appSiteUrl);
+    const appSiteUrlHashed = hasher.digest('hex');
+
+    const currentUsersCount = await User.countDocuments();
+    const currentActiveUsersCount = await User.countActiveUsers();
+    const attachmentType = this.crowi.configManager.getConfig('crowi', 'app:fileUploadType');
+
+    return {
+      version: this.crowi.version,
+      osInfo: {
+        type: os.type(),
+        platform: os.platform(),
+        arch: os.arch(),
+        totalmem: os.totalmem(),
+      },
+      appSiteUrl,
+      appSiteUrlHashed,
+      type: 'cloud', // TODO: set actual value
+      currentUsersCount,
+      currentActiveUsersCount,
+      wikiType: 'open', // TODO: set actual value
+      attachmentType,
+      activeExternalAccountTypes: undefined, // TODO: set actual value
+      deploymentType: undefined, // TODO: set actual value
+    };
+  }
+
+  getUserInfo(user: IUserHasId, appSiteUrlHashed: string): IUserInfo {
+    if (user) {
+      const hasher = crypto.createHmac('sha256', appSiteUrlHashed);
+      hasher.update(user._id.toString());
+
+      return {
+        userIdHash: hasher.digest('hex'),
+        type: user.admin ? UserType.admin : UserType.general,
+        userCreatedAt: user.createdAt,
+      };
+    }
+
+    return { type: UserType.guest };
+  }
+
+}
+
+export default QuestionnaireInfoService;