update first - 84
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
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);
|
||||
};
|
||||
import { IsInt, IsNotEmpty, IsOptional, IsPositive, IsString, Matches } from 'class-validator';
|
||||
export class CreateObjectiveDto {
|
||||
title;
|
||||
description;
|
||||
ownerId;
|
||||
quarter;
|
||||
}
|
||||
__decorate([
|
||||
IsString(),
|
||||
IsNotEmpty(),
|
||||
__metadata("design:type", String)
|
||||
], CreateObjectiveDto.prototype, "title", void 0);
|
||||
__decorate([
|
||||
IsString(),
|
||||
IsOptional(),
|
||||
__metadata("design:type", String)
|
||||
], CreateObjectiveDto.prototype, "description", void 0);
|
||||
__decorate([
|
||||
IsInt(),
|
||||
IsPositive(),
|
||||
__metadata("design:type", Number)
|
||||
], CreateObjectiveDto.prototype, "ownerId", void 0);
|
||||
__decorate([
|
||||
IsString(),
|
||||
Matches(/^Q[1-4]\/\d{4}$/),
|
||||
__metadata("design:type", String)
|
||||
], CreateObjectiveDto.prototype, "quarter", void 0);
|
||||
@@ -0,0 +1,66 @@
|
||||
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 { Body, Controller, Get, Inject, Param, ParseIntPipe, Post, Query, UseGuards, ValidationPipe } from '@nestjs/common';
|
||||
import { ok } from '../common/api-response.js';
|
||||
import { CurrentUser } from '../common/current-user.decorator.js';
|
||||
import { JwtAuthGuard } from '../common/jwt-auth.guard.js';
|
||||
import { RolesGuard } from '../common/roles.guard.js';
|
||||
import { CreateObjectiveDto } from './dto/create-objective.dto.js';
|
||||
import { ObjectivesService } from './objectives.service.js';
|
||||
let ObjectivesController = class ObjectivesController {
|
||||
objectivesService;
|
||||
constructor(objectivesService) {
|
||||
this.objectivesService = objectivesService;
|
||||
}
|
||||
async list(user, quarter) {
|
||||
const objectives = await this.objectivesService.list(user, quarter);
|
||||
return ok(objectives, { total: objectives.length });
|
||||
}
|
||||
async get(id, user) {
|
||||
return ok(await this.objectivesService.getById(id, user));
|
||||
}
|
||||
async create(dto, user) {
|
||||
return ok(await this.objectivesService.create(dto, user));
|
||||
}
|
||||
};
|
||||
__decorate([
|
||||
Get(),
|
||||
__param(0, CurrentUser()),
|
||||
__param(1, Query('quarter')),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object, String]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], ObjectivesController.prototype, "list", null);
|
||||
__decorate([
|
||||
Get(':id'),
|
||||
__param(0, Param('id', ParseIntPipe)),
|
||||
__param(1, CurrentUser()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Number, Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], ObjectivesController.prototype, "get", null);
|
||||
__decorate([
|
||||
Post(),
|
||||
__param(0, Body(new ValidationPipe({ expectedType: CreateObjectiveDto, whitelist: true, forbidNonWhitelisted: true, transform: true }))),
|
||||
__param(1, CurrentUser()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [CreateObjectiveDto, Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], ObjectivesController.prototype, "create", null);
|
||||
ObjectivesController = __decorate([
|
||||
Controller('objectives'),
|
||||
UseGuards(JwtAuthGuard, RolesGuard),
|
||||
__param(0, Inject(ObjectivesService)),
|
||||
__metadata("design:paramtypes", [ObjectivesService])
|
||||
], ObjectivesController);
|
||||
export { ObjectivesController };
|
||||
@@ -0,0 +1,21 @@
|
||||
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;
|
||||
};
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PrismaModule } from '../prisma/prisma.module.js';
|
||||
import { ObjectivesController } from './objectives.controller.js';
|
||||
import { ObjectivesService } from './objectives.service.js';
|
||||
let ObjectivesModule = class ObjectivesModule {
|
||||
};
|
||||
ObjectivesModule = __decorate([
|
||||
Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [ObjectivesController],
|
||||
providers: [ObjectivesService],
|
||||
exports: [ObjectivesService],
|
||||
})
|
||||
], ObjectivesModule);
|
||||
export { ObjectivesModule };
|
||||
@@ -0,0 +1,77 @@
|
||||
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 };
|
||||
Reference in New Issue
Block a user