Feat: Added import contacts function on frontend

This commit is contained in:
canove
2020-07-28 10:45:35 -03:00
parent 915ee712dc
commit 124297a849
11 changed files with 128 additions and 141 deletions

View File

@@ -1,67 +0,0 @@
const Contact = require("../models/Contact");
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 lowerSerachParam = searchParam.toLowerCase();
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"]],
});
return res.json(contacts);
};
exports.store = async (req, res) => {
const wbot = getWbot();
const io = getIO();
const { number, name } = req.body;
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`);
const contact = await Contact.create({
name,
number: `55${number}`,
profilePicUrl,
});
res.status(200).json(contact);
};

View File

@@ -1,29 +1,31 @@
const Sequelize = require("sequelize");
const { Op } = require("sequelize");
const Contact = require("../models/Contact");
const ContactCustomField = require("../models/ContactCustomField");
// const Message = require("../models/Message");
// const Sequelize = require("sequelize");
const { getIO } = require("../libs/socket");
// const { getWbot } = require("../libs/wbot");
const { getWbot } = require("../libs/wbot");
exports.index = async (req, res) => {
const { searchParam = "", pageNumber = 1, rowsPerPage = 10 } = req.query;
const whereCondition = {
name: Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("name")),
"LIKE",
"%" + searchParam.toLowerCase() + "%"
),
[Op.or]: [
{
name: Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("name")),
"LIKE",
"%" + searchParam.toLowerCase() + "%"
),
},
{ number: { [Op.like]: `%${searchParam}%` } },
],
};
let limit = +rowsPerPage;
let offset = limit * (pageNumber - 1);
//todo >> add contact number to search where condition
const { count, rows: contacts } = await Contact.findAndCountAll({
where: whereCondition,
limit,
@@ -35,22 +37,27 @@ exports.index = async (req, res) => {
};
exports.store = async (req, res) => {
// const wbot = getWbot();
const wbot = getWbot();
const io = getIO();
const newContact = req.body;
// const result = await wbot.isRegisteredUser(`55${number}@c.us`);
const result = await wbot.isRegisteredUser(`${newContact.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(
`${newContact.number}@c.us`
);
const contact = await Contact.create(newContact, {
include: "extraInfo",
});
const contact = await Contact.create(
{ ...newContact, profilePicUrl },
{
include: "extraInfo",
}
);
io.emit("contact", {
action: "create",

View File

@@ -0,0 +1,18 @@
const Contact = require("../models/Contact");
const { getIO } = require("../libs/socket");
const { getWbot, init } = require("../libs/wbot");
exports.store = async (req, res, next) => {
const io = getIO();
const wbot = getWbot();
const phoneContacts = await wbot.getContacts();
await Promise.all(
phoneContacts.map(async ({ number, name }) => {
await Contact.create({ number, name });
})
);
return res.status(200).json({ message: "contacts imported" });
};

View File

@@ -5,7 +5,7 @@ class Contact extends Sequelize.Model {
super.init(
{
name: { type: Sequelize.STRING },
number: { type: Sequelize.STRING },
number: { type: Sequelize.STRING, allowNull: false, unique: true },
email: { type: Sequelize.STRING, allowNull: false, defaultValue: "" },
profilePicUrl: { type: Sequelize.STRING },
},

View File

@@ -2,9 +2,12 @@ const express = require("express");
const isAuth = require("../middleware/is-auth");
const ContactController = require("../controllers/ContactController");
const ImportPhoneContactsController = require("../controllers/ImportPhoneContactsController");
const routes = express.Router();
routes.post("/contacts/import", isAuth, ImportPhoneContactsController.store);
routes.get("/contacts", isAuth, ContactController.index);
routes.get("/contacts/:contactId", isAuth, ContactController.show);

View File

@@ -28,7 +28,7 @@ const verifyContact = async (msgContact, profilePicUrl) => {
};
const verifyTicket = async contact => {
const [ticket, created] = await Ticket.findOrCreate({
const [ticket] = await Ticket.findOrCreate({
where: {
status: {
[Op.or]: ["open", "pending"],