mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 03:39:29 +00:00
32 lines
779 B
TypeScript
32 lines
779 B
TypeScript
import AppError from "../../errors/AppError";
|
|
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
|
|
import { getWbot } from "../../libs/wbot";
|
|
import Contact from "../../models/Contact";
|
|
|
|
const ImportContactsService = async (): Promise<void> => {
|
|
const defaultWhatsapp = await GetDefaultWhatsApp();
|
|
|
|
const wbot = getWbot(defaultWhatsapp.id);
|
|
|
|
let phoneContacts;
|
|
|
|
try {
|
|
phoneContacts = await wbot.getContacts();
|
|
} catch (err) {
|
|
console.log(
|
|
"Could not get whatsapp contacts from phone. Check connection page.",
|
|
err
|
|
);
|
|
}
|
|
|
|
if (phoneContacts) {
|
|
await Promise.all(
|
|
phoneContacts.map(async ({ number, name }) => {
|
|
await Contact.create({ number, name });
|
|
})
|
|
);
|
|
}
|
|
};
|
|
|
|
export default ImportContactsService;
|