All models using classes and sequelize migrations

This commit is contained in:
canove
2020-07-17 14:29:10 -03:00
parent a41f7e63ac
commit 9687a1ce13
22 changed files with 578 additions and 191 deletions

View File

@@ -1,67 +1,44 @@
const Contact = require("../models/Contact");
const Message = require("../models/Message");
const Sequelize = require("sequelize");
const { getIO } = require("../libs/socket");
const { getWbot } = require("../libs/wbot");
// const Message = require("../models/Message");
// const Sequelize = require("sequelize");
// const { getIO } = require("../libs/socket");
// const { getWbot } = require("../libs/wbot");
exports.index = async (req, res) => {
const { searchParam = "" } = req.query;
// const { searchParam = "" } = req.query;
const lowerSerachParam = searchParam.toLowerCase();
// const lowerSerachParam = searchParam.toLowerCase();
const whereCondition = {
name: Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("name")),
"LIKE",
"%" + lowerSerachParam + "%"
),
};
// const whereCondition = {
// name: Sequelize.where(
// Sequelize.fn("LOWER", Sequelize.col("name")),
// "LIKE",
// "%" + lowerSerachParam + "%"
// ),
// };
//todo >> add contact number to search where condition
const contacts = await Contact.findAll({
where: whereCondition,
attributes: {
include: [
[
Sequelize.literal(`(
SELECT COUNT(*)
FROM messages AS message
WHERE
message.contactId = contact.id
AND
message.read = 0
)`),
"unreadMessages",
],
],
},
order: [["updatedAt", "DESC"]],
});
const contacts = await Contact.findAll();
return res.json(contacts);
};
exports.store = async (req, res) => {
const wbot = getWbot();
const io = getIO();
const { number, name } = req.body;
// const wbot = getWbot();
// const io = getIO();
// const { number, name } = req.body;
const result = await wbot.isRegisteredUser(`55${number}@c.us`);
// const result = await wbot.isRegisteredUser(`55${number}@c.us`);
if (!result) {
return res
.status(400)
.json({ error: "The suplied number is not a valid Whatsapp number" });
}
const profilePicUrl = await wbot.getProfilePicUrl(`55${number}@c.us`);
// if (!result) {
// return res
// .status(400)
// .json({ error: "The suplied number is not a valid Whatsapp number" });
// }
// const profilePicUrl = await wbot.getProfilePicUrl(`55${number}@c.us`);
const contact = await Contact.create({
name,
number: `55${number}`,
profilePicUrl,
});
const { number, name } = await Contact.create(req.body);
res.status(200).json(contact);
res.status(200).json({ number, name });
};