added logbook
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { ConnectedAircraft, Mission, prisma } from "@repo/db";
|
||||
import { ConnectedAircraft, getPublicUser, Mission, prisma, User } from "@repo/db";
|
||||
import { io } from "index";
|
||||
import { sendNtfyMission } from "modules/ntfy";
|
||||
|
||||
@@ -9,6 +9,7 @@ export const sendAlert = async (
|
||||
}: {
|
||||
stationId?: number;
|
||||
},
|
||||
user: User,
|
||||
): Promise<{
|
||||
connectedAircrafts: ConnectedAircraft[];
|
||||
mission: Mission;
|
||||
@@ -44,7 +45,6 @@ export const sendAlert = async (
|
||||
});
|
||||
|
||||
for (const aircraft of connectedAircrafts) {
|
||||
console.log(`Sending mission to: station:${aircraft.stationId}`);
|
||||
io.to(`station:${aircraft.stationId}`).emit("mission-alert", {
|
||||
...mission,
|
||||
Stations,
|
||||
@@ -54,21 +54,15 @@ export const sendAlert = async (
|
||||
});
|
||||
if (!user) continue;
|
||||
if (user.settingsNtfyRoom) {
|
||||
await sendNtfyMission(
|
||||
mission,
|
||||
Stations,
|
||||
aircraft.Station,
|
||||
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,
|
||||
},
|
||||
});
|
||||
const existingMissionOnStationUser = await prisma.missionOnStationUsers.findFirst({
|
||||
where: {
|
||||
missionId: mission.id,
|
||||
userId: aircraft.userId,
|
||||
stationId: aircraft.stationId,
|
||||
},
|
||||
});
|
||||
if (!existingMissionOnStationUser)
|
||||
await prisma.missionOnStationUsers.create({
|
||||
data: {
|
||||
@@ -95,6 +89,17 @@ export const sendAlert = async (
|
||||
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 };
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import {
|
||||
getPublicUser,
|
||||
HpgValidationState,
|
||||
MissionAlertLog,
|
||||
MissionSdsLog,
|
||||
MissionStationLog,
|
||||
NotificationPayload,
|
||||
@@ -119,6 +121,11 @@ router.post("/:id/send-alert", async (req, res) => {
|
||||
vehicleName?: "ambulance" | "police" | "firebrigade";
|
||||
};
|
||||
|
||||
if (!req.user) {
|
||||
res.status(401).json({ error: "Unauthorized" });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (vehicleName) {
|
||||
const hpgAircrafts = await prisma.connectedAircraft.findMany({
|
||||
@@ -137,6 +144,17 @@ router.post("/:id/send-alert", async (req, res) => {
|
||||
hpgAmbulanceState: vehicleName === "ambulance" ? "DISPATCHED" : undefined,
|
||||
hpgFireEngineState: vehicleName === "firebrigade" ? "DISPATCHED" : undefined,
|
||||
hpgPoliceState: vehicleName === "police" ? "DISPATCHED" : undefined,
|
||||
missionLog: {
|
||||
push: {
|
||||
type: "alert-log",
|
||||
auto: false,
|
||||
timeStamp: new Date().toISOString(),
|
||||
data: {
|
||||
vehicle: vehicleName,
|
||||
user: getPublicUser(req.user as User),
|
||||
},
|
||||
} as any,
|
||||
},
|
||||
},
|
||||
});
|
||||
hpgAircrafts.forEach((aircraft) => {
|
||||
@@ -156,9 +174,13 @@ router.post("/:id/send-alert", async (req, res) => {
|
||||
io.to("dispatchers").emit("update-mission", newMission);
|
||||
return;
|
||||
}
|
||||
const { connectedAircrafts, mission } = await sendAlert(Number(id), {
|
||||
stationId,
|
||||
});
|
||||
const { connectedAircrafts, mission } = await sendAlert(
|
||||
Number(id),
|
||||
{
|
||||
stationId,
|
||||
},
|
||||
req.user,
|
||||
);
|
||||
|
||||
res.status(200).json({
|
||||
message: `Einsatz gesendet (${connectedAircrafts.length} Nutzer) `,
|
||||
@@ -231,7 +253,7 @@ router.post("/:id/validate-hpg", async (req, res) => {
|
||||
return;
|
||||
}
|
||||
res.json({
|
||||
message: "HPG validation started",
|
||||
message: "HPG validierung gestartet",
|
||||
});
|
||||
|
||||
io.to(`desktop:${activeAircraftinMission}`).emit(
|
||||
@@ -265,7 +287,8 @@ router.post("/:id/validate-hpg", async (req, res) => {
|
||||
message: `HPG Validierung erfolgreich`,
|
||||
} as NotificationPayload);
|
||||
if (config?.alertWhenValid) {
|
||||
sendAlert(Number(id), {});
|
||||
if (!req.user) return;
|
||||
sendAlert(Number(id), {}, req.user);
|
||||
}
|
||||
} else {
|
||||
io.to(`user:${req.user?.id}`).emit("notification", {
|
||||
@@ -276,25 +299,6 @@ router.post("/:id/validate-hpg", async (req, res) => {
|
||||
}
|
||||
},
|
||||
);
|
||||
// TODO: remove this after testing
|
||||
setTimeout(() => {
|
||||
io.to(`user:${req.user?.id}`).emit("notification", {
|
||||
type: "hpg-validation",
|
||||
status: "success",
|
||||
message: "HPG_BUSY",
|
||||
data: {
|
||||
mission,
|
||||
},
|
||||
} as NotificationPayload);
|
||||
io.to(`user:${req.user?.id}`).emit("notification", {
|
||||
type: "hpg-validation",
|
||||
status: "failed",
|
||||
message: `HPG Validation fehlgeschlagen`,
|
||||
data: {
|
||||
mission,
|
||||
},
|
||||
} as NotificationPayload);
|
||||
}, 5000);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.json({ error: (error as Error).message || "Failed to validate HPG" });
|
||||
|
||||
Reference in New Issue
Block a user