mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 04:09:26 +00:00
improvement: better error handling
This commit is contained in:
@@ -43,23 +43,22 @@ exports.store = async (req, res) => {
|
||||
const io = getIO();
|
||||
const newContact = req.body;
|
||||
|
||||
let isValidNumber;
|
||||
|
||||
try {
|
||||
isValidNumber = await wbot.isRegisteredUser(`${newContact.number}@c.us`);
|
||||
const isValidNumber = await wbot.isRegisteredUser(
|
||||
`${newContact.number}@c.us`
|
||||
);
|
||||
if (!isValidNumber) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "The suplied number is not a valid Whatsapp number" });
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("Could not check whatsapp contact. Is session details valid?");
|
||||
console.log(err);
|
||||
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`
|
||||
);
|
||||
@@ -76,26 +75,22 @@ exports.store = async (req, res) => {
|
||||
contact: contact,
|
||||
});
|
||||
|
||||
res.status(200).json(contact);
|
||||
return res.status(200).json(contact);
|
||||
};
|
||||
|
||||
exports.show = async (req, res) => {
|
||||
const { contactId } = req.params;
|
||||
|
||||
const { id, name, number, email, extraInfo } = await Contact.findByPk(
|
||||
contactId,
|
||||
{
|
||||
include: "extraInfo",
|
||||
}
|
||||
);
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
name,
|
||||
number,
|
||||
email,
|
||||
extraInfo,
|
||||
const contact = await Contact.findByPk(contactId, {
|
||||
include: "extraInfo",
|
||||
attributes: ["id", "name", "number", "email"],
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
return res.status(404).json({ error: "No contact found with this id." });
|
||||
}
|
||||
|
||||
return res.status(200).json(contact);
|
||||
};
|
||||
|
||||
exports.update = async (req, res) => {
|
||||
@@ -110,7 +105,7 @@ exports.update = async (req, res) => {
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
return res.status(400).json({ error: "No contact found with this ID" });
|
||||
return res.status(404).json({ error: "No contact found with this ID" });
|
||||
}
|
||||
|
||||
if (updatedContact.extraInfo) {
|
||||
@@ -140,7 +135,7 @@ exports.update = async (req, res) => {
|
||||
contact: contact,
|
||||
});
|
||||
|
||||
res.status(200).json(contact);
|
||||
return res.status(200).json(contact);
|
||||
};
|
||||
|
||||
exports.delete = async (req, res) => {
|
||||
@@ -150,7 +145,7 @@ exports.delete = async (req, res) => {
|
||||
const contact = await Contact.findByPk(contactId);
|
||||
|
||||
if (!contact) {
|
||||
return res.status(400).json({ error: "No contact found with this ID" });
|
||||
return res.status(404).json({ error: "No contact found with this ID" });
|
||||
}
|
||||
|
||||
await contact.destroy();
|
||||
@@ -160,5 +155,5 @@ exports.delete = async (req, res) => {
|
||||
contactId: contactId,
|
||||
});
|
||||
|
||||
res.status(200).json({ message: "Contact deleted" });
|
||||
return res.status(200).json({ message: "Contact deleted" });
|
||||
};
|
||||
|
||||
@@ -6,7 +6,16 @@ exports.store = async (req, res, next) => {
|
||||
const io = getIO();
|
||||
const wbot = getWbot();
|
||||
|
||||
const phoneContacts = await wbot.getContacts();
|
||||
let phoneContacts;
|
||||
|
||||
try {
|
||||
phoneContacts = await wbot.getContacts();
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return res.status(500).json({
|
||||
error: "Could not check whatsapp contact. Check connection page.",
|
||||
});
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
phoneContacts.map(async ({ number, name }) => {
|
||||
|
||||
@@ -38,9 +38,6 @@ const setMessagesAsRead = async ticket => {
|
||||
};
|
||||
|
||||
exports.index = async (req, res, next) => {
|
||||
// const wbot = getWbot();
|
||||
// const io = getIO();
|
||||
|
||||
const { ticketId } = req.params;
|
||||
const { searchParam = "", pageNumber = 1 } = req.query;
|
||||
|
||||
@@ -125,6 +122,10 @@ exports.store = async (req, res, next) => {
|
||||
],
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(404).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
|
||||
try {
|
||||
if (media) {
|
||||
const newMedia = MessageMedia.fromFilePath(req.file.path);
|
||||
|
||||
@@ -8,7 +8,7 @@ exports.store = async (req, res, next) => {
|
||||
|
||||
const user = await User.findOne({ where: { email: email } });
|
||||
if (!user) {
|
||||
return res.status(401).json({ error: "No user found with this email" });
|
||||
return res.status(404).json({ error: "No user found with this email" });
|
||||
}
|
||||
|
||||
if (!(await user.checkPassword(password))) {
|
||||
|
||||
@@ -13,7 +13,7 @@ exports.update = async (req, res) => {
|
||||
const setting = await Setting.findByPk(settingKey);
|
||||
|
||||
if (!setting) {
|
||||
return res.status(400).json({ error: "No setting found with this ID" });
|
||||
return res.status(404).json({ error: "No setting found with this ID" });
|
||||
}
|
||||
|
||||
await setting.update(req.body);
|
||||
|
||||
@@ -105,7 +105,7 @@ exports.index = async (req, res) => {
|
||||
|
||||
const hasMore = count > offset + tickets.length;
|
||||
|
||||
return res.json({ count, tickets, hasMore });
|
||||
return res.status(200).json({ count, tickets, hasMore });
|
||||
};
|
||||
|
||||
exports.store = async (req, res) => {
|
||||
@@ -139,7 +139,7 @@ exports.update = async (req, res) => {
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(400).json({ error: "No ticket found with this ID" });
|
||||
return res.status(404).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
|
||||
await ticket.update(req.body);
|
||||
|
||||
@@ -71,7 +71,11 @@ exports.store = async (req, res, next) => {
|
||||
.json({ error: "Only administrators can create users." });
|
||||
}
|
||||
|
||||
await schema.validate(req.body);
|
||||
try {
|
||||
await schema.validate(req.body);
|
||||
} catch (err) {
|
||||
return res.status(400).json({ error: err.message });
|
||||
}
|
||||
|
||||
const io = getIO();
|
||||
|
||||
@@ -88,14 +92,15 @@ exports.store = async (req, res, next) => {
|
||||
exports.show = async (req, res) => {
|
||||
const { userId } = req.params;
|
||||
|
||||
const { id, name, email, profile } = await User.findByPk(userId);
|
||||
|
||||
return res.status(200).json({
|
||||
id,
|
||||
name,
|
||||
email,
|
||||
profile,
|
||||
const user = await User.findByPk(userId, {
|
||||
attributes: ["id", "name", "email", "profile"],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
res.status(400).json({ error: "No user found with this id." });
|
||||
}
|
||||
|
||||
return res.status(200).json(user);
|
||||
};
|
||||
|
||||
exports.update = async (req, res) => {
|
||||
@@ -121,7 +126,7 @@ exports.update = async (req, res) => {
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
res.status(400).json({ error: "No user found with this id." });
|
||||
res.status(404).json({ error: "No user found with this id." });
|
||||
}
|
||||
|
||||
if (user.profile === "admin" && req.body.profile === "user") {
|
||||
|
||||
@@ -21,7 +21,7 @@ exports.delete = async (req, res) => {
|
||||
const dbSession = await Whatsapp.findByPk(sessionId);
|
||||
|
||||
if (!dbSession) {
|
||||
return res.status(200).json({ message: "Session not found" });
|
||||
return res.status(404).json({ message: "Session not found" });
|
||||
}
|
||||
|
||||
await dbSession.update({ session: "", status: "pending" });
|
||||
|
||||
Reference in New Issue
Block a user