refactor(biome): more format changes
This commit is contained in:
0
app/src/ws/channels/labelLogs.ts
Normal file
0
app/src/ws/channels/labelLogs.ts
Normal file
43
app/src/ws/channels/logs.ts
Normal file
43
app/src/ws/channels/logs.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { Namespace, Socket } from "socket.io";
|
||||
import { requireAuth } from "../../pkg/middleware/authMiddleware.js";
|
||||
|
||||
export const setupAllLogs = (io: Namespace) => {
|
||||
// Wrap middleware for Socket.IO
|
||||
io.use(async (socket: Socket, next) => {
|
||||
try {
|
||||
// Mock Express req/res object
|
||||
const req = socket.request as any;
|
||||
|
||||
// Create a mini next function that throws an error if unauthorized
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const fakeRes: any = {
|
||||
status: () => ({
|
||||
json: (obj: any) => reject(new Error(obj.error)),
|
||||
}),
|
||||
};
|
||||
const nextFn = (err?: any) => (err ? reject(err) : resolve());
|
||||
|
||||
// Call your middleware
|
||||
requireAuth("", ["systemAdmin", "admin"])(req, fakeRes, nextFn);
|
||||
});
|
||||
|
||||
// Auth passed
|
||||
next();
|
||||
} catch (err: any) {
|
||||
next(new Error(err.message || "Unauthorized"));
|
||||
}
|
||||
});
|
||||
|
||||
io.on("connection", (socket: Socket) => {
|
||||
console.log(
|
||||
"✅ Authenticated client connected to channel2:",
|
||||
socket.id
|
||||
);
|
||||
|
||||
socket.on("ping", () => socket.emit("pong"));
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
console.log("🔴 Client disconnected from channel2:", socket.id);
|
||||
});
|
||||
});
|
||||
};
|
||||
59
app/src/ws/channels/scheduler.ts
Normal file
59
app/src/ws/channels/scheduler.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { formatDate } from "date-fns";
|
||||
import type { Server, Socket } from "socket.io";
|
||||
import { db } from "../../pkg/db/db.js";
|
||||
import { orderScheduler } from "../../pkg/db/schema/orderScheduler.js";
|
||||
import { tryCatch } from "../../pkg/utils/tryCatch.js";
|
||||
|
||||
let ioInstance: Server | null = null;
|
||||
|
||||
export const setupScheduler = (io: Server, socket: Socket) => {
|
||||
ioInstance = io;
|
||||
|
||||
socket.on("joinScheduler", async () => {
|
||||
socket.join("scheduler");
|
||||
//console.log(`Socket ${socket.id} joined room "scheduler"`);
|
||||
|
||||
const { data, error } = await tryCatch(db.select().from(orderScheduler));
|
||||
|
||||
if (error) {
|
||||
setTimeout(() => {
|
||||
io.to("scheduler").emit("scheduler:update", {
|
||||
type: "init",
|
||||
error: error,
|
||||
});
|
||||
}, 1000);
|
||||
return;
|
||||
}
|
||||
|
||||
// initial push
|
||||
setTimeout(() => {
|
||||
io.to("scheduler").emit("scheduler:update", { type: "init", data: data });
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
// socket.on("ping", () => {
|
||||
// io.to("scheduler").emit("pong", { from: socket.id });
|
||||
// });
|
||||
|
||||
// setInterval(() => {
|
||||
// io.to("scheduler").emit("heartbeat", {
|
||||
// ts: formatDate(Date.now(), "M/d/yyyy HH:mm"),
|
||||
// });
|
||||
// }, 60_000);
|
||||
|
||||
// add in listen for changes to the db with pg listen?
|
||||
};
|
||||
|
||||
// --- Change Broadcast Function ---
|
||||
export const schedulerChange = async (data: any) => {
|
||||
if (!ioInstance) {
|
||||
console.error("⚠️ schedulerChange called before setupScheduler initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
ioInstance.to("scheduler").emit("scheduler:update", {
|
||||
ts: formatDate(Date.now(), "M/d/yyyy HH:mm"),
|
||||
data,
|
||||
});
|
||||
console.log("📢 Scheduler update broadcasted:", data);
|
||||
};
|
||||
Reference in New Issue
Block a user