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
@@ -0,0 +1,19 @@
import { IsInt, IsNotEmpty, IsOptional, IsPositive, IsString, Matches } from 'class-validator';
export class CreateObjectiveDto {
@IsString()
@IsNotEmpty()
title!: string;
@IsString()
@IsOptional()
description?: string;
@IsInt()
@IsPositive()
ownerId!: number;
@IsString()
@Matches(/^Q[1-4]\/\d{4}$/)
quarter!: string;
}
@@ -0,0 +1,34 @@
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 type { JwtUser } from '../common/auth.types.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';
@Controller('objectives')
@UseGuards(JwtAuthGuard, RolesGuard)
export class ObjectivesController {
constructor(@Inject(ObjectivesService) private readonly objectivesService: ObjectivesService) {}
@Get()
async list(@CurrentUser() user: JwtUser, @Query('quarter') quarter?: string) {
const objectives = await this.objectivesService.list(user, quarter);
return ok(objectives, { total: objectives.length });
}
@Get(':id')
async get(@Param('id', ParseIntPipe) id: number, @CurrentUser() user: JwtUser) {
return ok(await this.objectivesService.getById(id, user));
}
@Post()
async create(
@Body(new ValidationPipe({ expectedType: CreateObjectiveDto, whitelist: true, forbidNonWhitelisted: true, transform: true }))
dto: CreateObjectiveDto,
@CurrentUser() user: JwtUser,
) {
return ok(await this.objectivesService.create(dto, user));
}
}
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module.js';
import { ObjectivesController } from './objectives.controller.js';
import { ObjectivesService } from './objectives.service.js';
@Module({
imports: [PrismaModule],
controllers: [ObjectivesController],
providers: [ObjectivesService],
exports: [ObjectivesService],
})
export class ObjectivesModule {}
@@ -0,0 +1,69 @@
import { ForbiddenException, Inject, Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import type { JwtUser } from '../common/auth.types.js';
import { PrismaService } from '../prisma/prisma.service.js';
import type { CreateObjectiveDto } from './dto/create-objective.dto.js';
const objectiveInclude = {
owner: { select: { id: true, name: true, username: true, email: true, role: true } },
keyResults: { orderBy: { id: 'asc' as const } },
} satisfies Prisma.ObjectiveInclude;
export type ObjectiveWithRelations = Prisma.ObjectiveGetPayload<{ include: typeof objectiveInclude }>;
function averageProgress(keyResults: { progress: number }[]): number {
if (keyResults.length === 0) {
return 0;
}
const total = keyResults.reduce((sum, keyResult) => sum + keyResult.progress, 0);
return Math.round(total / keyResults.length);
}
@Injectable()
export class ObjectivesService {
constructor(@Inject(PrismaService) private readonly prisma: PrismaService) {}
async list(user: JwtUser, quarter?: string): Promise<ObjectiveWithRelations[]> {
const where: Prisma.ObjectiveWhereInput = {
...(quarter === undefined ? {} : { quarter }),
...(user.role === 'EMPLOYEE' ? { ownerId: user.sub } : {}),
};
return this.prisma.objective.findMany({
where,
include: objectiveInclude,
orderBy: { id: 'asc' },
});
}
async getById(id: number, user: JwtUser): Promise<ObjectiveWithRelations & { computedProgress: number }> {
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: CreateObjectiveDto, user: JwtUser): Promise<ObjectiveWithRelations> {
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,
});
}
}