Added route for position update
This commit is contained in:
55
apps/dispatch/app/api/position-log/route.ts
Normal file
55
apps/dispatch/app/api/position-log/route.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
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);
|
||||
};
|
||||
@@ -21,6 +21,7 @@ export const GET = async (req: NextRequest) => {
|
||||
id: decoded.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user)
|
||||
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
||||
setTimeout(async () => {
|
||||
|
||||
Reference in New Issue
Block a user