Skip to content

Commit

Permalink
fix: ajuste para assegurar o envio conforme sessão do ticket, corrigi…
Browse files Browse the repository at this point in the history
…ndo conflito do contato.
  • Loading branch information
ldurans committed Dec 1, 2022
1 parent 23cfd08 commit 28d28d1
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 47 deletions.
27 changes: 5 additions & 22 deletions backend/src/controllers/TicketController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
// se ticket criado pelo próprio usuário, não emitir socket.
if (!userId) {
const io = getIO();
io.to(`${tenantId}-${ticket.status}`).emit(`${tenantId}-ticket`, {
io.to(`${tenantId}:${ticket.status}`).emit(`${tenantId}:ticket`, {
action: "create",
ticket
});
Expand Down Expand Up @@ -154,23 +154,6 @@ export const update = async (
userIdRequest
});

// const io = getIO();

// if (ticket.status !== oldStatus || ticket.user?.id !== oldUserId) {
// io.to(`${tenantId}-${oldStatus}`).emit(`${tenantId}-ticket`, {
// action: "delete",
// ticketId: ticket.id
// });
// }

// io.to(`${tenantId}-${ticket.status}`)
// .to(`${tenantId}-notification`)
// .to(`${tenantId}-${ticketId}`)
// .emit(`${tenantId}-ticket`, {
// action: "updateStatus",
// ticket
// });

return res.status(200).json(ticket);
};

Expand All @@ -185,10 +168,10 @@ export const remove = async (
const ticket = await DeleteTicketService({ id: ticketId, tenantId, userId });

const io = getIO();
io.to(`${tenantId}-${ticket.status}`)
.to(`${tenantId}-${ticketId}`)
.to(`${tenantId}-notification`)
.emit(`${tenantId}-ticket`, {
io.to(`${tenantId}:${ticket.status}`)
.to(`${tenantId}:${ticketId}`)
.to(`${tenantId}:notification`)
.emit(`${tenantId}:ticket`, {
action: "delete",
ticketId: +ticketId
});
Expand Down
2 changes: 1 addition & 1 deletion backend/src/helpers/GetDefaultWhatsApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const GetDefaultWhatsApp = async (
where: { tenantId, type: "whatsapp", status: "CONNECTED" }
});

if (!defaultWhatsapp) {
if (!defaultWhatsapp || !tenantId) {
throw new AppError("ERR_NO_DEF_WAPP_FOUND");
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/helpers/socketEmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const emitEvent = ({ tenantId, type, payload }: ObjEvent): void => {
eventChannel = `${tenantId}:contactList`;
}

io.emit(eventChannel, {
io.to(tenantId.toString()).emit(eventChannel, {
type,
payload
});
Expand Down
3 changes: 3 additions & 0 deletions backend/src/libs/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export const initIO = (httpServer: Server): SocketIO => {
message: "Client connected in tenant",
data: socket.handshake.query
});

socket.join(tenantId.toString());

socket.on(`${tenantId}:joinChatBox`, ticketId => {
console.info({
message: `Client joined a ticket channel ${tenantId}:${ticketId}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AppError from "../../errors/AppError";
import socketEmit from "../../helpers/socketEmit";
import Contact from "../../models/Contact";
import ContactTag from "../../models/ContactTag";

Expand Down Expand Up @@ -56,12 +55,6 @@ const UpdateContactService = async ({
throw new AppError("ERR_NO_CONTACT_FOUND", 404);
}

socketEmit({
tenantId,
type: "contact:update",
payload: contact
});

return contact;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AppError from "../../errors/AppError";
import socketEmit from "../../helpers/socketEmit";
import Contact from "../../models/Contact";
import ContactWallet from "../../models/ContactWallet";

Expand Down Expand Up @@ -56,12 +55,6 @@ const UpdateContactWalletsService = async ({
throw new AppError("ERR_NO_CONTACT_FOUND", 404);
}

socketEmit({
tenantId,
type: "contact:update",
payload: contact
});

return contact;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { sleepRandomTime } from "../../utils/sleepRandomTime";
// import SetTicketMessagesAsRead from "../../helpers/SetTicketMessagesAsRead";

interface Session extends IgApiClientMQTT {
id?: number;
id: number;
accountLogin?:
| AccountRepositoryLoginResponseLogged_in_user
| AccountRepositoryCurrentUserResponseUser;
Expand Down Expand Up @@ -54,7 +54,7 @@ const SendMessagesSystemWbot = async (
{
model: Ticket,
as: "ticket",
where: { tenantId, channel: "instagram" },
where: { tenantId, channel: "instagram", whatsappId: instaBot.id },
include: ["contact"]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const MessengerSendMessagesSystem = async (
{
model: Ticket,
as: "ticket",
where: { tenantId, channel: "messenger" },
where: { tenantId, channel: "messenger", whatsappId: messengerBot.id },
include: ["contact"]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { logger } from "../../utils/logger";
// import SetTicketMessagesAsRead from "../../helpers/SetTicketMessagesAsRead";

interface Session extends Telegraf {
id?: number;
id: number;
}

const SendMessagesSystemWbot = async (
Expand Down Expand Up @@ -41,7 +41,7 @@ const SendMessagesSystemWbot = async (
{
model: Ticket,
as: "ticket",
where: { tenantId, channel: "telegram" },
where: { tenantId, channel: "telegram", whatsappId: tbot.id },
include: ["contact"]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const FindOrCreateTicketService = async ({
[Op.or]: ["open", "pending"]
},
tenantId,
whatsappId,
contactId: groupContact ? groupContact.id : contact.id
},
include: [
Expand Down Expand Up @@ -93,7 +94,8 @@ const FindOrCreateTicketService = async ({
ticket = await Ticket.findOne({
where: {
contactId: groupContact.id,
tenantId
tenantId,
whatsappId
},
order: [["updatedAt", "DESC"]],
include: [
Expand Down Expand Up @@ -141,8 +143,8 @@ const FindOrCreateTicketService = async ({
status: {
[Op.in]: ["open", "pending"]
},
userId: "1212",
tenantId,
whatsappId,
contactId: contact.id
},
order: [["updatedAt", "DESC"]],
Expand Down
5 changes: 3 additions & 2 deletions backend/src/services/WbotServices/SendMessagesSystemWbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Contact from "../../models/Contact";
// import SetTicketMessagesAsRead from "../../helpers/SetTicketMessagesAsRead";

interface Session extends Client {
id?: number;
id: number;
}

const SendMessagesSystemWbot = async (
Expand Down Expand Up @@ -52,7 +52,8 @@ const SendMessagesSystemWbot = async (
where: {
tenantId,
status: { [Op.ne]: "closed" },
channel: "whatsapp"
channel: "whatsapp",
whatsappId: wbot.id
},
include: ["contact"]
},
Expand Down

0 comments on commit 28d28d1

Please sign in to comment.