feat: Implement connected user API and integrate chat and report components
- Added API routes for fetching connected users, keywords, missions, and stations. - Created a new QueryProvider component for managing query states and socket events. - Introduced connection stores for dispatch and pilot, managing socket connections and states. - Updated Prisma schema for connected aircraft model. - Enhanced UI with toast notifications for status updates and chat interactions. - Implemented query functions for fetching connected users and keywords with error handling.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { getPublicUser, prisma, User } from "@repo/db";
|
||||
import { pubClient } from "modules/redis";
|
||||
import { Server, Socket } from "socket.io";
|
||||
|
||||
export const handleConnectDispatch =
|
||||
@@ -80,9 +79,10 @@ export const handleConnectDispatch =
|
||||
|
||||
socket.join("dispatchers"); // Dem Dispatcher-Raum beitreten
|
||||
socket.join(`user:${user.id}`); // Dem User-Raum beitreten
|
||||
socket.join("missions");
|
||||
|
||||
io.to("dispatchers").emit("dispatcher-update");
|
||||
io.to("pilots").emit("dispatcher-update");
|
||||
io.to("dispatchers").emit("dispatchers-update");
|
||||
io.to("pilots").emit("dispatchers-update");
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
console.log("Disconnected from dispatch server");
|
||||
@@ -94,6 +94,8 @@ export const handleConnectDispatch =
|
||||
logoutTime: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
io.to("dispatchers").emit("dispatchers-update");
|
||||
io.to("pilots").emit("dispatchers-update");
|
||||
});
|
||||
socket.on("reconnect", async () => {
|
||||
console.log("Reconnected to dispatch server");
|
||||
|
||||
@@ -1,25 +1,42 @@
|
||||
import { getPublicUser, prisma } from "@repo/db";
|
||||
import { Server, Socket } from "socket.io";
|
||||
|
||||
export const handleConnectDispatch =
|
||||
export const handleConnectPilot =
|
||||
(socket: Socket, io: Server) =>
|
||||
async ({
|
||||
logoffTime,
|
||||
stationId,
|
||||
}: {
|
||||
logoffTime: string;
|
||||
stationId: number;
|
||||
stationId: string;
|
||||
}) => {
|
||||
try {
|
||||
const user = socket.data.user; // User ID aus dem JWT-Token
|
||||
const userId = socket.data.user.id; // User ID aus dem JWT-Token
|
||||
console.log("User connected to dispatch server");
|
||||
const user = await prisma.user.findUnique({
|
||||
|
||||
if (!user) return Error("User not found");
|
||||
|
||||
const existingConnection = await prisma.connectedAircraft.findFirst({
|
||||
where: {
|
||||
id: userId,
|
||||
userId: user.id,
|
||||
logoutTime: null,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) return Error("User not found");
|
||||
if (existingConnection) {
|
||||
await io
|
||||
.to(`user:${user.id}`)
|
||||
.emit("force-disconnect", "double-connection");
|
||||
await prisma.connectedAircraft.updateMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
logoutTime: null,
|
||||
},
|
||||
data: {
|
||||
logoutTime: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
let parsedLogoffDate = null;
|
||||
if (logoffTime.length > 0) {
|
||||
@@ -44,7 +61,7 @@ export const handleConnectDispatch =
|
||||
lastHeartbeat: new Date().toISOString(),
|
||||
userId: userId,
|
||||
loginTime: new Date().toISOString(),
|
||||
stationId: stationId,
|
||||
stationId: parseInt(stationId),
|
||||
/* user: { connect: { id: userId } }, // Ensure the user relationship is set
|
||||
station: { connect: { id: stationId } }, // Ensure the station relationship is set */
|
||||
},
|
||||
@@ -54,8 +71,8 @@ export const handleConnectDispatch =
|
||||
socket.join(`user:${userId}`); // Join the user-specific room
|
||||
socket.join(`station:${stationId}`); // Join the station-specific room
|
||||
|
||||
io.to("dispatchers").emit("dispatcher-update");
|
||||
io.to("pilots").emit("dispatcher-update");
|
||||
io.to("dispatchers").emit("pilots-update");
|
||||
io.to("pilots").emit("pilots-update");
|
||||
|
||||
// Add a listener for station-specific events
|
||||
socket.on(`station:${stationId}:event`, async (data) => {
|
||||
@@ -66,7 +83,7 @@ export const handleConnectDispatch =
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
console.log("Disconnected from dispatch server");
|
||||
await prisma.connectedDispatcher.update({
|
||||
await prisma.connectedAircraft.update({
|
||||
where: {
|
||||
id: connectedAircraftEntry.id,
|
||||
},
|
||||
@@ -74,19 +91,8 @@ export const handleConnectDispatch =
|
||||
logoutTime: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
socket.on("reconnect", async () => {
|
||||
console.log("Reconnected to dispatch server");
|
||||
await prisma.connectedDispatcher.update({
|
||||
where: {
|
||||
id: connectedAircraftEntry.id,
|
||||
},
|
||||
data: {
|
||||
lastHeartbeat: new Date().toISOString(),
|
||||
logoutTime: null,
|
||||
},
|
||||
});
|
||||
io.to("dispatchers").emit("pilots-update");
|
||||
io.to("pilots").emit("pilots-update");
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error connecting to dispatch server:", error);
|
||||
|
||||
Reference in New Issue
Block a user