feat: add admin panel dto & validation
This commit is contained in:
@@ -12,6 +12,7 @@ import { UpdateQuestionDTO } from '../dtos/update-question.dto';
|
||||
import { validateMetadata } from '../helpers/validate-metadata.helper';
|
||||
import { AppException } from '@/common/exceptions/app.exception';
|
||||
import { Test } from '@/modules/tests/entities/test.entity';
|
||||
import { QuestionType } from '../enums/question-type.enum';
|
||||
|
||||
@Injectable()
|
||||
export class QuestionsCommandProvider {
|
||||
@@ -39,6 +40,16 @@ export class QuestionsCommandProvider {
|
||||
) {}
|
||||
|
||||
public async createOne(createQuestionDto: CreateQuestionDTO) {
|
||||
const metadataErrors = await validateMetadata(
|
||||
createQuestionDto.type ?? QuestionType.SINGLE_CHOICE,
|
||||
createQuestionDto.metadata ?? null,
|
||||
);
|
||||
if (metadataErrors.length)
|
||||
throw new AppException(
|
||||
'validation.wrongFieldFormat',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
{ field: '$t(questions.fields.metadata)' },
|
||||
);
|
||||
const test = await this.testsService.adminGetOne(createQuestionDto.test);
|
||||
|
||||
const newQuestion = this.questionsRepository.create({
|
||||
@@ -84,7 +95,11 @@ export class QuestionsCommandProvider {
|
||||
}
|
||||
|
||||
if (updateQuestionDto.test) {
|
||||
await this.testsService.adminGetOne(updateQuestionDto.test);
|
||||
if (updateQuestionDto.test !== question.testId) {
|
||||
throw new ConflictException(
|
||||
'جابجایی سؤال به آزمون دیگر مجاز نیست؛ سؤال جدید بسازید.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const type =
|
||||
@@ -107,7 +122,9 @@ export class QuestionsCommandProvider {
|
||||
);
|
||||
}
|
||||
|
||||
Object.assign(question, updateQuestionDto);
|
||||
const changes = { ...updateQuestionDto };
|
||||
delete changes.test;
|
||||
Object.assign(question, changes);
|
||||
|
||||
try {
|
||||
return await this.questionsRepository.save(question);
|
||||
|
||||
Reference in New Issue
Block a user