Heliport Search, Station connection history table, reworked mission close functionality
This commit is contained in:
@@ -2,6 +2,39 @@ import { MissionLog, NotificationPayload, prisma } from "@repo/db";
|
||||
import { io } from "index";
|
||||
import cron from "node-cron";
|
||||
|
||||
const removeMission = async (id: number, reason: string) => {
|
||||
const log: MissionLog = {
|
||||
type: "completed-log",
|
||||
auto: true,
|
||||
timeStamp: new Date().toISOString(),
|
||||
data: {},
|
||||
};
|
||||
|
||||
const updatedMission = await prisma.mission.update({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
data: {
|
||||
state: "finished",
|
||||
missionLog: {
|
||||
push: log as any,
|
||||
},
|
||||
},
|
||||
});
|
||||
io.to("dispatchers").emit("new-mission", { updatedMission });
|
||||
io.to("dispatchers").emit("notification", {
|
||||
type: "mission-auto-close",
|
||||
status: "chron",
|
||||
message: `Einsatz ${updatedMission.publicId} wurde aufgrund ${reason} geschlossen.`,
|
||||
data: {
|
||||
missionId: updatedMission.id,
|
||||
publicMissionId: updatedMission.publicId,
|
||||
},
|
||||
} as NotificationPayload);
|
||||
|
||||
console.log(`Mission ${updatedMission.id} closed due to inactivity.`);
|
||||
};
|
||||
|
||||
const removeClosedMissions = async () => {
|
||||
const oldMissions = await prisma.mission.findMany({
|
||||
where: {
|
||||
@@ -15,18 +48,6 @@ const removeClosedMissions = async () => {
|
||||
|
||||
const lastAlertTime = lastAlert ? new Date(lastAlert.timeStamp) : null;
|
||||
|
||||
const aircraftsInMission = await prisma.connectedAircraft.findMany({
|
||||
where: {
|
||||
stationId: {
|
||||
in: mission.missionStationIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const allConnectedAircraftsInIdleStatus = aircraftsInMission.every((a) =>
|
||||
["1", "2", "6"].includes(a.fmsStatus),
|
||||
);
|
||||
|
||||
const allStationsInMissionChangedFromStatus4to1Or8to1 = mission.missionStationIds.every(
|
||||
(stationId) => {
|
||||
const status4Log = (mission.missionLog as unknown as MissionLog[]).findIndex((l) => {
|
||||
@@ -69,67 +90,24 @@ const removeClosedMissions = async () => {
|
||||
},
|
||||
);
|
||||
|
||||
const missionHastManualReactivation = (mission.missionLog as unknown as MissionLog[]).some(
|
||||
const missionHasManualReactivation = (mission.missionLog as unknown as MissionLog[]).some(
|
||||
(l) => l.type === "reopened-log",
|
||||
);
|
||||
console.log({
|
||||
missionId: mission.publicId,
|
||||
allConnectedAircraftsInIdleStatus,
|
||||
lastAlertTime,
|
||||
allStationsInMissionChangedFromStatus4to1Or8to1,
|
||||
missionHastManualReactivation,
|
||||
});
|
||||
if (
|
||||
!allConnectedAircraftsInIdleStatus // If some aircrafts are still active, do not close the mission
|
||||
)
|
||||
return;
|
||||
|
||||
const now = new Date();
|
||||
if (missionHasManualReactivation) return;
|
||||
|
||||
if (!lastAlertTime) return;
|
||||
|
||||
// Case 1: Forgotten Mission, last alert more than 3 Hours ago
|
||||
// Case 2: All stations in mission changed from status 4 to 1 or from status 8 to 1
|
||||
if (
|
||||
!(
|
||||
now.getTime() - lastAlertTime.getTime() > 1000 * 60 * 180 ||
|
||||
allStationsInMissionChangedFromStatus4to1Or8to1
|
||||
) ||
|
||||
missionHastManualReactivation
|
||||
)
|
||||
return;
|
||||
const now = new Date();
|
||||
if (now.getTime() - lastAlertTime.getTime() > 1000 * 60 * 180)
|
||||
return removeMission(mission.id, "inaktivität");
|
||||
|
||||
const log: MissionLog = {
|
||||
type: "completed-log",
|
||||
auto: true,
|
||||
timeStamp: new Date().toISOString(),
|
||||
data: {},
|
||||
};
|
||||
|
||||
const updatedMission = await prisma.mission.update({
|
||||
where: {
|
||||
id: mission.id,
|
||||
},
|
||||
data: {
|
||||
state: "finished",
|
||||
missionLog: {
|
||||
push: log as any,
|
||||
},
|
||||
},
|
||||
});
|
||||
io.to("dispatchers").emit("new-mission", { updatedMission });
|
||||
io.to("dispatchers").emit("notification", {
|
||||
type: "mission-auto-close",
|
||||
status: "chron",
|
||||
message: `Einsatz ${updatedMission.publicId} wurde aufgrund ${allStationsInMissionChangedFromStatus4to1Or8to1 ? "des Freimeldens aller Stationen" : "von Inaktivität"} geschlossen.`,
|
||||
data: {
|
||||
missionId: updatedMission.id,
|
||||
publicMissionId: updatedMission.publicId,
|
||||
},
|
||||
} as NotificationPayload);
|
||||
console.log(`Mission ${mission.id} closed due to inactivity.`);
|
||||
// Case 2: All stations in mission changed from status 4 to 1/6 or from status 8 to 1/6
|
||||
if (allStationsInMissionChangedFromStatus4to1Or8to1)
|
||||
return removeMission(mission.id, "dem freimelden aller Stationen");
|
||||
});
|
||||
};
|
||||
|
||||
const removeConnectedAircrafts = async () => {
|
||||
const connectedAircrafts = await prisma.connectedAircraft.findMany({
|
||||
where: {
|
||||
|
||||
Reference in New Issue
Block a user