feat: add validations
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
import { BadRequestException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { QuestionRangeRule } from '../../entities/question-range-rule.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Question } from '../../entities/question.entity';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
import { AppTimeoutException } from '@/common/exceptions/app-timeout.exception';
|
||||
import { AppException } from '@/common/exceptions/app.exception';
|
||||
import { CreateQuestionRangeRuleDTO } from '../../dtos/create-question-range-rule.dto';
|
||||
import { AppResponse } from '@/common/responses';
|
||||
import { UpdateQuestionRangeRuleDTO } from '../../dtos/update-question-range-rule.dto';
|
||||
|
||||
@Injectable()
|
||||
export class QuestionRangeRuleCommandProvider {
|
||||
constructor(
|
||||
/**
|
||||
* Inject QuestionRangeRules Repository
|
||||
*/
|
||||
@InjectRepository(QuestionRangeRule)
|
||||
private readonly questionRangeRulesRepository: Repository<QuestionRangeRule>,
|
||||
|
||||
/**
|
||||
* Inject Questions Repository
|
||||
*/
|
||||
@InjectRepository(Question)
|
||||
private readonly questionsRepository: Repository<Question>,
|
||||
|
||||
/**
|
||||
* Inject I18n Service
|
||||
*/
|
||||
private readonly i18nService: I18nService,
|
||||
) {}
|
||||
|
||||
private async checkQuestionExistance(questionId: number) {
|
||||
let question: Question | null = null;
|
||||
|
||||
try {
|
||||
question = await this.questionsRepository.findOneBy({
|
||||
id: questionId,
|
||||
});
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
|
||||
if (!question) {
|
||||
throw new AppException(
|
||||
'common.errors.entityNotFound',
|
||||
HttpStatus.NOT_FOUND,
|
||||
{ entity: '$t(questions.fields.question)' },
|
||||
);
|
||||
}
|
||||
|
||||
return question;
|
||||
}
|
||||
|
||||
private async checkRangeOverlap(
|
||||
question: number,
|
||||
min: number,
|
||||
max: number,
|
||||
excludeSelf: boolean = false,
|
||||
): Promise<void> {
|
||||
const queryBuilder = this.questionRangeRulesRepository
|
||||
.createQueryBuilder('rule')
|
||||
.where('rule.questionId = :question', { question })
|
||||
.andWhere(
|
||||
'(rule.min BETWEEN :min AND :max) OR (rule.max BETWEEN :min AND :max) OR (:min BETWEEN rule.min AND rule.max) OR (:max BETWEEN rule.min AND rule.max)',
|
||||
{ min, max },
|
||||
);
|
||||
|
||||
let overlappingRules: Array<QuestionRangeRule>;
|
||||
|
||||
try {
|
||||
overlappingRules = await queryBuilder.getMany();
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
|
||||
if (overlappingRules.length > (excludeSelf ? 1 : 0)) {
|
||||
throw new AppException(
|
||||
'questions.errors.rangeRuleIntersectionConstraint',
|
||||
HttpStatus.CONFLICT,
|
||||
{ min, max },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async checkRangeRuleExistance(rangeRuleId: number) {
|
||||
let questionRangeRule: QuestionRangeRule | null = null;
|
||||
|
||||
try {
|
||||
questionRangeRule = await this.questionRangeRulesRepository.findOne({
|
||||
where: {
|
||||
id: rangeRuleId,
|
||||
},
|
||||
relations: {
|
||||
question: true,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
|
||||
if (!questionRangeRule) {
|
||||
throw new AppException(
|
||||
'common.errors.entityNotFound',
|
||||
HttpStatus.NOT_FOUND,
|
||||
{ entity: '$t(questions.fields.rangeRule)' },
|
||||
);
|
||||
}
|
||||
|
||||
return questionRangeRule;
|
||||
}
|
||||
|
||||
public async createOne(
|
||||
createQuestionRangeRuleDto: CreateQuestionRangeRuleDTO,
|
||||
) {
|
||||
const question = await this.checkQuestionExistance(
|
||||
createQuestionRangeRuleDto.question,
|
||||
);
|
||||
|
||||
await this.checkRangeOverlap(
|
||||
question.id,
|
||||
createQuestionRangeRuleDto.min,
|
||||
createQuestionRangeRuleDto.max,
|
||||
);
|
||||
|
||||
const newQuestionRangeRule = this.questionRangeRulesRepository.create({
|
||||
...createQuestionRangeRuleDto,
|
||||
question,
|
||||
});
|
||||
|
||||
try {
|
||||
await this.questionRangeRulesRepository.save(newQuestionRangeRule);
|
||||
return new AppResponse(
|
||||
newQuestionRangeRule,
|
||||
this.i18nService.translate('common.messages.createSuccess', {
|
||||
args: { entity: '$t(questions.fields.rangeRule)' },
|
||||
}),
|
||||
);
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async updateOne(
|
||||
id: number,
|
||||
updateQuestionRangeRuleDto: UpdateQuestionRangeRuleDTO,
|
||||
) {
|
||||
const questionRangeRule = await this.checkRangeRuleExistance(id);
|
||||
|
||||
if (updateQuestionRangeRuleDto.question) {
|
||||
await this.checkQuestionExistance(updateQuestionRangeRuleDto.question);
|
||||
}
|
||||
|
||||
const questionId =
|
||||
updateQuestionRangeRuleDto.question !== undefined
|
||||
? updateQuestionRangeRuleDto.question
|
||||
: questionRangeRule.question.id;
|
||||
|
||||
const min =
|
||||
updateQuestionRangeRuleDto.min !== undefined
|
||||
? updateQuestionRangeRuleDto.min
|
||||
: questionRangeRule.min;
|
||||
|
||||
const max =
|
||||
updateQuestionRangeRuleDto.max !== undefined
|
||||
? updateQuestionRangeRuleDto.max
|
||||
: questionRangeRule.max;
|
||||
|
||||
if (min >= max) {
|
||||
throw new BadRequestException(
|
||||
'questions.errors.rangeRuleMinMaxConstraint',
|
||||
);
|
||||
}
|
||||
|
||||
await this.checkRangeOverlap(questionId, min, max, true);
|
||||
|
||||
Object.assign(questionRangeRule, updateQuestionRangeRuleDto);
|
||||
|
||||
try {
|
||||
return await this.questionRangeRulesRepository.save(questionRangeRule);
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteOne(id: number) {
|
||||
const questionRangeRule = await this.checkRangeRuleExistance(id);
|
||||
|
||||
try {
|
||||
await this.questionRangeRulesRepository.delete(questionRangeRule.id);
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user