mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 04:39:20 +00:00
improvement: moved message creation to service
This commit is contained in:
@@ -4,28 +4,37 @@ import ShowTicketService from "../TicketServices/ShowTicketService";
|
|||||||
|
|
||||||
interface MessageData {
|
interface MessageData {
|
||||||
id: string;
|
id: string;
|
||||||
|
ticketId: number;
|
||||||
body: string;
|
body: string;
|
||||||
fromMe: boolean;
|
contactId?: number;
|
||||||
read: boolean;
|
vcardContactId?: number;
|
||||||
mediaType: string;
|
fromMe?: boolean;
|
||||||
|
read?: boolean;
|
||||||
|
mediaType?: string;
|
||||||
mediaUrl?: string;
|
mediaUrl?: string;
|
||||||
}
|
}
|
||||||
interface Request {
|
interface Request {
|
||||||
ticketId: string | number;
|
|
||||||
messageData: MessageData;
|
messageData: MessageData;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CreateMessageService = async ({
|
const CreateMessageService = async ({
|
||||||
messageData,
|
messageData
|
||||||
ticketId
|
|
||||||
}: Request): Promise<Message> => {
|
}: Request): Promise<Message> => {
|
||||||
const ticket = await ShowTicketService(ticketId);
|
const ticket = await ShowTicketService(messageData.ticketId);
|
||||||
|
|
||||||
if (!ticket) {
|
if (!ticket) {
|
||||||
throw new AppError("No ticket found with this ID", 404);
|
throw new AppError("No ticket found with this ID", 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
const message: Message = await ticket.$create("message", messageData);
|
await Message.upsert(messageData);
|
||||||
|
|
||||||
|
const message = await Message.findByPk(messageData.id, {
|
||||||
|
include: ["contact", "vcardContact"]
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!message) {
|
||||||
|
throw new AppError("Error while creating message on database.", 501);
|
||||||
|
}
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import { getWbot } from "../../libs/wbot";
|
|||||||
import AppError from "../../errors/AppError";
|
import AppError from "../../errors/AppError";
|
||||||
import ShowTicketService from "../TicketServices/ShowTicketService";
|
import ShowTicketService from "../TicketServices/ShowTicketService";
|
||||||
import ParseVcardToJson from "../../helpers/ParseVcardToJson";
|
import ParseVcardToJson from "../../helpers/ParseVcardToJson";
|
||||||
|
import CreateMessageService from "../MessageServices/CreateMessageService";
|
||||||
|
|
||||||
const writeFileAsync = promisify(writeFile);
|
const writeFileAsync = promisify(writeFile);
|
||||||
|
|
||||||
@@ -160,15 +161,19 @@ const handlMedia = async (
|
|||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
const newMessage: Message = await ticket.$create("message", {
|
const messageData = {
|
||||||
id: msg.id.id,
|
id: msg.id.id,
|
||||||
contactId: msg.fromMe ? null : contact.id,
|
ticketId: ticket.id,
|
||||||
|
contactId: msg.fromMe ? undefined : contact.id,
|
||||||
body: msg.body || media.filename,
|
body: msg.body || media.filename,
|
||||||
fromMe: msg.fromMe,
|
fromMe: msg.fromMe,
|
||||||
read: msg.fromMe,
|
read: msg.fromMe,
|
||||||
mediaUrl: media.filename,
|
mediaUrl: media.filename,
|
||||||
mediaType: media.mimetype.split("/")[0]
|
mediaType: media.mimetype.split("/")[0]
|
||||||
});
|
};
|
||||||
|
|
||||||
|
const newMessage = await CreateMessageService({ messageData });
|
||||||
|
|
||||||
await ticket.update({ lastMessage: msg.body || media.filename });
|
await ticket.update({ lastMessage: msg.body || media.filename });
|
||||||
return newMessage;
|
return newMessage;
|
||||||
};
|
};
|
||||||
@@ -184,20 +189,19 @@ const handleMessage = async (
|
|||||||
if (msg.hasMedia) {
|
if (msg.hasMedia) {
|
||||||
newMessage = await handlMedia(msg, ticket, contact);
|
newMessage = await handlMedia(msg, ticket, contact);
|
||||||
} else {
|
} else {
|
||||||
const { id } = await ticket.$create("message", {
|
const messageData = {
|
||||||
id: msg.id.id,
|
id: msg.id.id,
|
||||||
contactId: msg.fromMe ? null : contact.id,
|
ticketId: ticket.id,
|
||||||
vcardContactId: vcardContact ? vcardContact.id : null,
|
contactId: msg.fromMe ? undefined : contact.id,
|
||||||
|
vcardContactId: vcardContact ? vcardContact.id : undefined,
|
||||||
body: msg.body,
|
body: msg.body,
|
||||||
fromMe: msg.fromMe,
|
fromMe: msg.fromMe,
|
||||||
mediaType: msg.type,
|
mediaType: msg.type,
|
||||||
read: msg.fromMe
|
read: msg.fromMe
|
||||||
});
|
};
|
||||||
await ticket.update({ lastMessage: msg.body });
|
|
||||||
|
|
||||||
newMessage = await Message.findByPk(id, {
|
newMessage = await CreateMessageService({ messageData });
|
||||||
include: ["contact", "vcardContact"]
|
await ticket.update({ lastMessage: msg.body });
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
@@ -282,25 +286,25 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// wbot.on("media_uploaded", async msg => {
|
wbot.on("media_uploaded", async msg => {
|
||||||
// try {
|
try {
|
||||||
// let groupContact: Contact | undefined;
|
let groupContact: Contact | undefined;
|
||||||
// const msgContact = await wbot.getContactById(msg.to);
|
const msgContact = await wbot.getContactById(msg.to);
|
||||||
// if (msg.author) {
|
if (msg.author) {
|
||||||
// const msgGroupContact = await wbot.getContactById(msg.from);
|
const msgGroupContact = await wbot.getContactById(msg.from);
|
||||||
// groupContact = await verifyGroup(msgGroupContact);
|
groupContact = await verifyGroup(msgGroupContact);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// const profilePicUrl = await msgContact.getProfilePicUrl();
|
const profilePicUrl = await msgContact.getProfilePicUrl();
|
||||||
// const contact = await verifyContact(msgContact, profilePicUrl);
|
const contact = await verifyContact(msgContact, profilePicUrl);
|
||||||
// const ticket = await verifyTicket(contact, whatsappId, groupContact);
|
const ticket = await verifyTicket(contact, whatsappId, groupContact);
|
||||||
|
|
||||||
// await handleMessage(msg, ticket, contact);
|
await handleMessage(msg, ticket, contact);
|
||||||
// } catch (err) {
|
} catch (err) {
|
||||||
// Sentry.captureException(err);
|
Sentry.captureException(err);
|
||||||
// console.log(err);
|
console.log(err);
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
|
|
||||||
wbot.on("message_ack", async (msg, ack) => {
|
wbot.on("message_ack", async (msg, ack) => {
|
||||||
await new Promise(r => setTimeout(r, 500));
|
await new Promise(r => setTimeout(r, 500));
|
||||||
|
|||||||
Reference in New Issue
Block a user