improvement: better error handling

This commit is contained in:
canove
2020-09-04 17:09:39 -03:00
parent 3cb3fc1a20
commit f7fe3286b8
23 changed files with 166 additions and 532 deletions

View File

@@ -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" });
};

View File

@@ -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 }) => {

View File

@@ -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);

View File

@@ -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))) {

View File

@@ -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);

View File

@@ -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);

View File

@@ -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") {

View File

@@ -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" });

View File

@@ -1,48 +0,0 @@
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert(
"Contacts",
[
{
name: "Joana Doe",
profilePicUrl:
"https://images.unsplash.com/photo-1494790108377-be9c29b29330?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1834&q=80",
number: 5512345678,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "John Rulles",
profilePicUrl:
"https://images.unsplash.com/photo-1500648767791-00dcc994a43e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80",
number: 5512345679,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "Jonas Jhones",
profilePicUrl:
"https://images.unsplash.com/photo-1531427186611-ecfd6d936c79?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80",
number: 5512345680,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "Julie June",
profilePicUrl:
"https://images.unsplash.com/photo-1493666438817-866a91353ca9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1049&q=80",
number: 5512345681,
createdAt: new Date(),
updatedAt: new Date(),
},
],
{}
);
},
down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete("Contacts", null, {});
},
};

View File

@@ -1,261 +0,0 @@
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert(
"Tickets",
[
{
status: "pending",
lastMessage: "hello!",
contactId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 3,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 3,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 3,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 3,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 3,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 3,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 3,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 4,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 4,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 4,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 4,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 4,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 4,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 4,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 4,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
status: "pending",
lastMessage: "hello!",
contactId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
],
{}
);
},
down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete("Tickets", null, {});
},
};

View File

@@ -1,148 +0,0 @@
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert(
"Messages",
[
{
id: "12312321342",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 0,
ticketId: 1,
fromMe: false,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321313",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 3,
ticketId: 1,
fromMe: true,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321314",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 3,
ticketId: 1,
fromMe: true,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321315",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 0,
ticketId: 1,
fromMe: false,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321316",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 0,
ticketId: 5,
fromMe: false,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321355",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 3,
ticketId: 5,
fromMe: true,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321318",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 3,
ticketId: 5,
fromMe: true,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321319",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 0,
ticketId: 5,
fromMe: false,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321399",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 0,
ticketId: 11,
fromMe: false,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321391",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 3,
ticketId: 11,
fromMe: true,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321392",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 3,
ticketId: 11,
fromMe: true,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "12312321393",
body:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
ack: 0,
ticketId: 11,
fromMe: false,
read: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
],
{}
);
},
down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete("Messages", null, {});
},
};