posLog idle verbessert

This commit is contained in:
PxlLoewe
2025-07-07 04:14:42 -07:00
parent 413be72eca
commit cd2782006d

View File

@@ -3,6 +3,7 @@ import { getServerSession } from "api/auth/[...nextauth]/auth";
import { verify } from "jsonwebtoken"; import { verify } from "jsonwebtoken";
export const PUT = async (req: Request) => { export const PUT = async (req: Request) => {
try {
const session = await getServerSession(); const session = await getServerSession();
const token = req.headers.get("authorization")?.split(" ")[1]; const token = req.headers.get("authorization")?.split(" ")[1];
if (!token) return Response.json({ message: "Missing token" }, { status: 401 }); if (!token) return Response.json({ message: "Missing token" }, { status: 401 });
@@ -38,18 +39,8 @@ export const PUT = async (req: Request) => {
}, },
select: { select: {
id: true, id: true,
}, posLat: true,
}); posLng: true,
if (!activeAircraft) {
return Response.json({ message: "No active aircraft found" }, { status: 400 });
}
const positionLog = await prisma.positionLog.create({
data: {
...position,
connectedAircraftId: activeAircraft.id,
userId,
}, },
}); });
@@ -60,14 +51,34 @@ export const PUT = async (req: Request) => {
data: { data: {
userId, userId,
lastHeartbeat: new Date(), lastHeartbeat: new Date(),
posAlt: positionLog.alt, posAlt: position.alt,
posLat: positionLog.lat, posLat: position.lat,
posLng: positionLog.lng, posLng: position.lng,
posHeading: positionLog.heading, posHeading: position.heading,
posSpeed: positionLog.speed, posSpeed: position.speed,
posH145active: h145, posH145active: h145,
}, },
}); });
if (!activeAircraft) {
return Response.json({ message: "No active aircraft found" }, { status: 400 });
}
if (activeAircraft.posLat === position.lat && activeAircraft.posLng === position.lng) {
return Response.json({ message: "Position has not changed" }, { status: 400 });
}
const positionLog = await prisma.positionLog.create({
data: {
...position,
connectedAircraftId: activeAircraft.id,
userId,
},
});
return Response.json(positionLog); return Response.json(positionLog);
} catch (error) {
console.error("Error in PUT /api/position-log:", error);
return Response.json({ message: "Internal Server Error" }, { status: 500 });
}
}; };