Files
hefesto/backend/src/modules/metas/metas.service.ts

38 lines
1.4 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Meta } from './entities/meta.entity';
@Injectable()
export class MetasService {
constructor(@InjectRepository(Meta) private repo: Repository<Meta>) {}
async findAll(q: any) {
const qb = this.repo.createQueryBuilder('m');
if (q.tipo) qb.andWhere('m.tipo = :tipo', { tipo: q.tipo });
if (q.status) qb.andWhere('m.status = :s', { s: q.status });
if (q.centro_custo_id) qb.andWhere('m.centro_custo_id = :cc', { cc: q.centro_custo_id });
return qb.orderBy('m.created_at', 'DESC').getMany();
}
async create(dto: any) { return this.repo.save(this.repo.create(dto)); }
async update(id: string, dto: any) {
await this.repo.update(id, dto);
return this.repo.findOneBy({ id });
}
async progresso() {
const all = await this.repo.find();
const por_tipo = {} as any;
for (const m of all) {
if (!por_tipo[m.tipo]) por_tipo[m.tipo] = { total: 0, atingidas: 0, em_andamento: 0, atrasadas: 0 };
por_tipo[m.tipo].total++;
por_tipo[m.tipo][m.status]++;
}
const total = all.length;
const atingidas = all.filter(m => m.status === 'atingida').length;
return { total, atingidas, percentual_conclusao: total ? Math.round((atingidas / total) * 100) : 0, por_tipo, metas: all };
}
}