diff --git a/apps/dispatch-server/modules/chron.ts b/apps/dispatch-server/modules/chron.ts index 920b3d78..f7747c0b 100644 --- a/apps/dispatch-server/modules/chron.ts +++ b/apps/dispatch-server/modules/chron.ts @@ -13,30 +13,43 @@ const removeClosedMissions = async () => { }); const lastAlertTime = lastAlert ? new Date(lastAlert.timeStamp) : null; + const aircraftsInMission = await prisma.connectedAircraft.findMany({ + where: { + stationId: { + in: mission.missionStationIds, + }, + }, + }); + + if ( + !aircraftsInMission || + !aircraftsInMission.some((a) => ["1", "2", "6"].includes(a.fmsStatus)) + ) + return; + const now = new Date(); if (!lastAlertTime) return; // change State to closed if last alert was more than 180 minutes ago - if (lastAlertTime && now.getTime() - lastAlertTime.getTime() > 180 * 60 * 1000) { - const log: MissionLog = { - type: "completed-log", - auto: true, - timeStamp: new Date().toISOString(), - data: {}, - }; + if (now.getTime() - lastAlertTime.getTime() < 30 * 60 * 1000) return; + const log: MissionLog = { + type: "completed-log", + auto: true, + timeStamp: new Date().toISOString(), + data: {}, + }; - await prisma.mission.update({ - where: { - id: mission.id, + await prisma.mission.update({ + where: { + id: mission.id, + }, + data: { + state: "finished", + missionLog: { + push: log as any, }, - data: { - state: "finished", - missionLog: { - push: log as any, - }, - }, - }); - console.log(`Mission ${mission.id} closed due to inactivity.`); - } + }, + }); + console.log(`Mission ${mission.id} closed due to inactivity.`); }); };