import { BadRequestException, HttpStatus, Injectable } from '@nestjs/common'; import { CreateTestDTO } from '../dtos/create-test.dto'; import { InjectRepository } from '@nestjs/typeorm'; import { Test } from '../entities/test.entity'; import { Repository } from 'typeorm'; import { I18nService } from 'nestjs-i18n'; import { AppException } from '@/common/exceptions/app.exception'; import { AppResponse } from '@/common/responses'; import { AppTimeoutException } from '@/common/exceptions/app-timeout.exception'; import { UpdateTestDTO } from '../dtos/update-test.dto'; import { AccessType } from '../enums/access-types.enum'; import { PublishTestValidatorProvider } from './publish-test-validator.provider'; import { TestStatus } from '../enums/test-status.enum'; @Injectable() export class TestsCommandProvider { constructor( /** * Inject Tests Repository */ @InjectRepository(Test) private readonly testRepository: Repository, /** * Inject I18n Service */ private readonly i18nService: I18nService, /** * Inject Publish Test Validator Provider */ private readonly publishTestValidatorProvider: PublishTestValidatorProvider, ) {} public async createOne(createTestDto: CreateTestDTO) { const titleExists = await this.testRepository.findOne({ where: { title: createTestDto.title, }, }); if (titleExists) { throw new AppException('validation.duplicateField', HttpStatus.CONFLICT, { field: '$t(tests.fields.title)', }); } const newTest = this.testRepository.create(createTestDto); try { await this.testRepository.save(newTest); return new AppResponse( newTest, this.i18nService.translate('common.messages.createSuccess', { args: { entity: '$t(tests.fields.test)' }, }), ); } catch (err) { throw new AppTimeoutException(err); } } public async updateOne(id: number, updateTestDto: UpdateTestDTO) { const test = await this.testRepository.findOneBy({ id }); if (!test) { throw new AppException( 'common.errors.entityNotFound', HttpStatus.NOT_FOUND, { entity: '$t(tests.fields.test)' }, ); } const accessType = updateTestDto.accessType ?? test.accessType; const price = updateTestDto.price !== undefined ? updateTestDto.price : test.price; if ( (accessType === AccessType.PAID && price == null) || (accessType !== AccessType.PAID && price != null) ) { throw new BadRequestException('tests.errors.priceAccessConstraint'); } Object.assign(test, updateTestDto); if (test.status === TestStatus.PUBLISHED) { await this.publishTestValidatorProvider.validate(test.id); test.structureLocked = true; } try { return await this.testRepository.save(test); } catch (err) { throw new AppTimeoutException(err); } } public async deleteOne(id: number) { let test: Test | null = null; try { test = await this.testRepository.findOneBy({ id }); } catch (err) { throw new AppTimeoutException(err); } if (!test) throw new AppException( 'common.errors.entityNotFound', HttpStatus.NOT_FOUND, { entity: '$t(tests.fields.test)' }, ); try { await this.testRepository.delete(test.id); } catch (err) { throw new AppTimeoutException(err); } } public async incrementQuestionsCount(testId: number, count?: number) { try { await this.testRepository.increment( { id: testId }, 'questionsCount', count || 1, ); } catch (err) { throw new AppTimeoutException(err); } } public async decrementQuestionsCount(testId: number, count?: number) { try { await this.testRepository.decrement( { id: testId }, 'questionsCount', count || 1, ); } catch (err) { throw new AppTimeoutException(err); } } }