feat: employee history

This commit is contained in:
Methapon2001 2024-06-28 08:57:53 +07:00
parent 8ac2527217
commit 3e3d2cf01c
2 changed files with 47 additions and 0 deletions

View file

@ -472,6 +472,20 @@ model Employee {
employeeCheckup EmployeeCheckup[]
employeeWork EmployeeWork[]
employeeOtherInfo EmployeeOtherInfo[]
editHistory EmployeeHistory[]
}
model EmployeeHistory {
id String @id @default(uuid())
field String
valueBefore Json
valueAfter Json
timestamp DateTime @default(now())
masterId String
master Employee @relation(fields: [masterId], references: [id], onDelete: Cascade)
}
model EmployeeCheckup {

View file

@ -635,6 +635,32 @@ export class EmployeeController extends Controller {
});
});
const historyEntries: { field: string; valueBefore: string; valueAfter: string }[] = [];
for (const k of Object.keys(body)) {
const field = k as keyof typeof body;
if (field === "employeeCheckup") continue;
if (field === "employeeOtherInfo") continue;
if (field === "employeeWork") continue;
let valueBefore = employee[field];
let valueAfter = body[field];
if (valueBefore === undefined && valueAfter === undefined) continue;
if (valueBefore instanceof Date) valueBefore = valueBefore.toISOString();
if (valueBefore === null || valueBefore === undefined) valueBefore = "";
if (valueAfter instanceof Date) valueAfter = valueAfter.toISOString();
if (valueAfter === null || valueAfter === undefined) valueAfter = "";
if (valueBefore !== valueAfter) historyEntries.push({ field, valueBefore, valueAfter });
}
await prisma.employeeHistory.createMany({
data: historyEntries.map((v) => ({ ...v, masterId: employee.id })),
});
return Object.assign(record, {
profileImageUrl: await presignedGetObjectIfExist(
MINIO_BUCKET,
@ -663,4 +689,11 @@ export class EmployeeController extends Controller {
return await prisma.employee.delete({ where: { id: employeeId } });
}
@Get("{employeeId}/edit-history")
async editHistory(@Path() employeeId: string) {
return await prisma.employeeHistory.findMany({
where: { masterId: employeeId },
});
}
}