From d562056dc2470c7cccc47fd4e78df3d4f4d4c09c Mon Sep 17 00:00:00 2001 From: PxlLoewe <72106766+PxlLoewe@users.noreply.github.com> Date: Fri, 9 May 2025 12:17:29 -0700 Subject: [PATCH] Added route for position update --- .../api/{livekit => keywords}/token/route.ts | 0 apps/dispatch/app/api/position-log/route.ts | 55 ++++++++++++++++++ apps/hub/app/api/user/route.ts | 1 + grafana/grafana.db | Bin 1122304 -> 1122304 bytes 4 files changed, 56 insertions(+) rename apps/dispatch/app/api/{livekit => keywords}/token/route.ts (100%) create mode 100644 apps/dispatch/app/api/position-log/route.ts diff --git a/apps/dispatch/app/api/livekit/token/route.ts b/apps/dispatch/app/api/keywords/token/route.ts similarity index 100% rename from apps/dispatch/app/api/livekit/token/route.ts rename to apps/dispatch/app/api/keywords/token/route.ts diff --git a/apps/dispatch/app/api/position-log/route.ts b/apps/dispatch/app/api/position-log/route.ts new file mode 100644 index 00000000..7951c8a9 --- /dev/null +++ b/apps/dispatch/app/api/position-log/route.ts @@ -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); +}; diff --git a/apps/hub/app/api/user/route.ts b/apps/hub/app/api/user/route.ts index cf101c40..ad7d67d7 100644 --- a/apps/hub/app/api/user/route.ts +++ b/apps/hub/app/api/user/route.ts @@ -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 () => { diff --git a/grafana/grafana.db b/grafana/grafana.db index 26692ff71062e5a0e11b1f6635ab06d38012257a..60aadc356836b96667f9417fdc35fdf68e161671 100644 GIT binary patch delta 98 zcmZoT;L>owWr8$g$V3@u#*oH@)&$1Z1g6#m=GFw3)&$np1h&=$_N@sVrTVPLGvq>| srr*-%kY|<5kkh-^{!gC+h&h3n3y8UamowWr8%L$3z)tMvumX)&$1Z1g6#m=GFw3)&$np1h&=$_N@sVrTVOUGUOBv rPQRtkA