107 lines
3.1 KiB
TypeScript
107 lines
3.1 KiB
TypeScript
import { getPublicUser, prisma, User } from "@repo/db";
|
|
import { Server, Socket } from "socket.io";
|
|
|
|
export const handleConnectDispatch =
|
|
(socket: Socket, io: Server) =>
|
|
async ({ logoffTime, selectedZone }: { logoffTime: string; selectedZone: string }) => {
|
|
try {
|
|
const user: User = socket.data.user; // User ID aus dem JWT-Token
|
|
|
|
if (!user) return Error("User not found");
|
|
console.log("Disponent connected:", user.publicId);
|
|
|
|
if (!user.permissions?.includes("DISPO")) {
|
|
socket.emit("error", "You do not have permission to connect to the dispatch server.");
|
|
return;
|
|
}
|
|
|
|
const existingConnection = await prisma.connectedDispatcher.findFirst({
|
|
where: {
|
|
userId: user.id,
|
|
logoutTime: null,
|
|
},
|
|
});
|
|
|
|
if (existingConnection) {
|
|
await io.to(`user:${user.id}`).emit("force-disconnect", "double-connection");
|
|
await prisma.connectedDispatcher.updateMany({
|
|
where: {
|
|
userId: user.id,
|
|
logoutTime: null,
|
|
},
|
|
data: {
|
|
logoutTime: new Date().toISOString(),
|
|
},
|
|
});
|
|
}
|
|
|
|
let parsedLogoffDate = null;
|
|
if (logoffTime.length > 0) {
|
|
const now = new Date();
|
|
const [hours, minutes] = logoffTime.split(":").map(Number);
|
|
if (!hours || !minutes) {
|
|
throw new Error("Invalid logoffTime format");
|
|
}
|
|
parsedLogoffDate = new Date(now);
|
|
parsedLogoffDate.setHours(hours, minutes, 0, 0);
|
|
|
|
// If the calculated time is earlier than now, add one day to make it tomorrow
|
|
if (parsedLogoffDate <= now) {
|
|
parsedLogoffDate.setDate(parsedLogoffDate.getDate() + 1);
|
|
}
|
|
|
|
// If the calculated time is in the past, add one day to make it in the future
|
|
if (parsedLogoffDate <= now) {
|
|
parsedLogoffDate.setDate(parsedLogoffDate.getDate() + 1);
|
|
}
|
|
}
|
|
|
|
const connectedDispatcherEntry = await prisma.connectedDispatcher.create({
|
|
data: {
|
|
publicUser: getPublicUser(user) as any,
|
|
esimatedLogoutTime: parsedLogoffDate?.toISOString() || null,
|
|
lastHeartbeat: new Date().toISOString(),
|
|
userId: user.id,
|
|
loginTime: new Date().toISOString(),
|
|
},
|
|
});
|
|
|
|
socket.join("dispatchers"); // Dem Dispatcher-Raum beitreten
|
|
socket.join(`user:${user.id}`); // Dem User-Raum beitreten
|
|
|
|
io.to("dispatchers").emit("dispatchers-update");
|
|
io.to("pilots").emit("dispatchers-update");
|
|
|
|
// dispatch-events
|
|
socket.on("ptt", async ({ shouldTransmit, channel }) => {
|
|
if (shouldTransmit) {
|
|
io.to("dispatchers").emit("other-ptt", {
|
|
publicUser: getPublicUser(user),
|
|
channel,
|
|
source: "Leitstelle",
|
|
});
|
|
io.to("piots").emit("other-ptt", {
|
|
publicUser: getPublicUser(user),
|
|
channel,
|
|
source: "Leitstelle",
|
|
});
|
|
}
|
|
});
|
|
|
|
socket.on("disconnect", async () => {
|
|
await prisma.connectedDispatcher.update({
|
|
where: {
|
|
id: connectedDispatcherEntry.id,
|
|
},
|
|
data: {
|
|
logoutTime: new Date().toISOString(),
|
|
},
|
|
});
|
|
io.to("dispatchers").emit("dispatchers-update");
|
|
io.to("pilots").emit("dispatchers-update");
|
|
});
|
|
} catch (error) {
|
|
console.error("Error connecting to dispatch server:", error);
|
|
}
|
|
};
|