Added soem things
This commit is contained in:
@@ -1,24 +1,41 @@
|
||||
import { Prisma, prisma } from "@repo/db";
|
||||
import { PositionLog, Prisma, prisma, User } from "@repo/db";
|
||||
import { getServerSession } from "api/auth/[...nextauth]/auth";
|
||||
import { verify } from "jsonwebtoken";
|
||||
|
||||
export const PUT = async (req: Request) => {
|
||||
const session = await getServerSession();
|
||||
if (!session)
|
||||
const token = req.headers.get("authorization")?.split(" ")[1];
|
||||
if (!token)
|
||||
return Response.json({ message: "Missing token" }, { status: 401 });
|
||||
|
||||
const payload = await new Promise<User>((resolve, reject) => {
|
||||
verify(token, process.env.NEXTAUTH_HUB_SECRET as string, (err, decoded) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(decoded as User);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (!session && !payload)
|
||||
return Response.json({ message: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const userId = session?.user.id || payload.id;
|
||||
const { position, h145 } = (await req.json()) as {
|
||||
position: Prisma.PositionLogCreateInput;
|
||||
position: PositionLog;
|
||||
h145: boolean;
|
||||
};
|
||||
console.log("position", userId);
|
||||
if (!position) {
|
||||
return Response.json(
|
||||
{ message: "Missing id or position" },
|
||||
{ status: 400 },
|
||||
);
|
||||
return Response.json({ message: "Missing id or position" });
|
||||
}
|
||||
|
||||
console.log("position", position);
|
||||
|
||||
const activeAircraft = await prisma.connectedAircraft.findFirst({
|
||||
where: {
|
||||
userId: session.user.id,
|
||||
userId,
|
||||
logoutTime: null,
|
||||
},
|
||||
orderBy: {
|
||||
@@ -36,7 +53,10 @@ export const PUT = async (req: Request) => {
|
||||
);
|
||||
}
|
||||
const positionLog = await prisma.positionLog.create({
|
||||
data: position,
|
||||
data: {
|
||||
...position,
|
||||
userId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.connectedAircraft.update({
|
||||
@@ -44,6 +64,7 @@ export const PUT = async (req: Request) => {
|
||||
id: activeAircraft?.id,
|
||||
},
|
||||
data: {
|
||||
userId,
|
||||
lastHeartbeat: new Date(),
|
||||
posAlt: positionLog.alt,
|
||||
posLat: positionLog.lat,
|
||||
|
||||
Reference in New Issue
Block a user