fix: apps throws an error when importing contacts from phone

This commit is contained in:
canove
2020-10-17 11:16:17 -03:00
parent 55fbadc86e
commit f3ecb5dbd0
6 changed files with 21 additions and 8 deletions

View File

@@ -35,7 +35,7 @@ const ListContactsService = async ({
where: whereCondition, where: whereCondition,
limit, limit,
offset, offset,
order: [["createdAt", "DESC"]] order: [["name", "ASC"]]
}); });
const hasMore = count > offset + contacts.length; const hasMore = count > offset + contacts.length;

View File

@@ -1,4 +1,3 @@
import AppError from "../../errors/AppError";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp"; import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import { getWbot } from "../../libs/wbot"; import { getWbot } from "../../libs/wbot";
import Contact from "../../models/Contact"; import Contact from "../../models/Contact";
@@ -22,7 +21,20 @@ const ImportContactsService = async (): Promise<void> => {
if (phoneContacts) { if (phoneContacts) {
await Promise.all( await Promise.all(
phoneContacts.map(async ({ number, name }) => { phoneContacts.map(async ({ number, name }) => {
await Contact.create({ number, name }); if (!number) {
return null;
}
if (!name) {
name = number;
}
const numberExists = await Contact.findOne({
where: { number }
});
if (numberExists) return null;
return Contact.create({ number, name });
}) })
); );
} }

View File

@@ -1,6 +1,7 @@
import React, { useState, useEffect, useReducer } from "react"; import React, { useState, useEffect, useReducer } from "react";
import openSocket from "socket.io-client"; import openSocket from "socket.io-client";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
import { useHistory } from "react-router-dom";
import { makeStyles } from "@material-ui/core/styles"; import { makeStyles } from "@material-ui/core/styles";
import Table from "@material-ui/core/Table"; import Table from "@material-ui/core/Table";
@@ -86,6 +87,7 @@ const useStyles = makeStyles(theme => ({
const Contacts = () => { const Contacts = () => {
const classes = useStyles(); const classes = useStyles();
const history = useHistory();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [pageNumber, setPageNumber] = useState(1); const [pageNumber, setPageNumber] = useState(1);
@@ -191,6 +193,7 @@ const Contacts = () => {
const handleimportContact = async () => { const handleimportContact = async () => {
try { try {
await api.post("/contacts/import"); await api.post("/contacts/import");
history.go(0);
} catch (err) { } catch (err) {
const errorMsg = err.response?.data?.error; const errorMsg = err.response?.data?.error;
if (errorMsg) { if (errorMsg) {

View File

@@ -90,8 +90,7 @@ const messages = {
importTitlte: "Import contacts", importTitlte: "Import contacts",
deleteMessage: deleteMessage:
"Are you sure you want to delete this contact? All related tickets will be lost.", "Are you sure you want to delete this contact? All related tickets will be lost.",
importMessage: importMessage: "Do you want to import all contacts from the phone?",
"Do you want to import all contacts from the phone? This function is experimental, you will have to reload the page after importing.",
}, },
buttons: { buttons: {
import: "Import Contacts", import: "Import Contacts",

View File

@@ -93,7 +93,7 @@ const messages = {
deleteMessage: deleteMessage:
"¿Estás seguro que deseas borrar este contacto? Todos los tickets relacionados se perderán.", "¿Estás seguro que deseas borrar este contacto? Todos los tickets relacionados se perderán.",
importMessage: importMessage:
"¿Quieres importar todos los contactos desde tu teléfono? Esta función es experimental, tendrás que recargar la página después de importar.", "¿Quieres importar todos los contactos desde tu teléfono?",
}, },
buttons: { buttons: {
import: "Importar Contactos", import: "Importar Contactos",

View File

@@ -90,8 +90,7 @@ const messages = {
importTitlte: "Importar contatos", importTitlte: "Importar contatos",
deleteMessage: deleteMessage:
"Tem certeza que deseja deletar este contato? Todos os tickets relacionados serão perdidos.", "Tem certeza que deseja deletar este contato? Todos os tickets relacionados serão perdidos.",
importMessage: importMessage: "Deseja importas todos os contatos do telefone?",
"Deseja importas todos os contatos do telefone? Essa função é experimental, você terá que recarregar a página após a importação.",
}, },
buttons: { buttons: {
import: "Importar Contatos", import: "Importar Contatos",