mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 12:19:16 +00:00
migrated import contacts controller to typescript
This commit is contained in:
28
backend/src/services/WbotServices/ImportContactsService.ts
Normal file
28
backend/src/services/WbotServices/ImportContactsService.ts
Normal 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;
|
||||
Reference in New Issue
Block a user