changes pilot socket to reperate pilto socket, added pilot stats
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user