78 lines
3.3 KiB
JavaScript
78 lines
3.3 KiB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
};
|
|
import { ForbiddenException, Inject, Injectable, NotFoundException } from '@nestjs/common';
|
|
import { PrismaService } from '../prisma/prisma.service.js';
|
|
const objectiveInclude = {
|
|
owner: { select: { id: true, name: true, username: true, email: true, role: true } },
|
|
keyResults: { orderBy: { id: 'asc' } },
|
|
};
|
|
function averageProgress(keyResults) {
|
|
if (keyResults.length === 0) {
|
|
return 0;
|
|
}
|
|
const total = keyResults.reduce((sum, keyResult) => sum + keyResult.progress, 0);
|
|
return Math.round(total / keyResults.length);
|
|
}
|
|
let ObjectivesService = class ObjectivesService {
|
|
prisma;
|
|
constructor(prisma) {
|
|
this.prisma = prisma;
|
|
}
|
|
async list(user, quarter) {
|
|
const where = {
|
|
...(quarter === undefined ? {} : { quarter }),
|
|
...(user.role === 'EMPLOYEE' ? { ownerId: user.sub } : {}),
|
|
};
|
|
return this.prisma.objective.findMany({
|
|
where,
|
|
include: objectiveInclude,
|
|
orderBy: { id: 'asc' },
|
|
});
|
|
}
|
|
async getById(id, user) {
|
|
const objective = await this.prisma.objective.findUnique({ where: { id }, include: objectiveInclude });
|
|
if (objective === null) {
|
|
throw new NotFoundException('Objective not found');
|
|
}
|
|
if (user.role === 'EMPLOYEE' && objective.ownerId !== user.sub) {
|
|
throw new ForbiddenException('Objective belongs to another owner');
|
|
}
|
|
return { ...objective, computedProgress: averageProgress(objective.keyResults) };
|
|
}
|
|
async create(dto, user) {
|
|
if (user.role === 'EMPLOYEE' && dto.ownerId !== user.sub) {
|
|
throw new ForbiddenException('Employees can create only their own objectives');
|
|
}
|
|
const owner = await this.prisma.user.findUnique({ where: { id: dto.ownerId } });
|
|
if (owner === null) {
|
|
throw new NotFoundException('Owner not found');
|
|
}
|
|
return this.prisma.objective.create({
|
|
data: {
|
|
title: dto.title,
|
|
description: dto.description,
|
|
ownerId: dto.ownerId,
|
|
quarter: dto.quarter,
|
|
status: 'NOT_STARTED',
|
|
},
|
|
include: objectiveInclude,
|
|
});
|
|
}
|
|
};
|
|
ObjectivesService = __decorate([
|
|
Injectable(),
|
|
__param(0, Inject(PrismaService)),
|
|
__metadata("design:paramtypes", [PrismaService])
|
|
], ObjectivesService);
|
|
export { ObjectivesService };
|