Skip to content

Commit d6d7cd1

Browse files
authored
Merge pull request #65 from Kisagi07/63-update-best-attendance-to-only-filter-based-on-late-amount-first-then-if-more-filter-again-by-attendances
Update Attendance Best API
2 parents f8b0d11 + da87542 commit d6d7cd1

File tree

2 files changed

+1320
-925
lines changed

2 files changed

+1320
-925
lines changed

app/api/attendances/best/route.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ import { NextRequest, NextResponse } from "next/server";
22
import prisma from "@/app/prisma";
33
import { fromDate, parseDate, parseTime } from "@internationalized/date";
44
import calculateLateInLogs from "@/app/helper/calculateLate";
5+
import { job_positions, logs } from "@prisma/client";
6+
7+
interface Score {
8+
totalLate: number;
9+
attendances: number;
10+
workFromHome: number;
11+
}
512

613
const GET = async (req: NextRequest) => {
714
// get today's date
8-
const now = fromDate(new Date(), "etc/utc");
9-
const nextYear = now.add({ years: 1 });
10-
const prevYear = now.add({ years: -1 });
15+
const now = fromDate(new Date(), "etc/utc");
1116
const beginningOfYear = now.set({month: 1, day:1, hour: 0, minute:0, second: 0,millisecond: 0}).toDate()
1217
const endOfYear = now.set({month: 12, day:31, hour: 23, minute:59, second: 59,millisecond: 999}).toDate()
1318
// fetch users with `logs` and `job_position` between last year and next year
@@ -25,7 +30,7 @@ const GET = async (req: NextRequest) => {
2530
},
2631
where: {
2732
role: {
28-
not: "admin",
33+
notIn: ["admin", "ex_employee", "ex_intern"]
2934
},
3035
},
3136
});
@@ -39,8 +44,8 @@ const GET = async (req: NextRequest) => {
3944
const company = await prisma.company.findFirst();
4045
const tolerance = company?.tolerance_time ?? 0;
4146
// score to determine the best employee
42-
let score: Record<string, Record<string, number>> = {};
43-
// iterate over users
47+
let score: Record<string, Score> = {};
48+
// calculate score based on how many are late
4449
users.forEach((user) => {
4550
if (user.job_position) {
4651
// calculate all logs tha have type of `work_from_home`, `work_from_office`, and `special_attendance`
@@ -51,39 +56,44 @@ const GET = async (req: NextRequest) => {
5156
log.type === "special_attendance"
5257
);
5358
// initial scrone
54-
let initialScore = logs.length;
59+
// let initialScore = logs.length;
5560
// get total late
5661
const totalLate = calculateLateInLogs(logs, tolerance, user.job_position.shift_start);
5762
// only work from home logs
5863
const workFromHome = logs.filter((log) => log.type === "work_from_home");
5964
// set user score and its needed properties
60-
if (score[user.name!]) {
61-
score[user.name!]["score"] = initialScore - totalLate;
62-
score[user.name!]["totalLate"] = totalLate;
63-
score[user.name!]["workFromHome"] = workFromHome.length;
65+
if (score[user.name!]) {
66+
score[user.name!]["totalLate"] = totalLate;
6467
score[user.name!]["attendances"] = logs.length;
68+
score[user.name!]["workFromHome"] = workFromHome.length;
6569
} else {
66-
score[user.name!] = {
67-
score: initialScore,
68-
totalLate: totalLate,
69-
workFromHome: workFromHome.length,
70+
score[user.name!] = {
71+
totalLate: totalLate,
7072
attendances: logs.length,
73+
workFromHome: workFromHome.length
7174
};
7275
}
7376
}
74-
});
75-
// get the user with the highest score along with its properties
76-
const bestEmployee = Object.entries(score).reduce((acc, curr) => {
77-
if (acc[1].score < curr[1].score) {
78-
return curr;
77+
});
78+
79+
let lowestTotalLate = 0;
80+
81+
// find the lowest totalLate number in score
82+
for (const key in score) {
83+
if (score[key].totalLate < lowestTotalLate) {
84+
lowestTotalLate = score[key].totalLate;
7985
}
80-
return acc;
81-
});
86+
}
8287

83-
88+
let filtered = Object.entries(score).filter((user) => user[1].totalLate === lowestTotalLate);
89+
90+
if (filtered.length > 1) {
91+
// filter with the most attendance
92+
filtered = filtered.filter((user) => user[1].attendances === Math.max(...filtered.map((user) => user[1].attendances)));
93+
return NextResponse.json(filtered[0])
94+
}
8495

85-
// return
86-
return NextResponse.json(bestEmployee);
96+
return NextResponse.json({best: filtered[0], score});
8797
};
8898

8999
export const dynamic = "force-dynamic";

0 commit comments

Comments
 (0)