feat: 题型硬编码优化 (#343)
Co-authored-by: jiangchunfu <jiangchunfu@kaike.la>
This commit is contained in:
parent
da1749fb53
commit
2b32850046
37
server/src/enums/question.ts
Normal file
37
server/src/enums/question.ts
Normal file
@ -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',
|
||||||
|
}
|
@ -22,6 +22,7 @@ import { HttpException } from 'src/exceptions/httpException';
|
|||||||
import { EXCEPTION_CODE } from 'src/enums/exceptionCode';
|
import { EXCEPTION_CODE } from 'src/enums/exceptionCode';
|
||||||
import { AggregationStatisDto } from '../dto/aggregationStatis.dto';
|
import { AggregationStatisDto } from '../dto/aggregationStatis.dto';
|
||||||
import { handleAggretionData } from '../utils';
|
import { handleAggretionData } from '../utils';
|
||||||
|
import { QUESTION_TYPE } from 'src/enums/question';
|
||||||
|
|
||||||
@ApiTags('survey')
|
@ApiTags('survey')
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@ -103,15 +104,15 @@ export class DataStatisticController {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
const allowQuestionType = [
|
const allowQuestionType = [
|
||||||
'radio',
|
QUESTION_TYPE.RADIO,
|
||||||
'checkbox',
|
QUESTION_TYPE.CHECKBOX,
|
||||||
'binary-choice',
|
QUESTION_TYPE.BINARY_CHOICE,
|
||||||
'radio-star',
|
QUESTION_TYPE.RADIO_STAR,
|
||||||
'radio-nps',
|
QUESTION_TYPE.RADIO_NPS,
|
||||||
'vote',
|
QUESTION_TYPE.VOTE,
|
||||||
];
|
];
|
||||||
const fieldList = responseSchema.code.dataConf.dataList
|
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);
|
.map((item) => item.field);
|
||||||
const dataMap = responseSchema.code.dataConf.dataList.reduce((pre, cur) => {
|
const dataMap = responseSchema.code.dataConf.dataList.reduce((pre, cur) => {
|
||||||
pre[cur.field] = cur;
|
pre[cur.field] = cur;
|
||||||
|
@ -8,9 +8,10 @@ import { keyBy } from 'lodash';
|
|||||||
import { DataItem } from 'src/interfaces/survey';
|
import { DataItem } from 'src/interfaces/survey';
|
||||||
import { ResponseSchema } from 'src/models/responseSchema.entity';
|
import { ResponseSchema } from 'src/models/responseSchema.entity';
|
||||||
import { getListHeadByDataList, transformAndMergeArrayFields } from '../utils';
|
import { getListHeadByDataList, transformAndMergeArrayFields } from '../utils';
|
||||||
|
import { QUESTION_TYPE } from 'src/enums/question';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DataStatisticService {
|
export class DataStatisticService {
|
||||||
private radioType = ['radio-star', 'radio-nps'];
|
private radioType = [QUESTION_TYPE.RADIO_STAR, QUESTION_TYPE.RADIO_NPS];
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(SurveyResponse)
|
@InjectRepository(SurveyResponse)
|
||||||
@ -68,7 +69,7 @@ export class DataStatisticService {
|
|||||||
}
|
}
|
||||||
// 处理选项的更多输入框
|
// 处理选项的更多输入框
|
||||||
if (
|
if (
|
||||||
this.radioType.includes(itemConfig.type) &&
|
this.radioType.includes(itemConfig.type as QUESTION_TYPE) &&
|
||||||
!data[`${itemConfigKey}_custom`]
|
!data[`${itemConfigKey}_custom`]
|
||||||
) {
|
) {
|
||||||
data[`${itemConfigKey}_custom`] =
|
data[`${itemConfigKey}_custom`] =
|
||||||
|
@ -5,6 +5,7 @@ import normalCode from '../template/surveyTemplate/survey/normal.json';
|
|||||||
import npsCode from '../template/surveyTemplate/survey/nps.json';
|
import npsCode from '../template/surveyTemplate/survey/nps.json';
|
||||||
import registerCode from '../template/surveyTemplate/survey/register.json';
|
import registerCode from '../template/surveyTemplate/survey/register.json';
|
||||||
import voteCode from '../template/surveyTemplate/survey/vote.json';
|
import voteCode from '../template/surveyTemplate/survey/vote.json';
|
||||||
|
import { QUESTION_TYPE } from 'src/enums/question';
|
||||||
|
|
||||||
const schemaDataMap = {
|
const schemaDataMap = {
|
||||||
normal: normalCode,
|
normal: normalCode,
|
||||||
@ -31,7 +32,7 @@ export async function getSchemaBySurveyType(surveyType: string) {
|
|||||||
export function getListHeadByDataList(dataList) {
|
export function getListHeadByDataList(dataList) {
|
||||||
const listHead = dataList.map((question) => {
|
const listHead = dataList.map((question) => {
|
||||||
let othersCode;
|
let othersCode;
|
||||||
const radioType = ['radio-star', 'radio-nps'];
|
const radioType = [QUESTION_TYPE.RADIO_STAR, QUESTION_TYPE.RADIO_NPS];
|
||||||
if (radioType.includes(question.type)) {
|
if (radioType.includes(question.type)) {
|
||||||
const rangeConfigKeys = question.rangeConfig
|
const rangeConfigKeys = question.rangeConfig
|
||||||
? Object.keys(question.rangeConfig)
|
? Object.keys(question.rangeConfig)
|
||||||
@ -59,12 +60,12 @@ export function getListHeadByDataList(dataList) {
|
|||||||
listHead.push({
|
listHead.push({
|
||||||
field: 'difTime',
|
field: 'difTime',
|
||||||
title: '答题耗时(秒)',
|
title: '答题耗时(秒)',
|
||||||
type: 'text',
|
type: QUESTION_TYPE.TEXT,
|
||||||
});
|
});
|
||||||
listHead.push({
|
listHead.push({
|
||||||
field: 'createDate',
|
field: 'createDate',
|
||||||
title: '提交时间',
|
title: '提交时间',
|
||||||
type: 'text',
|
type: QUESTION_TYPE.TEXT,
|
||||||
});
|
});
|
||||||
return listHead;
|
return listHead;
|
||||||
}
|
}
|
||||||
@ -111,7 +112,14 @@ export function handleAggretionData({ dataMap, item }) {
|
|||||||
pre[cur.id] = cur;
|
pre[cur.id] = cur;
|
||||||
return pre;
|
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 {
|
return {
|
||||||
...item,
|
...item,
|
||||||
title: dataMap[item.field].title,
|
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<string, any> = {};
|
const summary: Record<string, any> = {};
|
||||||
const average = getAverage({ aggregation: item.data.aggregation });
|
const average = getAverage({ aggregation: item.data.aggregation });
|
||||||
const median = getMedian({ aggregation: item.data.aggregation });
|
const median = getMedian({ aggregation: item.data.aggregation });
|
||||||
@ -138,10 +148,10 @@ export function handleAggretionData({ dataMap, item }) {
|
|||||||
summary['average'] = average;
|
summary['average'] = average;
|
||||||
summary['median'] = median;
|
summary['median'] = median;
|
||||||
summary['variance'] = variance;
|
summary['variance'] = variance;
|
||||||
if (type === 'radio-nps') {
|
if (type === QUESTION_TYPE.RADIO_NPS) {
|
||||||
summary['nps'] = getNps({ aggregation: item.data.aggregation });
|
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 = [];
|
const arr = [];
|
||||||
for (let i = range[0]; i <= range[1]; i++) {
|
for (let i = range[0]; i <= range[1]; i++) {
|
||||||
arr.push(i);
|
arr.push(i);
|
||||||
|
Loading…
Reference in New Issue
Block a user