From f3ecb5dbd00c043e28bc759e3cd3ea2efdcc0139 Mon Sep 17 00:00:00 2001 From: canove Date: Sat, 17 Oct 2020 11:16:17 -0300 Subject: [PATCH] fix: apps throws an error when importing contacts from phone --- .../ContactServices/ListContactsService.ts | 2 +- .../WbotServices/ImportContactsService.ts | 16 ++++++++++++++-- frontend/src/pages/Contacts/index.js | 3 +++ frontend/src/translate/languages/en.js | 3 +-- frontend/src/translate/languages/es.js | 2 +- frontend/src/translate/languages/pt.js | 3 +-- 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/backend/src/services/ContactServices/ListContactsService.ts b/backend/src/services/ContactServices/ListContactsService.ts index eeb4128..0bd7a38 100644 --- a/backend/src/services/ContactServices/ListContactsService.ts +++ b/backend/src/services/ContactServices/ListContactsService.ts @@ -35,7 +35,7 @@ const ListContactsService = async ({ where: whereCondition, limit, offset, - order: [["createdAt", "DESC"]] + order: [["name", "ASC"]] }); const hasMore = count > offset + contacts.length; diff --git a/backend/src/services/WbotServices/ImportContactsService.ts b/backend/src/services/WbotServices/ImportContactsService.ts index a45a249..717e61f 100644 --- a/backend/src/services/WbotServices/ImportContactsService.ts +++ b/backend/src/services/WbotServices/ImportContactsService.ts @@ -1,4 +1,3 @@ -import AppError from "../../errors/AppError"; import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp"; import { getWbot } from "../../libs/wbot"; import Contact from "../../models/Contact"; @@ -22,7 +21,20 @@ const ImportContactsService = async (): Promise => { if (phoneContacts) { await Promise.all( 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 }); }) ); } diff --git a/frontend/src/pages/Contacts/index.js b/frontend/src/pages/Contacts/index.js index 24db75b..95f1fcd 100644 --- a/frontend/src/pages/Contacts/index.js +++ b/frontend/src/pages/Contacts/index.js @@ -1,6 +1,7 @@ import React, { useState, useEffect, useReducer } from "react"; import openSocket from "socket.io-client"; import { toast } from "react-toastify"; +import { useHistory } from "react-router-dom"; import { makeStyles } from "@material-ui/core/styles"; import Table from "@material-ui/core/Table"; @@ -86,6 +87,7 @@ const useStyles = makeStyles(theme => ({ const Contacts = () => { const classes = useStyles(); + const history = useHistory(); const [loading, setLoading] = useState(false); const [pageNumber, setPageNumber] = useState(1); @@ -191,6 +193,7 @@ const Contacts = () => { const handleimportContact = async () => { try { await api.post("/contacts/import"); + history.go(0); } catch (err) { const errorMsg = err.response?.data?.error; if (errorMsg) { diff --git a/frontend/src/translate/languages/en.js b/frontend/src/translate/languages/en.js index 9c22416..9c2dad1 100644 --- a/frontend/src/translate/languages/en.js +++ b/frontend/src/translate/languages/en.js @@ -90,8 +90,7 @@ const messages = { importTitlte: "Import contacts", deleteMessage: "Are you sure you want to delete this contact? All related tickets will be lost.", - importMessage: - "Do you want to import all contacts from the phone? This function is experimental, you will have to reload the page after importing.", + importMessage: "Do you want to import all contacts from the phone?", }, buttons: { import: "Import Contacts", diff --git a/frontend/src/translate/languages/es.js b/frontend/src/translate/languages/es.js index b9572a4..0ad9583 100644 --- a/frontend/src/translate/languages/es.js +++ b/frontend/src/translate/languages/es.js @@ -93,7 +93,7 @@ const messages = { deleteMessage: "¿Estás seguro que deseas borrar este contacto? Todos los tickets relacionados se perderán.", 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: { import: "Importar Contactos", diff --git a/frontend/src/translate/languages/pt.js b/frontend/src/translate/languages/pt.js index e7fd31d..cf16e42 100644 --- a/frontend/src/translate/languages/pt.js +++ b/frontend/src/translate/languages/pt.js @@ -90,8 +90,7 @@ const messages = { importTitlte: "Importar contatos", deleteMessage: "Tem certeza que deseja deletar este contato? Todos os tickets relacionados serão perdidos.", - importMessage: - "Deseja importas todos os contatos do telefone? Essa função é experimental, você terá que recarregar a página após a importação.", + importMessage: "Deseja importas todos os contatos do telefone?", }, buttons: { import: "Importar Contatos",