feat: add choices entity admin crud

This commit is contained in:
2026-06-17 11:11:26 +03:30
parent 0eea63fec1
commit 1e8036abca
31 changed files with 805 additions and 201 deletions
@@ -0,0 +1,155 @@
import { ConflictException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Question } from '../entities/question.entity';
import { QueryFailedError, Repository } from 'typeorm';
import { TestsService } from '@/modules/tests/providers/tests.service';
import { I18nService } from 'nestjs-i18n';
import { CreateQuestionDTO } from '../dtos/create-question.dto';
import { AppResponse } from '@/common/responses';
import { UNIQUE_TEST_ORDER_IDX } from '../constants';
import { AppTimeoutException } from '@/common/exceptions/app-timeout.exception';
import { UpdateQuestionDTO } from '../dtos/update-question.dto';
import { validateMetadata } from '../helpers/validate-metadata.helper';
import { AppException } from '@/common/exceptions/app.exception';
@Injectable()
export class QuestionsCommandProvider {
constructor(
/**
* Inject Questions Repository
*/
@InjectRepository(Question)
private readonly questionsRepository: Repository<Question>,
/**
* Inject Tests Service
*/
private readonly testsService: TestsService,
/**
* Inject I18n Service
*/
private readonly i18nService: I18nService,
) {}
public async createOne(createQuestionDto: CreateQuestionDTO) {
const test = await this.testsService.adminGetOne(createQuestionDto.test);
if (!test) {
throw new AppException(
'common.errors.entityNotFound',
HttpStatus.NOT_FOUND,
{ entity: '$t(tests.fields.test)' },
);
}
const newQuestion = this.questionsRepository.create({
...createQuestionDto,
test,
});
try {
await this.questionsRepository.save(newQuestion);
return new AppResponse(
newQuestion,
this.i18nService.translate('common.messages.createSuccess', {
args: { entity: '$t(questions.fields.question)' },
}),
);
} catch (err) {
if (
err instanceof QueryFailedError &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(err as any).constraint === UNIQUE_TEST_ORDER_IDX
) {
throw new ConflictException('questions.errors.uniqueOrderConstraint');
}
throw new AppTimeoutException(err);
}
}
public async updateOne(id: number, updateQuestionDto: UpdateQuestionDTO) {
const question = await this.questionsRepository.findOneBy({ id });
if (!question) {
throw new AppException(
'common.errors.entityNotFound',
HttpStatus.NOT_FOUND,
{ entity: '$t(questions.fields.question)' },
);
}
if (updateQuestionDto.test) {
const test = await this.testsService.adminGetOne(updateQuestionDto.test);
if (!test) {
throw new AppException(
'common.errors.entityNotFound',
HttpStatus.NOT_FOUND,
{ entity: '$t(tests.fields.test)' },
);
}
}
const type =
updateQuestionDto.type !== undefined
? updateQuestionDto.type
: question.type;
const metadata =
updateQuestionDto.metadata !== undefined
? updateQuestionDto.metadata
: question.metadata;
const errors = await validateMetadata(type, metadata);
if (errors && errors.length > 0) {
throw new AppException(
'validation.wrongFieldFormat',
HttpStatus.BAD_REQUEST,
{ field: '$t(questions.fields.metadata)' },
);
}
Object.assign(question, updateQuestionDto);
try {
return await this.questionsRepository.save(question);
} catch (err) {
if (
err instanceof QueryFailedError &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(err as any).constraint === UNIQUE_TEST_ORDER_IDX
) {
throw new ConflictException('questions.errors.uniqueOrderConstraint');
}
throw new AppTimeoutException(err);
}
}
public async deleteOne(id: number) {
let question: Question | null = null;
try {
question = await this.questionsRepository.findOneBy({ id });
} catch (err) {
throw new AppTimeoutException(err);
}
if (!question)
throw new AppException(
'common.errors.entityNotFound',
HttpStatus.NOT_FOUND,
{ entity: '$t(questions.fields.question)' },
);
try {
await this.questionsRepository.delete(question.id);
} catch (err) {
console.log(err);
throw new AppTimeoutException(err);
}
}
}