mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
migrated import contacts controller to typescript
This commit is contained in:
@@ -1,38 +1,8 @@
|
||||
// const Contact = require("../models/Contact");
|
||||
// const Whatsapp = require("../models/Whatsapp");
|
||||
// const { getIO } = require("../libs/socket");
|
||||
// const { getWbot, initWbot } = require("../libs/wbot");
|
||||
import { Request, Response } from "express";
|
||||
import ImportContactsService from "../services/WbotServices/ImportContactsService";
|
||||
|
||||
// exports.store = async (req, res, next) => {
|
||||
// const defaultWhatsapp = await Whatsapp.findOne({
|
||||
// where: { default: true }
|
||||
// });
|
||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
await ImportContactsService();
|
||||
|
||||
// if (!defaultWhatsapp) {
|
||||
// return res
|
||||
// .status(404)
|
||||
// .json({ error: "No default WhatsApp found. Check Connection page." });
|
||||
// }
|
||||
|
||||
// const io = getIO();
|
||||
// const wbot = getWbot(defaultWhatsapp);
|
||||
|
||||
// let phoneContacts;
|
||||
|
||||
// try {
|
||||
// phoneContacts = await wbot.getContacts();
|
||||
// } catch (err) {
|
||||
// console.log(err);
|
||||
// return res.status(500).json({
|
||||
// error: "Could not check whatsapp contact. Check connection page."
|
||||
// });
|
||||
// }
|
||||
|
||||
// await Promise.all(
|
||||
// phoneContacts.map(async ({ number, name }) => {
|
||||
// await Contact.create({ number, name });
|
||||
// })
|
||||
// );
|
||||
|
||||
// return res.status(200).json({ message: "contacts imported" });
|
||||
// };
|
||||
return res.status(200).json({ message: "contacts imported" });
|
||||
};
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Request, Response } from "express";
|
||||
import { getIO } from "../libs/socket";
|
||||
import { initWbot } from "../libs/wbot";
|
||||
import wbotMessageListener from "../services/WbotServices/wbotMessageListener";
|
||||
import wbotMonitor from "../services/WbotServices/wbotMonitor";
|
||||
|
||||
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
|
||||
import DeleteWhatsAppService from "../services/WhatsappService/DeleteWhatsAppService";
|
||||
@@ -39,7 +40,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(whatsapp);
|
||||
// wbotMonitor(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
|
||||
|
||||
@@ -2,11 +2,15 @@ import express from "express";
|
||||
import isAuth from "../middleware/isAuth";
|
||||
|
||||
import * as ContactController from "../controllers/ContactController";
|
||||
// import ImportPhoneContactsController from "../controllers/ImportPhoneContactsController";
|
||||
import * as ImportPhoneContactsController from "../controllers/ImportPhoneContactsController";
|
||||
|
||||
const contactRoutes = express.Router();
|
||||
|
||||
// contactRoutes.post("/contacts/import", isAuth, ImportPhoneContactsController.store);
|
||||
contactRoutes.post(
|
||||
"/contacts/import",
|
||||
isAuth,
|
||||
ImportPhoneContactsController.store
|
||||
);
|
||||
|
||||
contactRoutes.get("/contacts", isAuth, ContactController.index);
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import { initWbot } from "./libs/wbot";
|
||||
// const wbotMonitor = require("./services/wbotMonitor");
|
||||
import Whatsapp from "./models/Whatsapp";
|
||||
import wbotMessageListener from "./services/WbotServices/wbotMessageListener";
|
||||
import wbotMonitor from "./services/WbotServices/wbotMonitor";
|
||||
|
||||
Sentry.init({ dsn: process.env.SENTRY_DSN });
|
||||
|
||||
@@ -58,7 +59,7 @@ const startWhatsAppSessions = async () => {
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(whatsapp);
|
||||
// wbotMonitor(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
});
|
||||
|
||||
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;
|
||||
@@ -1,86 +0,0 @@
|
||||
const Sentry = require("@sentry/node");
|
||||
|
||||
const wbotMessageListener = require("./wbotMessageListener");
|
||||
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot, initWbot } = require("../libs/wbot");
|
||||
|
||||
const wbotMonitor = whatsapp => {
|
||||
const io = getIO();
|
||||
const sessionName = whatsapp.name;
|
||||
const wbot = getWbot(whatsapp.id);
|
||||
|
||||
try {
|
||||
wbot.on("change_state", async newState => {
|
||||
console.log("Monitor session:", sessionName, newState);
|
||||
try {
|
||||
await whatsapp.update({ status: newState });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: whatsapp,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("change_battery", async batteryInfo => {
|
||||
const { battery, plugged } = batteryInfo;
|
||||
console.log(
|
||||
`Battery session: ${sessionName} ${battery}% - Charging? ${plugged}`
|
||||
);
|
||||
|
||||
try {
|
||||
await whatsapp.update({ battery, plugged });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: whatsapp,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("disconnected", async reason => {
|
||||
console.log("Disconnected session:", sessionName, reason);
|
||||
try {
|
||||
await whatsapp.update({ status: "disconnected" });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: whatsapp,
|
||||
});
|
||||
|
||||
setTimeout(
|
||||
() =>
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
})
|
||||
.catch(err => {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}),
|
||||
2000
|
||||
);
|
||||
});
|
||||
|
||||
// setInterval(() => {
|
||||
// wbot.resetState();
|
||||
// }, 20000);
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = wbotMonitor;
|
||||
87
backend/src/services/WbotServices/wbotMonitor.ts
Normal file
87
backend/src/services/WbotServices/wbotMonitor.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import * as Sentry from "@sentry/node";
|
||||
|
||||
import wbotMessageListener from "./wbotMessageListener";
|
||||
|
||||
import { getIO } from "../../libs/socket";
|
||||
import { getWbot, initWbot } from "../../libs/wbot";
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
|
||||
const wbotMonitor = (whatsapp: Whatsapp): void => {
|
||||
const io = getIO();
|
||||
const sessionName = whatsapp.name;
|
||||
const wbot = getWbot(whatsapp.id);
|
||||
|
||||
try {
|
||||
wbot.on("change_state", async newState => {
|
||||
console.log("Monitor session:", sessionName, newState);
|
||||
try {
|
||||
await whatsapp.update({ status: newState });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: whatsapp
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("change_battery", async batteryInfo => {
|
||||
const { battery, plugged } = batteryInfo;
|
||||
console.log(
|
||||
`Battery session: ${sessionName} ${battery}% - Charging? ${plugged}`
|
||||
);
|
||||
|
||||
try {
|
||||
await whatsapp.update({ battery, plugged });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: whatsapp
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("disconnected", async reason => {
|
||||
console.log("Disconnected session:", sessionName, reason);
|
||||
try {
|
||||
await whatsapp.update({ status: "disconnected" });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: whatsapp
|
||||
});
|
||||
|
||||
setTimeout(
|
||||
() =>
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
})
|
||||
.catch(err => {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}),
|
||||
2000
|
||||
);
|
||||
});
|
||||
|
||||
// setInterval(() => {
|
||||
// wbot.resetState();
|
||||
// }, 20000);
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
};
|
||||
|
||||
export default wbotMonitor;
|
||||
Reference in New Issue
Block a user