diff --git a/libs/common/src/util/buildTypeORMIncludeQuery.ts b/libs/common/src/util/buildTypeORMIncludeQuery.ts index c2d401e..029bd67 100644 --- a/libs/common/src/util/buildTypeORMIncludeQuery.ts +++ b/libs/common/src/util/buildTypeORMIncludeQuery.ts @@ -16,6 +16,9 @@ const mappingInclude: { [key: string]: any } = { subspace: { subspace: true, }, + project: { + project: true, + }, }; export function buildTypeORMIncludeQuery( diff --git a/src/project/services/index.ts b/src/project/services/index.ts new file mode 100644 index 0000000..82e8c13 --- /dev/null +++ b/src/project/services/index.ts @@ -0,0 +1 @@ +export * from './project.service'; diff --git a/src/project/services/project.service.ts b/src/project/services/project.service.ts new file mode 100644 index 0000000..62070a2 --- /dev/null +++ b/src/project/services/project.service.ts @@ -0,0 +1,140 @@ +import { ProjectRepository } from '@app/common/modules/project/repositiories'; +import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; +import { CreateProjectDto } from '../dto'; +import { BaseResponseDto } from '@app/common/dto/base.response.dto'; +import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; +import { ProjectEntity } from '@app/common/modules/project/entities'; +import { + TypeORMCustomModel, + TypeORMCustomModelFindAllQuery, +} from '@app/common/models/typeOrmCustom.model'; +import { ProjectDto } from '@app/common/modules/project/dtos'; +import { PageResponse } from '@app/common/dto/pagination.response.dto'; + +@Injectable() +export class ProjectService { + constructor(private readonly projectRepository: ProjectRepository) {} + + async createProject( + createProjectDto: CreateProjectDto, + ): Promise { + const { name } = createProjectDto; + + try { + // Check if the project name already exists + const isNameExist = await this.validate(name); + if (isNameExist) { + throw new HttpException( + `Project with name ${name} already exists`, + HttpStatus.CONFLICT, + ); + } + + const newProject = this.projectRepository.create(createProjectDto); + const savedProject = await this.projectRepository.save(newProject); + + return new SuccessResponseDto({ + message: `Project with ID ${savedProject.uuid} successfully created`, + data: savedProject, + statusCode: HttpStatus.CREATED, + }); + } catch (error) { + if (error instanceof HttpException) { + throw error; + } + + throw new HttpException( + 'An error occurred while creating the project. Please try again later.', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + + async deleteProject(uuid: string): Promise { + try { + // Find the project by UUID + const project = await this.findOne(uuid); + + if (!project) { + throw new HttpException( + `Project with ID ${uuid} not found`, + HttpStatus.NOT_FOUND, + ); + } + + // Delete the project + await this.projectRepository.delete({ uuid }); + + return new SuccessResponseDto({ + message: `Project with ID ${uuid} successfully deleted`, + statusCode: HttpStatus.OK, + }); + } catch (error) { + if (error instanceof HttpException) { + throw error; + } + + throw new HttpException( + `An error occurred while deleting the project ${uuid}`, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + + async listProjects( + pageable: Partial, + ): Promise { + try { + pageable.modelName = 'project'; + const customModel = TypeORMCustomModel(this.projectRepository); + + const { baseResponseDto, paginationResponseDto } = + await customModel.findAll(pageable); + return new PageResponse( + baseResponseDto, + paginationResponseDto, + ); + } catch (error) { + throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + async getProject(uuid: string): Promise { + try { + // Find the project by UUID + const project = await this.findOne(uuid); + + if (!project) { + throw new HttpException( + `Project with ID ${uuid} not found`, + HttpStatus.NOT_FOUND, + ); + } + + return new SuccessResponseDto({ + message: `Project with ID ${uuid} retrieved successfully`, + data: project, + statusCode: HttpStatus.OK, + }); + } catch (error) { + if (error instanceof HttpException) { + throw error; + } + + throw new HttpException( + `An error occurred while retrieving the project with id ${uuid}`, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + + async findOne(uuid: string): Promise { + const project = await this.projectRepository.findOne({ where: { uuid } }); + return project; + } + + async validate(name: string): Promise { + const project = await this.projectRepository.findOne({ where: { name } }); + return !project; + } +}