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