update first - 84

This commit is contained in:
thanhnv
2026-06-30 02:21:39 +09:00
commit 07ac1bdcdd
561 changed files with 88164 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
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 { JwtModule } from '@nestjs/jwt';
import { AuthModule } from './auth/auth.module.js';
import { KeyResultsModule } from './key-results/key-results.module.js';
import { ObjectivesModule } from './objectives/objectives.module.js';
import { PrismaModule } from './prisma/prisma.module.js';
import { UsersModule } from './users/users.module.js';
let AppModule = class AppModule {
};
AppModule = __decorate([
Module({
imports: [
JwtModule.register({
global: true,
secret: process.env.JWT_SECRET ?? 'dev-secret-change-me',
signOptions: { expiresIn: '2h' },
}),
PrismaModule,
AuthModule,
UsersModule,
ObjectivesModule,
KeyResultsModule,
],
})
], AppModule);
export { AppModule };
@@ -0,0 +1,46 @@
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, Inject, Post, Res, ValidationPipe } from '@nestjs/common';
import { ok } from '../common/api-response.js';
import { AuthService } from './auth.service.js';
import { LoginDto } from './dto/login.dto.js';
let AuthController = class AuthController {
authService;
constructor(authService) {
this.authService = authService;
}
async login(dto, response) {
const result = await this.authService.login(dto.username, dto.password);
response.cookie('okr_token', result.token, {
httpOnly: true,
sameSite: 'lax',
secure: false,
maxAge: 2 * 60 * 60 * 1000,
});
return ok(result);
}
};
__decorate([
Post('login'),
__param(0, Body(new ValidationPipe({ expectedType: LoginDto, whitelist: true, forbidNonWhitelisted: true }))),
__param(1, Res({ passthrough: true })),
__metadata("design:type", Function),
__metadata("design:paramtypes", [LoginDto, Object]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "login", null);
AuthController = __decorate([
Controller('auth'),
__param(0, Inject(AuthService)),
__metadata("design:paramtypes", [AuthService])
], AuthController);
export { AuthController };
+20
View File
@@ -0,0 +1,20 @@
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 { AuthController } from './auth.controller.js';
import { AuthService } from './auth.service.js';
let AuthModule = class AuthModule {
};
AuthModule = __decorate([
Module({
imports: [PrismaModule],
controllers: [AuthController],
providers: [AuthService],
})
], AuthModule);
export { AuthModule };
+62
View File
@@ -0,0 +1,62 @@
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 { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import bcrypt from 'bcrypt';
import { PrismaService } from '../prisma/prisma.service.js';
let AuthService = class AuthService {
prisma;
jwtService;
constructor(prisma, jwtService) {
this.prisma = prisma;
this.jwtService = jwtService;
}
async login(usernameOrEmail, password) {
const user = await this.prisma.user.findFirst({
where: {
OR: [{ username: usernameOrEmail }, { email: usernameOrEmail }],
},
});
if (user === null) {
throw new UnauthorizedException('Invalid credentials');
}
const validPassword = await bcrypt.compare(password, user.passwordHash);
if (!validPassword) {
throw new UnauthorizedException('Invalid credentials');
}
const token = await this.jwtService.signAsync({
sub: user.id,
email: user.email,
role: user.role,
name: user.name,
});
return {
token,
user: {
id: user.id,
name: user.name,
username: user.username,
email: user.email,
role: user.role,
},
};
}
};
AuthService = __decorate([
Injectable(),
__param(0, Inject(PrismaService)),
__param(1, Inject(JwtService)),
__metadata("design:paramtypes", [PrismaService,
JwtService])
], AuthService);
export { AuthService };
+23
View File
@@ -0,0 +1,23 @@
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 { IsString, MinLength } from 'class-validator';
export class LoginDto {
username;
password;
}
__decorate([
IsString(),
__metadata("design:type", String)
], LoginDto.prototype, "username", void 0);
__decorate([
IsString(),
MinLength(8),
__metadata("design:type", String)
], LoginDto.prototype, "password", void 0);
@@ -0,0 +1,3 @@
export function ok(data, meta) {
return meta === undefined ? { success: true, data } : { success: true, data, meta };
}
+1
View File
@@ -0,0 +1 @@
export {};
@@ -0,0 +1,5 @@
import { createParamDecorator } from '@nestjs/common';
export const CurrentUser = createParamDecorator((_data, context) => {
const request = context.switchToHttp().getRequest();
return request.user;
});
@@ -0,0 +1,53 @@
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 { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
function readCookieToken(request) {
const header = request.headers.cookie;
if (header === undefined) {
return undefined;
}
const tokenPair = header
.split(';')
.map((part) => part.trim())
.find((part) => part.startsWith('okr_token='));
return tokenPair?.slice('okr_token='.length);
}
let JwtAuthGuard = class JwtAuthGuard {
jwtService;
constructor(jwtService) {
this.jwtService = jwtService;
}
canActivate(context) {
const request = context.switchToHttp().getRequest();
const authHeader = request.headers.authorization;
const bearer = authHeader?.startsWith('Bearer ') === true ? authHeader.slice(7) : undefined;
const token = bearer ?? readCookieToken(request);
if (token === undefined || token.length === 0) {
throw new UnauthorizedException('Authentication required');
}
try {
request.user = this.jwtService.verify(token);
return true;
}
catch {
throw new UnauthorizedException('Invalid or expired token');
}
}
};
JwtAuthGuard = __decorate([
Injectable(),
__param(0, Inject(JwtService)),
__metadata("design:paramtypes", [JwtService])
], JwtAuthGuard);
export { JwtAuthGuard };
@@ -0,0 +1,3 @@
import { SetMetadata } from '@nestjs/common';
export const ROLES_KEY = 'roles';
export const Roles = (...roles) => SetMetadata(ROLES_KEY, roles);
+41
View File
@@ -0,0 +1,41 @@
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 } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { ROLES_KEY } from './roles.decorator.js';
let RolesGuard = class RolesGuard {
reflector;
constructor(reflector) {
this.reflector = reflector;
}
canActivate(context) {
const roles = this.reflector.getAllAndOverride(ROLES_KEY, [
context.getHandler(),
context.getClass(),
]);
if (roles === undefined || roles.length === 0) {
return true;
}
const request = context.switchToHttp().getRequest();
if (!roles.includes(request.user.role)) {
throw new ForbiddenException('Insufficient role');
}
return true;
}
};
RolesGuard = __decorate([
Injectable(),
__param(0, Inject(Reflector)),
__metadata("design:paramtypes", [Reflector])
], RolesGuard);
export { RolesGuard };
@@ -0,0 +1,47 @@
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 { IsDateString, IsInt, IsNotEmpty, IsPositive, IsString, Max, Min } from 'class-validator';
export class CreateKeyResultDto {
objectiveId;
title;
startValue;
targetValue;
progress;
deadline;
}
__decorate([
IsInt(),
IsPositive(),
__metadata("design:type", Number)
], CreateKeyResultDto.prototype, "objectiveId", void 0);
__decorate([
IsString(),
IsNotEmpty(),
__metadata("design:type", String)
], CreateKeyResultDto.prototype, "title", void 0);
__decorate([
IsInt(),
__metadata("design:type", Number)
], CreateKeyResultDto.prototype, "startValue", void 0);
__decorate([
IsInt(),
IsPositive(),
__metadata("design:type", Number)
], CreateKeyResultDto.prototype, "targetValue", void 0);
__decorate([
IsInt(),
Min(0),
Max(100),
__metadata("design:type", Number)
], CreateKeyResultDto.prototype, "progress", void 0);
__decorate([
IsDateString(),
__metadata("design:type", String)
], CreateKeyResultDto.prototype, "deadline", void 0);
@@ -0,0 +1,25 @@
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, IsOptional, IsString, Max, Min } from 'class-validator';
export class UpdateProgressDto {
progress;
comment;
}
__decorate([
IsInt(),
Min(0),
Max(100),
__metadata("design:type", Number)
], UpdateProgressDto.prototype, "progress", void 0);
__decorate([
IsString(),
IsOptional(),
__metadata("design:type", String)
], UpdateProgressDto.prototype, "comment", void 0);
@@ -0,0 +1,67 @@
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, Patch, Post, 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 { CreateKeyResultDto } from './dto/create-key-result.dto.js';
import { UpdateProgressDto } from './dto/update-progress.dto.js';
import { KeyResultsService } from './key-results.service.js';
let KeyResultsController = class KeyResultsController {
keyResultsService;
constructor(keyResultsService) {
this.keyResultsService = keyResultsService;
}
async get(id, user) {
return ok(await this.keyResultsService.getById(id, user));
}
async create(dto, user) {
return ok(await this.keyResultsService.create(dto, user));
}
async updateProgress(id, dto, user) {
return ok(await this.keyResultsService.updateProgress(id, dto, user));
}
};
__decorate([
Get(':id'),
__param(0, Param('id', ParseIntPipe)),
__param(1, CurrentUser()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, Object]),
__metadata("design:returntype", Promise)
], KeyResultsController.prototype, "get", null);
__decorate([
Post(),
__param(0, Body(new ValidationPipe({ expectedType: CreateKeyResultDto, whitelist: true, forbidNonWhitelisted: true, transform: true }))),
__param(1, CurrentUser()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [CreateKeyResultDto, Object]),
__metadata("design:returntype", Promise)
], KeyResultsController.prototype, "create", null);
__decorate([
Patch(':id/progress'),
__param(0, Param('id', ParseIntPipe)),
__param(1, Body(new ValidationPipe({ expectedType: UpdateProgressDto, whitelist: true, forbidNonWhitelisted: true, transform: true }))),
__param(2, CurrentUser()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, UpdateProgressDto, Object]),
__metadata("design:returntype", Promise)
], KeyResultsController.prototype, "updateProgress", null);
KeyResultsController = __decorate([
Controller('key-results'),
UseGuards(JwtAuthGuard, RolesGuard),
__param(0, Inject(KeyResultsService)),
__metadata("design:paramtypes", [KeyResultsService])
], KeyResultsController);
export { KeyResultsController };
@@ -0,0 +1,20 @@
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 { KeyResultsController } from './key-results.controller.js';
import { KeyResultsService } from './key-results.service.js';
let KeyResultsModule = class KeyResultsModule {
};
KeyResultsModule = __decorate([
Module({
imports: [PrismaModule],
controllers: [KeyResultsController],
providers: [KeyResultsService],
})
], KeyResultsModule);
export { KeyResultsModule };
@@ -0,0 +1,104 @@
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 keyResultInclude = {
objective: {
include: {
owner: { select: { id: true, name: true, username: true, email: true, role: true } },
},
},
};
let KeyResultsService = class KeyResultsService {
prisma;
constructor(prisma) {
this.prisma = prisma;
}
async getById(id, user) {
const keyResult = await this.prisma.keyResult.findUnique({ where: { id }, include: keyResultInclude });
if (keyResult === null) {
throw new NotFoundException('Key result not found');
}
this.assertCanRead(keyResult.objective.ownerId, user);
return keyResult;
}
async create(dto, user) {
const objective = await this.prisma.objective.findUnique({ where: { id: dto.objectiveId } });
if (objective === null) {
throw new NotFoundException('Objective not found');
}
this.assertCanWrite(objective.ownerId, user);
const keyResult = await this.prisma.keyResult.create({
data: {
objectiveId: dto.objectiveId,
title: dto.title,
progress: dto.progress,
startValue: dto.startValue,
targetValue: dto.targetValue,
deadline: new Date(dto.deadline),
},
include: keyResultInclude,
});
await this.recalculateObjectiveStatus(dto.objectiveId);
return keyResult;
}
async updateProgress(id, dto, user) {
const existing = await this.prisma.keyResult.findUnique({ where: { id }, include: keyResultInclude });
if (existing === null) {
throw new NotFoundException('Key result not found');
}
this.assertCanWrite(existing.objective.ownerId, user);
const updated = await this.prisma.$transaction(async (tx) => {
const keyResult = await tx.keyResult.update({
where: { id },
data: { progress: dto.progress },
include: keyResultInclude,
});
await tx.progressUpdate.create({
data: {
keyResultId: id,
progress: dto.progress,
comment: dto.comment,
createdById: user.sub,
},
});
return keyResult;
});
await this.recalculateObjectiveStatus(existing.objectiveId);
return updated;
}
assertCanRead(ownerId, user) {
if (user.role === 'EMPLOYEE' && ownerId !== user.sub) {
throw new ForbiddenException('Key result belongs to another owner');
}
}
assertCanWrite(ownerId, user) {
if (user.role === 'EMPLOYEE' && ownerId !== user.sub) {
throw new ForbiddenException('Only the owner can update this key result');
}
}
async recalculateObjectiveStatus(objectiveId) {
const keyResults = await this.prisma.keyResult.findMany({ where: { objectiveId } });
const average = keyResults.length === 0
? 0
: Math.round(keyResults.reduce((total, keyResult) => total + keyResult.progress, 0) / keyResults.length);
const status = average === 0 ? 'NOT_STARTED' : average >= 100 ? 'COMPLETED' : 'IN_PROGRESS';
await this.prisma.objective.update({ where: { id: objectiveId }, data: { status } });
}
};
KeyResultsService = __decorate([
Injectable(),
__param(0, Inject(PrismaService)),
__metadata("design:paramtypes", [PrismaService])
], KeyResultsService);
export { KeyResultsService };
+23
View File
@@ -0,0 +1,23 @@
import 'reflect-metadata';
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module.js';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api/v1');
const allowedOrigins = (process.env.FRONTEND_ORIGIN ?? 'http://localhost:5173,http://127.0.0.1:5173')
.split(',')
.map((origin) => origin.trim())
.filter((origin) => origin.length > 0);
app.enableCors({
origin: allowedOrigins,
credentials: true,
});
app.useGlobalPipes(new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}));
await app.listen(Number(process.env.PORT ?? 3000));
}
void bootstrap();
@@ -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 };
@@ -0,0 +1,17 @@
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 { PrismaService } from './prisma.service.js';
let PrismaModule = class PrismaModule {
};
PrismaModule = __decorate([
Module({
providers: [PrismaService],
exports: [PrismaService],
})
], PrismaModule);
export { PrismaModule };
@@ -0,0 +1,20 @@
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 { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
let PrismaService = class PrismaService extends PrismaClient {
async onModuleInit() {
await this.$connect();
}
async onModuleDestroy() {
await this.$disconnect();
}
};
PrismaService = __decorate([
Injectable()
], PrismaService);
export { PrismaService };
@@ -0,0 +1,41 @@
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 { Controller, Get, Inject, UseGuards } from '@nestjs/common';
import { ok } from '../common/api-response.js';
import { JwtAuthGuard } from '../common/jwt-auth.guard.js';
import { Roles } from '../common/roles.decorator.js';
import { RolesGuard } from '../common/roles.guard.js';
import { UsersService } from './users.service.js';
let UsersController = class UsersController {
usersService;
constructor(usersService) {
this.usersService = usersService;
}
async list() {
return ok(await this.usersService.listUsers());
}
};
__decorate([
Get(),
Roles('ADMIN', 'MANAGER'),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], UsersController.prototype, "list", null);
UsersController = __decorate([
Controller('users'),
UseGuards(JwtAuthGuard, RolesGuard),
__param(0, Inject(UsersService)),
__metadata("design:paramtypes", [UsersService])
], UsersController);
export { UsersController };
+21
View File
@@ -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 { UsersController } from './users.controller.js';
import { UsersService } from './users.service.js';
let UsersModule = class UsersModule {
};
UsersModule = __decorate([
Module({
imports: [PrismaModule],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})
], UsersModule);
export { UsersModule };
+32
View File
@@ -0,0 +1,32 @@
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 { Inject, Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service.js';
let UsersService = class UsersService {
prisma;
constructor(prisma) {
this.prisma = prisma;
}
async listUsers() {
return this.prisma.user.findMany({
orderBy: { id: 'asc' },
select: { id: true, name: true, username: true, email: true, role: true },
});
}
};
UsersService = __decorate([
Injectable(),
__param(0, Inject(PrismaService)),
__metadata("design:paramtypes", [PrismaService])
], UsersService);
export { UsersService };