Skip to content

Commit

Permalink
backend/src/libs/socket.ts: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Jul 11, 2024
1 parent ccdf683 commit 0d62afe
Showing 1 changed file with 70 additions and 51 deletions.
121 changes: 70 additions & 51 deletions backend/src/libs/socket.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Server as SocketIO } from "socket.io";
import { instrument } from "@socket.io/admin-ui";
import { Server } from "http";
import { verify } from "jsonwebtoken";
import AppError from "../errors/AppError";
import { logger } from "../utils/logger";
import User from "../models/User";
import Queue from "../models/Queue";
import Ticket from "../models/Ticket";
import { verify } from "jsonwebtoken";
import authConfig from "../config/auth";
import { CounterManager } from "./counter";

Expand All @@ -20,20 +20,18 @@ export const initIO = (httpServer: Server): SocketIO => {
});

if (process.env.SOCKET_ADMIN && JSON.parse(process.env.SOCKET_ADMIN)) {
User.findByPk(1).then(
(adminUser) => {
instrument(io, {
auth: {
type: "basic",
username: adminUser.email,
password: adminUser.passwordHash
},
mode: "development",
});
}
);
}

User.findByPk(1).then(adminUser => {
instrument(io, {
auth: {
type: "basic",
username: adminUser.email,
password: adminUser.passwordHash
},
mode: "development"
});
});
}

io.on("connection", async socket => {
logger.info("Client Connected");
const { token } = socket.handshake.query;
Expand Down Expand Up @@ -69,7 +67,7 @@ export const initIO = (httpServer: Server): SocketIO => {

socket.join(`company-${user.companyId}-mainchannel`);
socket.join(`user-${user.id}`);

if (user.super) {
socket.join("super");
}
Expand All @@ -79,24 +77,31 @@ export const initIO = (httpServer: Server): SocketIO => {
return;
}
Ticket.findByPk(ticketId).then(
(ticket) => {
if (ticket && ticket.companyId === user.companyId
&& (ticket.userId === user.id || user.profile === "admin")) {
ticket => {
if (
ticket &&
ticket.companyId === user.companyId &&
(ticket.userId === user.id || user.profile === "admin")
) {
const c = counters.incrementCounter(`ticket-${ticketId}`);
if (c === 1) {
socket.join(ticketId);
}
logger.debug(`joinChatbox[${c}]: Channel: ${ticketId} by user ${user.id}`)
logger.debug(
`joinChatbox[${c}]: Channel: ${ticketId} by user ${user.id}`
);
} else {
logger.info(`Invalid attempt to join channel of ticket ${ticketId} by user ${user.id}`)
logger.info(
`Invalid attempt to join channel of ticket ${ticketId} by user ${user.id}`
);
}
},
(error) => {
error => {
logger.error(error, `Error fetching ticket ${ticketId}`);
}
);
});

socket.on("leaveChatBox", async (ticketId: string) => {
if (!ticketId || ticketId === "undefined") {
return;
Expand All @@ -108,69 +113,83 @@ export const initIO = (httpServer: Server): SocketIO => {
socket.leave(ticketId);
}

logger.debug(`leaveChatbox[${c}]: Channel: ${ticketId} by user ${user.id}`)
logger.debug(
`leaveChatbox[${c}]: Channel: ${ticketId} by user ${user.id}`
);
});

socket.on("joinNotification", async () => {
socket.on("joinNotification", async () => {
const c = counters.incrementCounter("notification");
if (c === 1) {
if (user.profile === "admin") {
socket.join(`company-${user.companyId}-notification`);
} else {
user.queues.forEach((queue) => {
logger.debug(`User ${user.id} of company ${user.companyId} joined queue ${queue.id} channel.`);
socket.join(`queue-${queue.id}-notification`);
});
}
}
if (user.profile === "admin") {
socket.join(`company-${user.companyId}-notification`);
} else {
user.queues.forEach(queue => {
logger.debug(
`User ${user.id} of company ${user.companyId} joined queue ${queue.id} channel.`
);
socket.join(`queue-${queue.id}-notification`);
});
}
}
logger.debug(`joinNotification[${c}]: User: ${user.id}`);
});
socket.on("leaveNotification", async () => {
});

socket.on("leaveNotification", async () => {
const c = counters.decrementCounter("notification");
if (c === 0) {
if (user.profile === "admin") {
socket.leave(`company-${user.companyId}-notification`);
} else {
user.queues.forEach((queue) => {
logger.debug(`User ${user.id} of company ${user.companyId} leaved queue ${queue.id} channel.`);
user.queues.forEach(queue => {
logger.debug(
`User ${user.id} of company ${user.companyId} leaved queue ${queue.id} channel.`
);
socket.leave(`queue-${queue.id}-notification`);
});
}
}
logger.debug(`leaveNotification[${c}]: User: ${user.id}`);
});
socket.on("joinTickets", (status: string) => {

socket.on("joinTickets", (status: string) => {
if (counters.incrementCounter(`status-${status}`) === 1) {
if (user.profile === "admin") {
logger.debug(`Admin ${user.id} of company ${user.companyId} joined ${status} tickets channel.`);
logger.debug(
`Admin ${user.id} of company ${user.companyId} joined ${status} tickets channel.`
);
socket.join(`company-${user.companyId}-${status}`);
} else if (status === "pending") {
user.queues.forEach((queue) => {
logger.debug(`User ${user.id} of company ${user.companyId} joined queue ${queue.id} pending tickets channel.`);
user.queues.forEach(queue => {
logger.debug(
`User ${user.id} of company ${user.companyId} joined queue ${queue.id} pending tickets channel.`
);
socket.join(`queue-${queue.id}-pending`);
});
} else {
logger.debug(`User ${user.id} cannot subscribe to ${status}`);
}
}
});
}
});

socket.on("leaveTickets", (status: string) => {
if (counters.decrementCounter(`status-${status}`) === 0) {
if (user.profile === "admin") {
logger.debug(`Admin ${user.id} of company ${user.companyId} leaved ${status} tickets channel.`);
logger.debug(
`Admin ${user.id} of company ${user.companyId} leaved ${status} tickets channel.`
);
socket.leave(`company-${user.companyId}-${status}`);
} else if (status === "pending") {
user.queues.forEach((queue) => {
logger.debug(`User ${user.id} of company ${user.companyId} leaved queue ${queue.id} pending tickets channel.`);
user.queues.forEach(queue => {
logger.debug(
`User ${user.id} of company ${user.companyId} leaved queue ${queue.id} pending tickets channel.`
);
socket.leave(`queue-${queue.id}-pending`);
});
}
}
});

socket.emit("ready");
return io;
});
Expand Down

0 comments on commit 0d62afe

Please sign in to comment.