diff --git a/server/src/enums/question.ts b/server/src/enums/question.ts new file mode 100644 index 00000000..e55b0c46 --- /dev/null +++ b/server/src/enums/question.ts @@ -0,0 +1,37 @@ +/** + * @description 问卷题目类型 + */ +export enum QUESTION_TYPE { + /** + * 单行输入框 + */ + TEXT = 'text', + /** + * 多行输入框 + */ + TEXTAREA = 'textarea', + /** + * 单项选择 + */ + RADIO = 'radio', + /** + * 多项选择 + */ + CHECKBOX = 'checkbox', + /** + * 判断题 + */ + BINARY_CHOICE = 'binary-choice', + /** + * 评分 + */ + RADIO_STAR = 'radio-star', + /** + * nps评分 + */ + RADIO_NPS = 'radio-nps', + /** + * 投票 + */ + VOTE = 'vote', +} diff --git a/server/src/modules/survey/controllers/dataStatistic.controller.ts b/server/src/modules/survey/controllers/dataStatistic.controller.ts index 452b4fc6..499744b4 100644 --- a/server/src/modules/survey/controllers/dataStatistic.controller.ts +++ b/server/src/modules/survey/controllers/dataStatistic.controller.ts @@ -22,6 +22,7 @@ import { HttpException } from 'src/exceptions/httpException'; import { EXCEPTION_CODE } from 'src/enums/exceptionCode'; import { AggregationStatisDto } from '../dto/aggregationStatis.dto'; import { handleAggretionData } from '../utils'; +import { QUESTION_TYPE } from 'src/enums/question'; @ApiTags('survey') @ApiBearerAuth() @@ -103,15 +104,15 @@ export class DataStatisticController { }; } const allowQuestionType = [ - 'radio', - 'checkbox', - 'binary-choice', - 'radio-star', - 'radio-nps', - 'vote', + QUESTION_TYPE.RADIO, + QUESTION_TYPE.CHECKBOX, + QUESTION_TYPE.BINARY_CHOICE, + QUESTION_TYPE.RADIO_STAR, + QUESTION_TYPE.RADIO_NPS, + QUESTION_TYPE.VOTE, ]; const fieldList = responseSchema.code.dataConf.dataList - .filter((item) => allowQuestionType.includes(item.type)) + .filter((item) => allowQuestionType.includes(item.type as QUESTION_TYPE)) .map((item) => item.field); const dataMap = responseSchema.code.dataConf.dataList.reduce((pre, cur) => { pre[cur.field] = cur; diff --git a/server/src/modules/survey/services/dataStatistic.service.ts b/server/src/modules/survey/services/dataStatistic.service.ts index ce31fed6..6ccfb5db 100644 --- a/server/src/modules/survey/services/dataStatistic.service.ts +++ b/server/src/modules/survey/services/dataStatistic.service.ts @@ -8,9 +8,10 @@ import { keyBy } from 'lodash'; import { DataItem } from 'src/interfaces/survey'; import { ResponseSchema } from 'src/models/responseSchema.entity'; import { getListHeadByDataList, transformAndMergeArrayFields } from '../utils'; +import { QUESTION_TYPE } from 'src/enums/question'; @Injectable() export class DataStatisticService { - private radioType = ['radio-star', 'radio-nps']; + private radioType = [QUESTION_TYPE.RADIO_STAR, QUESTION_TYPE.RADIO_NPS]; constructor( @InjectRepository(SurveyResponse) @@ -68,7 +69,7 @@ export class DataStatisticService { } // 处理选项的更多输入框 if ( - this.radioType.includes(itemConfig.type) && + this.radioType.includes(itemConfig.type as QUESTION_TYPE) && !data[`${itemConfigKey}_custom`] ) { data[`${itemConfigKey}_custom`] = diff --git a/server/src/modules/survey/utils/index.ts b/server/src/modules/survey/utils/index.ts index 0baee98e..4f9a17bc 100644 --- a/server/src/modules/survey/utils/index.ts +++ b/server/src/modules/survey/utils/index.ts @@ -5,6 +5,7 @@ import normalCode from '../template/surveyTemplate/survey/normal.json'; import npsCode from '../template/surveyTemplate/survey/nps.json'; import registerCode from '../template/surveyTemplate/survey/register.json'; import voteCode from '../template/surveyTemplate/survey/vote.json'; +import { QUESTION_TYPE } from 'src/enums/question'; const schemaDataMap = { normal: normalCode, @@ -31,7 +32,7 @@ export async function getSchemaBySurveyType(surveyType: string) { export function getListHeadByDataList(dataList) { const listHead = dataList.map((question) => { let othersCode; - const radioType = ['radio-star', 'radio-nps']; + const radioType = [QUESTION_TYPE.RADIO_STAR, QUESTION_TYPE.RADIO_NPS]; if (radioType.includes(question.type)) { const rangeConfigKeys = question.rangeConfig ? Object.keys(question.rangeConfig) @@ -59,12 +60,12 @@ export function getListHeadByDataList(dataList) { listHead.push({ field: 'difTime', title: '答题耗时(秒)', - type: 'text', + type: QUESTION_TYPE.TEXT, }); listHead.push({ field: 'createDate', title: '提交时间', - type: 'text', + type: QUESTION_TYPE.TEXT, }); return listHead; } @@ -111,7 +112,14 @@ export function handleAggretionData({ dataMap, item }) { pre[cur.id] = cur; return pre; }, {}); - if (['radio', 'checkbox', 'vote', 'binary-choice'].includes(type)) { + if ( + [ + QUESTION_TYPE.RADIO, + QUESTION_TYPE.CHECKBOX, + QUESTION_TYPE.VOTE, + QUESTION_TYPE.BINARY_CHOICE, + ].includes(type) + ) { return { ...item, title: dataMap[item.field].title, @@ -127,7 +135,9 @@ export function handleAggretionData({ dataMap, item }) { }), }, }; - } else if (['radio-star', 'radio-nps'].includes(type)) { + } else if ( + [QUESTION_TYPE.RADIO_STAR, QUESTION_TYPE.RADIO_NPS].includes(type) + ) { const summary: Record = {}; const average = getAverage({ aggregation: item.data.aggregation }); const median = getMedian({ aggregation: item.data.aggregation }); @@ -138,10 +148,10 @@ export function handleAggretionData({ dataMap, item }) { summary['average'] = average; summary['median'] = median; summary['variance'] = variance; - if (type === 'radio-nps') { + if (type === QUESTION_TYPE.RADIO_NPS) { summary['nps'] = getNps({ aggregation: item.data.aggregation }); } - const range = type === 'radio-nps' ? [0, 10] : [1, 5]; + const range = type === QUESTION_TYPE.RADIO_NPS ? [0, 10] : [1, 5]; const arr = []; for (let i = range[0]; i <= range[1]; i++) { arr.push(i);