migrated import contacts controller to typescript

This commit is contained in:
canove
2020-09-22 11:09:52 -03:00
parent 9bab61c7dc
commit 0d8b4cd60c
7 changed files with 131 additions and 126 deletions

View File

@@ -0,0 +1,28 @@
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) {
throw new AppError(
"Could not check whatsapp contact. Check connection page."
);
}
await Promise.all(
phoneContacts.map(async ({ number, name }) => {
await Contact.create({ number, name });
})
);
};
export default ImportContactsService;