2022-01-06 01:26:15 +00:00
|
|
|
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
|
|
|
|
import { getWbot } from "../../libs/wbot";
|
|
|
|
import Contact from "../../models/Contact";
|
|
|
|
import { logger } from "../../utils/logger";
|
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
import { createOrUpdateContactCache } from '../../helpers/ContactsCache'
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
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;
|
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
let contact = await Contact.create({ number, name });
|
|
|
|
|
|
|
|
// await contact.reload()
|
|
|
|
|
|
|
|
// TEST DEL
|
|
|
|
await createOrUpdateContactCache(`contact:${contact.id}`, {id:contact.id, name, number, profilePicUrl: contact.profilePicUrl, isGroup: contact.isGroup, extraInfo: '', email:'' })
|
|
|
|
//
|
|
|
|
|
|
|
|
// return Contact.create({ number, name });
|
|
|
|
return contact
|
2022-01-06 01:26:15 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ImportContactsService;
|