feat: 题型硬编码优化 (#343)

Co-authored-by: jiangchunfu <jiangchunfu@kaike.la>
This commit is contained in:
Jiangchunfu 2024-07-16 11:25:08 +08:00 committed by sudoooooo
parent 1eabbf5df2
commit c0387b1521
4 changed files with 65 additions and 16 deletions

View 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',
}

View File

@ -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;

View File

@ -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`] =

View File

@ -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<string, any> = {};
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);