projeto-hit/backend/src/services/WbotServices/ImportContactsService.ts

42 lines
993 B
TypeScript
Raw Normal View History

import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import { getWbot } from "../../libs/wbot";
import Contact from "../../models/Contact";
import { logger } from "../../utils/logger";
const ImportContactsService = async (): Promise<void> => {
const defaultWhatsapp = await GetDefaultWhatsApp();
const wbot = getWbot(defaultWhatsapp.id);
let phoneContacts;
try {
phoneContacts = await wbot.getContacts();
} catch (err) {
logger.error(`Could not get whatsapp contacts from phone. Err: ${err}`);
}
if (phoneContacts) {
await Promise.all(
phoneContacts.map(async ({ 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 });
})
);
}
};
export default ImportContactsService;