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, /** * Inject Questions Repository */ @InjectRepository(Question) private readonly questionsRepository: Repository, /** * 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, excludeId?: number, ): Promise { const queryBuilder = this.questionRangeRulesRepository .createQueryBuilder('rule') .where({ question: { id: 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 }, ); if (excludeId !== undefined) queryBuilder.andWhere('rule.id != :excludeId', { excludeId }); let overlappingRules: Array; try { overlappingRules = await queryBuilder.getMany(); } catch (err) { throw new AppTimeoutException(err); } if (overlappingRules.length > 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, id); const changes = { ...updateQuestionRangeRuleDto }; delete changes.question; Object.assign(questionRangeRule, changes, { question: { id: questionId }, questionId, }); 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); } } }