added Callback and custon notification Toast, client notification event handler
This commit is contained in:
24
apps/dispatch-server/modules/expressMiddleware.ts
Normal file
24
apps/dispatch-server/modules/expressMiddleware.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { prisma, User } from "@repo/db";
|
||||
import { NextFunction } from "express";
|
||||
|
||||
interface AttachUserRequest extends Request {
|
||||
user?: User | null;
|
||||
}
|
||||
|
||||
interface AttachUserMiddleware {
|
||||
(req: AttachUserRequest, res: Response, next: NextFunction): Promise<void>;
|
||||
}
|
||||
|
||||
export const authMiddleware: AttachUserMiddleware = async (req, res, next) => {
|
||||
const authHeader = (req.headers as any).authorization;
|
||||
if (authHeader && authHeader.startsWith("User ")) {
|
||||
const userId = authHeader.split(" ")[1];
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
});
|
||||
req.user = user;
|
||||
}
|
||||
next();
|
||||
};
|
||||
101
apps/dispatch-server/modules/mission.ts
Normal file
101
apps/dispatch-server/modules/mission.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { ConnectedAircraft, Mission, prisma } from "@repo/db";
|
||||
import { io } from "index";
|
||||
import { sendNtfyMission } from "modules/ntfy";
|
||||
|
||||
export const sendAlert = async (
|
||||
id: number,
|
||||
{
|
||||
stationId,
|
||||
}: {
|
||||
stationId?: number;
|
||||
},
|
||||
): Promise<{
|
||||
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) {
|
||||
console.log(`Sending mission to: station:${aircraft.stationId}`);
|
||||
io.to(`station:${aircraft.stationId}`).emit("mission-alert", {
|
||||
...mission,
|
||||
Stations,
|
||||
});
|
||||
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,
|
||||
},
|
||||
});
|
||||
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",
|
||||
},
|
||||
});
|
||||
return { connectedAircrafts, mission };
|
||||
};
|
||||
Reference in New Issue
Block a user