option selection limit
This commit is contained in:
parent
cffe037269
commit
4725446613
@ -63,6 +63,7 @@ export interface DataItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Option {
|
export interface Option {
|
||||||
|
limit: number; //此选项总数限额, 0为无限
|
||||||
text: string;
|
text: string;
|
||||||
others: boolean;
|
others: boolean;
|
||||||
mustOthers?: boolean;
|
mustOthers?: boolean;
|
||||||
|
@ -2,13 +2,49 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { MongoRepository } from 'typeorm';
|
import { MongoRepository } from 'typeorm';
|
||||||
import { SurveyResponse } from 'src/models/surveyResponse.entity';
|
import { SurveyResponse } from 'src/models/surveyResponse.entity';
|
||||||
|
import { ResponseSchema } from 'src/models/responseSchema.entity';
|
||||||
|
import { EXCEPTION_CODE } from 'src/enums/exceptionCode';
|
||||||
|
import { HttpException } from 'src/exceptions/httpException';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SurveyResponseService {
|
export class SurveyResponseService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(SurveyResponse)
|
@InjectRepository(SurveyResponse)
|
||||||
private readonly surveyResponseRepository: MongoRepository<SurveyResponse>,
|
private readonly surveyResponseRepository: MongoRepository<SurveyResponse>,
|
||||||
|
@InjectRepository(ResponseSchema)
|
||||||
|
private readonly responseSchemaRepository: MongoRepository<ResponseSchema>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
//homework: 找到此问卷的scheme
|
||||||
|
async getResponseSchemaByPath(surveyPath: string) {
|
||||||
|
return this.responseSchemaRepository.findOne({
|
||||||
|
where: { surveyPath },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//homework: 提交问卷之前,计算此问卷(surveyPath)的某选择题(field)里某选项(hash)被选的总数
|
||||||
|
async getOptionsInResponseByHash(
|
||||||
|
surveyPath: string,
|
||||||
|
field: string,
|
||||||
|
hash: string,
|
||||||
|
) {
|
||||||
|
const myOptionsInResponse = await this.surveyResponseRepository.find({
|
||||||
|
where: {
|
||||||
|
surveyPath: surveyPath,
|
||||||
|
//[`data.${field}`]: { $or: [{ $eq: hash }, { $elemMatch: hash }] },
|
||||||
|
$or: [
|
||||||
|
{
|
||||||
|
[`data.${field}`]: hash,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[`data.${field}`]: { $elemMatch: { $eq: hash } },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const myOptionsCount = myOptionsInResponse.length;
|
||||||
|
return myOptionsCount;
|
||||||
|
}
|
||||||
|
|
||||||
async createSurveyResponse({
|
async createSurveyResponse({
|
||||||
data,
|
data,
|
||||||
clientTime,
|
clientTime,
|
||||||
@ -27,11 +63,40 @@ export class SurveyResponseService {
|
|||||||
optionTextAndId,
|
optionTextAndId,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//homework: 根据提交的数据,判断每个选项是否超额
|
||||||
|
const myScheme = await this.getResponseSchemaByPath(
|
||||||
|
newSubmitData.surveyPath,
|
||||||
|
);
|
||||||
|
let myJudge = 1;
|
||||||
|
for (const eachDataItem of myScheme.code.dataConf.dataList) {
|
||||||
|
if (eachDataItem.type === 'radio') {
|
||||||
|
const myField = eachDataItem.field;
|
||||||
|
for (const eachOption of eachDataItem.options) {
|
||||||
|
const myOptionHash = eachOption.hash;
|
||||||
|
if (newSubmitData.data[myField] === myOptionHash) {
|
||||||
|
const temCount = await this.getOptionsInResponseByHash(
|
||||||
|
newSubmitData.surveyPath,
|
||||||
|
myField,
|
||||||
|
myOptionHash,
|
||||||
|
);
|
||||||
|
if (temCount + 1 > eachOption.limit) myJudge = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 提交问卷
|
// 提交问卷
|
||||||
|
if (myJudge === 1) {
|
||||||
const res = await this.surveyResponseRepository.save(newSubmitData);
|
const res = await this.surveyResponseRepository.save(newSubmitData);
|
||||||
// res是加密后的数据,需要手动调用loaded才会触发解密
|
// res是加密后的数据,需要手动调用loaded才会触发解密
|
||||||
res.onDataLoaded();
|
res.onDataLoaded();
|
||||||
return res;
|
return res;
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
'超出提交总数限制',
|
||||||
|
EXCEPTION_CODE.RESPONSE_OVER_LIMIT,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSurveyResponseTotalByPath(surveyPath: string) {
|
async getSurveyResponseTotalByPath(surveyPath: string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user