Skip to content

Commit

Permalink
ImportContactsService.ts: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Jul 4, 2024
1 parent a2e9e42 commit 8b89fa5
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions backend/src/services/WbotServices/ImportContactsService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as Sentry from "@sentry/node";
import { isString, isArray } from "lodash";
import path from "path";
import fs from "fs";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import { getWbot } from "../../libs/wbot";
import Contact from "../../models/Contact";
import { logger } from "../../utils/logger";
import ShowBaileysService from "../BaileysServices/ShowBaileysService";
import CreateContactService from "../ContactServices/CreateContactService";
import { isString, isArray } from "lodash";
import path from "path";
import fs from 'fs';

const ImportContactsService = async (companyId: number): Promise<void> => {
const defaultWhatsapp = await GetDefaultWhatsApp(companyId);
Expand All @@ -20,35 +20,38 @@ const ImportContactsService = async (companyId: number): Promise<void> => {
phoneContacts = JSON.parse(JSON.stringify(contactsString.contacts));

const publicFolder = path.resolve(__dirname, "..", "..", "..", "public");
const beforeFilePath = path.join(publicFolder, 'contatos_antes.txt');
fs.writeFile(beforeFilePath, JSON.stringify(phoneContacts, null, 2), (err) => {
if (err) {
logger.error(`Failed to write contacts to file: ${err}`);
throw err;
const beforeFilePath = path.join(publicFolder, "contatos_antes.txt");
fs.writeFile(
beforeFilePath,
JSON.stringify(phoneContacts, null, 2),
err => {
if (err) {
logger.error(`Failed to write contacts to file: ${err}`);
throw err;
}
console.log("O arquivo contatos_antes.txt foi criado!");
}
console.log('O arquivo contatos_antes.txt foi criado!');
});

);
} catch (err) {
Sentry.captureException(err);
logger.error(`Could not get whatsapp contacts from phone. Err: ${err}`);
}

const publicFolder = path.resolve(__dirname, "..", "..", "..", "public");
const afterFilePath = path.join(publicFolder, 'contatos_depois.txt');
fs.writeFile(afterFilePath, JSON.stringify(phoneContacts, null, 2), (err) => {
const afterFilePath = path.join(publicFolder, "contatos_depois.txt");
fs.writeFile(afterFilePath, JSON.stringify(phoneContacts, null, 2), err => {
if (err) {
logger.error(`Failed to write contacts to file: ${err}`);
throw err;
}
console.log('O arquivo contatos_depois.txt foi criado!');
console.log("O arquivo contatos_depois.txt foi criado!");
});

const phoneContactsList = isString(phoneContacts)
? JSON.parse(phoneContacts)
: phoneContacts;

console.log(isArray(phoneContacts))
console.log(isArray(phoneContacts));
if (Array.isArray(phoneContactsList)) {
phoneContactsList.forEach(async ({ id, name, notify }) => {
if (id === "status@broadcast" || id.includes("g.us")) return;
Expand All @@ -58,7 +61,7 @@ const ImportContactsService = async (companyId: number): Promise<void> => {
where: { number, companyId }
});

console.log(existingContact)
console.log(existingContact);
if (existingContact) {
// Atualiza o nome do contato existente
existingContact.name = name || notify;
Expand Down

0 comments on commit 8b89fa5

Please sign in to comment.