mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-17 19:37:02 +00:00
improvement: better error messages in frontend
This commit is contained in:
@@ -41,13 +41,23 @@ exports.store = async (req, res) => {
|
||||
const io = getIO();
|
||||
const newContact = req.body;
|
||||
|
||||
const result = await wbot.isRegisteredUser(`${newContact.number}@c.us`);
|
||||
let isValidNumber;
|
||||
|
||||
if (!result) {
|
||||
try {
|
||||
isValidNumber = await wbot.isRegisteredUser(`${newContact.number}@c.us`);
|
||||
} catch (err) {
|
||||
console.log("Could not check whatsapp contact. Is session details valid?");
|
||||
return res.status(500).json({
|
||||
error: "Could not check whatsapp contact. Check connection page.",
|
||||
});
|
||||
}
|
||||
|
||||
if (!isValidNumber) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "The suplied number is not a valid Whatsapp number" });
|
||||
}
|
||||
|
||||
const profilePicUrl = await wbot.getProfilePicUrl(
|
||||
`${newContact.number}@c.us`
|
||||
);
|
||||
|
||||
@@ -125,51 +125,62 @@ exports.store = async (req, res, next) => {
|
||||
],
|
||||
});
|
||||
|
||||
if (media) {
|
||||
const newMedia = MessageMedia.fromFilePath(req.file.path);
|
||||
try {
|
||||
if (media) {
|
||||
const newMedia = MessageMedia.fromFilePath(req.file.path);
|
||||
|
||||
message.mediaUrl = req.file.filename;
|
||||
if (newMedia.mimetype) {
|
||||
message.mediaType = newMedia.mimetype.split("/")[0];
|
||||
message.mediaUrl = req.file.filename;
|
||||
if (newMedia.mimetype) {
|
||||
message.mediaType = newMedia.mimetype.split("/")[0];
|
||||
} else {
|
||||
message.mediaType = "other";
|
||||
}
|
||||
|
||||
sentMessage = await wbot.sendMessage(
|
||||
`${ticket.contact.number}@c.us`,
|
||||
newMedia
|
||||
);
|
||||
|
||||
await ticket.update({ lastMessage: message.mediaUrl });
|
||||
} else {
|
||||
message.mediaType = "other";
|
||||
sentMessage = await wbot.sendMessage(
|
||||
`${ticket.contact.number}@c.us`,
|
||||
message.body
|
||||
);
|
||||
await ticket.update({ lastMessage: message.body });
|
||||
}
|
||||
|
||||
sentMessage = await wbot.sendMessage(
|
||||
`${ticket.contact.number}@c.us`,
|
||||
newMedia
|
||||
} catch (err) {
|
||||
console.log(
|
||||
"Could not create whatsapp message. Is session details valid? "
|
||||
);
|
||||
|
||||
await ticket.update({ lastMessage: message.mediaUrl });
|
||||
} else {
|
||||
sentMessage = await wbot.sendMessage(
|
||||
`${ticket.contact.number}@c.us`,
|
||||
message.body
|
||||
);
|
||||
await ticket.update({ lastMessage: message.body });
|
||||
}
|
||||
|
||||
message.id = sentMessage.id.id;
|
||||
if (sentMessage) {
|
||||
message.id = sentMessage.id.id;
|
||||
const newMessage = await ticket.createMessage(message);
|
||||
|
||||
const newMessage = await ticket.createMessage(message);
|
||||
const serialziedMessage = {
|
||||
...newMessage.dataValues,
|
||||
mediaUrl: `${
|
||||
message.mediaUrl
|
||||
? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${message.mediaUrl}`
|
||||
: ""
|
||||
}`,
|
||||
};
|
||||
|
||||
const serialziedMessage = {
|
||||
...newMessage.dataValues,
|
||||
mediaUrl: `${
|
||||
message.mediaUrl
|
||||
? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${message.mediaUrl}`
|
||||
: ""
|
||||
}`,
|
||||
};
|
||||
io.to(ticketId).to("notification").emit("appMessage", {
|
||||
action: "create",
|
||||
message: serialziedMessage,
|
||||
ticket: ticket,
|
||||
contact: ticket.contact,
|
||||
});
|
||||
|
||||
io.to(ticketId).to("notification").emit("appMessage", {
|
||||
action: "create",
|
||||
message: serialziedMessage,
|
||||
ticket: ticket,
|
||||
contact: ticket.contact,
|
||||
});
|
||||
await setMessagesAsRead(ticket);
|
||||
|
||||
await setMessagesAsRead(ticket);
|
||||
return res.json({ newMessage, ticket });
|
||||
}
|
||||
|
||||
return res.json({ newMessage, ticket });
|
||||
return res
|
||||
.status(500)
|
||||
.json({ error: "Cannot sent whatsapp message. Check connection page." });
|
||||
};
|
||||
|
||||
@@ -86,7 +86,8 @@ const ContactModal = ({ open, onClose, contactId }) => {
|
||||
await api.post("/contacts", values);
|
||||
}
|
||||
} catch (err) {
|
||||
alert(err);
|
||||
alert(err.response.data.error);
|
||||
console.log(err);
|
||||
}
|
||||
handleClose();
|
||||
};
|
||||
|
||||
@@ -163,8 +163,8 @@ const MessageInput = () => {
|
||||
try {
|
||||
await api.post(`/messages/${ticketId}`, formData);
|
||||
} catch (err) {
|
||||
alert(err.response.data.error);
|
||||
console.log(err);
|
||||
alert(err);
|
||||
}
|
||||
setLoading(false);
|
||||
setMedia(mediaInitialState);
|
||||
@@ -182,7 +182,8 @@ const MessageInput = () => {
|
||||
try {
|
||||
await api.post(`/messages/${ticketId}`, message);
|
||||
} catch (err) {
|
||||
alert(err);
|
||||
alert(err.response.data.error);
|
||||
console.log(err);
|
||||
}
|
||||
setInputMessage("");
|
||||
setShowEmoji(false);
|
||||
@@ -217,20 +218,19 @@ const MessageInput = () => {
|
||||
}
|
||||
const formData = new FormData();
|
||||
const filename = `${new Date().getTime()}.mp3`;
|
||||
console.log(blob);
|
||||
formData.append("media", blob, filename);
|
||||
formData.append("body", filename);
|
||||
formData.append("fromMe", true);
|
||||
try {
|
||||
await api.post(`/messages/${ticketId}`, formData);
|
||||
} catch (err) {
|
||||
alert(err.response.data.error);
|
||||
console.log(err);
|
||||
alert(err);
|
||||
}
|
||||
setRecording(false);
|
||||
setLoading(false);
|
||||
})
|
||||
.catch(e => console.log(e));
|
||||
.catch(err => console.log(err));
|
||||
};
|
||||
|
||||
const handleCancelAudio = () => {
|
||||
|
||||
@@ -32,11 +32,7 @@ const RouteWrapper = ({ component: Component, isPrivate = false, ...rest }) => {
|
||||
}
|
||||
|
||||
if (isAuth && !isPrivate) {
|
||||
return (
|
||||
<Redirect
|
||||
to={{ pathname: "/dashboard", state: { from: rest.location } }}
|
||||
/>
|
||||
);
|
||||
return <Redirect to={{ pathname: "/", state: { from: rest.location } }} />;
|
||||
}
|
||||
|
||||
return <Route {...rest} component={Component} />;
|
||||
|
||||
Reference in New Issue
Block a user