add update project

This commit is contained in:
hannathkadher
2024-12-09 12:26:53 +04:00
parent 7f739b88d8
commit 768ea7cad8

View File

@ -128,6 +128,52 @@ export class ProjectService {
}
}
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 } });
return project;