diff --git a/apps/dispatch-server/modules/mission.ts b/apps/dispatch-server/modules/mission.ts index e79c95a8..4f9fe07c 100644 --- a/apps/dispatch-server/modules/mission.ts +++ b/apps/dispatch-server/modules/mission.ts @@ -14,113 +14,118 @@ export const sendAlert = async ( connectedAircrafts: ConnectedAircraft[]; mission: Mission; }> => { - const mission = await prisma.mission.findUnique({ - where: { id: id }, - }); - const Stations = await prisma.station.findMany({ - where: { - id: { - in: mission?.missionStationIds, - }, - }, - }); - - if (!mission) { - throw new Error("Mission not found"); - } - - // connectedAircrafts the alert is sent to - const connectedAircrafts = await prisma.connectedAircraft.findMany({ - where: { - stationId: stationId - ? stationId - : { - in: mission.missionStationIds, - }, - logoutTime: null, - }, - include: { - Station: true, - }, - }); - - for (const aircraft of connectedAircrafts) { - io.to(`station:${aircraft.stationId}`).emit("mission-alert", { - ...mission, - Stations, + try { + const mission = await prisma.mission.findUnique({ + where: { id: id }, }); - io.to(`desktop:${aircraft.userId}`).emit("mission-alert", { - missionId: mission.id, - }); - - const user = await prisma.user.findUnique({ - where: { id: aircraft.userId }, - }); - if (!user) continue; - if (user.settingsNtfyRoom) { - await sendNtfyMission(mission, Stations, aircraft.Station, user.settingsNtfyRoom); - } - const existingMissionOnStationUser = await prisma.missionOnStationUsers.findFirst({ + const Stations = await prisma.station.findMany({ where: { - missionId: mission.id, - userId: aircraft.userId, - stationId: aircraft.stationId, + id: { + in: mission?.missionStationIds, + }, }, }); - if (!existingMissionOnStationUser) - await prisma.missionOnStationUsers.create({ - data: { + if (!mission) { + throw new Error("Mission not found"); + } + + // connectedAircrafts the alert is sent to + const connectedAircrafts = await prisma.connectedAircraft.findMany({ + where: { + stationId: stationId + ? stationId + : { + in: mission.missionStationIds, + }, + logoutTime: null, + }, + include: { + Station: true, + }, + }); + + for (const aircraft of connectedAircrafts) { + io.to(`station:${aircraft.stationId}`).emit("mission-alert", { + ...mission, + Stations, + }); + io.to(`desktop:${aircraft.userId}`).emit("mission-alert", { + missionId: mission.id, + }); + + const user = await prisma.user.findUnique({ + where: { id: aircraft.userId }, + }); + if (!user) continue; + if (user.settingsNtfyRoom) { + await sendNtfyMission(mission, Stations, aircraft.Station, user.settingsNtfyRoom); + } + const existingMissionOnStationUser = await prisma.missionOnStationUsers.findFirst({ + where: { 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((err) => { - // Ignore if the entry already exists - }); - if (user === "HPG") { - await prisma.mission.update({ - where: { id: Number(id) }, - data: { - state: "running", - missionLog: { - push: { - type: "alert-log", - auto: true, - timeStamp: new Date().toISOString(), - } as any, + 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((err) => { + // Ignore if the entry already exists + }); + if (user === "HPG") { + await prisma.mission.update({ + where: { id: Number(id) }, + data: { + state: "running", + missionLog: { + push: { + type: "alert-log", + auto: true, + timeStamp: new Date().toISOString(), + } as any, + }, }, - }, - }); - } else { - await prisma.mission.update({ - where: { id: Number(id) }, - data: { - state: "running", - missionLog: { - push: { - type: "alert-log", - auto: false, - timeStamp: new Date().toISOString(), - data: { - stationId: stationId, - user: getPublicUser(user, { ignorePrivacy: true }), - }, - } as any, + }); + } else { + await prisma.mission.update({ + where: { id: Number(id) }, + data: { + state: "running", + missionLog: { + push: { + type: "alert-log", + auto: false, + timeStamp: new Date().toISOString(), + data: { + stationId: stationId, + user: getPublicUser(user, { ignorePrivacy: true }), + }, + } as any, + }, }, - }, - }); + }); + } + return { connectedAircrafts, mission }; + } catch (error) { + console.error("Error sending mission alert:", error); + throw new Error("Ein Fehler ist aufgetreten. Bitte melde den Fehler als Bug"); } - return { connectedAircrafts, mission }; }; diff --git a/apps/dispatch-server/modules/ntfy.ts b/apps/dispatch-server/modules/ntfy.ts index bc86df56..3ba75dde 100644 --- a/apps/dispatch-server/modules/ntfy.ts +++ b/apps/dispatch-server/modules/ntfy.ts @@ -50,10 +50,7 @@ const getRthCallsigns = (mission: Mission, stations: Station[]) => { return `🚁 RTH${callsigns.length > 1 ? "s" : ""}: ${callsigns.join(" / ")} `; }; -const getNtfyHeader = ( - mission: Mission, - clientStation: Station, -): NtfyHeader => ({ +const getNtfyHeader = (mission: Mission, clientStation: Station): NtfyHeader => ({ headers: { Title: `${clientStation.bosCallsignShort} / ${mission.missionKeywordAbbreviation} / ${mission.missionKeywordCategory}`, Tags: "pager", @@ -76,9 +73,13 @@ export const sendNtfyMission = async ( clientStation: Station, ntfyRoom: string, ) => { - axios.post( - `https://ntfy.sh/${ntfyRoom}`, - getNtfyData(mission, stations), - getNtfyHeader(mission, clientStation), - ); + try { + await axios.post( + `https://ntfy.sh/${ntfyRoom}`, + getNtfyData(mission, stations), + getNtfyHeader(mission, clientStation), + ); + } catch (error) { + console.error("Error sending Ntfy mission:", error); + } }; diff --git a/apps/dispatch-server/routes/mission.ts b/apps/dispatch-server/routes/mission.ts index bcb18a2a..e487e59f 100644 --- a/apps/dispatch-server/routes/mission.ts +++ b/apps/dispatch-server/routes/mission.ts @@ -189,7 +189,11 @@ router.post("/:id/send-alert", async (req, res) => { return; } catch (error) { console.error(error); - res.status(500).json({ error: "Failed to send mission" }); + res + .status(500) + .json({ + error: `Ein Fehler ist aufgetreten. Bitte melde den Fehler als Bug (${(error as Error).message})`, + }); return; } });