Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions apps/web/app/api/get-audit-logs/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export async function POST(
const {user_id} = await req.json()
// 2. Strict check for the ID
if (!user_id) {
return NextResponse.json({ error: "Route parameter id not found" }, { status: 400 });
return NextResponse.json(
{ error: "Route parameters not found"},
{ status: 400 }
);
}

console.log("Audit logs accessed by user:", user_id);
Expand All @@ -19,12 +22,18 @@ export async function POST(

// check if user exists
if (!user){
return NextResponse.json({ error: `user ${user_id} not found` }, { status: 404 });
return NextResponse.json(
{ error: "User not found" },
{ status: 404 }
);
}

// 2. Strict check for the ID
if (user.userRole != 'Admin') {
return NextResponse.json({ error: "User role is not admin" }, { status: 401 });
return NextResponse.json(
{ error: "Only for admin access"},
{ status: 401 }
);
}

const res = await prisma.auditLog.findMany();
Expand Down
10 changes: 8 additions & 2 deletions apps/web/app/api/get-lockers/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export async function POST(
const {user_id} = await req.json()
// 2. Strict check for the ID
if (!user_id) {
return NextResponse.json({ error: "Route parameter id not found" }, { status: 400 });
return NextResponse.json(
{ error: "Route parameters not found"},
{ status: 400 }
);
}

console.log("Locker data accessed by user:", user_id);
Expand All @@ -20,7 +23,10 @@ export async function POST(
});

if (user === undefined || user === null) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
return NextResponse.json(
{ error: "User not found" },
{ status: 404 }
);
}

// get lockers
Expand Down
29 changes: 23 additions & 6 deletions apps/web/app/api/locker/add-user/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ export async function POST(
const { qr_data, locker_id } = await req.json();
// 2. Strict check for the ID
if (!qr_data ||locker_id == undefined || locker_id == null) {
return NextResponse.json({ error: "Route parameters not found" }, { status: 400 });
return NextResponse.json(
{ error: "Route parameters not found" },
{ status: 400 }
);
}
const user_data = JSON.parse(qr_data);
const uin = user_data.uin;
const name = user_data.name;
const l_id = Number(locker_id);
// Check if locker id is a number (Note: 0 is a valid number!)
if (isNaN(l_id)) {
return NextResponse.json({ error: `Value ${l_id} is not a valid number` }, { status: 400 });
return NextResponse.json(
{ error: `Value ${l_id} is not a valid number` },
{ status: 400 }
);
}

// // MOSIP verification
Expand All @@ -34,7 +40,10 @@ export async function POST(

// check if locker exists
if (!locker){
return NextResponse.json({ error: `Locker ${l_id} not found` }, { status: 404 });
return NextResponse.json(
{ error: `Locker ${l_id} not found` },
{ status: 404 }
);
}

// create user if it does not exist
Expand All @@ -51,7 +60,10 @@ export async function POST(


if (await get_locker_state(locker) != "REGISTER"){
return NextResponse.json({ error: `Past Registration period or registration hasn't started` }, { status: 404 });
return NextResponse.json(
{ error: `Past Registration period or registration hasn't started` },
{ status: 404 }
);
}

// check for duplicates already added
Expand All @@ -62,7 +74,9 @@ export async function POST(
}
})
if (isAdded){
return NextResponse.json({ error: `User ${name} with uin ${uin} is already a user for locker ${l_id}` }, { status: 404 });
return NextResponse.json(
{ error: `User ${name} with uin ${uin} is already a user for locker ${l_id}` },
{ status: 404 });
}

// add user to locker
Expand All @@ -78,6 +92,9 @@ export async function POST(
return NextResponse.json({ message: `user, ${name} with uin ${uin}, has been added as user to locker ${l_id}`});
} catch (e) {
console.error(e);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
}
30 changes: 24 additions & 6 deletions apps/web/app/api/locker/close-locker/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,44 @@ export async function POST(
const { weight, locker_id } = await req.json();
// Strict check for the params
if (locker_id == undefined || locker_id == null || weight == null || weight == undefined ) {
return NextResponse.json({ error: "Route parameters not found" }, { status: 400 });
return NextResponse.json(
{ error: "Route parameters not found" },
{ status: 400 }
);
}

const w_new = parseInt(weight, 10)
const l_id = parseInt(locker_id, 10)
// Check if weight and locker id is a number (Note: 0 is a valid number!)
if (isNaN(w_new)) {
return NextResponse.json({ error: `Value ${w_new} is not a valid number` }, { status: 400 });
return NextResponse.json(
{ error: `Value ${w_new} is not a valid number` },
{ status: 400 }
);
}
if (isNaN(l_id)) {
return NextResponse.json({ error: `Value ${l_id} is not a valid number` }, { status: 400 });
return NextResponse.json(
{ error: `Value ${l_id} is not a valid number` },
{ status: 400 })
;
}

// check if locker exists
const locker = await prisma.locker.findUnique({
where: { lockerId: l_id }
});
if (!locker){
return NextResponse.json({ error: `Locker ${l_id} not found` }, { status: 404 });
return NextResponse.json(
{ error: `Locker ${l_id} not found` },
{ status: 404 }
);
}

if(await isLockerClosed(locker) ){
return NextResponse.json({ error: `Locker ${l_id} already closed` }, { status: 409 });
return NextResponse.json(
{ error: `Locker ${l_id} already closed` },
{ status: 409 }
);
}

// Update weight to bypass tamper detection
Expand All @@ -48,6 +63,9 @@ export async function POST(
});
} catch (e) {
console.error(e);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
}
30 changes: 24 additions & 6 deletions apps/web/app/api/locker/finish-reg/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,38 @@ export async function POST(
try {
const { locker_id } = await req.json()
if (locker_id == undefined || locker_id == null ) {
return NextResponse.json({ error: "Route parameters not found" }, { status: 400 });
return NextResponse.json(
{ error: "Route parameters not found" },
{ status: 400 }
);
}
const l_id = parseInt(locker_id, 10)
// Check if locker id is a number (Note: 0 is a valid number!)
if (isNaN(l_id)) {
return NextResponse.json({ error: `Value ${l_id} is not a valid number` }, { status: 400 });
return NextResponse.json(
{ error: `Value ${l_id} is not a valid number` },
{ status: 400 }
);
}

// Check if locker exists
const locker = await prisma.locker.findUnique({
where: { lockerId: l_id }
});
if (!locker){
return NextResponse.json({ error: `Locker ${l_id} not found` }, { status: 404 });
return NextResponse.json(
{ error: `Locker ${l_id} not found` },
{ status: 404 }
);
}

//check if locker is not in IDLE state
const state = await get_locker_state(locker)
if(state != "REGISTER"){
return NextResponse.json({ error: `Not in Registration Period` }, { status: 409 });
return NextResponse.json(
{ error: `Not in Registration Period` },
{ status: 409 }
);
}

// check if no users added to locker
Expand All @@ -36,7 +48,10 @@ export async function POST(
})
if (!users){
create_audit_log(l_id, 'Registration_Finished', 'Unsuccessfull registration. No users added')
return NextResponse.json({ error: "No users added " }, { status: 409 });
return NextResponse.json(
{ error: "No users added" },
{ status: 409 }
);
}

create_audit_log(l_id, 'Registration_Finished', 'Registration Finished')
Expand All @@ -46,6 +61,9 @@ export async function POST(

} catch (error) {
console.error("Checkout Error:", error);
return NextResponse.json({ error: "Failed to checkout" }, { status: 500 });
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
}
15 changes: 12 additions & 3 deletions apps/web/app/api/locker/get-status/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ export async function POST(

// Strict check for the ID
if (locker_id == undefined || locker_id == null ) {
return NextResponse.json({ error: "Route parameter 'id' not found" }, { status: 400 });
return NextResponse.json(
{ error: "Route parameters not found" },
{ status: 400 }
);
}

const l_id = parseInt(locker_id, 10)
// Check if it's actually a number (Note: 0 is a valid number!)
if (isNaN(l_id)) {
return NextResponse.json({ error: `Value '${l_id}' is not a valid number` }, { status: 400 });
return NextResponse.json(
{ error: `Value '${l_id}' is not a valid number` },
{ status: 400 }
);
}

console.log("Status Request for ID:", l_id, locker_id);
Expand All @@ -26,7 +32,10 @@ export async function POST(
});
// check if locker in database
if (!locker){
return NextResponse.json({ error: `Locker ${locker_id} not found` }, { status: 404 });
return NextResponse.json(
{ error: `Locker ${locker_id} not found` },
{ status: 404 }
);
}

// 4. Logic State
Expand Down
30 changes: 24 additions & 6 deletions apps/web/app/api/locker/open-locker/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ export async function POST(
const { qr_data, locker_id } = await req.json();
// Strict check for the params
if (!qr_data || locker_id == undefined || locker_id == null) {
return NextResponse.json({ error: "Route parameters not found" }, { status: 400 });
return NextResponse.json(
{ error: "Route parameters not found" },
{ status: 400 }
);
}
const user_data = JSON.parse(qr_data);
const uin = user_data.uin;
const name = user_data.name;
const l_id = Number(locker_id);
// Check if locker id is a number (Note: 0 is a valid number!)
if (isNaN(l_id)) {
return NextResponse.json({ error: `Value ${l_id} is not a valid number` }, { status: 400 });
return NextResponse.json(
{ error: `Value ${l_id} is not a valid number` },
{ status: 400 }
);
}

// // MOSIP verification
Expand All @@ -33,10 +39,16 @@ export async function POST(
where: { lockerId: l_id }
});
if (!user) {
return NextResponse.json({ error: `User not found` }, { status: 404 });
return NextResponse.json(
{ error: "User not found" },
{ status: 404 }
);
}
if (!locker){
return NextResponse.json({ error: `Locker ${l_id} not found` }, { status: 404 });
return NextResponse.json(
{ error: `Locker ${l_id} not found` },
{ status: 404 }
);
}

// check if existing relationship between locker and user
Expand All @@ -59,10 +71,16 @@ export async function POST(
return NextResponse.json({ message: "Denied", name: user.name });
}
else{
return NextResponse.json({ error: "Locker is not occupied" }, { status: 404 });
return NextResponse.json(
{ error: "Locker is not occupied" },
{ status: 404 }
);
}
} catch (e) {
console.error(e);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
}
Loading