91 lines
2.5 KiB
TypeScript
91 lines
2.5 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|||
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
||
|
|
import { Repository } from 'typeorm';
|
||
|
|
import { Project } from './project.entity';
|
||
|
|
import { CreateProjectDto, UpdateProjectDto } from './dto';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class ProjectsService {
|
||
|
|
constructor(
|
||
|
|
@InjectRepository(Project)
|
||
|
|
private projectsRepository: Repository<Project>,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
async findAll(query) {
|
||
|
|
const { page = 1, limit = 10, status, customer_id } = query;
|
||
|
|
const skip = (page - 1) * limit;
|
||
|
|
|
||
|
|
const queryBuilder = this.projectsRepository.createQueryBuilder('project');
|
||
|
|
|
||
|
|
if (status) {
|
||
|
|
queryBuilder.where('project.status = :status', { status });
|
||
|
|
}
|
||
|
|
|
||
|
|
if (customer_id) {
|
||
|
|
queryBuilder.where('project.customer_id = :customer_id', { customer_id });
|
||
|
|
}
|
||
|
|
|
||
|
|
const [projects, total] = await queryBuilder
|
||
|
|
.leftJoinAndSelect('project.customer', 'customer')
|
||
|
|
.skip(skip)
|
||
|
|
.take(limit)
|
||
|
|
.getManyAndCount();
|
||
|
|
|
||
|
|
return {
|
||
|
|
projects,
|
||
|
|
total,
|
||
|
|
page,
|
||
|
|
limit,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
async findOne(id: string) {
|
||
|
|
const project = await this.projectsRepository.findOne({
|
||
|
|
where: { id },
|
||
|
|
relations: ['customer', 'contracts', 'purchases', 'subcontracts'],
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!project) {
|
||
|
|
throw new NotFoundException(`Project with ID ${id} not found`);
|
||
|
|
}
|
||
|
|
|
||
|
|
return project;
|
||
|
|
}
|
||
|
|
|
||
|
|
async create(createProjectDto: CreateProjectDto) {
|
||
|
|
const project = this.projectsRepository.create(createProjectDto);
|
||
|
|
return this.projectsRepository.save(project);
|
||
|
|
}
|
||
|
|
|
||
|
|
async update(id: string, updateProjectDto: UpdateProjectDto) {
|
||
|
|
const project = await this.findOne(id);
|
||
|
|
Object.assign(project, updateProjectDto);
|
||
|
|
return this.projectsRepository.save(project);
|
||
|
|
}
|
||
|
|
|
||
|
|
async remove(id: string) {
|
||
|
|
const project = await this.findOne(id);
|
||
|
|
return this.projectsRepository.remove(project);
|
||
|
|
}
|
||
|
|
|
||
|
|
async calculateDuration(id: string) {
|
||
|
|
const project = await this.findOne(id);
|
||
|
|
|
||
|
|
if (!project.start_date || !project.end_date) {
|
||
|
|
return { message: 'Start date or end date is not set' };
|
||
|
|
}
|
||
|
|
|
||
|
|
const start = new Date(project.start_date);
|
||
|
|
const end = new Date(project.end_date);
|
||
|
|
const duration = Math.ceil((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24));
|
||
|
|
|
||
|
|
return { duration_days: duration };
|
||
|
|
}
|
||
|
|
|
||
|
|
async updateStatus(id: string, status: string) {
|
||
|
|
const project = await this.findOne(id);
|
||
|
|
project.status = status;
|
||
|
|
return this.projectsRepository.save(project);
|
||
|
|
}
|
||
|
|
}
|