Files
heala-backend/src/modules/tests/providers/tests.service.ts
T
2026-06-01 14:45:01 +03:30

58 lines
1.5 KiB
TypeScript

import {
HttpStatus,
Injectable,
RequestTimeoutException,
} from '@nestjs/common';
import { Repository } from 'typeorm';
import { Test } from '../entities/test.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { CreateTestDTO } from '../dtos/create-test.dto';
import { AppException } from '@/common/exceptions/app.exception';
import { AppResponse } from '@/common/responses';
import { I18nService } from 'nestjs-i18n';
@Injectable()
export class TestsService {
constructor(
/**
* Inject Tests Repository
*/
@InjectRepository(Test)
private readonly testRepository: Repository<Test>,
/**
* Inject I18n Service
*/
private readonly i18nService: I18nService,
) {}
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('tests.messages.createSuccess'),
);
} catch (err) {
throw new RequestTimeoutException('common.errors.requestTimeout', {
cause: err,
description: 'Error connecting to the database',
});
}
}
}