changes pilot socket to reperate pilto socket, added pilot stats
This commit is contained in:
@@ -32,7 +32,6 @@ const router = Router();
|
||||
|
||||
router.get("/token", async (req, res) => {
|
||||
const roomName = req.query.roomName as string;
|
||||
console.log("roomName", roomName);
|
||||
res.send({
|
||||
token: await createToken(roomName),
|
||||
});
|
||||
|
||||
@@ -80,4 +80,86 @@ router.delete("/:id", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Send mission
|
||||
|
||||
router.post("/:id/send-alert", async (req, res) => {
|
||||
const { id } = req.params;
|
||||
try {
|
||||
const mission = await prisma.mission.findUnique({
|
||||
where: { id: Number(id) },
|
||||
});
|
||||
const Stations = await prisma.station.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: mission?.missionStationIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!mission) {
|
||||
res.status(404).json({ error: "Mission not found" });
|
||||
return;
|
||||
}
|
||||
const connectedAircrafts = await prisma.connectedAircraft.findMany({
|
||||
where: {
|
||||
stationId: {
|
||||
in: mission.missionStationIds,
|
||||
},
|
||||
logoutTime: null,
|
||||
},
|
||||
});
|
||||
|
||||
for (const aircraft of connectedAircrafts) {
|
||||
console.log(`Sending mission to: station:${aircraft.stationId}`);
|
||||
io.to(`station:${aircraft.stationId}`).emit("mission-alert", {
|
||||
...mission,
|
||||
Stations,
|
||||
});
|
||||
const existingMissionOnStationUser =
|
||||
await prisma.missionOnStationUsers.findFirst({
|
||||
where: {
|
||||
missionId: mission.id,
|
||||
userId: aircraft.userId,
|
||||
stationId: aircraft.stationId,
|
||||
},
|
||||
});
|
||||
if (!existingMissionOnStationUser)
|
||||
await prisma.missionOnStationUsers.create({
|
||||
data: {
|
||||
missionId: mission.id,
|
||||
userId: aircraft.userId,
|
||||
stationId: aircraft.stationId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// for statistics only
|
||||
await prisma.missionsOnStations
|
||||
.createMany({
|
||||
data: mission.missionStationIds.map((stationId) => ({
|
||||
missionId: mission.id,
|
||||
stationId,
|
||||
})),
|
||||
})
|
||||
.catch(() => {
|
||||
// Ignore if the entry already exists
|
||||
});
|
||||
|
||||
await prisma.mission.update({
|
||||
where: { id: Number(id) },
|
||||
data: {
|
||||
state: "running",
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
message: `Einsatz gesendet (${connectedAircrafts.length} Nutzer) `,
|
||||
});
|
||||
io.to("dispatchers").emit("update-mission", mission);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: "Failed to send mission" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -14,6 +14,8 @@ export const handleConnectPilot =
|
||||
const user = socket.data.user; // User ID aus dem JWT-Token
|
||||
const userId = socket.data.user.id; // User ID aus dem JWT-Token
|
||||
|
||||
console.log("Pilot connected:", userId);
|
||||
|
||||
if (!user) return Error("User not found");
|
||||
|
||||
const existingConnection = await prisma.connectedAircraft.findFirst({
|
||||
@@ -62,8 +64,6 @@ export const handleConnectPilot =
|
||||
userId: userId,
|
||||
loginTime: new Date().toISOString(),
|
||||
stationId: parseInt(stationId),
|
||||
/* user: { connect: { id: userId } }, // Ensure the user relationship is set
|
||||
station: { connect: { id: stationId } }, // Ensure the station relationship is set */
|
||||
},
|
||||
});
|
||||
|
||||
@@ -71,6 +71,8 @@ export const handleConnectPilot =
|
||||
socket.join(`user:${userId}`); // Join the user-specific room
|
||||
socket.join(`station:${stationId}`); // Join the station-specific room
|
||||
|
||||
console.log(`Pilot in: station:${stationId}`);
|
||||
|
||||
io.to("dispatchers").emit("pilots-update");
|
||||
io.to("pilots").emit("pilots-update");
|
||||
|
||||
@@ -83,14 +85,16 @@ export const handleConnectPilot =
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
console.log("Disconnected from dispatch server");
|
||||
await prisma.connectedAircraft.update({
|
||||
where: {
|
||||
id: connectedAircraftEntry.id,
|
||||
},
|
||||
data: {
|
||||
logoutTime: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
await prisma.connectedAircraft
|
||||
.update({
|
||||
where: {
|
||||
id: connectedAircraftEntry.id,
|
||||
},
|
||||
data: {
|
||||
logoutTime: new Date().toISOString(),
|
||||
},
|
||||
})
|
||||
.catch(console.error);
|
||||
io.to("dispatchers").emit("pilots-update");
|
||||
io.to("pilots").emit("pilots-update");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user