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,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 };