Files
var-monorepo/apps/dispatch/app/api/position-log/route.ts
2025-05-09 12:17:29 -07:00

56 lines
1.2 KiB
TypeScript

import { Prisma, prisma } from "@repo/db";
import { getServerSession } from "api/auth/[...nextauth]/auth";
export const PUT = async (req: Request) => {
const session = await getServerSession();
if (!session)
return Response.json({ message: "Unauthorized" }, { status: 401 });
const { position, h145 } = (await req.json()) as {
position: Prisma.PositionLogCreateInput;
h145: boolean;
};
if (!position) {
return Response.json(
{ message: "Missing id or position" },
{ status: 400 },
);
}
const positionLog = await prisma.positionLog.create({
data: position,
});
const activeAircraft = await prisma.connectedAircraft.findFirst({
where: {
userId: session.user.id,
logoutTime: null,
},
orderBy: {
loginTime: "desc",
},
select: {
id: true,
},
});
await prisma.connectedAircraft.update({
where: {
id: activeAircraft?.id,
},
data: {
lastHeartbeat: new Date(),
posAlt: positionLog.alt,
posLat: positionLog.lat,
posLng: positionLog.lng,
posHeading: positionLog.heading,
posSpeed: positionLog.speed,
posH145active: h145,
positionLogIds: {
push: positionLog.id,
},
},
});
return Response.json(positionLog);
};