Skip to content

Commit

Permalink
socket.ts: allow user to join group ticket
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Nov 14, 2024
1 parent 3c4b6d6 commit feb6d89
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions backend/src/libs/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,23 @@ import Ticket from "../models/Ticket";
import authConfig from "../config/auth";
import { CounterManager } from "./counter";
import UserSocketSession from "../models/UserSocketSession";
import { GetCompanySetting } from "../helpers/CheckSettings";

let io: SocketIO;

const joinTicketChannel = async (
socket,
ticketId: string,
user: User,
counters: CounterManager
) => {
const c = counters.incrementCounter(`ticket-${ticketId}`);
if (c === 1) {
socket.join(ticketId);
}
logger.debug(`joinChatbox[${c}]: Channel: ${ticketId} by user ${user.id}`);
};

export const initIO = (httpServer: Server): SocketIO => {
io = new SocketIO(httpServer, {
cors: {
Expand Down Expand Up @@ -142,19 +156,34 @@ export const initIO = (httpServer: Server): SocketIO => {
return;
}
Ticket.findByPk(ticketId).then(
ticket => {
async 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);
joinTicketChannel(socket, ticketId, user, counters);
} else if (
ticket.isGroup &&
(await GetCompanySetting(
user.companyId,
"groupsTab",
"disabled"
)) === "enabled"
) {
let queueFound = false;
user.queues.forEach(queue => {
if (queue.id === ticket.queueId) {
queueFound = true;
}
});
if (queueFound) {
joinTicketChannel(socket, ticketId, user, counters);
} else {
logger.info(
`Invalid attempt to join channel of ticket ${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}`
Expand Down

0 comments on commit feb6d89

Please sign in to comment.