Skip to content

Commit f34cf4d

Browse files
authored
Merge pull request #78 from Kisagi07:patch/users-api-changes
Add 'withoutUser' parameter to attendance API and improve user retrieval logic
2 parents 34aef1b + 59bbc69 commit f34cf4d

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

app/api/attendances/route.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export async function GET(req: NextRequest) {
2020
const searchParams = req.nextUrl.searchParams;
2121
// get all allowed search params
2222
let limit = Number(searchParams.get("limit")) || undefined;
23+
const withoutUser = searchParams.has("withoutUser");
2324
const groupedNamedDate = searchParams.has("grouped-name-date");
2425
const of = searchParams.get("of")?.split(",").map(Number);
2526
const month = searchParams.get("month") ?? undefined;
@@ -32,10 +33,7 @@ export async function GET(req: NextRequest) {
3233
if (type && !Object.values(logs_type).includes(type)) {
3334
return NextResponse.json({ message: `Allowed type: ${Object.values(logs_type).join(", ")}` });
3435
}
35-
36-
if (!year) {
37-
year = new Date().getFullYear().toString();
38-
}
36+
3937

4038
if (year && isNaN(Number(year))) {
4139
return NextResponse.json({ message: "Invalid year" });
@@ -53,19 +51,28 @@ export async function GET(req: NextRequest) {
5351
endDate = new Date(`${(Number(year) + 1)}-01-01`);
5452
}
5553

54+
if (month && !year) {
55+
year = new Date().getFullYear().toString();
56+
startDate = new Date(`${year}-01-01`);
57+
endDate = new Date(`${(Number(year) + 1)}-01-01`);
58+
}
59+
5660
if (year && month) {
5761
startDate = new Date(`${year}-${month}-01`);
5862
endDate = new Date(`${year}-${(Number(month) + 1)}-01`);
5963
}
64+
6065

6166
let logs = (await prisma.logs.findMany({
6267
take: limit,
6368
include: {
64-
user: {
65-
include: {
66-
job_position: true,
67-
},
68-
},
69+
user: withoutUser
70+
? undefined
71+
: {
72+
include: {
73+
job_position: true,
74+
},
75+
},
6976
},
7077
orderBy: {
7178
created_at: "desc",
@@ -76,9 +83,9 @@ export async function GET(req: NextRequest) {
7683
},
7784
date: {
7885
lte: endDate,
79-
gte: startDate
86+
gte: startDate,
8087
},
81-
type
88+
type,
8289
},
8390
})) as LogWithUserWithJob[];
8491

app/api/users/[work_id]/route.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@ const changeExRole = async (
1717
return updatedUser;
1818
};
1919

20-
export async function GET(req: NextRequest, props: { params: Promise<{ work_id: string }> }) {
20+
export async function GET(
21+
req: NextRequest,
22+
props: { params: Promise<{ work_id: string }> }
23+
) {
2124
const params = await props.params;
2225
const searchParams = req.nextUrl.searchParams;
2326
const monthlyStatus = searchParams.has("monthly-status");
2427

2528
const user = await prisma.users.findFirst({
26-
where: {
27-
work_id: params.work_id,
28-
},
29+
where: params.work_id.includes("ID")
30+
? {
31+
work_id: params.work_id,
32+
}
33+
: {
34+
id: Number(params.work_id),
35+
},
2936
select: {
3037
name: true,
3138
work_id: true,
@@ -45,12 +52,15 @@ export async function GET(req: NextRequest, props: { params: Promise<{ work_id:
4552
if (!user) return NextResponse.json(null);
4653

4754
user.profile_picture = user.profile_picture
48-
? `${process.env.APP_URL}/api/public${user.profile_picture}`
55+
? `${process.env.APP_URL}/api/images/${user.profile_picture}`
4956
: null;
5057
return NextResponse.json(user);
5158
}
5259

53-
export async function DELETE(req: NextRequest, props: { params: Promise<{ work_id: string }> }) {
60+
export async function DELETE(
61+
req: NextRequest,
62+
props: { params: Promise<{ work_id: string }> }
63+
) {
5464
const params = await props.params;
5565
const user = await prisma.users.findFirst({
5666
where: {
@@ -83,9 +93,13 @@ export async function DELETE(req: NextRequest, props: { params: Promise<{ work_i
8393
return NextResponse.json({ message: "Deleted", data: { deleted } });
8494
}
8595

86-
export async function PUT(req: NextRequest, props: { params: Promise<{ work_id: string }> }) {
96+
export async function PUT(
97+
req: NextRequest,
98+
props: { params: Promise<{ work_id: string }> }
99+
) {
87100
const params = await props.params;
88-
const { name, job_position_id, gender, role, password, toEx, unEx } = await req.json();
101+
const { name, job_position_id, gender, role, password, toEx, unEx } =
102+
await req.json();
89103

90104
let user = await prisma.users.findFirst({
91105
where: {
@@ -124,7 +138,11 @@ export async function PUT(req: NextRequest, props: { params: Promise<{ work_id:
124138
}
125139
}
126140
// if its not unique return error
127-
if (!unique) return NextResponse.json({ error: "PIN already in use" }, { status: 409 });
141+
if (!unique)
142+
return NextResponse.json(
143+
{ error: "PIN already in use" },
144+
{ status: 409 }
145+
);
128146
await prisma.users.update({
129147
where: {
130148
id: user.id,

0 commit comments

Comments
 (0)