Files
backend/src/project/services/project.service.ts
T

199 lines
5.7 KiB
TypeScript

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';
import { CommandBus } from '@nestjs/cqrs';
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
@Injectable()
export class ProjectService {
constructor(
private readonly projectRepository: ProjectRepository,
private commandBus: CommandBus,
) {}
async createProject(
createProjectDto: CreateProjectDto,
): Promise<BaseResponseDto> {
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);
await this.commandBus.execute(new CreateOrphanSpaceCommand(savedProject));
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. ${error}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async deleteProject(uuid: string): Promise<BaseResponseDto> {
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<TypeORMCustomModelFindAllQuery>,
): Promise<BaseResponseDto> {
try {
pageable.modelName = 'project';
const customModel = TypeORMCustomModel(this.projectRepository);
const { baseResponseDto, paginationResponseDto } =
await customModel.findAll(pageable);
return new PageResponse<ProjectDto>(
baseResponseDto,
paginationResponseDto,
);
} catch (error) {
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
async getProject(uuid: string): Promise<BaseResponseDto> {
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 updateProject(
uuid: string,
updateProjectDto: CreateProjectDto,
): Promise<BaseResponseDto> {
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,
);
}
if (updateProjectDto.name && updateProjectDto.name !== project.name) {
const isNameExist = await this.validate(updateProjectDto.name);
if (isNameExist) {
throw new HttpException(
`Project with name ${updateProjectDto.name} already exists`,
HttpStatus.CONFLICT,
);
}
}
// Update the project details
Object.assign(project, updateProjectDto);
const updatedProject = await this.projectRepository.save(project);
return new SuccessResponseDto({
message: `Project with ID ${uuid} successfully updated`,
data: updatedProject,
statusCode: HttpStatus.OK,
});
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
`An error occurred while updating the project with ID ${uuid}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async findOne(uuid: string): Promise<ProjectEntity> {
const project = await this.projectRepository.findOne({ where: { uuid } });
if (!project) {
throw new HttpException(
`Invalid project with uuid ${uuid}`,
HttpStatus.NOT_FOUND,
);
}
return project;
}
async validate(name: string): Promise<boolean> {
return await this.projectRepository.exists({ where: { name } });
}
}